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.
14 int main(int argc, char *argv[])
17 int errors = 0, all_errors = 0;
21 MPI_Init(&argc, &argv);
22 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
23 MPI_Comm_size(MPI_COMM_WORLD, &nproc);
27 printf("Error: must be run with two or more processes\n");
28 MPI_Abort(MPI_COMM_WORLD, 1);
31 /** Create using MPI_Win_create() **/
34 MPI_Alloc_mem(4 * sizeof(int), MPI_INFO_NULL, &buf);
40 MPI_Win_create(buf, 4 * sizeof(int) * (rank == 0), sizeof(int),
41 MPI_INFO_NULL, MPI_COMM_WORLD, &window);
43 /* PROC_NULL Communication */
45 MPI_Request pn_req[4];
48 MPI_Win_lock_all(0, window);
50 MPI_Rget_accumulate(&val[0], 1, MPI_INT, &res, 1, MPI_INT, MPI_PROC_NULL, 0, 1, MPI_INT,
51 MPI_REPLACE, window, &pn_req[0]);
52 MPI_Rget(&val[1], 1, MPI_INT, MPI_PROC_NULL, 1, 1, MPI_INT, window, &pn_req[1]);
53 MPI_Rput(&val[2], 1, MPI_INT, MPI_PROC_NULL, 2, 1, MPI_INT, window, &pn_req[2]);
54 MPI_Raccumulate(&val[3], 1, MPI_INT, MPI_PROC_NULL, 3, 1, MPI_INT, MPI_REPLACE, window,
57 assert(pn_req[0] != MPI_REQUEST_NULL);
58 assert(pn_req[1] != MPI_REQUEST_NULL);
59 assert(pn_req[2] != MPI_REQUEST_NULL);
60 assert(pn_req[3] != MPI_REQUEST_NULL);
62 MPI_Win_unlock_all(window);
64 MPI_Waitall(4, pn_req, MPI_STATUSES_IGNORE);
67 MPI_Barrier(MPI_COMM_WORLD);
69 MPI_Win_lock(MPI_LOCK_SHARED, 0, 0, window);
71 /* GET-ACC: Test third-party communication, through rank 0. */
72 for (i = 0; i < ITER; i++) {
74 int val = -1, exp = -1;
76 /* Processes form a ring. Process 0 starts first, then passes a token
77 * to the right. Each process, in turn, performs third-party
78 * communication via process 0's window. */
80 MPI_Recv(NULL, 0, MPI_BYTE, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
83 MPI_Rget_accumulate(&rank, 1, MPI_INT, &val, 1, MPI_INT, 0, 0, 1, MPI_INT, MPI_REPLACE,
85 assert(gacc_req != MPI_REQUEST_NULL);
86 MPI_Wait(&gacc_req, MPI_STATUS_IGNORE);
88 exp = (rank + nproc - 1) % nproc;
91 printf("%d - Got %d, expected %d\n", rank, val, exp);
95 if (rank < nproc - 1) {
96 MPI_Send(NULL, 0, MPI_BYTE, rank + 1, 0, MPI_COMM_WORLD);
99 MPI_Barrier(MPI_COMM_WORLD);
102 MPI_Barrier(MPI_COMM_WORLD);
106 MPI_Win_sync(window);
108 /* GET+PUT: Test third-party communication, through rank 0. */
109 for (i = 0; i < ITER; i++) {
111 int val = -1, exp = -1;
113 /* Processes form a ring. Process 0 starts first, then passes a token
114 * to the right. Each process, in turn, performs third-party
115 * communication via process 0's window. */
117 MPI_Recv(NULL, 0, MPI_BYTE, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
120 MPI_Rget(&val, 1, MPI_INT, 0, 0, 1, MPI_INT, window, &req);
121 assert(req != MPI_REQUEST_NULL);
122 MPI_Wait(&req, MPI_STATUS_IGNORE);
124 /* Use flush to guarantee remote completion */
125 MPI_Put(&rank, 1, MPI_INT, 0, 0, 1, MPI_INT, window);
126 MPI_Win_flush(0, window);
128 exp = (rank + nproc - 1) % nproc;
131 printf("%d - Got %d, expected %d\n", rank, val, exp);
135 if (rank < nproc - 1) {
136 MPI_Send(NULL, 0, MPI_BYTE, rank + 1, 0, MPI_COMM_WORLD);
139 MPI_Barrier(MPI_COMM_WORLD);
142 MPI_Barrier(MPI_COMM_WORLD);
146 MPI_Win_sync(window);
148 /* GET+ACC: Test third-party communication, through rank 0. */
149 for (i = 0; i < ITER; i++) {
151 int val = -1, exp = -1;
153 /* Processes form a ring. Process 0 starts first, then passes a token
154 * to the right. Each process, in turn, performs third-party
155 * communication via process 0's window. */
157 MPI_Recv(NULL, 0, MPI_BYTE, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
160 MPI_Rget(&val, 1, MPI_INT, 0, 0, 1, MPI_INT, window, &req);
161 assert(req != MPI_REQUEST_NULL);
162 MPI_Wait(&req, MPI_STATUS_IGNORE);
164 /* Use flush to guarantee remote completion */
165 MPI_Accumulate(&rank, 1, MPI_INT, 0, 0, 1, MPI_INT, MPI_REPLACE, window);
166 MPI_Win_flush(0, window);
168 exp = (rank + nproc - 1) % nproc;
171 printf("%d - Got %d, expected %d\n", rank, val, exp);
175 if (rank < nproc - 1) {
176 MPI_Send(NULL, 0, MPI_BYTE, rank + 1, 0, MPI_COMM_WORLD);
179 MPI_Barrier(MPI_COMM_WORLD);
181 MPI_Win_unlock(0, window);
183 MPI_Barrier(MPI_COMM_WORLD);
185 /* Wait inside of an epoch */
187 MPI_Request pn_req[4];
189 const int target = 0;
191 MPI_Win_lock_all(0, window);
193 MPI_Rget_accumulate(&val[0], 1, MPI_INT, &res, 1, MPI_INT, target, 0, 1, MPI_INT,
194 MPI_REPLACE, window, &pn_req[0]);
195 MPI_Rget(&val[1], 1, MPI_INT, target, 1, 1, MPI_INT, window, &pn_req[1]);
196 MPI_Rput(&val[2], 1, MPI_INT, target, 2, 1, MPI_INT, window, &pn_req[2]);
197 MPI_Raccumulate(&val[3], 1, MPI_INT, target, 3, 1, MPI_INT, MPI_REPLACE, window,
200 assert(pn_req[0] != MPI_REQUEST_NULL);
201 assert(pn_req[1] != MPI_REQUEST_NULL);
202 assert(pn_req[2] != MPI_REQUEST_NULL);
203 assert(pn_req[3] != MPI_REQUEST_NULL);
205 MPI_Waitall(4, pn_req, MPI_STATUSES_IGNORE);
207 MPI_Win_unlock_all(window);
210 MPI_Barrier(MPI_COMM_WORLD);
212 /* Wait outside of an epoch */
214 MPI_Request pn_req[4];
216 const int target = 0;
218 MPI_Win_lock_all(0, window);
220 MPI_Rget_accumulate(&val[0], 1, MPI_INT, &res, 1, MPI_INT, target, 0, 1, MPI_INT,
221 MPI_REPLACE, window, &pn_req[0]);
222 MPI_Rget(&val[1], 1, MPI_INT, target, 1, 1, MPI_INT, window, &pn_req[1]);
223 MPI_Rput(&val[2], 1, MPI_INT, target, 2, 1, MPI_INT, window, &pn_req[2]);
224 MPI_Raccumulate(&val[3], 1, MPI_INT, target, 3, 1, MPI_INT, MPI_REPLACE, window,
227 assert(pn_req[0] != MPI_REQUEST_NULL);
228 assert(pn_req[1] != MPI_REQUEST_NULL);
229 assert(pn_req[2] != MPI_REQUEST_NULL);
230 assert(pn_req[3] != MPI_REQUEST_NULL);
232 MPI_Win_unlock_all(window);
234 MPI_Waitall(4, pn_req, MPI_STATUSES_IGNORE);
237 /* Wait in a different epoch */
239 MPI_Request pn_req[4];
241 const int target = 0;
243 MPI_Win_lock_all(0, window);
245 MPI_Rget_accumulate(&val[0], 1, MPI_INT, &res, 1, MPI_INT, target, 0, 1, MPI_INT,
246 MPI_REPLACE, window, &pn_req[0]);
247 MPI_Rget(&val[1], 1, MPI_INT, target, 1, 1, MPI_INT, window, &pn_req[1]);
248 MPI_Rput(&val[2], 1, MPI_INT, target, 2, 1, MPI_INT, window, &pn_req[2]);
249 MPI_Raccumulate(&val[3], 1, MPI_INT, target, 3, 1, MPI_INT, MPI_REPLACE, window,
252 assert(pn_req[0] != MPI_REQUEST_NULL);
253 assert(pn_req[1] != MPI_REQUEST_NULL);
254 assert(pn_req[2] != MPI_REQUEST_NULL);
255 assert(pn_req[3] != MPI_REQUEST_NULL);
257 MPI_Win_unlock_all(window);
259 MPI_Win_lock_all(0, window);
260 MPI_Waitall(4, pn_req, MPI_STATUSES_IGNORE);
261 MPI_Win_unlock_all(window);
264 /* Wait in a fence epoch */
266 MPI_Request pn_req[4];
268 const int target = 0;
270 MPI_Win_lock_all(0, window);
272 MPI_Rget_accumulate(&val[0], 1, MPI_INT, &res, 1, MPI_INT, target, 0, 1, MPI_INT,
273 MPI_REPLACE, window, &pn_req[0]);
274 MPI_Rget(&val[1], 1, MPI_INT, target, 1, 1, MPI_INT, window, &pn_req[1]);
275 MPI_Rput(&val[2], 1, MPI_INT, target, 2, 1, MPI_INT, window, &pn_req[2]);
276 MPI_Raccumulate(&val[3], 1, MPI_INT, target, 3, 1, MPI_INT, MPI_REPLACE, window,
279 assert(pn_req[0] != MPI_REQUEST_NULL);
280 assert(pn_req[1] != MPI_REQUEST_NULL);
281 assert(pn_req[2] != MPI_REQUEST_NULL);
282 assert(pn_req[3] != MPI_REQUEST_NULL);
284 MPI_Win_unlock_all(window);
286 MPI_Win_fence(0, window);
287 MPI_Waitall(4, pn_req, MPI_STATUSES_IGNORE);
288 MPI_Win_fence(0, window);
291 MPI_Win_free(&window);
295 MPI_Reduce(&errors, &all_errors, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
297 if (rank == 0 && all_errors == 0)
298 printf(" No Errors\n");