我想了解mpi_bcast。更具体地说,在下面的代码中我期望最终结果是buf0-> 1990和buf1 - > 1991.
#include <mpi.h>
#include <stdio.h>
int main(int argc, char** argv) {
int rank;
int buf0;
int buf1;
const int root=0;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank == 0){
buf0 = 1990;
} else {
buf1 = 1991;
}
printf("[%d]: Before Bcast, buf0 is %d\n", rank, buf0);
printf("[%d]: Before Bcast, buf1 is %d\n", rank, buf1);
/* everyone calls bcast, data is taken from root and ends up in everyone's buf */
MPI_Bcast(&buf0, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&buf1, 1, MPI_INT, 1, MPI_COMM_WORLD);
printf("[%d]: After Bcast, buf0 is %d\n", rank, buf0);
printf("[%d]: After Bcast, buf1 is %d\n", rank, buf1);
MPI_Finalize();
return 0;
}
然而,我得到的输出如下,我期望两个线程都具有buf0的值并且buf1同步但是线程0的buf1是0.我在这里遗漏了一些东西。
mpirun -np 2 mpi_counter_1
[0]: Before Bcast, buf0 is 1990
[0]: Before Bcast, buf1 is 0
[1]: Before Bcast, buf0 is 0
[1]: Before Bcast, buf1 is 1991
[0]: After Bcast, buf0 is 1990
[0]: After Bcast, buf1 is 0
[1]: After Bcast, buf0 is 1990
[1]: After Bcast, buf1 is 1991