Consider thread safety when combining MPI with threads

Use MPI_Init_thread to determine/select MPI's level of thread support:

  • This is important! Substitute MPI_Init_thread() for the usual call to MPI_Init()
  • MPI_Init_thread() informs MPI of the level of thread support that the program requires
  • If messaging to/from multithreaded regions is required, MPI will initialize itself appropriately (if it is equipped to support multithreading)
  • If messaging to/from multithreaded regions is not required, MPI may choose to skip or adjust initialization for threads
  • Upon return, MPI_Init_thread() includes information about the actual level of thread support that MPI will use
  • If thread support is inadequate, the program may need to take action accordingly

Thread safety is identified/controlled by MPI's provided types:

Single
only one thread exists in each MPI task—no multithreading
Funneled
only the main thread can make MPI calls; in multithreaded regions, all MPI calls must be funneled through the main thread
Serialized
multiple threads can call MPI, but synchronized so that only 1 call is in progress at a time
Multiple
MPI is thread safe, i.e., it works correctly when called by multiple threads at once

Monotonic values are assigned to the corresponding MPI parameters:

MPI_THREAD_SINGLE < MPI_THREAD_FUNNELED < MPI_THREAD_SERIALIZED < MPI_THREAD_MULTIPLE

Referring back to the diagram on the previous page, only MPI implementations with MPI_THREAD_MULTIPLE capability will be able to do the fully multithreaded kind of messaging depicted at the bottom.

If MPI_Init() is called instead of MPI_Init_thread(), the level of thread support will automatically be set to MPI_THREAD_SINGLE. This can be verified by checking the response from MPI_Query_thread(&response). In this case, the code is assumed not to be hybrid, i.e., it should not be multithreaded. Even though MPI calls from threaded regions may work just fine, correct functionality is not assured and unexpected results may occur, especially if multiple threads call MPI at the same time.

 
©  |   Cornell University    |   Center for Advanced Computing    |   Copyright Statement    |   Inclusivity Statement