Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do I need to use MPI_Barrier()?

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?

like image 899
Jiew Meng Avatar asked Nov 09 '12 09:11

Jiew Meng


People also ask

What is the use of MPI barrier?

A barrier can be used to synchronize all processes in a communicator. Each process wait till all processes reach this point before proceeding further. Given an array, divide it into equal contiguous parts and send to nodes, one part each. This is equivalent to n sends.

What is MPI collective communication?

Collective communication is defined as communication that involves a group of processes. The functions of this type provided by MPI are the following: Barrier synchronization across all group members (Sec. Barrier synchronization ). Broadcast from one member to all members of a group (Sec.

How is MPI barrier implemented?

First thing to note is that MPI's barrier has no setup costs: A process reaching an MPI_Barrier call will block until all other members of the group have also called MPI_Barrier . Note that MPI does not require them to reach the same call, just any call to MPI_Barrier .


2 Answers

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.

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 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!

like image 66
Markus Mayr Avatar answered Sep 19 '22 21:09

Markus Mayr


One use of MPI_Barrier is for example to control access to an external resource such as the filesystem, which is not accessed using MPI. For example, if you want each process to write stuff to a file in sequence, you could do it like this:

int rank, size; MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); for ( int ii = 0; ii < size; ++ii ) {     if ( rank == ii ) {         // my turn to write to the file         writeStuffToTheFile();     }     MPI_Barrier(MPI_COMM_WORLD); } 

That way, you can be sure that no two processes are concurrently calling writeStuffToTheFile.

like image 27
Edric Avatar answered Sep 22 '22 21:09

Edric