# MPI topic: Hybrid computing

##### Experimental html version of Parallel Programming in MPI, OpenMP, and PETSc by Victor Eijkhout. download the textbook at https:/theartofhpc.com/pcse


# 13 MPI topic: Hybrid computing

While the MPI standard itself makes no mention of threads -- process being the primary unit of computation -- the use of threads is allowed. Below we will discuss what provisions exist for doing so.

Using threads and other shared memory models in combination with MPI leads of course to the question how race condition s are handled. Example of a code with a data race that pertains to MPI:

#pragma omp sections
#pragma omp section
MPI_Send( x, /* to process 2 */ )
#pragma omp section
MPI_Recv( x, /* from process 3 */ )


The MPI standard here puts the burden on the user: this code is not legal, and behavior is not defined.

## 13.1 MPI support for threading

crumb trail: > mpi-hybrid > MPI support for threading

In hybrid execution, the main question is whether all threads are allowed to make MPI calls. To determine this, replace the MPI_Init call by

Here the required and provided parameters can take the following (monotonically increasing) values:

• MPI_THREAD_FUNNELED : The program may use multiple threads, but only the main thread will make MPI calls.

The main thread is usually the one selected by the

master directive, but technically it is the only that executes MPI_Init_thread  . If you call this routine in a parallel region, the main thread may be different from the master.

• MPI_THREAD_SERIALIZED : The program may use multiple threads, all of which may make MPI calls, but there will never be simultaneous MPI calls in more than one thread.

After the initialization call, you can query the support level with

.

In case more than one thread performs communication,

Python note The thread level can be set through the mpi4py.rc object (section  2.2.2  ):

mpi4py.rc.threads # default True


Available levels are

multiple  , serialized  , funneled  , single  .

MPL note

enum mpl::threading_modes {
};


End of MPL note

The mpiexec

program usually propagates environment variables  , so the value of OMP_NUM_THREADS when you call mpiexec will be seen by each MPI process.

• It is possible to use blocking sends in threads, and let the threads block. This does away with the need for polling.

• You can not send to a thread number: use the MPI

message tag to send to a specific thread.

Exercise Consider the 2D heat equation and explore the mix of MPI/OpenMP parallelism:

• Give each node one MPI process that is fully multi-threaded.

• Give each core an MPI process and don't use multi-threading.

Discuss theoretically why the former can give higher performance. Implement both schemes as special cases of the general hybrid case, and run tests to find the optimal mix.
End of exercise

// thread.c
comm = MPI_COMM_WORLD;
MPI_Comm_rank(comm,&procno);
MPI_Comm_size(comm,&nprocs);

if (procno==0) {
}
}
MPI_Finalize();