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);
26 if (rank == 0) printf("Error: must be run with two or more processes\n");
27 MPI_Abort(MPI_COMM_WORLD, 1);
30 /** Create using MPI_Win_create() **/
33 MPI_Alloc_mem(4*sizeof(int), MPI_INFO_NULL, &buf);
38 MPI_Win_create(buf, 4*sizeof(int)*(rank == 0), 1, MPI_INFO_NULL, MPI_COMM_WORLD, &window);
40 /* PROC_NULL Communication */
42 MPI_Request pn_req[4];
45 MPI_Win_lock_all(0, window);
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]);
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);
57 MPI_Win_unlock_all(window);
59 MPI_Waitall(4, pn_req, MPI_STATUSES_IGNORE);
62 MPI_Barrier(MPI_COMM_WORLD);
64 MPI_Win_lock(MPI_LOCK_SHARED, 0, 0, window);
66 /* GET-ACC: Test third-party communication, through rank 0. */
67 for (i = 0; i < ITER; i++) {
69 int val = -1, exp = -1;
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. */
75 MPI_Recv(NULL, 0, MPI_BYTE, rank-1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
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);
82 exp = (rank + nproc-1) % nproc;
85 printf("%d - Got %d, expected %d\n", rank, val, exp);
90 MPI_Send(NULL, 0, MPI_BYTE, rank+1, 0, MPI_COMM_WORLD);
93 MPI_Barrier(MPI_COMM_WORLD);
96 MPI_Barrier(MPI_COMM_WORLD);
98 if (rank == 0) *buf = nproc-1;
101 /* GET+PUT: Test third-party communication, through rank 0. */
102 for (i = 0; i < ITER; i++) {
104 int val = -1, exp = -1;
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. */
110 MPI_Recv(NULL, 0, MPI_BYTE, rank-1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
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);
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);
121 exp = (rank + nproc-1) % nproc;
124 printf("%d - Got %d, expected %d\n", rank, val, exp);
128 if (rank < nproc-1) {
129 MPI_Send(NULL, 0, MPI_BYTE, rank+1, 0, MPI_COMM_WORLD);
132 MPI_Barrier(MPI_COMM_WORLD);
135 MPI_Barrier(MPI_COMM_WORLD);
137 if (rank == 0) *buf = nproc-1;
138 MPI_Win_sync(window);
140 /* GET+ACC: Test third-party communication, through rank 0. */
141 for (i = 0; i < ITER; i++) {
143 int val = -1, exp = -1;
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. */
149 MPI_Recv(NULL, 0, MPI_BYTE, rank-1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
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);
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);
160 exp = (rank + nproc-1) % nproc;
163 printf("%d - Got %d, expected %d\n", rank, val, exp);
167 if (rank < nproc-1) {
168 MPI_Send(NULL, 0, MPI_BYTE, rank+1, 0, MPI_COMM_WORLD);
171 MPI_Barrier(MPI_COMM_WORLD);
173 MPI_Win_unlock(0, window);
175 MPI_Barrier(MPI_COMM_WORLD);
177 /* Wait inside of an epoch */
179 MPI_Request pn_req[4];
181 const int target = 0;
183 MPI_Win_lock_all(0, window);
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]);
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);
195 MPI_Waitall(4, pn_req, MPI_STATUSES_IGNORE);
197 MPI_Win_unlock_all(window);
200 MPI_Barrier(MPI_COMM_WORLD);
202 /* Wait outside of an epoch */
204 MPI_Request pn_req[4];
206 const int target = 0;
208 MPI_Win_lock_all(0, window);
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]);
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);
220 MPI_Win_unlock_all(window);
222 MPI_Waitall(4, pn_req, MPI_STATUSES_IGNORE);
225 /* Wait in a different epoch */
227 MPI_Request pn_req[4];
229 const int target = 0;
231 MPI_Win_lock_all(0, window);
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]);
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);
243 MPI_Win_unlock_all(window);
245 MPI_Win_lock_all(0, window);
246 MPI_Waitall(4, pn_req, MPI_STATUSES_IGNORE);
247 MPI_Win_unlock_all(window);
250 /* Wait in a fence epoch */
252 MPI_Request pn_req[4];
254 const int target = 0;
256 MPI_Win_lock_all(0, window);
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]);
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);
268 MPI_Win_unlock_all(window);
270 MPI_Win_fence(0, window);
271 MPI_Waitall(4, pn_req, MPI_STATUSES_IGNORE);
272 MPI_Win_fence(0, window);
275 MPI_Win_free(&window);
276 if (buf) MPI_Free_mem(buf);
278 MPI_Reduce(&errors, &all_errors, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
280 if (rank == 0 && all_errors == 0)
281 printf(" No Errors\n");