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