【问题标题】:While running my code in MPI, the root process never executes despite following the correct syntax在 MPI 中运行我的代码时,尽管遵循正确的语法,根进程永远不会执行
【发布时间】:2019-08-24 19:06:53
【问题描述】:

在尝试使用 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 

除了检查我的代码是否存在逻辑错误外,我真的不知道该怎么做如果 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。

【问题讨论】:

  • stdout 默认为行缓冲。
  • 请尽量在您的问题描述中更清楚。 “发生了一些奇怪的事情”是一个非常模糊的标题。
  • @Zulan,感谢您的意见。我会记住的。
  • 标记 C,但 mpic++(这是 C++ 驱动程序)????
  • 我找不到mpic的,所以我即兴创作。

标签: c parallel-processing mpi


【解决方案1】:

您需要在最后一个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 是在一行上打印一条消息(它创建一个新行)。而且它没有缓冲。

【讨论】:

  • [pappuyadav:28670] *** 处理接收到的信号 *** [pappuyadav:28670] 信号:分段错误 (11) [pappuyadav:28670] 信号代码:地址未映射 (1) [pappuyadav :28670] 地址失败:0x7fff31a8b000 [pappuyadav:28670] [0] /lib/x86_64-linux-gnu/libc.so.6(+0x3ef20)[0x7f7bcc028f20] [pappuyadav:28670] [1] ./q4(+ 0xfd4)[0x560f23a80fd4] [pappuyadav:28670] [2] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x7f7bcc00bb97] [pappuyadav:28670] [3] ./q4(+0xb5a) [0x560f23a80b5a] [pappuyadav:28670] *** 错误信息结束 ***
  • 这就是我得到的。
  • 对不起。我明白了。
  • 即使使用刷新,这并不意味着 MPI 也必须刷新其 IO,是吗?
  • fflush(stdout); 只会刷新标准输出。其他 MPI I/O 不会受到影响。
【解决方案2】:

万一以后有人读到这篇文章,这是对马蒂厄所说的话的补充。 代码应该是这样的

 if (rank == 0 )
 {
  printf("Enter a length for the string that is divisible by the number of processes \n");
    fflush(stdout);
.
.
.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 2014-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    相关资源
    最近更新 更多