【问题标题】:Can you send an array within an array using MPI_Send and MPI_Recv?您可以使用 MPI_Send 和 MPI_Recv 在数组中发送数组吗?
【发布时间】:2020-03-23 10:39:03
【问题描述】:

这是我程序的最基本功能,因此不一定可重现。但是,我想知道是否有办法使用 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)

【问题讨论】:

    标签: c parallel-processing mpi


    【解决方案1】:

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

    但是,如果您使用自定义类型,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 兼容,这是一个好主意)。

    【讨论】:

    • 我要补充一点,出于性能原因,您应该尽可能将数据分配到连续内存中。
    猜你喜欢
    • 2015-05-18
    • 2015-04-22
    • 2013-04-07
    • 2011-11-11
    • 2015-01-26
    • 1970-01-01
    • 2013-12-06
    • 2018-09-10
    • 2014-04-17
    相关资源
    最近更新 更多