c - 在 MPI 中运行我的代码时,尽管遵循正确的语法,但根进程永远不会执行

标签 c parallel-processing mpi

在尝试使用 MPI 库执行用 C 编写的代码时,我遇到了一些非常奇怪的情况。 当我尝试时,我的代码尚未生成语法错误

 mpirun -n 3 ./q4

我明白了

hello
hello
hello
from the other side.
from the other side.
from the other side.

似乎永远不会进入rank 0进程。我不知道为什么会发生这种情况。该代码在结构上与我编写的其他一些示例相同(如果需要,我可以提供整个代码) 但是,如果我在第六行之后输入任意两个随机内容,我会得到这个

1213
123
Enter a length for the string that is divisible by the number of processes Number of vowels 27 

我真的不知道该怎么做才能修复它,除了我检查了我的代码是否有逻辑错误,这些错误没有,即使有,它们也晚得多,这意味着至少第一个下的代码if case 应该执行。

int main(int argc, char * argv[])
{
 printf("hello\n");
 int rank,m,size,num;
 MPI_Init(&argc,&argv);
 MPI_Comm_rank(MPI_COMM_WORLD,&rank);
 MPI_Comm_size(MPI_COMM_WORLD,&size);
 printf("from the other side.\n");
 char str[100];
 if (rank == 0 )
 {
  printf("Enter a length for the string that is divisible by the number of processes ");
  scanf("%d",&m);
  scanf("%s",str);
 }
.
.

如果相关的话,我正在运行 Ubuntu 18.04。

最佳答案

您需要在最后一个 printf 之后添加一些 fflush(stdout); 以强制文本刷新。

printf文本中没有\n时,文本不会立即显示。

所以你应该写:

printf("Enter a length for the string that is divisible by the number of processes ");
fflush(stdout);
scanf("%d",&m);
....

或更简单:

puts("Enter a length for the string that is divisible by the number of processes ");
scanf("%d",&m);
....

puts 是在一行上打印一条消息(它创建一个新行)。而且它没有缓冲。

关于c - 在 MPI 中运行我的代码时,尽管遵循正确的语法,但根进程永远不会执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55492001/

相关文章:

r - mclapply 与 parLapply 速度

c - 为什么 Clang 会看到类型冲突?

c - 尽管满足条件,但不输入 if 条件

c - 使用 isdigit() 和 isalpha() 命令的最简单方法是什么?

c++ - 从 c/c++ 或 linux 发布到 facebook 墙上

c - 避免丢失集群上运行的作业的数据

c++ - 熵和并行随机数生成器播种

c# - 读取/写入文件的最大并行度是多少?

parallel-processing - 如何检查 OpenMPI 中使用了哪些 MCA 参数?

c++ - 大型项目的 MPI 编码实践 multiple MPI_Finalize();或者只有一个?