# Stop process from continuing until others have finished MPI

I am looping an array using MPI. The problem is, i think that some processes are moving onto their next iteration before other precesses have finished theirs. This is causing me problems because data calculated by each process at each iteration is needed in the next. Is their a way that i can pause the processes at the end of the loop until all other have finished their current iteration? (Im using Fortran90 if it makes any difference) Thanks!

An MPI_Barrier can be used to synchronize all processes in a communicator. Each of the processes has to wait till all other processes reach the barrier before all of them can proceed further:

MPI_BARRIER(COMM, IERROR)


Here COMM is the communicator handle and IERROR the error status.

Note that sometimes there are more clever ways to deal with such dependencies algorithmically, but this is highly problem-dependent.

• This is working for what I originally needed, however I am now looking for something that can be used as a condition on a while loop so that no process may leave the loop until all other processes have finished. Is there anything you can suggest? – WireInTheGhost Jan 10 '14 at 18:40
• MPI_Waitall does this... but as Bill has been saying, it's rarely a good idea. – mabraham Jan 12 '14 at 19:28
• That's not quite what I've been saying, @mabraham. :) In this case, I'd look at MPI_Testany(). That's the one that's more or less designed for while loops. – Bill Barth Jan 15 '14 at 12:58
• Oops, I meant MPI_Testall(). It will return a set of flags for all the statuses you pass it. When they're all true, then your while loop can exit. – Bill Barth Jan 15 '14 at 13:20
• Or just use the return value of MPI_Testall ;-) Or just the fact that MPI_Waitall has returned! This assumes there is no work for a rank to do while it is waiting, per @user2538235 description. – mabraham Jan 15 '14 at 14:32

While Christian has given you the right answer, it's almost certainly not what you want. Unnecessary synchronization is guaranteed to limit the scalability of your code. If all processors need data from all other processors, then using a collective like MPI_Alltoall() might be more appropriate. If one task has received all the required data from other tasks, there's usually no harm in that task continuing even if others are still working on the previous iteration. In that case, you can probably use point-to-point communications only.

• Thanks, I am new to MPI and the problem I am trying to solve isnt the simplest. I am almost certain there are much better ways to achieve what I am trying to. Not all processes need information from all others, some need bits from others, is this still something that I should be using? – WireInTheGhost Jan 10 '14 at 18:43
• Sounds like point-to-point communication between processes is the right approach to me. However, I'd say that once a process has received all the data it needs, there's no reason for it not to continue iterating. I.e. once all the MPI_Irecv()s have been completed for that iteration, you have everything that you need on that task, so it may proceed. In that way, either the MPI_Waitall() that completes all the receives serves the function of the MPI_Barrier() without requiring collective communication. – Bill Barth Jan 10 '14 at 19:17