Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
activate some more rma tests
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / manyrma2.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2010 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6
7 /* This test is a simplification of the one in perf/manyrma.c that tests
8    for correct handling of the case where many RMA operations occur between
9    synchronization events.
10    This is one of the ways that RMA may be used, and is used in the
11    reference implementation of the graph500 benchmark.
12 */
13 #include "mpi.h"
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #define MAX_COUNT 512
19 #define MAX_RMA_SIZE 2  /* 16 in manyrma performance test */
20 #define MAX_RUNS 8
21 #define MAX_ITER_TIME  5.0      /* seconds */
22
23 typedef enum { SYNC_NONE = 0,
24     SYNC_ALL = -1, SYNC_FENCE = 1, SYNC_LOCK = 2, SYNC_PSCW = 4
25 } sync_t;
26 typedef enum { RMA_NONE = 0, RMA_ALL = -1, RMA_PUT = 1, RMA_ACC = 2, RMA_GET = 4 } rma_t;
27 /* Note GET not yet implemented */
28 /* By default, run only a subset of the available tests, to keep the
29    total runtime reasonably short.  Command line arguments may be used
30    to run other tests. */
31 sync_t syncChoice = SYNC_FENCE;
32 rma_t rmaChoice = RMA_ACC;
33
34 static int verbose = 0;
35
36 void RunAccFence(MPI_Win win, int destRank, int cnt, int sz);
37 void RunAccLock(MPI_Win win, int destRank, int cnt, int sz);
38 void RunPutFence(MPI_Win win, int destRank, int cnt, int sz);
39 void RunPutLock(MPI_Win win, int destRank, int cnt, int sz);
40 void RunAccPSCW(MPI_Win win, int destRank, int cnt, int sz,
41                 MPI_Group exposureGroup, MPI_Group accessGroup);
42 void RunPutPSCW(MPI_Win win, int destRank, int cnt, int sz,
43                 MPI_Group exposureGroup, MPI_Group accessGroup);
44
45 int main(int argc, char *argv[])
46 {
47     int arraysize, i, cnt, sz, maxCount = MAX_COUNT, *arraybuffer;
48     int wrank, wsize, destRank, srcRank;
49     MPI_Win win;
50     MPI_Group wgroup, accessGroup, exposureGroup;
51     int maxSz = MAX_RMA_SIZE;
52     double start, end;
53
54     MPI_Init(&argc, &argv);
55
56     for (i = 1; i < argc; i++) {
57         if (strcmp(argv[i], "-put") == 0) {
58             if (rmaChoice == RMA_ALL)
59                 rmaChoice = RMA_NONE;
60             rmaChoice |= RMA_PUT;
61         }
62         else if (strcmp(argv[i], "-acc") == 0) {
63             if (rmaChoice == RMA_ALL)
64                 rmaChoice = RMA_NONE;
65             rmaChoice |= RMA_ACC;
66         }
67         else if (strcmp(argv[i], "-fence") == 0) {
68             if (syncChoice == SYNC_ALL)
69                 syncChoice = SYNC_NONE;
70             syncChoice |= SYNC_FENCE;
71         }
72         else if (strcmp(argv[i], "-lock") == 0) {
73             if (syncChoice == SYNC_ALL)
74                 syncChoice = SYNC_NONE;
75             syncChoice |= SYNC_LOCK;
76         }
77         else if (strcmp(argv[i], "-pscw") == 0) {
78             if (syncChoice == SYNC_ALL)
79                 syncChoice = SYNC_NONE;
80             syncChoice |= SYNC_PSCW;
81         }
82         else if (strcmp(argv[i], "-maxsz") == 0) {
83             i++;
84             maxSz = atoi(argv[i]);
85         }
86         else if (strcmp(argv[i], "-maxcount") == 0) {
87             i++;
88             maxCount = atoi(argv[i]);
89         }
90         else {
91             fprintf(stderr, "Unrecognized argument %s\n", argv[i]);
92             fprintf(stderr,
93                     "%s [ -put ] [ -acc ] [ -lock ] [ -fence ] [ -pscw ] [ -maxsz msgsize ]\n",
94                     argv[0]);
95             MPI_Abort(MPI_COMM_WORLD, 1);
96         }
97     }
98
99     MPI_Comm_rank(MPI_COMM_WORLD, &wrank);
100     MPI_Comm_size(MPI_COMM_WORLD, &wsize);
101     destRank = wrank + 1;
102     while (destRank >= wsize)
103         destRank = destRank - wsize;
104     srcRank = wrank - 1;
105     if (srcRank < 0)
106         srcRank += wsize;
107
108     /* Create groups for PSCW */
109     MPI_Comm_group(MPI_COMM_WORLD, &wgroup);
110     MPI_Group_incl(wgroup, 1, &destRank, &accessGroup);
111     MPI_Group_incl(wgroup, 1, &srcRank, &exposureGroup);
112     MPI_Group_free(&wgroup);
113
114     arraysize = maxSz * MAX_COUNT;
115 #ifdef USE_WIN_ALLOCATE
116     MPI_Win_allocate(arraysize * sizeof(int), (int) sizeof(int), MPI_INFO_NULL,
117                      MPI_COMM_WORLD, &arraybuffer, &win);
118     if (!arraybuffer) {
119         fprintf(stderr, "Unable to allocate %d words\n", arraysize);
120         MPI_Abort(MPI_COMM_WORLD, 1);
121     }
122 #else
123     arraybuffer = (int *) malloc(arraysize * sizeof(int));
124     if (!arraybuffer) {
125         fprintf(stderr, "Unable to allocate %d words\n", arraysize);
126         MPI_Abort(MPI_COMM_WORLD, 1);
127     }
128
129     MPI_Win_create(arraybuffer, arraysize * sizeof(int), (int) sizeof(int),
130                    MPI_INFO_NULL, MPI_COMM_WORLD, &win);
131 #endif
132
133     if (maxCount > MAX_COUNT) {
134         fprintf(stderr, "MaxCount must not exceed %d\n", MAX_COUNT);
135         MPI_Abort(MPI_COMM_WORLD, 1);
136     }
137
138     if ((syncChoice & SYNC_FENCE) && (rmaChoice & RMA_ACC)) {
139         for (sz = 1; sz <= maxSz; sz = sz + sz) {
140             if (wrank == 0 && verbose)
141                 printf("Accumulate with fence, %d elements\n", sz);
142             for (cnt = 1; cnt <= maxCount; cnt *= 2) {
143                 start = MPI_Wtime();
144                 RunAccFence(win, destRank, cnt, sz);
145                 end = MPI_Wtime();
146                 if (end - start > MAX_ITER_TIME)
147                     break;
148             }
149         }
150     }
151
152     if ((syncChoice & SYNC_LOCK) && (rmaChoice & RMA_ACC)) {
153         for (sz = 1; sz <= maxSz; sz = sz + sz) {
154             if (wrank == 0 && verbose)
155                 printf("Accumulate with lock, %d elements\n", sz);
156             for (cnt = 1; cnt <= maxCount; cnt *= 2) {
157                 start = MPI_Wtime();
158                 RunAccLock(win, destRank, cnt, sz);
159                 end = MPI_Wtime();
160                 if (end - start > MAX_ITER_TIME)
161                     break;
162             }
163         }
164     }
165
166     if ((syncChoice & SYNC_FENCE) && (rmaChoice & RMA_PUT)) {
167         for (sz = 1; sz <= maxSz; sz = sz + sz) {
168             if (wrank == 0 && verbose)
169                 printf("Put with fence, %d elements\n", sz);
170             for (cnt = 1; cnt <= maxCount; cnt *= 2) {
171                 start = MPI_Wtime();
172                 RunPutFence(win, destRank, cnt, sz);
173                 end = MPI_Wtime();
174                 if (end - start > MAX_ITER_TIME)
175                     break;
176             }
177         }
178     }
179
180     if ((syncChoice & SYNC_LOCK) && (rmaChoice & RMA_PUT)) {
181         for (sz = 1; sz <= maxSz; sz = sz + sz) {
182             if (wrank == 0 && verbose)
183                 printf("Put with lock, %d elements\n", sz);
184             for (cnt = 1; cnt <= maxCount; cnt *= 2) {
185                 start = MPI_Wtime();
186                 RunPutLock(win, destRank, cnt, sz);
187                 end = MPI_Wtime();
188                 if (end - start > MAX_ITER_TIME)
189                     break;
190             }
191         }
192     }
193
194     if ((syncChoice & SYNC_PSCW) && (rmaChoice & RMA_PUT)) {
195         for (sz = 1; sz <= maxSz; sz = sz + sz) {
196             if (wrank == 0 && verbose)
197                 printf("Put with pscw, %d elements\n", sz);
198             for (cnt = 1; cnt <= maxCount; cnt *= 2) {
199                 start = MPI_Wtime();
200                 RunPutPSCW(win, destRank, cnt, sz, exposureGroup, accessGroup);
201                 end = MPI_Wtime();
202                 if (end - start > MAX_ITER_TIME)
203                     break;
204             }
205         }
206     }
207
208     if ((syncChoice & SYNC_PSCW) && (rmaChoice & RMA_ACC)) {
209         for (sz = 1; sz <= maxSz; sz = sz + sz) {
210             if (wrank == 0 && verbose)
211                 printf("Accumulate with pscw, %d elements\n", sz);
212             for (cnt = 1; cnt <= maxCount; cnt *= 2) {
213                 start = MPI_Wtime();
214                 RunAccPSCW(win, destRank, cnt, sz, exposureGroup, accessGroup);
215                 end = MPI_Wtime();
216                 if (end - start > MAX_ITER_TIME)
217                     break;
218             }
219         }
220     }
221
222     MPI_Win_free(&win);
223
224 #ifndef USE_WIN_ALLOCATE
225     free(arraybuffer);
226 #endif
227
228     MPI_Group_free(&accessGroup);
229     MPI_Group_free(&exposureGroup);
230
231     /* If we get here without timing out or failing, we succeeded */
232     if (wrank == 0)
233         printf(" No Errors\n");
234
235     MPI_Finalize();
236     return 0;
237 }
238
239
240 void RunAccFence(MPI_Win win, int destRank, int cnt, int sz)
241 {
242     int k, i, j, one = 1;
243
244     for (k = 0; k < MAX_RUNS; k++) {
245         MPI_Barrier(MPI_COMM_WORLD);
246         MPI_Win_fence(0, win);
247         j = 0;
248         for (i = 0; i < cnt; i++) {
249             MPI_Accumulate(&one, sz, MPI_INT, destRank, j, sz, MPI_INT, MPI_SUM, win);
250             j += sz;
251         }
252         MPI_Win_fence(0, win);
253     }
254 }
255
256 void RunAccLock(MPI_Win win, int destRank, int cnt, int sz)
257 {
258     int k, i, j, one = 1;
259
260     for (k = 0; k < MAX_RUNS; k++) {
261         MPI_Barrier(MPI_COMM_WORLD);
262         MPI_Win_lock(MPI_LOCK_SHARED, destRank, 0, win);
263         j = 0;
264         for (i = 0; i < cnt; i++) {
265             MPI_Accumulate(&one, sz, MPI_INT, destRank, j, sz, MPI_INT, MPI_SUM, win);
266             j += sz;
267         }
268         MPI_Win_unlock(destRank, win);
269     }
270 }
271
272 void RunPutFence(MPI_Win win, int destRank, int cnt, int sz)
273 {
274     int k, i, j, one = 1;
275
276     for (k = 0; k < MAX_RUNS; k++) {
277         MPI_Barrier(MPI_COMM_WORLD);
278         MPI_Win_fence(0, win);
279         j = 0;
280         for (i = 0; i < cnt; i++) {
281             MPI_Put(&one, sz, MPI_INT, destRank, j, sz, MPI_INT, win);
282             j += sz;
283         }
284         MPI_Win_fence(0, win);
285     }
286 }
287
288 void RunPutLock(MPI_Win win, int destRank, int cnt, int sz)
289 {
290     int k, i, j, one = 1;
291
292     for (k = 0; k < MAX_RUNS; k++) {
293         MPI_Barrier(MPI_COMM_WORLD);
294         MPI_Win_lock(MPI_LOCK_SHARED, destRank, 0, win);
295         j = 0;
296         for (i = 0; i < cnt; i++) {
297             MPI_Put(&one, sz, MPI_INT, destRank, j, sz, MPI_INT, win);
298             j += sz;
299         }
300         MPI_Win_unlock(destRank, win);
301     }
302 }
303
304 void RunPutPSCW(MPI_Win win, int destRank, int cnt, int sz,
305                 MPI_Group exposureGroup, MPI_Group accessGroup)
306 {
307     int k, i, j, one = 1;
308
309     for (k = 0; k < MAX_RUNS; k++) {
310         MPI_Barrier(MPI_COMM_WORLD);
311         MPI_Win_post(exposureGroup, 0, win);
312         MPI_Win_start(accessGroup, 0, win);
313         j = 0;
314         for (i = 0; i < cnt; i++) {
315             MPI_Put(&one, sz, MPI_INT, destRank, j, sz, MPI_INT, win);
316             j += sz;
317         }
318         MPI_Win_complete(win);
319         MPI_Win_wait(win);
320     }
321 }
322
323 void RunAccPSCW(MPI_Win win, int destRank, int cnt, int sz,
324                 MPI_Group exposureGroup, MPI_Group accessGroup)
325 {
326     int k, i, j, one = 1;
327
328     for (k = 0; k < MAX_RUNS; k++) {
329         MPI_Barrier(MPI_COMM_WORLD);
330         MPI_Win_post(exposureGroup, 0, win);
331         MPI_Win_start(accessGroup, 0, win);
332         j = 0;
333         for (i = 0; i < cnt; i++) {
334             MPI_Accumulate(&one, sz, MPI_INT, destRank, j, sz, MPI_INT, MPI_SUM, win);
335             j += sz;
336         }
337         MPI_Win_complete(win);
338         MPI_Win_wait(win);
339     }
340 }