在MPI中,多进程scanf只接受一次输入并将垃圾值分配给其他?

我正在尝试使用scanf编写MPI代码,它将单独为所有进程输入输入,但只有一个进程从用户获取输入,而其他进程将垃圾值分配给该变量。 该计划如下

#include  #include  #include "mpi.h" #include int main(int argc, char* argv[]) { int i, size, rank; int arr; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); printf("Enter the number\n"); scanf("%d",&i); printf("%d\n",i); MPI_Finalize(); exit(0); } 

stdin只被转发到排名0.无论如何,从stdin读取是一个坏主意,资本非常,并且标准不支持。

stdio不是为并行而设计的。 您可能会注意到,无论您何时运行该问题,它都可能会产生不同的输出。 通常,我们在node0中输入然后bcast到所有的