User Tools

Site Tools


c:mpi:scan

MPI Scan

MPI_Scan is similar to MPI_Reduce, but instead of gathering the reduced result at a single root process, it performs a parallel prefix reduction, providing each process with a partial result of the reduction operation up to that point in the sequence of processes.

In this example, each process will generate a random number, and we'll use MPI_Scan to compute the cumulative sum of these numbers across all processes. Each process will receive the sum of random numbers from the first process up to itself.

mpi-scan.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_Scan to compute the cumulative sum of all random numbers
    int cum_sum;
    MPI_Scan(&rand_num, &cum_sum, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
 
    // Each process prints its partial sum
    printf("Process %d has cumulative sum %d\n", world_rank, cum_sum);
 
    // Finalize the MPI environment
    MPI_Finalize();
}

Initialization: The MPI environment is initialized.

Random Number Generation: Each process generates a random number.

Scan Operation: MPI_Scan is used to compute the cumulative sum of the random numbers. Each process receives the sum of random numbers from the start up to its rank.

Output: Each process prints its rank and its cumulative sum.

Finalization: The MPI environment is finalized.

This program demonstrates the use of MPI_Scan for computing cumulative sums across processes in an MPI environment.

c/mpi/scan.txt · Last modified: 2024/01/17 11:11 by odefta