2
$\begingroup$

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!

$\endgroup$
5
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – user2538235 Jan 10 '14 at 18:40
  • $\begingroup$ MPI_Waitall does this... but as Bill has been saying, it's rarely a good idea. $\endgroup$ – mabraham Jan 12 '14 at 19:28
  • $\begingroup$ 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. $\endgroup$ – Bill Barth Jan 15 '14 at 12:58
  • $\begingroup$ 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. $\endgroup$ – Bill Barth Jan 15 '14 at 13:20
  • $\begingroup$ 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. $\endgroup$ – mabraham Jan 15 '14 at 14:32
2
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – user2538235 Jan 10 '14 at 18:43
  • $\begingroup$ 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. $\endgroup$ – Bill Barth Jan 10 '14 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.