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)
33 for (i = 0; i < N; i++)
34 junk += data[i] * (double) step;
37 int main(int argc, char *argv[])
40 int errors = 0, all_errors = 0;
42 MPI_Request put_req[M] = { MPI_REQUEST_NULL };
45 double data[M][N]; /* M buffers of length N */
48 MPI_Init(&argc, &argv);
49 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
50 MPI_Comm_size(MPI_COMM_WORLD, &nproc);
54 MPI_Info_create(&win_info);
56 #ifdef USE_WIN_ALLOC_SHM
57 MPI_Info_set(win_info, "alloc_shm", "true");
59 MPI_Info_set(win_info, "alloc_shm", "false");
62 MPI_Win_allocate(NSTEPS * N * sizeof(double), sizeof(double), win_info,
63 MPI_COMM_WORLD, &baseptr, &win);
65 MPI_Win_lock_all(0, win);
67 for (i = 0; i < NSTEPS; i++) {
68 int target = (rank + 1) % nproc;
71 /* Find a free put request */
76 MPI_Waitany(M, put_req, &j, MPI_STATUS_IGNORE);
79 MPI_Rget(data[j], N, MPI_DOUBLE, target, i * N, N, MPI_DOUBLE, win, &get_req);
80 MPI_Wait(&get_req, MPI_STATUS_IGNORE);
83 MPI_Rput(data[j], N, MPI_DOUBLE, target, i * N, N, MPI_DOUBLE, win, &put_req[j]);
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");