Blame test/mpi/rma/req_example.c

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *
Packit 0848f5
 *  (C) 2003 by Argonne National Laboratory.
Packit 0848f5
 *      See COPYRIGHT in top-level directory.
Packit 0848f5
 */
Packit 0848f5
#include <mpi.h>
Packit 0848f5
#include <stdio.h>
Packit 0848f5
#include <assert.h>
Packit 0848f5
#include "mpitest.h"
Packit 0848f5
Packit 0848f5
#define NSTEPS 100
Packit 0848f5
#define N 1000
Packit 0848f5
#define M 10
Packit 0848f5
Packit 0848f5
/* This is example 11.21 from the MPI 3.0 spec:
Packit 0848f5
 *
Packit 0848f5
 * The following example shows how request-based operations can be used to
Packit 0848f5
 * overlap communication with computation. Each process fetches, processes,
Packit 0848f5
 * and writes the result for NSTEPS chunks of data. Instead of a single
Packit 0848f5
 * buffer, M local buffers are used to allow up to M communication operations
Packit 0848f5
 * to overlap with computation.
Packit 0848f5
 */
Packit 0848f5
Packit 0848f5
/* Use a global variable to inhibit compiler optimizations in the compute
Packit 0848f5
 * function. */
Packit 0848f5
double junk = 0.0;
Packit 0848f5
Packit 0848f5
void compute(int step, double *data)
Packit 0848f5
{
Packit 0848f5
    int i;
Packit 0848f5
Packit 0848f5
    for (i = 0; i < N; i++)
Packit 0848f5
        junk += data[i] * (double) step;
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
int main(int argc, char *argv[])
Packit 0848f5
{
Packit 0848f5
    int i, rank, nproc;
Packit 0848f5
    int errors = 0, all_errors = 0;
Packit 0848f5
    MPI_Win win;
Packit 0848f5
    MPI_Request put_req[M] = { MPI_REQUEST_NULL };
Packit 0848f5
    MPI_Request get_req;
Packit 0848f5
    double *baseptr;
Packit 0848f5
    double data[M][N];          /* M buffers of length N */
Packit 0848f5
    MPI_Info win_info;
Packit 0848f5
Packit 0848f5
    MPI_Init(&argc, &argv);
Packit 0848f5
    MPI_Comm_rank(MPI_COMM_WORLD, &rank;;
Packit 0848f5
    MPI_Comm_size(MPI_COMM_WORLD, &nproc);
Packit 0848f5
Packit 0848f5
    assert(M < NSTEPS);
Packit 0848f5
Packit 0848f5
    MPI_Info_create(&win_info);
Packit 0848f5
Packit 0848f5
#ifdef USE_WIN_ALLOC_SHM
Packit 0848f5
    MPI_Info_set(win_info, "alloc_shm", "true");
Packit 0848f5
#else
Packit 0848f5
    MPI_Info_set(win_info, "alloc_shm", "false");
Packit 0848f5
#endif
Packit 0848f5
Packit 0848f5
    MPI_Win_allocate(NSTEPS * N * sizeof(double), sizeof(double), win_info,
Packit 0848f5
                     MPI_COMM_WORLD, &baseptr, &win);
Packit 0848f5
Packit 0848f5
    MPI_Win_lock_all(0, win);
Packit 0848f5
Packit 0848f5
    for (i = 0; i < NSTEPS; i++) {
Packit 0848f5
        int target = (rank + 1) % nproc;
Packit 0848f5
        int j;
Packit 0848f5
Packit 0848f5
        /* Find a free put request */
Packit 0848f5
        if (i < M) {
Packit 0848f5
            j = i;
Packit 0848f5
        }
Packit 0848f5
        else {
Packit 0848f5
            MPI_Waitany(M, put_req, &j, MPI_STATUS_IGNORE);
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        MPI_Rget(data[j], N, MPI_DOUBLE, target, i * N, N, MPI_DOUBLE, win, &get_req);
Packit 0848f5
        MPI_Wait(&get_req, MPI_STATUS_IGNORE);
Packit 0848f5
Packit 0848f5
        compute(i, data[j]);
Packit 0848f5
        MPI_Rput(data[j], N, MPI_DOUBLE, target, i * N, N, MPI_DOUBLE, win, &put_req[j]);
Packit 0848f5
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MPI_Waitall(M, put_req, MPI_STATUSES_IGNORE);
Packit 0848f5
    MPI_Win_unlock_all(win);
Packit 0848f5
Packit 0848f5
    MPI_Win_free(&win);
Packit 0848f5
Packit 0848f5
    MPI_Info_free(&win_info);
Packit 0848f5
Packit 0848f5
    MPI_Reduce(&errors, &all_errors, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
Packit 0848f5
Packit 0848f5
    if (rank == 0 && all_errors == 0)
Packit 0848f5
        printf(" No Errors\n");
Packit 0848f5
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
Packit 0848f5
    return 0;
Packit 0848f5
}