MPI:Printf 语句没有在正确的时间执行 [英] MPI: Printf Statement is not executed at the right time

查看:78
本文介绍了MPI:Printf 语句没有在正确的时间执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小程序.

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

int main(int argc, char *argv[])
{
int rank, size;
int buf;
int err;
MPI_Status status;

err = MPI_Init(&argc, &argv);
if(err == MPI_SUCCESS) {
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

if(rank == 0) {
printf("Buffer size is less than 10\n");
printf("Enter the buffer size: ");
scanf("%d", &buf);
MPI_Send(&buf, 1, MPI_INT, 1, 10,
MPI_COMM_WORLD);
}
else {
MPI_Recv(&buf, 1, MPI_INT, 0, 10,
MPI_COMM_WORLD, &status);
}
printf("process[%d]: buffer size : %d\n", rank,buf);

}
err = MPI_Finalize();
return 0;
}

输出为:

[veda@home-pc hpc]$ mpicc test.c
[veda@home-pc hpc]$ mpiexec -np 2 a.out
Buffer size is less than 10
3
Enter the buffer size: process[0]: buffer size : 3
process[1]: buffer size : 3

在这个输出中,你可以注意到

In this output, you can notice that

输入缓冲区大小:

在我输入缓冲区大小后正在打印/提示.

is printing/prompting after I entered the size of the buffer.

谁能告诉我如何解决这个问题.

Can anyone tell me how to solve this problem.

推荐答案

我怀疑您的标准输出是行缓冲的."Enter the buffer size: " 直到 "process[%d]: buffer size : %d\n" (包含换行符)被打印后才会打印.解决方案是在 printfscanf 之间显式刷新标准输出:

I suspect your standard output is line-buffered. The "Enter the buffer size: " is not printed until "process[%d]: buffer size : %d\n" (containing a newline) is printed. The solution is to explicitly flush the standard output between the printf and the scanf:

printf("Enter the buffer size: ");
fflush(stdout);
scanf("%d", &buf);

这篇关于MPI:Printf 语句没有在正确的时间执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆