OpenMPI — Collective communication

MPI_Gather() example

In this program each process generates an array of integers, "tagged" according to the process' rank. The local arrays are then all gathered together into a large array at the root.

/*
* gather() arrays to root, display in order.
* 2019-12-05
*/
#include <stdio.h>
#include <string.h> // strlen(), strncpy()
#include <time.h>   // ctime(), time()
#include <unistd.h> // sleep()
#include <stdlib.h> // malloc()
#include "mpi.h"

#define INTS_PER_PROC 3

int main(int argc, char **argv)
{
    /* Initialize MPI execution environment */
    MPI_Init(&argc, &argv);

    // Identify process' rank, world size, and host's processor-name:
    int rank, nprocs, namelen;
    char hostname[MPI_MAX_PROCESSOR_NAME];
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
    MPI_Get_processor_name(hostname, &namelen);

    int root = 0;

    int localbuf[INTS_PER_PROC];
    for (int i = 0; i < INTS_PER_PROC; i++) {
        localbuf[i] = (rank+1)*100 + i;
        printf(" -- %d: %4d", rank, localbuf[i]);
    }
    printf("\n");

    int *globalbuf = NULL;
    if (rank == root)
        globalbuf = malloc(nprocs * INTS_PER_PROC * sizeof(int));

    MPI_Gather(
            localbuf, INTS_PER_PROC, MPI_INT,
            globalbuf, INTS_PER_PROC, MPI_INT,
            root, MPI_COMM_WORLD );

    if (rank == root)
        for (int i = 0; i < nprocs * INTS_PER_PROC; i++)
            printf("globalbuf[%2d]: %4d\n", i, globalbuf[i]);

    MPI_Finalize(); /* Terminate MPI execution environment */
}