MPI Calculate Polynom Using Pipeline

An MPI program in English to calculate a polynomial using a pipeline approach. In this example, we'll compute a polynomial of the form f(x) = ax^2 + bx + c, where a, b and c are coefficients and x is the variable.
We will use three MPI processes, each corresponding to one term of the polynomial.

The first process will calculate ax^2, the second will calculate bx and the third will calculate c. Each process will add its result to a running total passed along the pipeline. The last process will output the final result.

pipeline-polynom.c
#include <mpi.h>
#include <stdio.h>
 
int main(int argc, char** argv) {
    // Initialize the MPI environment
    MPI_Init(NULL, NULL);
 
    // Get the rank of the process
    int world_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
 
    // Define the coefficients and the value of x
    int coefficients[3] = {1, 2, 1}; // Coefficients for x^2, x, and constant term
    int x = 2; // The value of x
 
    // Each process performs its part of the calculation
    int local_result = 0;
    if (world_rank == 0) {
        // First process calculates ax^2
        local_result = coefficients[0] * x * x;
    } else if (world_rank == 1) {
        // Second process calculates bx
        local_result = coefficients[1] * x;
    } else if (world_rank == 2) {
        // Third process calculates c
        local_result = coefficients[2];
    }
 
    // Pipeline to sum up the results
    int sum = 0;
    if (world_rank != 0) {
        MPI_Recv(&sum, 1, MPI_INT, world_rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
    }
    sum += local_result;
    if (world_rank != 2) {
        MPI_Send(&sum, 1, MPI_INT, world_rank + 1, 0, MPI_COMM_WORLD);
    }
 
    // The last process prints the final result
    if (world_rank == 2) {
        printf("The result of the polynomial is %d\n", sum);
    }
 
    // Finalize the MPI environment
    MPI_Finalize();
    return 0;
}

Initialization: The MPI environment is initialized. Calculation: Each MPI process calculates a part of the polynomial based on its rank.

Pipeline Communication: The processes use MPI send and receive calls to pass along a running sum. Each process, except the first, receives a sum from the previous process, adds its local result, and sends the new sum to the next process.

Final Result: The last process (rank 2) outputs the final result of the polynomial calculation.

Finalization: The MPI environment is finalized.

You should specify exactly three processes. The output will be the result of the polynomial calculation given the coefficients and the value of x.