gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:34:02 27 4
gpt4 key购买 nike

这是我的程序的最基本功能,因此不一定是可重现的。但是,我想知道是否有一种方法可以使用 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 的轻松兼容性,这是一个好主意)。

关于可以使用 MPI_Send 和 MPI_Recv 发送数组内的数组吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59077556/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com