1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
4 * (C) 2003 by Argonne National Laboratory.
5 * See COPYRIGHT in top-level directory.
16 /* This is example 11.21 from the MPI 3.0 spec:
18 * The following example shows how request-based operations can be used to
19 * overlap communication with computation. Each process fetches, processes,
20 * and writes the result for NSTEPS chunks of data. Instead of a single
21 * buffer, M local buffers are used to allow up to M communication operations
22 * to overlap with computation.
25 /* Use a global variable to inhibit compiler optimizations in the compute
29 void compute(int step, double *data) {
32 for (i = 0; i < N; i++)
33 junk += data[i] * (double) step;
36 int main( int argc, char *argv[] )
39 int errors = 0, all_errors = 0;
41 MPI_Request put_req[M] = { MPI_REQUEST_NULL };
44 double data[M][N]; /* M buffers of length N */
47 MPI_Init(&argc, &argv);
48 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
49 MPI_Comm_size(MPI_COMM_WORLD, &nproc);
53 MPI_Info_create(&win_info);
55 #ifdef USE_WIN_ALLOC_SHM
56 MPI_Info_set(win_info, "alloc_shm", "true");
58 MPI_Info_set(win_info, "alloc_shm", "false");
61 MPI_Win_allocate(NSTEPS*N*sizeof(double), sizeof(double), win_info,
62 MPI_COMM_WORLD, &baseptr, &win);
64 MPI_Win_lock_all(0, win);
66 for (i = 0; i < NSTEPS; i++) {
67 int target = (rank+1) % nproc;
70 /* Find a free put request */
74 MPI_Waitany(M, put_req, &j, MPI_STATUS_IGNORE);
77 MPI_Rget(data[j], N, MPI_DOUBLE, target, i*N, N, MPI_DOUBLE, win,
79 MPI_Wait(&get_req,MPI_STATUS_IGNORE);
82 MPI_Rput(data[j], N, MPI_DOUBLE, target, i*N, N, MPI_DOUBLE, win,
87 MPI_Waitall(M, put_req, MPI_STATUSES_IGNORE);
88 MPI_Win_unlock_all(win);
92 MPI_Info_free(&win_info);
94 MPI_Reduce(&errors, &all_errors, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
96 if (rank == 0 && all_errors == 0)
97 printf(" No Errors\n");