User Tools

Site Tools


c:mpi:pipeline

MPI Pipeline

In a pipeline pattern, data is passed through a series of processes in stages, much like an assembly line in a factory. Each process in the pipeline performs some operation on the data and then passes it to the next process.

Let's create a simple pipeline where each process receives a number, adds its rank to this number, and passes it to the next process in the pipeline. The first process starts with a number (say, 0), and the last process prints the final result. This example will show a linear pipeline, where each process communicates only with its immediate neighbor.

mpi-pipeline.c
#include <mpi.h>
#include <stdio.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);
 
    int number;
    if (world_rank == 0) {
        // Start with the number 0
        number = 0;
        MPI_Send(&number, 1, MPI_INT, world_rank + 1, 0, MPI_COMM_WORLD);
    } else {
        MPI_Recv(&number, 1, MPI_INT, world_rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        number += world_rank; // Add the rank to the number
 
        if (world_rank < world_size - 1) {
            // Not the last process, so send to the next process
            MPI_Send(&number, 1, MPI_INT, world_rank + 1, 0, MPI_COMM_WORLD);
        } else {
            // Last process prints the final result
            printf("Process %d received number %d\n", world_rank, number);
        }
    }
 
    // Finalize the MPI environment
    MPI_Finalize();
}

Initialization: The MPI environment is initialized.

Pipeline Operations: The first process (rank 0) initializes the number and sends it to the next process. Each subsequent process receives the number, adds its rank to it, and passes it to the next process in the line. The last process (rank world_size - 1) prints the final result.

Finalization: The MPI environment is finalized.

This example illustrates a basic pipeline where each process contributes to processing the data as it flows through the pipeline. The final output will be the cumulative sum of the ranks of all processes in the pipeline.

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