问题描述
使用MPI_Barrier的OpenMPI实现时出现一些同步问题:
I having some synchronization issues using the OpenMPI implementation of MPI_Barrier:
int rank;
int nprocs;
int rc = MPI_Init(&argc, &argv);
if(rc != MPI_SUCCESS) {
fprintf(stderr, "Unable to set up MPI");
MPI_Abort(MPI_COMM_WORLD, rc);
}
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
printf("P%d\n", rank);
fflush(stdout);
MPI_Barrier(MPI_COMM_WORLD);
printf("P%d again\n", rank);
MPI_Finalize();
对于mpirun -n 2 ./a.out
for mpirun -n 2 ./a.out
输出应为:P0P1...
output should be:P0P1...
输出有时是 :P0再次P0P1再次是P1
output is sometimes:P0P0 againP1P1 again
怎么回事?
推荐答案
打印输出行在终端上的显示顺序不一定是打印内容的顺序.为此,您正在使用共享资源(stdout
),因此始终必须存在订购问题. (并且fflush
在这里无济于事,无论如何stdout
都是行缓冲的.)
The order in which your print out lines appear on your terminal is not necessarily the order in which things are printed. You are using a shared resource (stdout
) for that so there always must be an ordering problem. (And fflush
doesn't help here, stdout
is line buffered anyhow.)
您可以尝试在输出的前面加上时间戳,并将所有这些保存到不同的文件中,每个MPI进程一个.
You could try to prefix your output with a timestamp and save all of this to different files, one per MPI process.
然后要检查您的日志,可以将两个文件合并在一起并根据时间戳进行排序.
Then to inspect your log you could merge the two files together and sort according to the timestamp.
那么您的问题应该消失了.
Your problem should disappear, then.
这篇关于OpenMPI MPI_Barrier问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!