Rookie HPC

Collectives

Synchronisation

C | FORTRAN-legacy | FORTRAN-2008

MPI_Barrier

Definition

MPI_Barrier blocks all MPI processes in the given communicator until they all call this routine.

Copy

Feedback

int MPI_Barrier(MPI_Comm communicator);

Parameters

communicator
The communicator in which the barrier takes place.

Returned value

The error code returned from the barrier.

MPI_SUCCESS
The routine successfully completed.

Example

Copy

Feedback

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

/**
 * @brief Illustrates how to use an MPI barrier.
 **/
int main(int argc, char* argv[])
{
    MPI_Init(&argc, &argv);

    // Get my rank
    int my_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

    printf("[MPI process %d] I start waiting on the barrier.\n", my_rank);
    MPI_Barrier(MPI_COMM_WORLD);
    printf("[MPI process %d] I know all MPI processes have waited on the barrier.\n", my_rank);

    MPI_Finalize();

    return EXIT_SUCCESS;
}