Another MPI Scatter/Gather Lab

From Education

Jump to: navigation, search

MPI Collective Operations Exercise


This is a simple exercise to teach you how to use two of MPI's collective operations: MPI_Scatter and MPI_Gather.

The problem is simple: 1) the root process initializes an array of numbers; 2) the array is distributed (i.e. scattered) to all processes; 3) each process doubles all the numbers; 4) the root process collects the arrays (i.e. gathers) from all processes; 5) the root process prints out the whole array (for verification).

Below are the function headers for Scatter and Gather (for C, Fortran's aren't too much different). Note that the number of elements in the entire array should be a multiple of the size of the smaller arrays. That is, using the variable names from MPI_Scatter:

sendcount == N * recvcount

Where N is the number of processes in the communicator.

int MPI_Scatter(void *sendbuf,		// Address of the entire array
		int sendcount,		// Number of elements to send to each process
		MPI_Datatype sendtype,	// Type of the elements in the array
		void *recvbuf,		// Address of the smaller sub-array
		int recvcount,		// Number of elements in the sub-array
		MPI_Datatype recvtype,	// Type of the elements in the array
		int root,		// Rank of the process collecting everything
		MPI_Comm comm)		// Communicator to send messages over

MPI_Gather is just the reverse of MPI_Scatter:

int MPI_Gather(void *sendbuf,		// Address of the smaller sub-array
		int sendcount,		// Number of elements in the sub-array
		MPI_Datatype sendtype,	// Type of the elements in the array
		void *recvbuf,		// Address of the entire array
		int recvcount,		// Number of elements sent to each process
		MPI_Datatype recvtype,	// Type of the elements in the array
		int root,		// Rank of the process collecting everything
		MPI_Comm comm)		// Communicator to send messages over
Personal tools
SC Education sites