Rookie HPC

Reduction

C | FORTRAN-legacy | FORTRAN-2008

MPI_LAND

Definition

MPI_LAND is a predefined reduction operation that will calculate the logical and of the values reduced.

Example

Copy

Feedback

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

/**
 * @brief Illustrates how to use a logical and reduction operation.
 * @details This application consists of a logical and reduction; every MPI process
 * sends its value for reduction before the logical and of these values is stored in
 * the root MPI process. It can be visualised as follows, with MPI process 0 as
 * root:
 *
 * +-----------+ +-----------+ +-----------+ +-----------+
 * | Process 0 | | Process 1 | | Process 2 | | Process 3 |
 * +-+-------+-+ +-+-------+-+ +-+-------+-+ +-+-------+-+
 *   | Value |     | Value |     | Value |     | Value |
 *   |  true |     |  true |     |  true |     | false |
 *   +-------+     +-------+     +-------+     +-------+
 *            \         |           |         /
 *             \        |           |        /
 *              \       |           |       /
 *               \      |           |      /
 *                +-----+-----+-----+-----+
 *                            |
 *                        +---+---+
 *                        |  LAND |
 *                        +---+---+
 *                            |
 *                        +---+---+
 *                        | false |
 *                      +-+-------+-+
 *                      | Process 0 |
 *                      +-----------+
 **/
int main(int argc, char* argv[])
{
    MPI_Init(&argc, &argv);

    // Get the number of processes and check only 4 are used.
    int size;
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    if(size != 4)
    {
        printf("This application is meant to be run with 4 processes.\n");
        MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
    }

    // Determine root's rank
    int root_rank = 0;

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

    // Each MPI process sends its rank to reduction, root MPI process collects the result
    bool my_value = (my_rank != 3);
    bool reduction_result = false;
    MPI_Reduce(&my_value, &reduction_result, 1, MPI_C_BOOL, MPI_LAND, root_rank, MPI_COMM_WORLD);

    if(my_rank == root_rank)
    {
        printf("The logical and of all values is %s.\n", reduction_result ? "true" : "false");
    }

    MPI_Finalize();

    return EXIT_SUCCESS;
}