Parallel processing MPI_Recv()缓冲区指针无效
我有一个动态分配的数组,由秩0使用MPI_Send()发送到其他秩 在接收端,使用malloc()为动态数组分配内存 MPI_Recv()发生在其他列组上。在这个接收函数中,我得到无效的缓冲区指针错误 代码在概念上与此类似:Parallel processing MPI_Recv()缓冲区指针无效,parallel-processing,mpi,Parallel Processing,Mpi,我有一个动态分配的数组,由秩0使用MPI_Send()发送到其他秩 在接收端,使用malloc()为动态数组分配内存 MPI_Recv()发生在其他列组上。在这个接收函数中,我得到无效的缓冲区指针错误 代码在概念上与此类似: struct graph{ int count; int * array; } a_graph; int x = 10; MPI_Status status; //ONLY 2 RANKS ARE PRESENT. RANK 0 SENDS MS
struct graph{
int count;
int * array;
} a_graph;
int x = 10;
MPI_Status status;
//ONLY 2 RANKS ARE PRESENT. RANK 0 SENDS MSG TO RANK 1
if (rank == 0){
a_graph * my_graph = malloc(sizeof(my_graph))
my_graph->count = x;
my_graph->array = malloc(sizeof(int)*my_graph->count);
for(int i =0; i < my_graph->count; i++)
my_graph->array[i] = i;
MPI_Send(my_graph->array,my_graph->count,int,1,0,MPI_COMM_WORLD);
free(my_graph->array);
free(my_graph);
}
else if (rank == 1){
a_graph * my_graph = malloc(sizeof(my_graph))
my_graph->count = x;
my_graph->array = malloc(sizeof(int)*my_graph->count);
MPI_Recv(my_graph->array,my_graph->count,int,0,0,MPI_COMM_WORLD,&status) // MPI INVALID BUFFER POINTER ERROR HAPPENS AT THIS RECV
}
结构图{
整数计数;
int*数组;
}a_图;
int x=10;
MPI_状态;
//只有两个级别的人员在场。等级0向等级1发送消息
如果(秩==0){
a_图*my_图=malloc(sizeof(my_图))
my_graph->count=x;
my_graph->array=malloc(sizeof(int)*my_graph->count);
对于(int i=0;i我不明白为什么会发生这种情况,因为内存是在发送方和接收方列中分配的下面是祖兰建议您制作的一个最小的、有效的、可验证的(MWVE)示例。请在以后的问题中提供MWVE。无论如何,您需要使用MPI数据类型
MPI_INT
而不是INT
进行发送和接收
#include <mpi.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct graph{
int count;
int * array;
} a_graph;
int main()
{
MPI_Init(NULL, NULL);
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
int x = 10;
MPI_Status status;
//ONLY 2 RANKS ARE PRESENT. RANK 0 SENDS MSG TO RANK 1
if (rank == 0){
a_graph * my_graph = malloc(sizeof(a_graph));
my_graph->count = x;
my_graph->array = malloc(sizeof(int)*my_graph->count);
for(int i =0; i < my_graph->count; i++)
my_graph->array[i] = i;
MPI_Send(my_graph->array,my_graph->count,MPI_INT,1,0,MPI_COMM_WORLD);
free(my_graph->array);
free(my_graph);
}
else if (rank == 1){
a_graph * my_graph = malloc(sizeof(a_graph));
my_graph->count = x;
my_graph->array = malloc(sizeof(int)*my_graph->count);
MPI_Recv(my_graph->array,my_graph->count,MPI_INT,0,0,MPI_COMM_WORLD,&status);
for (int i=0; i<my_graph->count; ++i)
{
printf("%i\n", my_graph->array[i]);
}
}
MPI_Finalize();
return 0;
}
#包括
#包括
#包括
类型定义结构图{
整数计数;
int*数组;
}a_图;
int main()
{
MPI_Init(NULL,NULL);
整数秩;
MPI通信等级(MPI通信世界和等级);
int x=10;
MPI_状态;
//只有2个列组存在。列组0向列组1发送消息
如果(秩==0){
a_图*my_图=malloc(sizeof(a_图));
my_graph->count=x;
my_graph->array=malloc(sizeof(int)*my_graph->count);
对于(int i=0;icount;i++)
my_graph->array[i]=i;
MPI_发送(我的图->数组,我的图->计数,MPI_INT,1,0,MPI_COMM_WORLD);
免费(my_graph->array);
免费(我的图);
}
else if(秩==1){
a_图*my_图=malloc(sizeof(a_图));
my_graph->count=x;
my_graph->array=malloc(sizeof(int)*my_graph->count);
MPI_Recv(我的图->数组,我的图->计数,MPI_INT,0,0,MPI_COMM_WORLD,&status);
对于(int i=0;i计数;++i)
{
printf(“%i\n”,我的图->数组[i]);
}
}
MPI_Finalize();
返回0;
}
请提供一个错误消息和您收到的具体错误消息。它也有助于包括MPI的实现和版本。我已经按照你的建议编辑了这个问题。使用gcc的编译器(MPICC)版本5.4.0,该示例既不完整也不可验证。代码甚至不能远程编译。请仔细再读一遍这一页。还提供了具体的错误。GCC不是MPI实现。