【问题标题】:In MPI for multiple process scanf taking input only once and assign garbage values to other?在 MPI 中,多进程 scanf 只接受一次输入并将垃圾值分配给其他进程?
【发布时间】:2013-09-14 13:38:11
【问题描述】:

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

#include <stdlib.h>
#include <stdio.h>
#include "mpi.h"
#include<string.h>

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);
}

【问题讨论】:

    标签: c gcc compiler-construction mpi


    【解决方案1】:

    stdin 仅转发到 0 级。无论如何,从 stdin 读取是一个大写 VERY 的坏主意,并且不受标准支持。

    【讨论】:

      【解决方案2】:

      stdio 不是为并行设计的。您可能会注意到,每次运行问题时,它可能会给出不同的输出。 一般来说,我们输入node0,然后bcast到所有的oth

      【讨论】:

      • 除了从 node0 获取输入和广播之外,我是否可以使用其他标准输入法执行相同的任务
      • 我想你可以用文件输入。