c - 在 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
除了检查我的代码是否存在逻辑错误之外,我真的不知道该怎么做执行。
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。
解决方案
您需要fflush(stdout);
在最后一个之后添加一些printf
以强制文本刷新。
当文本中没有\n
时printf
,文本不会立即显示。
所以你应该写:
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
是在一行上打印一条消息(它创建一个新行)。而且它没有缓冲。
推荐阅读
- pandas - 指数衰减插值
- angular - 如何将值分配给 angular 中的 httpheaders 标头
- angular - Ngrx 效果:无法从商店或服务中获取实体
- php - 输入字段被呈现为纯文本
- django - 通过查询值列表进行查询集过滤
- javascript - 这是使用承诺时打破“THEN 链”的好方法吗?
- visual-studio-code - VS Code - 获取和使用当前图标集
- reactjs - 如何从 onClick 传递引用以打开模式
- python - Sonnet 中的 LSTM 时间步长
- sql - 使用 PATINDEX 在 SQL Server 中使用多种格式查找文本中的时间