MPI Reduce

The MPI_Reduce function is used to perform a reduction operation (like sum, max, min, etc.) on values held by each process, gathering them into a single result at a designated root process.

In this example, each process will generate a random number, and we'll use MPI_Reduce to compute the sum of these numbers across all processes. The sum will be gathered at the root process (usually rank 0).

mpi-reduce.c
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
int main(int argc, char** argv) {
    // Initialize the MPI environment
    MPI_Init(&argc, &argv);
 
    // Get the rank of the process and the total number of processes
    int world_rank, world_size;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);
 
    // Seed the random number generator to get different results each time
    srand(time(NULL) + world_rank);
 
    // Generate a random number at each process
    int rand_num = rand() % 100;
 
    printf("Process %d generated number %d\n", world_rank, rand_num);
 
    // Use MPI_Reduce to sum all random numbers and send the result to the root process
    int sum;
    MPI_Reduce(&rand_num, &sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
 
    // The root process prints the result
    if (world_rank == 0) {
        printf("The sum of all random numbers is %d\n", sum);
    }
 
    // Finalize the MPI environment
    MPI_Finalize();
}

Initialization: The MPI environment is initialized.

Random Number Generation: Each process generates a random number.

Reduction Operation: MPI_Reduce is used to compute the sum of all the random numbers. The parameters include the buffer containing the input data, the buffer where the reduced data will be stored, the count of elements, the data type, the reduction operation (MPI_SUM in this case), the rank of the root process, and the communicator.

Output: The root process (rank 0) prints the total sum of all random numbers generated across all processes.

Finalization: The MPI environment is finalized.

This program demonstrates a simple use case of MPI_Reduce for summing values across processes.