Scatter and gather

Questions

  • How can I split data across the ranks of my program?

  • How can I join data from the ranks of my program?

Objectives

  • Know the difference between scatter and gather

Scatter

An MPI_Scatter call sends data from one rank to all other ranks.

../_images/MPI_Scatter.svg

After the call, all ranks in the communicator have the one value sent from the root rank, ordered by rank number.

MPI_Scatter is blocking and introduces collective synchronization into the program.

This can be useful to allow one rank to share values to all other ranks in the communicator. For example, one rank might compute some values, and then scatter the content to all other ranks. They can then use this as input for future work.

Gather

An MPI_Gather call sends data from all ranks to a single rank. It is the inverse operation of MPI_Scatter.

../_images/MPI_Gather.svg

After the call, the root rank has one value from each other rank in the communicator, ordered by rank number.

MPI_Gather is blocking and introduces collective synchronization into the program.

This can be useful to allow one rank to collect values from all other ranks in the communicator. For example, all ranks might compute some values, and then the root rank gathers the content. It can then use this as input for future work. One use case is to combine data so that one rank can compute a combined property, or write all the data to a file.

Exercise: scatter and gather (1)

Use scatter and gather

You can find a scaffold for the code scatter-and-gather-1.c in the content/code/day-2/00_scatter-and-gather folder. A working solution is in the solution subfolder. It’s similar to the broadcast code we saw earlier. Try to compile with:

mpicc -g -Wall -std=c11 scatter-and-gather-1.c -o scatter-and-gather-1
  1. When you have the code compiling, try to run with:

    mpiexec -np 4 ./scatter-and-gather-1
    
  2. Use clues from the compiler and the comments in the code to change the code so it compiles and runs. Try to get all ranks to report success :-)

Exercise: scatter and gather (2)

Use scatter and gather on more data

In this exercise you’ll use MPI_Scatter to split a matrix into row vectors. The number of rows in the matrix is equal to the number of processes, and each vector will be stored on the individual processes. After scatter, you can also use MPI_Gather to assemble the vectors into a matrix.

You can find a scaffold for the code scatter-and-gather-2.c in the content/code/day-2/00_scatter-and-gather folder.

A working solution is in the solution subfolder. It’s similar to the broadcast code we saw earlier. Try to compile with:

mpicc -g -Wall -std=c11 scatter-and-gather-2.c -o scatter-and-gather-2
  1. When you have the code compiling, try to run with different number of MPI processes.

  2. Try to get all ranks to report success :-)

Exercise: scatter and gather (3)

Use scatter and gather to compute inner product of vectors

In this exercise you’ll use MPI_Scatter to split two vectors into segments and compute their inner product in parallel. After scatter, you can use MPI_Reduce to collect the final result, and use MPI_Gather to collect timing information.

You can find a scaffold for the code scatter-and-gather-3.c in the content/code/day-2/00_scatter-and-gather folder.

A working solution is in the solution subfolder. It’s similar to the broadcast code we saw earlier. Try to compile with:

mpicc -g -Wall -std=c11 scatter-and-gather-3.c -o scatter-and-gather-3
  1. When you have the code compiling, try to run with different number of MPI processes.

  2. Try to get rank 0 to report success :-)

See also

Keypoints

  • MPI applications can scatter and gather data to suit common application patterns.