Single-Threaded MPI
When the thread support level is MPI_THREAD_SINGLE
, each MPI task runs only on a single thread, as shown.
Question: Is the program that follows an example of single-threaded MPI? Notice that all the MPI calls appear outside of the OpenMP parallel-for loop; no message passing is performed in the loop.
![Example of MPI_THREAD_SINGLE: there is only a single thread, and it calls MPI_Send and MPI_Recv](mpi_thread_single.png)
each MPI task has a single thread—no multithreading
The answer really depends on how the code is compiled. If it is compiled without -qopenmp
(Intel) or -fopenmp
(GCC), then the OpenMP parallel for
directive is ignored, and the program is essentially equivalent to a non-hybrid, single-threaded, MPI-only code. In that case, it is fine to call MPI_Init()
at the beginning of the program, rather than MPI_Init_thread()
. The level of thread support will be MPI_THREAD_SINGLE
, which is completely appropriate if OpenMP is not activated.
However, if the program is compiled with an OpenMP option, then the above example is erroneous. MPI_Init_thread()
should be called instead of MPI_Init()
. The required level of thread support should be MPI_THREAD_FUNNELED
, one step up from MPI_THREAD_SINGLE
. Overall, we are looking at a potential bug in the code, which needs to be removed by initializing MPI with a call to MPI_Init_thread(...MPI_THREAD_FUNNELED,...)
, in order to ensure correctness when OpenMP is enabled. The code should exit if the initialization call returns pvd=MPI_THREAD_SINGLE
.