MPI_Gather-向主机发送数据

MPI_Gather - Send data to the master

本文关键字:数据 主机 Gather- MPI      更新时间:2023-10-16

我想使用MPI_Gather将数据发送到列组0。但我做错了什么:

int size;
int rank;
int i;
int b;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
int* bb= new int[size];
//slave
if(rank!=0) {·
    b=13;·
    printf("slave %d n", b);
    // sleep(5);
    MPI_Gather(&b,1,MPI_INT,bb,1,MPI_INT,0,MPI_COMM_WORLD);
}
//master
if(rank==0)
{
    b=12;
    printf("master %d n", b);
    for (i = 0; i < size; i++) {
        printf("%dt", bb[i]);
    }
}
MPI_Finalize();
return 0;

我期望bb=[12,13]。你知道怎么做吗?非常感谢。

主进程还必须调用MPI_Gather。您应该将调用移出条件块:
//slave
if(rank!=0) {
    b=13;
    printf("slave %d n", b);
}
else {
    b=12;
    printf("master %d n", b);
}
// all
MPI_Gather(&b,1,MPI_INT,bb,1,MPI_INT,0,MPI_COMM_WORLD);
//master
if(rank==0)
{        
    for (i = 0; i < size; i++) {
        printf("%dt", bb[i]);
    }
}