Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Activate some more tests.
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / req_example.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2003 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7 #include <mpi.h>
8 #include <stdio.h>
9 #include <assert.h>
10 #include "mpitest.h"
11
12 #define NSTEPS 100
13 #define N 1000
14 #define M 10
15
16 /* This is example 11.21 from the MPI 3.0 spec:
17  *
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.
23  */
24
25 /* Use a global variable to inhibit compiler optimizations in the compute
26  * function. */
27 double junk = 0.0;
28 void compute(int step, double *data);
29 void compute(int step, double *data)
30 {
31     int i;
32
33     for (i = 0; i < N; i++)
34         junk += data[i] * (double) step;
35 }
36
37 int main(int argc, char *argv[])
38 {
39     int i, rank, nproc;
40     int errors = 0, all_errors = 0;
41     MPI_Win win;
42     MPI_Request put_req[M] = { MPI_REQUEST_NULL };
43     MPI_Request get_req;
44     double *baseptr;
45     double data[M][N];          /* M buffers of length N */
46     MPI_Info win_info;
47
48     MPI_Init(&argc, &argv);
49     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
50     MPI_Comm_size(MPI_COMM_WORLD, &nproc);
51
52     assert(M < NSTEPS);
53
54     MPI_Info_create(&win_info);
55
56 #ifdef USE_WIN_ALLOC_SHM
57     MPI_Info_set(win_info, (char*)"alloc_shm", (char*)"true");
58 #else
59     MPI_Info_set(win_info, (char*)"alloc_shm", (char*)"false");
60 #endif
61
62     MPI_Win_allocate(NSTEPS * N * sizeof(double), sizeof(double), win_info,
63                      MPI_COMM_WORLD, &baseptr, &win);
64
65     MPI_Win_lock_all(0, win);
66
67     for (i = 0; i < NSTEPS; i++) {
68         int target = (rank + 1) % nproc;
69         int j;
70
71         /* Find a free put request */
72         if (i < M) {
73             j = i;
74         }
75         else {
76             MPI_Waitany(M, put_req, &j, MPI_STATUS_IGNORE);
77         }
78
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);
81
82         compute(i, data[j]);
83         MPI_Rput(data[j], N, MPI_DOUBLE, target, i * N, N, MPI_DOUBLE, win, &put_req[j]);
84
85     }
86
87     MPI_Waitall(M, put_req, MPI_STATUSES_IGNORE);
88     MPI_Win_unlock_all(win);
89
90     MPI_Win_free(&win);
91
92     MPI_Info_free(&win_info);
93
94     MPI_Reduce(&errors, &all_errors, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
95
96     if (rank == 0 && all_errors == 0)
97         printf(" No Errors\n");
98
99     MPI_Finalize();
100
101     return 0;
102 }