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
With the MPI_THREAD_SINGLE level of thread support,
each MPI task has a single thread—no multithreading
#include <mpi.h>
int main(int argc, char **argv) {
int rank, size, ie, i;

ie = MPI_Init(&argc, &argv[]);
ie = MPI_Comm_rank(...&rank);
ie = MPI_Comm_size(...&size);

//Setup shared mem, comp/comm

#pragma omp parallel for
for (i=0; i<n; i++) {
    //<work>
}

//Compute & communicate

ie = MPI_Finalize();
}
C
include 'mpif.h'
program hybsingle
integer irk, isz, ie, i

call MPI_Init(ie)
call MPI_Comm_rank(...irk,ie)
call MPI_Comm_size(...isz,ie)

!Setup shared mem, comp/comm

!$OMP parallel do
do i=1,n
    !<work>
enddo

!Compute & communicate

call MPI_Finalize(ie)
end
Fortran

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.

 
© 2025  |   Cornell University    |   Center for Advanced Computing    |   Copyright Statement    |   Access Statement