要获得一种无缓冲的输出,您可以做的是执行输出并使用 interleaved 刷新MPI_Barrier。如果您有P进程,则当前进程的排名存储在变量中rank并且您正在使用 communicator comm,您可以执行以下操作:
for (int p = 0; p < P; ++p)
{
// Only one process writes at this time
// std::endl flushes the buffer
if (p == rank)
std::cout << "Message from process " << rank << std::endl;
// Block the other processes until the one that is writing
// flushes the buffer
MPI_Barrier(comm);
}
当然,这只是一个 C++ 示例。您可能必须将其翻译成 Fortran 的 C。另请注意,此代码仍然不能保证 100% 的概率输出实际上是您所期望的,但有很好的概率。
无论如何,原则是始终在两个输出操作之间添加屏障并刷新缓冲区。