One-sided

C | Fortran-2008 | Fortran-90

MPI_Accumulate

Definition

MPI_Accumulate allows to one-sidedly update data by combining the existing data and the data sent with an accumulation operator.

Copy

Feedback

int MPI_Accumulate(const void* origin_address,
                   int origin_count,
                   MPI_Datatype origin_datatype,
                   int target_rank,
                   MPI_Aint target_displacement,
                   int target_count,
                   MPI_Datatype target_datatype,
                   MPI_Op operation,
                   MPI_Win window)

Parameters

origin_address

The address of the origin buffer; the one containing the data to send.

origin_count

The number of elements to send from the origin buffer.

origin_datatype

The type of an element in the origin buffer.

target_rank

The rank of the MPI process whose window is to receive the data.

target_displacement

The index position of the target buffer. This is made possible by the displacement unit specified on the target when creating the window. Incrementing the target_displacement by 1 will increment the position in the target buffer by <displacement_unit> bytes.

target_count

The number of elements in the target buffer.

target_datatype

The type of an element in the target buffer.

operation

The operation to apply during the accumulation. The predefined operations for MPI_REDUCE can be used, however, user-defined functions cannot.

window

The window to which accumulate the data on the target MPI process.

Return value

The error code returned from the accumulation:

Example

Copy

Feedback

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

/**
 * @brief Illustrate how to accumulate data.
 * @details This application consists of two MPI processes. MPI process 0
 * exposes a window containing an integer initialised to 0. All the other MPI
 * processes add their rank to that value. After the MPI_Accumulate is issued, 
 * each MPI process calls on MPI_Win_fence to synchronise. Finally, MPI process
 * 0 prints the total value.
 **/
int main(int argc, char* argv[])
{
    MPI_Init(&argc, &argv);

    // Get my rank
    int my_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

    // Create the window
    int window_buffer = 0;
    MPI_Win window;
    MPI_Win_create(&window_buffer, sizeof(int), sizeof(int), MPI_INFO_NULL, MPI_COMM_WORLD, &window);
    if(my_rank == 0)
    {
        printf("[MPI process 0] Value in my window_buffer before MPI_Accumulate: %d.\n", window_buffer);
    }
    MPI_Win_fence(0, window);

    if(my_rank > 0)
    {
        // Push my value into the first integer in MPI process 0 window
        MPI_Accumulate(&my_rank, 1, MPI_INT, 0, 0, 1, MPI_INT, MPI_SUM, window);
        printf("[MPI process %d] I accumulate data %d in MPI process 0 window via MPI_Accumulate.\n", my_rank, my_rank);
    }

    // Wait for the MPI_Accumulate issued to complete before going any further
    MPI_Win_fence(0, window);

    if(my_rank == 0)
    {
        printf("[MPI process 0] Value in my window_buffer after MPI_Accumulate: %d.\n", window_buffer);
    }

    // Destroy the window
    MPI_Win_free(&window);

    MPI_Finalize();

    return EXIT_SUCCESS;
}