MPI:MPI_Type_create_struct在send \ recv之后导致分段错误

时间:2017-01-07 22:06:02

标签: c multithreading segmentation-fault mpi

您好我正在尝试为我的结构创建新的MPI_Datatype:

typedef struct Handler {
int max, pivot;
int *array, *temp, *index;
}*Handler;

像这样:

void build_derived_type(Handler handler, int size, MPI_Datatype* message_type_ptr) {
int block_lengths[5];
MPI_Aint displacements[5];
MPI_Datatype typelist[5];
MPI_Aint addresses[6]; // Helper array

typelist[0] = MPI_INT;
typelist[1] = MPI_INT;
typelist[2] = MPI_INT;
typelist[3] = MPI_INT;
typelist[4] = MPI_INT;

block_lengths[0] = block_lengths[1] = 1;
block_lengths[2] = block_lengths[3] = block_lengths[4] = size;

MPI_Get_address(handler, &addresses[0]);
MPI_Get_address(&handler->max, &addresses[1]);
MPI_Get_address(&handler->pivot, &addresses[2]);
MPI_Get_address(handler->array, &addresses[3]);
MPI_Get_address(handler->temp, &addresses[4]);
MPI_Get_address(handler->index, &addresses[5]);

displacements[0] = &handler->max - addresses[0];
displacements[1] = &handler->pivot - addresses[0];
displacements[2] = handler->array - addresses[0];
displacements[3] = handler->temp - addresses[0];
displacements[4] = handler->index - addresses[0];

MPI_Type_create_struct(5, block_lengths, displacements, typelist, message_type_ptr);

MPI_Type_commit(message_type_ptr);
}

我想我做错了,因为已经在send \ recv:

Handler handler;
MPI_Datatype handler_type;

MPI_Comm_rank(MPI_COMM_WORLD, &id);

handler = init_handler(size);
build_derived_type(handler, &handler_type);



if (id == MASTER) {
    MPI_Comm_size(MPI_COMM_WORLD, &th_);
    for (int i = 1; i < th_; i++) {
         MPI_Send(init_handler(size), 1, handler_type, i, HANDLER_TAG, MPI_COMM_WORLD);
    }
} else {
    MPI_Recv(handler, 1, handler_type, MASTER, HANDLER_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}

这里是init_handler代码:

Handler init_handler(const int size) {
    Handler handler = (Handler) malloc(sizeof(struct Handler));
    if (!handler)
        return NULL;

handler->max = size - 1;
handler->pivot = 0;

handler->array = (int*) malloc(sizeof(int) * size);
handler->temp = (int*) malloc(sizeof(int) * size);
handler->index = (int*) malloc(sizeof(int) * size);

for (int i = 0; i < size; i++)
    handler->array[i] = handler->index[i] = i;

return handler;
}

我收到以下错误(代码编译):

  

mpirun注意到信号11(分段故障)上节点bl201上的PID 19574的进程等级3退出。

我做错了什么?感谢。

0 个答案:

没有答案