我有一个小程序.
#include "mpi.h" #includeint 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
在此输出中,您可以注意到这一点
输入缓冲区大小:
在输入缓冲区大小后打印/提示.
谁能告诉我如何解决这个问题.
我怀疑你的标准输出是行缓冲的.在"Enter the buffer size: "
打印"process[%d]: buffer size : %d\n"
(包含换行符)之前不会打印.解决方案是显式刷新printf
和之间的标准输出scanf
:
printf("Enter the buffer size: "); fflush(stdout); scanf("%d", &buf);