#include #include int main(int argc, char** argv) { // Initialize the MPI environment MPI_Init(&argc, &argv); // Get the total number of processes and the rank of this process int world_rank, world_size; MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); MPI_Comm_size(MPI_COMM_WORLD, &world_size); // Ensure the total number of processes is divisible by 4 if (world_size % 4 != 0) { fprintf(stderr, "World size must be a multiple of 4\n"); MPI_Abort(MPI_COMM_WORLD, 1); } // Calculate the color for MPI_Comm_split. This will be used to divide processes into groups int color = world_rank / 4; // Split the world communicator into smaller communicators, one for each group of 4 processes MPI_Comm new_comm; MPI_Comm_split(MPI_COMM_WORLD, color, world_rank, &new_comm); // Get the new rank and the size of the new group int new_rank, new_size; MPI_Comm_rank(new_comm, &new_rank); MPI_Comm_size(new_comm, &new_size); // Ring communication within the new group int token; if (new_rank == 0) { token = world_rank; MPI_Send(&token, 1, MPI_INT, (new_rank + 1) % new_size, 0, new_comm); MPI_Recv(&token, 1, MPI_INT, new_size - 1, 0, new_comm, MPI_STATUS_IGNORE); } else { MPI_Recv(&token, 1, MPI_INT, new_rank - 1, 0, new_comm, MPI_STATUS_IGNORE); MPI_Send(&token, 1, MPI_INT, (new_rank + 1) % new_size, 0, new_comm); } // Each process prints its old and new rank, and the size of the new group, along with the token received printf("Old Rank: %d, New Rank: %d, Size of New Group: %d, Received Token: %d\n", world_rank, new_rank, new_size, token); // Finalize the new communicator MPI_Comm_free(&new_comm); // Finalize the MPI environment MPI_Finalize(); }