MPI_Recv()无效的缓冲区指针

时间:2017-03-29 16:30:13

标签: parallel-processing mpi

我有一个动态分配的数组,使用MPI_Send()按等级0发送到其他等级 在接收端,使用malloc()为动态数组分配内存 MPI_Recv()发生在其他等级上。在此接收函数中,我得到无效的缓冲区指针错误。

代码在概念上与此相似:

s = pd.DataFrame(gender_count)

我不明白为什么会发生这种情况,因为内存分配在发送方和接收方等级

1 个答案:

答案 0 :(得分:0)

以下是Zulan建议您制作的最小,可工作且可验证的(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;
}