Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://github.com/mpoquet/simgrid
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / reqops.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 ITER 100
13
14 int main( int argc, char *argv[] )
15 {
16     int rank, nproc, i;
17     int errors = 0, all_errors = 0;
18     int *buf;
19     MPI_Win window;
20
21     MPI_Init(&argc, &argv);
22     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
23     MPI_Comm_size(MPI_COMM_WORLD, &nproc);
24
25     if (nproc < 2) {
26         if (rank == 0) printf("Error: must be run with two or more processes\n");
27         MPI_Abort(MPI_COMM_WORLD, 1);
28     }
29
30     /** Create using MPI_Win_create() **/
31
32     if (rank == 0) {
33       MPI_Alloc_mem(4*sizeof(int), MPI_INFO_NULL, &buf);
34       *buf = nproc-1;
35     } else
36       buf = NULL;
37
38     MPI_Win_create(buf, 4*sizeof(int)*(rank == 0), 1, MPI_INFO_NULL, MPI_COMM_WORLD, &window);
39
40     /* PROC_NULL Communication */
41     {
42         MPI_Request pn_req[4];
43         int val[4], res;
44
45         MPI_Win_lock_all(0, window);
46
47         MPI_Rget_accumulate(&val[0], 1, MPI_INT, &res, 1, MPI_INT, MPI_PROC_NULL, 0, 1, MPI_INT, MPI_REPLACE, window, &pn_req[0]);
48         MPI_Rget(&val[1], 1, MPI_INT, MPI_PROC_NULL, 1, 1, MPI_INT, window, &pn_req[1]);
49         MPI_Rput(&val[2], 1, MPI_INT, MPI_PROC_NULL, 2, 1, MPI_INT, window, &pn_req[2]);
50         MPI_Raccumulate(&val[3], 1, MPI_INT, MPI_PROC_NULL, 3, 1, MPI_INT, MPI_REPLACE, window, &pn_req[3]);
51
52         assert(pn_req[0] != MPI_REQUEST_NULL);
53         assert(pn_req[1] != MPI_REQUEST_NULL);
54         assert(pn_req[2] != MPI_REQUEST_NULL);
55         assert(pn_req[3] != MPI_REQUEST_NULL);
56
57         MPI_Win_unlock_all(window);
58
59         MPI_Waitall(4, pn_req, MPI_STATUSES_IGNORE);
60     }
61
62     MPI_Barrier(MPI_COMM_WORLD);
63
64     MPI_Win_lock(MPI_LOCK_SHARED, 0, 0, window);
65
66     /* GET-ACC: Test third-party communication, through rank 0. */
67     for (i = 0; i < ITER; i++) {
68         MPI_Request gacc_req;
69         int val = -1, exp = -1;
70
71         /* Processes form a ring.  Process 0 starts first, then passes a token
72          * to the right.  Each process, in turn, performs third-party
73          * communication via process 0's window. */
74         if (rank > 0) {
75             MPI_Recv(NULL, 0, MPI_BYTE, rank-1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
76         }
77
78         MPI_Rget_accumulate(&rank, 1, MPI_INT, &val, 1, MPI_INT, 0, 0, 1, MPI_INT, MPI_REPLACE, window, &gacc_req);
79         assert(gacc_req != MPI_REQUEST_NULL);
80         MPI_Wait(&gacc_req, MPI_STATUS_IGNORE);
81
82         exp = (rank + nproc-1) % nproc;
83
84         if (val != exp) {
85             printf("%d - Got %d, expected %d\n", rank, val, exp);
86             errors++;
87         }
88
89         if (rank < nproc-1) {
90             MPI_Send(NULL, 0, MPI_BYTE, rank+1, 0, MPI_COMM_WORLD);
91         }
92
93         MPI_Barrier(MPI_COMM_WORLD);
94     }
95
96     MPI_Barrier(MPI_COMM_WORLD);
97
98     if (rank == 0) *buf = nproc-1;
99     MPI_Win_sync(window);
100
101     /* GET+PUT: Test third-party communication, through rank 0. */
102     for (i = 0; i < ITER; i++) {
103         MPI_Request req;
104         int val = -1, exp = -1;
105
106         /* Processes form a ring.  Process 0 starts first, then passes a token
107          * to the right.  Each process, in turn, performs third-party
108          * communication via process 0's window. */
109         if (rank > 0) {
110             MPI_Recv(NULL, 0, MPI_BYTE, rank-1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
111         }
112
113         MPI_Rget(&val, 1, MPI_INT, 0, 0, 1, MPI_INT, window, &req);
114         assert(req != MPI_REQUEST_NULL);
115         MPI_Wait(&req, MPI_STATUS_IGNORE);
116
117         MPI_Rput(&rank, 1, MPI_INT, 0, 0, 1, MPI_INT, window, &req);
118         assert(req != MPI_REQUEST_NULL);
119         MPI_Wait(&req, MPI_STATUS_IGNORE);
120
121         exp = (rank + nproc-1) % nproc;
122
123         if (val != exp) {
124             printf("%d - Got %d, expected %d\n", rank, val, exp);
125             errors++;
126         }
127
128         if (rank < nproc-1) {
129             MPI_Send(NULL, 0, MPI_BYTE, rank+1, 0, MPI_COMM_WORLD);
130         }
131
132         MPI_Barrier(MPI_COMM_WORLD);
133     }
134
135     MPI_Barrier(MPI_COMM_WORLD);
136
137     if (rank == 0) *buf = nproc-1;
138     MPI_Win_sync(window);
139
140     /* GET+ACC: Test third-party communication, through rank 0. */
141     for (i = 0; i < ITER; i++) {
142         MPI_Request req;
143         int val = -1, exp = -1;
144
145         /* Processes form a ring.  Process 0 starts first, then passes a token
146          * to the right.  Each process, in turn, performs third-party
147          * communication via process 0's window. */
148         if (rank > 0) {
149             MPI_Recv(NULL, 0, MPI_BYTE, rank-1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
150         }
151
152         MPI_Rget(&val, 1, MPI_INT, 0, 0, 1, MPI_INT, window, &req);
153         assert(req != MPI_REQUEST_NULL);
154         MPI_Wait(&req, MPI_STATUS_IGNORE);
155
156         MPI_Raccumulate(&rank, 1, MPI_INT, 0, 0, 1, MPI_INT, MPI_REPLACE, window, &req);
157         assert(req != MPI_REQUEST_NULL);
158         MPI_Wait(&req, MPI_STATUS_IGNORE);
159
160         exp = (rank + nproc-1) % nproc;
161
162         if (val != exp) {
163             printf("%d - Got %d, expected %d\n", rank, val, exp);
164             errors++;
165         }
166
167         if (rank < nproc-1) {
168             MPI_Send(NULL, 0, MPI_BYTE, rank+1, 0, MPI_COMM_WORLD);
169         }
170
171         MPI_Barrier(MPI_COMM_WORLD);
172     }
173     MPI_Win_unlock(0, window);
174
175     MPI_Barrier(MPI_COMM_WORLD);
176
177     /* Wait inside of an epoch */
178     {
179         MPI_Request pn_req[4];
180         int val[4], res;
181         const int target = 0;
182
183         MPI_Win_lock_all(0, window);
184
185         MPI_Rget_accumulate(&val[0], 1, MPI_INT, &res, 1, MPI_INT, target, 0, 1, MPI_INT, MPI_REPLACE, window, &pn_req[0]);
186         MPI_Rget(&val[1], 1, MPI_INT, target, 1, 1, MPI_INT, window, &pn_req[1]);
187         MPI_Rput(&val[2], 1, MPI_INT, target, 2, 1, MPI_INT, window, &pn_req[2]);
188         MPI_Raccumulate(&val[3], 1, MPI_INT, target, 3, 1, MPI_INT, MPI_REPLACE, window, &pn_req[3]);
189
190         assert(pn_req[0] != MPI_REQUEST_NULL);
191         assert(pn_req[1] != MPI_REQUEST_NULL);
192         assert(pn_req[2] != MPI_REQUEST_NULL);
193         assert(pn_req[3] != MPI_REQUEST_NULL);
194
195         MPI_Waitall(4, pn_req, MPI_STATUSES_IGNORE);
196
197         MPI_Win_unlock_all(window);
198     }
199
200     MPI_Barrier(MPI_COMM_WORLD);
201
202     /* Wait outside of an epoch */
203     {
204         MPI_Request pn_req[4];
205         int val[4], res;
206         const int target = 0;
207
208         MPI_Win_lock_all(0, window);
209
210         MPI_Rget_accumulate(&val[0], 1, MPI_INT, &res, 1, MPI_INT, target, 0, 1, MPI_INT, MPI_REPLACE, window, &pn_req[0]);
211         MPI_Rget(&val[1], 1, MPI_INT, target, 1, 1, MPI_INT, window, &pn_req[1]);
212         MPI_Rput(&val[2], 1, MPI_INT, target, 2, 1, MPI_INT, window, &pn_req[2]);
213         MPI_Raccumulate(&val[3], 1, MPI_INT, target, 3, 1, MPI_INT, MPI_REPLACE, window, &pn_req[3]);
214
215         assert(pn_req[0] != MPI_REQUEST_NULL);
216         assert(pn_req[1] != MPI_REQUEST_NULL);
217         assert(pn_req[2] != MPI_REQUEST_NULL);
218         assert(pn_req[3] != MPI_REQUEST_NULL);
219
220         MPI_Win_unlock_all(window);
221
222         MPI_Waitall(4, pn_req, MPI_STATUSES_IGNORE);
223     }
224
225     /* Wait in a different epoch */
226     {
227         MPI_Request pn_req[4];
228         int val[4], res;
229         const int target = 0;
230
231         MPI_Win_lock_all(0, window);
232
233         MPI_Rget_accumulate(&val[0], 1, MPI_INT, &res, 1, MPI_INT, target, 0, 1, MPI_INT, MPI_REPLACE, window, &pn_req[0]);
234         MPI_Rget(&val[1], 1, MPI_INT, target, 1, 1, MPI_INT, window, &pn_req[1]);
235         MPI_Rput(&val[2], 1, MPI_INT, target, 2, 1, MPI_INT, window, &pn_req[2]);
236         MPI_Raccumulate(&val[3], 1, MPI_INT, target, 3, 1, MPI_INT, MPI_REPLACE, window, &pn_req[3]);
237
238         assert(pn_req[0] != MPI_REQUEST_NULL);
239         assert(pn_req[1] != MPI_REQUEST_NULL);
240         assert(pn_req[2] != MPI_REQUEST_NULL);
241         assert(pn_req[3] != MPI_REQUEST_NULL);
242
243         MPI_Win_unlock_all(window);
244
245         MPI_Win_lock_all(0, window);
246         MPI_Waitall(4, pn_req, MPI_STATUSES_IGNORE);
247         MPI_Win_unlock_all(window);
248     }
249
250     /* Wait in a fence epoch */
251     {
252         MPI_Request pn_req[4];
253         int val[4], res;
254         const int target = 0;
255
256         MPI_Win_lock_all(0, window);
257
258         MPI_Rget_accumulate(&val[0], 1, MPI_INT, &res, 1, MPI_INT, target, 0, 1, MPI_INT, MPI_REPLACE, window, &pn_req[0]);
259         MPI_Rget(&val[1], 1, MPI_INT, target, 1, 1, MPI_INT, window, &pn_req[1]);
260         MPI_Rput(&val[2], 1, MPI_INT, target, 2, 1, MPI_INT, window, &pn_req[2]);
261         MPI_Raccumulate(&val[3], 1, MPI_INT, target, 3, 1, MPI_INT, MPI_REPLACE, window, &pn_req[3]);
262
263         assert(pn_req[0] != MPI_REQUEST_NULL);
264         assert(pn_req[1] != MPI_REQUEST_NULL);
265         assert(pn_req[2] != MPI_REQUEST_NULL);
266         assert(pn_req[3] != MPI_REQUEST_NULL);
267
268         MPI_Win_unlock_all(window);
269
270         MPI_Win_fence(0, window);
271         MPI_Waitall(4, pn_req, MPI_STATUSES_IGNORE);
272         MPI_Win_fence(0, window);
273     }
274
275     MPI_Win_free(&window);
276     if (buf) MPI_Free_mem(buf);
277
278     MPI_Reduce(&errors, &all_errors, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
279
280     if (rank == 0 && all_errors == 0)
281         printf(" No Errors\n");
282
283     MPI_Finalize();
284
285     return 0;
286 }