您可以使用 MPI_Send 和 MPI_Recv 发送数组中的数组吗?

Can you send an array within an array using MPI_Send and MPI_Recv?

这是我程序的最基本功能,因此不一定可重现。但是,我想知道是否有办法使用 MPI 发送数组数组?或者这是不可能的,我应该展平我的阵列?任何帮助将不胜感激,因为我一直在努力解决这个问题。

int *individual_topIds;
int **cell_topIds;
cell_topIds = (int**) malloc(sizeof(int*)*25*boxes);
if(rank == 0) {
    for (int i = 0; i < boxes; i++) {
        individual_topIds = (int*) malloc(sizeof(int)*25);
        for(int j = 0; j < cellMatrix[i].numTop; j++){
            individual_topIds[j] = cellMatrix[i].aTopIds[j];
        }
        cell_topIds[i] = individual_topIds;
     }
     MPI_Send(cell_topIds, boxes*25, MPI_INT, 1, 10, MPI_COMM_WORLD);
}

然后在我的排名== 1 部分。我试过只用盒子发送和接收,而不是盒子*25。

for 1 -> boxes
  MPI_Recv(cell_topIds, boxes*25, MPI_INT, 0, 10, MPI_COMM_WORLD, &status);
  int *ptop;
  ptop = (int*) malloc(sizeof(int)*25);
  ptop = cell_topIds[i];
  printf("1\n");
  for(int j = 0; j < sizeof(&ptop)/sizeof(int); j++){
         printf("%d, ", ptop[j]);
  }
  printf("2\n");
end for i -> boxes
free(ptop);

编辑:忘记提及打印的输出是段错误 发现错误:分段错误(信号 11)

这不是一个措辞特别好的问题。

但是,如果您使用自定义类型,MPI 将允许您发送数组的数组,如下所示:

#include "mpi.h"
#include <stdio.h>

struct Partstruct
{
    char c;
    double d[6];
    char b[7];
};

int main(int argc, char *argv[])
{
    struct Partstruct particle[1000];
    int i, j, myrank;
    MPI_Status status;
    MPI_Datatype Particletype;
    MPI_Datatype type[3] = { MPI_CHAR, MPI_DOUBLE, MPI_CHAR };
    int blocklen[3] = { 1, 6, 7 };
    MPI_Aint disp[3];

    MPI_Init(&argc, &argv);

    disp[0] = &particle[0].c - &particle[0];
    disp[1] = &particle[0].d - &particle[0];
    disp[2] = &particle[0].b - &particle[0];
    MPI_Type_create_struct(3, blocklen, disp, type, &Particletype);
    MPI_Type_commit(&Particletype);

    MPI_Comm_rank(MPI_COMM_WORLD, &myrank);

    if (myrank == 0)
    {
        MPI_Send(particle, 1000, Particletype, 1, 123, MPI_COMM_WORLD);
    }
    else if (myrank == 1)
    {
        MPI_Recv(particle, 1000, Particletype, 0, 123, MPI_COMM_WORLD, &status);
    }
    MPI_Finalize();
    return 0;
}

或者,使用平面阵列设计(出于性能原因以及与 MPI 的轻松兼容性,这是一个好主意)。