笛卡尔拓扑mpi中的边界交换

Border exchange in cartesian topology mpi

我想在我的 mpi 程序中执行边界交换。 我的结构看起来像这样:

cell** local_petri_A;

local_petri_A = calloc(p_local_petri_x_dim,sizeof(*local_petri_A));

for(int i = 0; i < p_local_petri_x_dim ; i ++){
        local_petri_A[i] = calloc(p_local_petri_y_dim,sizeof(**local_petri_A));
    }

其中单元格是:

typedef struct {
  int color;
  int strength;
} cell;

我想要一个像这张图一样的交换方案:

所以我把我的程序放在笛卡尔拓扑中,首先定义 mpi 类型来执行交换: void create_types(){

////////////////////////////////
////////////////////////////////
// cell type
const int    nitems=2;
int          blocklengths[2] = {1,1};
MPI_Datatype types[2] = {MPI_INT, MPI_INT};
MPI_Aint     offsets[2];

offsets[0] = offsetof(cell, color);
offsets[1] = offsetof(cell, strength);

MPI_Type_create_struct(nitems, blocklengths, offsets, types, &mpi_cell_t);
MPI_Type_commit(&mpi_cell_t);
////////////////////////////////
///////////////////////////////

MPI_Type_vector ( x_inside , 1 , 1 , mpi_cell_t , & border_row_t );
MPI_Type_commit ( & border_row_t );
/*we put the stride to x_dim to get only one column*/
MPI_Type_vector ( y_inside  , 1 , p_local_petri_x_dim , MPI_DOUBLE , & border_col_t );
MPI_Type_commit ( & border_col_t );

}

然后最后尝试从南到北进行交换:

   /*send to the north receive from the south */
   MPI_Sendrecv ( & local_petri_A[0][1] , 1 , border_row_t , p_north , TAG_EXCHANGE ,& local_petri_A [0][ p_local_petri_y_dim  -1] , 1 , border_row_t , p_south , TAG_EXCHANGE ,cart_comm , MPI_STATUS_IGNORE );
   /*send to the south receive from the north */
   MPI_Sendrecv ( & local_petri_A[0][ p_local_petri_y_dim  -2] , 1 , border_row_t , p_south , TAG_EXCHANGE ,& local_petri_A [0][0] , 1 , border_row_t , p_north , TAG_EXCHANGE ,cart_comm , MPI_STATUS_IGNORE );

注意:在本节中,x_inside 和 y_inside 是数组的“内部”维度(没有幻影部分),p_local_petri_dim 是整个数组的维度。

然后我有这个错误:

我做错了什么吗?

提前感谢您的帮助。

问题在于您分配二维数组的方式。 您分配了一个数组数组,因此连续内存中不太可能有两行。因此,列的 ddt 与二维数组布局不匹配。

您可以参考MPI_Bcast a dynamic 2d array正确分配您的数组。

附带说明一下,Fortran 没有这种问题,所以如果这是一个选项,那会让您的生活更轻松。