OutOfBound OutOfBound - 3 years ago 111
C++ Question

Is it safe to call MPI_Init from different thread?

Is it safe to call

from a

int main(int argc, char** argv) {

std::thread mpi_thread([...](){

// All MPI calls are done by this thread.


// The main thread is doing different stuff

return 0;

Answer Source

The MPI standard has a section about MPI and Threads. One important part is:

Initialization and Completion The call to MPI_FINALIZE should occur on the same thread that initialized MPI. We call this thread the main thread. The call should occur only after all process threads have completed their MPI calls, and have no pending communications or I/O operations.

Your code does fulfill this requirement.

MPI knows several levels of thread support. Your code requires:

MPI_THREAD_FUNNELED The process may be multi-threaded, but the application must ensure that only the main thread makes MPI calls.

For good measure, you should call the following instead of MPI_Init:

int provided;
MPI_Init_thread(NULL, NULL, MPI_THREAD_FUNNELED, &provided);
if (provided < MPI_THREAD_FUNNELED)
    MPI_Abort(MPI_COMM_WORLD, -1);

A MPI library that is not thread compliant must return provided == MPI_THREAD_SINGLE indicating you won't be able to use this implementation correctly with your code.

In practice, you should be fine with the common implementations. For more information about the different levels of thread support, see 12.4.3 in the MPI standard. With the higher levels of thread support, your mileage may vary depending on the implementation you chose.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download