Rookie HPC

Miscellaneous

C | FORTRAN-legacy | FORTRAN-2008

MPI_Init_thread

Definition

MPI_Init_thread initialises the MPI environment like MPI_Init does, except that the former also explicitly indicates the level of multithreading support needed: MPI_THREAD_SINGLE, MPI_THREAD_FUNNELED, MPI_THREAD_SERIALIZED or MPI_THREAD_MULTIPLE. MPI_Init is equivalent to MPI_Init_thread with the MPI_THREAD_SINGLE thread support level. The routine MPI_Init_thread must be called by each MPI process, once and before any other MPI routine.

Copy

Feedback

int MPI_Init_thread(int* argc,
                    char*** argv,
                    int required,
                    int provided);

Parameters

argc
The number of arguments passed through main, or NULL.
argv
The argument values passed through main, or NULL.
required
The level of threading support desired. It must be either MPI_THREAD_SINGLE, MPI_THREAD_FUNNELED, MPI_THREAD_SERIALIZED or MPI_THREAD_MULTIPLE.
provided
The level of threading support provided by the implementation, which may differ from the one desired. Thread support levels are monotonic, that is, MPI_THREAD_SINGLE < MPI_THREAD_FUNNELED < MPI_THREAD_SERIALIZED < MPI_THREAD_MULTIPLE. Therefore, the thread support level required is not met if it is strictly greater than the thread support level provided.

Returned value

The error code returned during the thread initialisation.

MPI_SUCCESS
The routine successfully completed.

Example

Copy

Feedback

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

/**
 * @brief Illustrates how to initialise the MPI environment with multithreading
 * support.
 * @details This application initialised MPI and asks for the 
 * MPI_THREAD_SERIALIZED thread support level. It then compares it with the
 * thread support level provided by the MPI implementation.
 **/
int main(int argc, char* argv[])
{
    // Initilialise MPI and ask for thread support
    int provided;
    MPI_Init_thread(&argc, &argv, MPI_THREAD_SERIALIZED, &provided);
    if(provided < MPI_THREAD_SERIALIZED)
    {
        printf("The threading support level is lesser than that demanded.\n");
        MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
    }
    else
    {
        printf("The threading support level corresponds to that demanded.\n");
    }

    // Tell MPI to shut down.
    MPI_Finalize();

    return EXIT_SUCCESS;
}