MPI Scatter
Using MPI_Scatter to distribute parts of an array from the master process to all processes in an MPI communicator.
MPI_Scatter is used to divide an array into equal segments and distribute each segment to a different process.
- mpi-scatter.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 int world_rank; MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); // Get the total number of processes int world_size; MPI_Comm_size(MPI_COMM_WORLD, &world_size); // The master process (rank 0) prepares the data const int elements_per_proc = 3; int *send_data = NULL; if (world_rank == 0) { const int total_elements = elements_per_proc * world_size; send_data = new int[total_elements]; // Initialize the array with values for (int i = 0; i < total_elements; i++) { send_data[i] = i + 1; } } // Each process will receive a portion of the array from the master process int recv_data[elements_per_proc]; // Scatter the data from the master process to all processes MPI_Scatter(send_data, elements_per_proc, MPI_INT, recv_data, elements_per_proc, MPI_INT, 0, MPI_COMM_WORLD); // Each process prints its received portion printf("Process %d received elements: ", world_rank); for (int i = 0; i < elements_per_proc; i++) { printf("%d ", recv_data[i]); } printf("\n"); // Clean up if (world_rank == 0) { delete[] send_data; } // Finalize the MPI environment MPI_Finalize(); }
Initialization: The MPI environment is initialized with MPI_Init.
Data Preparation: The master process (rank 0) prepares an array (send_data) with data to be distributed.
Scatter Operation: MPI_Scatter is called by all processes. It divides the send_data array into segments of elements_per_proc and distributes them to each process. Each process receives its segment in the recv_data array. The parameters of MPI_Scatter include the send buffer (only significant at the master process), the number of elements sent to each process, the datatype of the elements, the receive buffer, the number of elements received, the datatype of the receive buffer, the rank of the sending process, and the communicator.
Output: Each process prints the elements it received.
Cleanup: The master process frees the allocated memory.
Finalization: The MPI environment is finalized with MPI_Finalize.