#include #include 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(); }