什么时候需要使用MPI_Barrier()? [英] When do I need to use MPI_Barrier()?

查看:139
本文介绍了什么时候需要使用MPI_Barrier()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么时候需要使用障碍物?例如,在分散/聚集之前/之后是否需要它?还是OMPI应该确保在分散/聚集之前所有流程都达到了这一点?同样,在广播之后,我可以期望所有进程都已经接收到该消息吗?

I wonder when do I need to use barrier? Do I need it before/after a scatter/gather for example? Or should OMPI ensure all processes have reached that point before scatter/gather-ing? Similarly, after a broadcast can I expect all processes to already receive the message?

推荐答案

MPI-3.0之前的MPI中的所有集体操作都被阻止,这意味着在返回缓冲区后使用传递给它们的所有缓冲区是安全的.特别是,这意味着当这些函数之一返回时,所有数据都已接收. (但是,这并不意味着所有数据都已发送!) 因此,如果所有缓冲区都已经有效,则在进行集体操作之前/之后,MPI_Barrier并不是必需的(或非常有帮助).

All collective operations in MPI before MPI-3.0 are blocking, which means that it is safe to use all buffers passed to them after they return. In particular, this means that all data was received when one of these functions returns. (However, it does not imply that all data was sent!) So MPI_Barrier is not necessary (or very helpful) before/after collective operations, if all buffers are valid already.

还请注意,MPI_Barrier不会神奇地等待非阻塞调用.如果您使用无阻塞发送/接收,并且两个进程在发送/接收对之后都等待一个MPI_Barrier,则不能保证该进程在MPI_Barrier之后发送/接收了所有数据.请改用MPI_Wait(和朋友).因此,以下代码包含错误:

Please also note, that MPI_Barrier does not magically wait for non-blocking calls. If you use a non-blocking send/recv and both processes wait at an MPI_Barrier after the send/recv pair, it is not guaranteed that the processes sent/received all data after the MPI_Barrier. Use MPI_Wait (and friends) instead. So the following piece of code contains errors:

/* ERRORNOUS CODE */

Code for Process 0:
Process 0 sends something using MPI_Isend
MPI_Barrier(MPI_COMM_WORLD);
Process 0 uses buffer passed to MPI_Isend // (!)

Code for Process 1:
Process 1 recvs something using MPI_Irecv
MPI_Barrier(MPI_COMM_WORLD);
Process 1 uses buffer passed to MPI_Irecv // (!)

标有(!)的两行都是不安全的!

Both lines that are marked with (!) are unsafe!

MPI_Barrier仅在少数情况下有用.大多数时候,您并不关心进程是否同步.更好地了解阻塞和非阻塞呼叫!

MPI_Barrier is only useful in a handful of cases. Most of the time you do not care whether your processes sync up. Better read about blocking and non-blocking calls!

这篇关于什么时候需要使用MPI_Barrier()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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