Gather/Scatter Effect
In order to illustrate the gather and scatter functions, we give a matrix-style depiction below:

Let's consider in detail how MPI_Gather might be used to facilitate a distributed matrix computation.
Example: matrix-vector multiplication
- Matrix is distributed by rows (i.e., row-major order)
- Product vector is needed in entirety by one process
- MPI_Gather will be used to collect the product from processes
Description of Sample Code
The problem associated with the following sample code is the multiplication of a matrix A, size 100x100, by a vector b of length 100. The example uses four MPI processes, so each process will work on its own chunk of 25 rows of A. Since b is the same for each process, it will simply be replicated across processes. The vector c will therefore have 25 elements calculated by each process; these are stored in cpart. Here is a picture of how the overall computation is distributed:

- A
- a matrix partitioned across rows and distributed to processes as Apart
- b
- a vector present on all processes
- c
- a partitioned vector updated by each process independently
The MPI_Gather routine will retrieve cpart from each process and store the result in ctotal, which is the complete vector c.