1 | #include "ep_lib.hpp" |
---|
2 | |
---|
3 | #include <stdio.h> |
---|
4 | #include <assert.h> |
---|
5 | #include "ep_declaration.hpp" |
---|
6 | #include <omp.h> |
---|
7 | #include <time.h> /* time */ |
---|
8 | #include <ctime> |
---|
9 | #include <ratio> |
---|
10 | #include <chrono> |
---|
11 | |
---|
12 | using namespace ep_lib; |
---|
13 | using namespace std::chrono; |
---|
14 | |
---|
15 | |
---|
16 | |
---|
17 | int main(int argc, char **argv) |
---|
18 | { |
---|
19 | srand (time(NULL)); |
---|
20 | |
---|
21 | //printf("Testing ep_lib\n"); |
---|
22 | int required=3, provided; |
---|
23 | |
---|
24 | MPI_Init_thread(&argc, &argv, required, &provided); |
---|
25 | |
---|
26 | assert(required==provided); |
---|
27 | |
---|
28 | int mpi_rank; |
---|
29 | int mpi_size; |
---|
30 | |
---|
31 | MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank); |
---|
32 | MPI_Comm_size(MPI_COMM_WORLD, &mpi_size); |
---|
33 | |
---|
34 | |
---|
35 | |
---|
36 | #pragma omp parallel default(shared) |
---|
37 | { |
---|
38 | MPI_Comm_rank(MPI_COMM_WORLD , &mpi_rank); |
---|
39 | |
---|
40 | int num_ep = omp_get_num_threads(); |
---|
41 | MPI_Info info; |
---|
42 | |
---|
43 | //printf("mpi_rank = %d, thread_num = %d\n", mpi_rank, omp_get_thread_num()); |
---|
44 | |
---|
45 | MPI_Comm *ep_comm; |
---|
46 | #pragma omp master |
---|
47 | { |
---|
48 | MPI_Comm *ep_comm; |
---|
49 | MPI_Comm_create_endpoints(MPI_COMM_WORLD->mpi_comm, num_ep, info, ep_comm); |
---|
50 | passage = ep_comm; |
---|
51 | } |
---|
52 | |
---|
53 | #pragma omp barrier |
---|
54 | |
---|
55 | |
---|
56 | MPI_Comm comm_for_dup; // this should act as EP_COMM_WORLD |
---|
57 | MPI_Comm comm; // this should act as EP_COMM_WORLD |
---|
58 | |
---|
59 | comm_for_dup = passage[omp_get_thread_num()]; |
---|
60 | MPI_Comm_dup(comm_for_dup, &comm); |
---|
61 | |
---|
62 | MPI_Comm_free(&comm_for_dup); |
---|
63 | |
---|
64 | MPI_Barrier(comm); |
---|
65 | |
---|
66 | int rank, size; |
---|
67 | MPI_Comm_rank(comm, &rank); |
---|
68 | MPI_Comm_size(comm, &size); |
---|
69 | |
---|
70 | if(rank == 0) printf(" \t test MPI_Comm_dup \t OK \n"); |
---|
71 | |
---|
72 | // TIMING SYCHRONIZATION |
---|
73 | { |
---|
74 | int n=100000; |
---|
75 | |
---|
76 | MPI_Barrier(comm); |
---|
77 | |
---|
78 | high_resolution_clock::time_point t1 = high_resolution_clock::now(); |
---|
79 | |
---|
80 | for(int i=0; i<n; i++) |
---|
81 | MPI_Barrier_local(comm); |
---|
82 | |
---|
83 | high_resolution_clock::time_point t2 = high_resolution_clock::now(); |
---|
84 | duration<double> time_span = duration_cast<duration<double>>(t2 - t1); |
---|
85 | #pragma omp master |
---|
86 | std::cout << "proc "<< mpi_rank <<" ep_barrier "<< time_span.count() << " seconds."<<std::endl; |
---|
87 | |
---|
88 | t1 = high_resolution_clock::now(); |
---|
89 | |
---|
90 | for(int i=0; i<n; i++) |
---|
91 | { |
---|
92 | #pragma omp barrier |
---|
93 | } |
---|
94 | |
---|
95 | t2 = high_resolution_clock::now(); |
---|
96 | time_span = duration_cast<duration<double>>(t2 - t1); |
---|
97 | |
---|
98 | #pragma omp master |
---|
99 | std::cout << "proc "<< mpi_rank <<" omp_barrier "<< time_span.count() << " seconds."<<std::endl; |
---|
100 | |
---|
101 | t1 = high_resolution_clock::now(); |
---|
102 | |
---|
103 | for(int i=0; i<n; i++) |
---|
104 | { |
---|
105 | //#pragma omp barrier |
---|
106 | } |
---|
107 | |
---|
108 | t2 = high_resolution_clock::now(); |
---|
109 | time_span = duration_cast<duration<double>>(t2 - t1); |
---|
110 | |
---|
111 | MPI_Barrier(comm); |
---|
112 | |
---|
113 | #pragma omp master |
---|
114 | std::cout << "proc "<< mpi_rank <<" for_loop "<< time_span.count() << " seconds."<<std::endl; |
---|
115 | }// END TIMING SYCHRONIZATION |
---|
116 | |
---|
117 | // TEST of p2p blocking communication |
---|
118 | { |
---|
119 | MPI_Barrier(comm); |
---|
120 | MPI_Barrier(comm); |
---|
121 | |
---|
122 | MPI_Comm equal_comm = comm; |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | |
---|
127 | double sendbuf[10]; |
---|
128 | double recvbuf[20]; |
---|
129 | |
---|
130 | int sender; |
---|
131 | if(rank == 0) sender = rand() % size; |
---|
132 | MPI_Bcast(&sender, 1, MPI_INT, 0, comm); |
---|
133 | |
---|
134 | int receiver = sender; |
---|
135 | if(rank == 0) while(sender == receiver) {receiver = rand() % size;} |
---|
136 | MPI_Bcast(&receiver, 1, MPI_INT, 0, comm); |
---|
137 | |
---|
138 | |
---|
139 | |
---|
140 | |
---|
141 | if(rank == sender) |
---|
142 | { |
---|
143 | for(int i=0; i<10; i++) sendbuf[i] = 99.99; |
---|
144 | MPI_Send(sendbuf, 10, MPI_DOUBLE, receiver, 99, equal_comm); |
---|
145 | for(int i=0; i<10; i++) sendbuf[i] = -99.99; |
---|
146 | MPI_Send(sendbuf, 10, MPI_DOUBLE, receiver, 11, equal_comm); |
---|
147 | } |
---|
148 | |
---|
149 | if(rank == receiver) |
---|
150 | { |
---|
151 | MPI_Status status; |
---|
152 | for(int i=0; i<20; i++) recvbuf[i] = 0.0; |
---|
153 | MPI_Recv(&recvbuf[10], 10, MPI_DOUBLE, sender, 99, comm, &status); |
---|
154 | MPI_Recv(recvbuf, 10, MPI_DOUBLE, sender, 11, comm, &status); |
---|
155 | |
---|
156 | for(int i=0; i<20; i++) std::cout << "recvbuf["<< i <<"] = "<< recvbuf[i] << std::endl; |
---|
157 | printf("sender = %d\nreceiver = %d \tTEST of p2p blocking communication\tOK\n", sender, receiver); |
---|
158 | } |
---|
159 | |
---|
160 | MPI_Barrier(comm); |
---|
161 | |
---|
162 | }//TEST of p2p blocking communication |
---|
163 | |
---|
164 | // TEST of p2p non-blocking communication |
---|
165 | { |
---|
166 | MPI_Barrier(comm); |
---|
167 | MPI_Barrier(comm); |
---|
168 | |
---|
169 | double sendbuf[10]; |
---|
170 | double recvbuf[20]; |
---|
171 | |
---|
172 | int sender; |
---|
173 | if(rank == 0) sender = rand() % size; |
---|
174 | MPI_Bcast(&sender, 1, MPI_INT, 0, comm); |
---|
175 | |
---|
176 | int receiver = sender; |
---|
177 | if(rank == 0) receiver = rand() % size; |
---|
178 | MPI_Bcast(&receiver, 1, MPI_INT, 0, comm); |
---|
179 | |
---|
180 | |
---|
181 | |
---|
182 | MPI_Request request[2]; |
---|
183 | |
---|
184 | if(rank == sender) |
---|
185 | { |
---|
186 | |
---|
187 | for(int i=0; i<10; i++) sendbuf[i] = 99.99; |
---|
188 | MPI_Isend(sendbuf, 10, MPI_DOUBLE, receiver, 99, comm, &request[0]); |
---|
189 | for(int i=0; i<10; i++) sendbuf[i] = -99.99; |
---|
190 | MPI_Isend(sendbuf, 10, MPI_DOUBLE, receiver, 11, comm, &request[1]); |
---|
191 | } |
---|
192 | |
---|
193 | if(rank == receiver) |
---|
194 | { |
---|
195 | for(int i=0; i<20; i++) recvbuf[i] = 0.0; |
---|
196 | MPI_Irecv(&recvbuf[10], 10, MPI_DOUBLE, sender, 11, comm, &request[0]); |
---|
197 | MPI_Irecv(recvbuf, 10, MPI_DOUBLE, sender, 99, comm, &request[1]); |
---|
198 | } |
---|
199 | |
---|
200 | MPI_Barrier(comm); |
---|
201 | |
---|
202 | if(rank == receiver || rank == sender) |
---|
203 | { |
---|
204 | MPI_Status status[2]; |
---|
205 | MPI_Waitall(2, request, status); |
---|
206 | } |
---|
207 | |
---|
208 | MPI_Barrier(comm); |
---|
209 | |
---|
210 | if(rank == receiver) |
---|
211 | { |
---|
212 | for(int i=0; i<20; i++) std::cout << "recvbuf["<< i <<"] = "<< recvbuf[i] << std::endl; |
---|
213 | printf("sender = %d\nreceiver = %d \tTEST of p2p non-blocking communication\tOK\n", sender, receiver); |
---|
214 | } |
---|
215 | |
---|
216 | }//TEST of p2p blocking communication |
---|
217 | |
---|
218 | |
---|
219 | // TEST OF BCAST FROM A RANDOM ROOT |
---|
220 | { |
---|
221 | int bcast_root; |
---|
222 | |
---|
223 | if(rank == 0) bcast_root = rand() % size; |
---|
224 | |
---|
225 | MPI_Bcast(&bcast_root, 1, MPI_INT, 0, comm); |
---|
226 | |
---|
227 | int sendbuf[2]; |
---|
228 | |
---|
229 | sendbuf[0] = rank; |
---|
230 | sendbuf[1] = size; |
---|
231 | |
---|
232 | MPI_Bcast(sendbuf, 2, MPI_INT, bcast_root, comm); |
---|
233 | |
---|
234 | int bcast_test = 0; |
---|
235 | if(sendbuf[0] == bcast_root && sendbuf[1] == size) bcast_test = 1; |
---|
236 | |
---|
237 | int bcast_result; |
---|
238 | |
---|
239 | MPI_Reduce(&bcast_test, &bcast_result, 1, MPI_INT, MPI_MIN, bcast_root, comm); |
---|
240 | |
---|
241 | if(bcast_result && rank == bcast_root) printf("root = %d : \t test MPI_Bcast \t OK\n", bcast_root); |
---|
242 | if(!bcast_result && rank == bcast_root) printf("root = %d : \t test MPI_Bcast \t FAILED %d\n", bcast_root, bcast_result); |
---|
243 | } |
---|
244 | |
---|
245 | MPI_Barrier(comm); |
---|
246 | |
---|
247 | // TEST OF GATHER FROM A RAMDOM ROOT |
---|
248 | { |
---|
249 | int gather_root; |
---|
250 | |
---|
251 | if(rank == 0) gather_root = rand() % size; |
---|
252 | |
---|
253 | MPI_Bcast(&gather_root, 1, MPI_INT, 0, comm); |
---|
254 | |
---|
255 | double sendbuf[2]; |
---|
256 | sendbuf[0] = rank * 1.0; |
---|
257 | sendbuf[1] = size * (-1.0); |
---|
258 | |
---|
259 | std::vector<double>recvbuf(2*size, 0); |
---|
260 | |
---|
261 | MPI_Gather(sendbuf, 2, MPI_DOUBLE, recvbuf.data(), 2, MPI_DOUBLE, gather_root, comm); |
---|
262 | |
---|
263 | bool gather_result = true; |
---|
264 | |
---|
265 | if(rank == gather_root) |
---|
266 | { |
---|
267 | for(int i=0; i<size; i++) |
---|
268 | { |
---|
269 | if(abs(recvbuf[2*i] - i) > 1.e-10 || abs(recvbuf[2*i+1] + size) > 1.e-10) |
---|
270 | { |
---|
271 | gather_result = false; |
---|
272 | break; |
---|
273 | } |
---|
274 | } |
---|
275 | |
---|
276 | if(gather_result) printf("root = %d : \t test MPI_Gather \t OK \n", gather_root); |
---|
277 | else printf("root = %d : \t test MPI_Gather \t FAILED\n", gather_root); |
---|
278 | } |
---|
279 | } |
---|
280 | |
---|
281 | MPI_Barrier(comm); |
---|
282 | |
---|
283 | // TEST OF GATHERV FROM A RAMDOM ROOT |
---|
284 | { |
---|
285 | int gatherv_root; |
---|
286 | |
---|
287 | if(rank == 0) gatherv_root = rand() % size; |
---|
288 | |
---|
289 | MPI_Bcast(&gatherv_root, 1, MPI_INT, 0, comm); |
---|
290 | |
---|
291 | int sendbuf[2]; |
---|
292 | sendbuf[0] = rank; |
---|
293 | sendbuf[1] = -size; |
---|
294 | |
---|
295 | std::vector<int>recvbuf(2*size, 0); |
---|
296 | |
---|
297 | std::vector<int>recvcounts(size, 2); |
---|
298 | std::vector<int>displs(size, 0); |
---|
299 | |
---|
300 | for(int i=0; i<size; i++) displs[i] = 2*(size-1-i); |
---|
301 | |
---|
302 | MPI_Gatherv(sendbuf, 2, MPI_INT, recvbuf.data(), recvcounts.data(), displs.data(), MPI_INT, gatherv_root, comm); |
---|
303 | |
---|
304 | bool gatherv_result = true; |
---|
305 | |
---|
306 | if(rank == gatherv_root) |
---|
307 | { |
---|
308 | for(int i=0; i<size; i++) |
---|
309 | { |
---|
310 | if(abs(recvbuf[2*i] - (size-1-i)) > 1.e-10 || abs(recvbuf[2*i+1] + size) > 1.e-10) |
---|
311 | { |
---|
312 | gatherv_result = false; printf("%lf %lf root = %d, i = %d\n", recvbuf[2*i], recvbuf[2*i+1], gatherv_root, i); |
---|
313 | break; |
---|
314 | } |
---|
315 | } |
---|
316 | |
---|
317 | //for(int i=0; i<size*2; i++) printf("%lf\t", recvbuf[i]); |
---|
318 | //printf("\n"); |
---|
319 | |
---|
320 | if(gatherv_result) printf("root = %d : \t test MPI_Gatherv \t OK\n", gatherv_root); |
---|
321 | else printf("root = %d : \t test MPI_Gatherv \t FAILED\n", gatherv_root); |
---|
322 | } |
---|
323 | } |
---|
324 | |
---|
325 | MPI_Barrier(comm); |
---|
326 | |
---|
327 | // TEST OF ALLGATHER |
---|
328 | { |
---|
329 | double sendbuf[2]; |
---|
330 | sendbuf[0] = rank * 1.0; |
---|
331 | sendbuf[1] = size * (-1.0); |
---|
332 | |
---|
333 | std::vector<double>recvbuf(2*size, 0); |
---|
334 | |
---|
335 | MPI_Allgather(sendbuf, 2, MPI_DOUBLE, recvbuf.data(), 2, MPI_DOUBLE, comm); |
---|
336 | |
---|
337 | int allgather_test = 1; |
---|
338 | |
---|
339 | for(int i=0; i<size; i++) |
---|
340 | { |
---|
341 | if(abs(recvbuf[2*i] - i) > 1.e-10 || abs(recvbuf[2*i+1] + size) > 1.e-10) |
---|
342 | { |
---|
343 | allgather_test = 0; |
---|
344 | break; |
---|
345 | } |
---|
346 | } |
---|
347 | |
---|
348 | int allgather_result; |
---|
349 | MPI_Reduce(&allgather_test, &allgather_result, 1, MPI_INT, MPI_MIN, 0, comm); |
---|
350 | |
---|
351 | if(rank == 0 && allgather_result) printf(" \t test MPI_Allgather \t OK \n"); |
---|
352 | if(rank == 0 && !allgather_result) printf(" \t test MPI_Allgather \t OK \n"); |
---|
353 | |
---|
354 | } |
---|
355 | |
---|
356 | MPI_Barrier(comm); |
---|
357 | |
---|
358 | // TEST OF ALLGATHERV |
---|
359 | { |
---|
360 | int sendbuf[2]; |
---|
361 | sendbuf[0] = rank; |
---|
362 | sendbuf[1] = -size; |
---|
363 | |
---|
364 | std::vector<int>recvbuf(2*size, 0); |
---|
365 | |
---|
366 | std::vector<int>recvcounts(size, 2); |
---|
367 | std::vector<int>displs(size, 0); |
---|
368 | |
---|
369 | for(int i=0; i<size; i++) displs[i] = 2*(size-1-i); |
---|
370 | |
---|
371 | MPI_Allgatherv(sendbuf, 2, MPI_INT, recvbuf.data(), recvcounts.data(), displs.data(), MPI_INT, comm); |
---|
372 | |
---|
373 | int allgatherv_test = 1; |
---|
374 | |
---|
375 | |
---|
376 | |
---|
377 | for(int i=0; i<size; i++) |
---|
378 | { |
---|
379 | if(abs(recvbuf[2*i] - (size-1-i)) > 1.e-10 || abs(recvbuf[2*i+1] + size) > 1.e-10) |
---|
380 | { |
---|
381 | allgatherv_test = 0; printf("ID : %d %d %d %d %d\n", rank, recvbuf[2*i], recvbuf[2*i+1] , recvbuf[2*i] - (size-1-i), recvbuf[2*i+1] + size); |
---|
382 | break; |
---|
383 | } |
---|
384 | } |
---|
385 | |
---|
386 | |
---|
387 | int allgatherv_result; |
---|
388 | MPI_Reduce(&allgatherv_test, &allgatherv_result, 1, MPI_INT, MPI_MIN, 0, comm); |
---|
389 | |
---|
390 | if(rank == 0 && allgatherv_result) printf(" \t test MPI_Allgatherv \t OK \n"); |
---|
391 | if(rank == 0 && !allgatherv_result) printf(" \t test MPI_Allgatherv \t FAILED %d\n", allgatherv_result); |
---|
392 | |
---|
393 | } |
---|
394 | |
---|
395 | MPI_Barrier(comm); |
---|
396 | |
---|
397 | // TEST OF REDUCE |
---|
398 | { |
---|
399 | int reduce_root; |
---|
400 | |
---|
401 | if(rank == 0) reduce_root = rand() % size; |
---|
402 | |
---|
403 | MPI_Bcast(&reduce_root, 1, MPI_INT, 0, comm); |
---|
404 | |
---|
405 | int sendbuf[2]; |
---|
406 | sendbuf[0] = rank; |
---|
407 | sendbuf[1] = -size; |
---|
408 | |
---|
409 | std::vector<int>recvbuf(2, 0); |
---|
410 | |
---|
411 | MPI_Op op = MPI_MIN; |
---|
412 | |
---|
413 | MPI_Reduce(sendbuf, recvbuf.data(), 2, MPI_INT, op, reduce_root, comm); |
---|
414 | |
---|
415 | |
---|
416 | bool reduce_result = true; |
---|
417 | |
---|
418 | if(rank == reduce_root) |
---|
419 | { |
---|
420 | for(int i=0; i<2; i++) |
---|
421 | { |
---|
422 | if((op == MPI_SUM && (abs(recvbuf[0]-(size-1)*size/2) > 1.e-10 || abs(recvbuf[1] + size * size) > 1.e-10) ) || |
---|
423 | (op == MPI_MAX && (abs(recvbuf[0]-(size-1)) > 1.e-10 || abs(recvbuf[1] + size) > 1.e-10) ) || |
---|
424 | (op == MPI_MIN && (abs(recvbuf[0]) > 1.e-10 || abs(recvbuf[1] + size) > 1.e-10) ) ) |
---|
425 | { |
---|
426 | reduce_result = false; printf("%d %d root = %d, i = %d\n", recvbuf[0], recvbuf[1], reduce_root, i); |
---|
427 | break; |
---|
428 | } |
---|
429 | } |
---|
430 | } |
---|
431 | |
---|
432 | if(rank == reduce_root && reduce_result) printf("root = %d : \t test MPI_Reduce \t OK\n", reduce_root); |
---|
433 | if(rank == reduce_root && !reduce_result) printf("root = %d : \t test MPI_Reduce \t FAILED\n", reduce_root); |
---|
434 | } |
---|
435 | |
---|
436 | |
---|
437 | MPI_Barrier(comm); |
---|
438 | |
---|
439 | // TEST OF ALLREDUCE |
---|
440 | { |
---|
441 | |
---|
442 | int sendbuf[2]; |
---|
443 | sendbuf[0] = rank; |
---|
444 | sendbuf[1] = -size; |
---|
445 | |
---|
446 | std::vector<int>recvbuf(2, 0); |
---|
447 | |
---|
448 | MPI_Op op = MPI_MIN; |
---|
449 | |
---|
450 | MPI_Allreduce(sendbuf, recvbuf.data(), 2, MPI_INT, op, comm); |
---|
451 | |
---|
452 | |
---|
453 | int allreduce_test = 1; |
---|
454 | |
---|
455 | |
---|
456 | if((op == MPI_SUM && (abs(recvbuf[0]-(size-1)*size/2) > 1.e-10 || abs(recvbuf[1] + size * size) > 1.e-10) ) || |
---|
457 | (op == MPI_MAX && (abs(recvbuf[0]-(size-1)) > 1.e-10 || abs(recvbuf[1] + size) > 1.e-10) ) || |
---|
458 | (op == MPI_MIN && (abs(recvbuf[0]) > 1.e-10 || abs(recvbuf[1] + size) > 1.e-10) ) ) |
---|
459 | { |
---|
460 | allreduce_test = 0; printf("%d %d\n", recvbuf[0], recvbuf[1]); |
---|
461 | } |
---|
462 | |
---|
463 | |
---|
464 | int allreduce_result; |
---|
465 | MPI_Reduce(&allreduce_test, &allreduce_result, 1, MPI_INT, MPI_MIN, 0, comm); |
---|
466 | |
---|
467 | if(rank == 0 && allreduce_result) printf(" \t test MPI_Allreduce \t OK\n"); |
---|
468 | if(rank == 0 && !allreduce_result) printf(" \t test MPI_Allreduce \t FAILED\n"); |
---|
469 | } |
---|
470 | |
---|
471 | |
---|
472 | MPI_Barrier(comm); |
---|
473 | |
---|
474 | // TEST OF REDUCE_SCATTER |
---|
475 | { |
---|
476 | |
---|
477 | std::vector<int>sendbuf(2*size, rank); |
---|
478 | std::vector<int>recvbuf(2, -1); |
---|
479 | std::vector<int>recvcounts(size, 2); |
---|
480 | |
---|
481 | MPI_Op op = MPI_MIN; |
---|
482 | |
---|
483 | MPI_Reduce_scatter(sendbuf.data(), recvbuf.data(), recvcounts.data(), MPI_INT, op, comm); |
---|
484 | |
---|
485 | |
---|
486 | int reduce_scatter_test = 1; |
---|
487 | |
---|
488 | |
---|
489 | if((op == MPI_SUM && (abs(recvbuf[0]-(size-1)*size/2) > 1.e-10 || abs(recvbuf[0]-(size-1)*size/2) > 1.e-10) ) || |
---|
490 | (op == MPI_MAX && (abs(recvbuf[0]-(size-1)) > 1.e-10 || abs(recvbuf[1]-(size-1)) > 1.e-10) ) || |
---|
491 | (op == MPI_MIN && (abs(recvbuf[0]) > 1.e-10 || abs(recvbuf[1] ) > 1.e-10) ) ) |
---|
492 | { |
---|
493 | reduce_scatter_test = 0; //printf("%d %d id = %d\n", recvbuf[0], recvbuf[1], rank); |
---|
494 | } |
---|
495 | |
---|
496 | |
---|
497 | int reduce_scatter_result; |
---|
498 | MPI_Reduce(&reduce_scatter_test, &reduce_scatter_result, 1, MPI_INT, MPI_MIN, 0, comm); |
---|
499 | |
---|
500 | if(rank == 0 && reduce_scatter_result) printf(" \t test MPI_Reduce_scatter OK\n"); |
---|
501 | if(rank == 0 && !reduce_scatter_result) printf(" \t test MPI_Reduce_scatter FAILED\n"); |
---|
502 | } |
---|
503 | |
---|
504 | MPI_Barrier(comm); |
---|
505 | |
---|
506 | // TEST OF SCATTER |
---|
507 | { |
---|
508 | |
---|
509 | int scatter_root; |
---|
510 | |
---|
511 | if(rank == 0) scatter_root = rand() % size; |
---|
512 | |
---|
513 | MPI_Bcast(&scatter_root, 1, MPI_INT, 0, comm); |
---|
514 | |
---|
515 | std::vector<int>sendbuf(2*size, rank); |
---|
516 | std::vector<int>recvbuf(2, -1); |
---|
517 | std::vector<int>recvcounts(size, 2); |
---|
518 | |
---|
519 | if(rank == scatter_root) |
---|
520 | { |
---|
521 | for(int i=0; i<size; i++) |
---|
522 | { |
---|
523 | sendbuf[2*i] = i; |
---|
524 | sendbuf[2*i+1] = size; |
---|
525 | } |
---|
526 | //for(int i=0; i<size*2; i++) printf("%d\t", sendbuf[i]); |
---|
527 | } |
---|
528 | |
---|
529 | |
---|
530 | MPI_Scatter(sendbuf.data(), 2, MPI_INT, recvbuf.data(), 2, MPI_INT, scatter_root, comm); |
---|
531 | |
---|
532 | //printf("ID = %d : %d %d\n", rank, recvbuf[0], recvbuf[1]); |
---|
533 | |
---|
534 | int scatter_test = 1; |
---|
535 | |
---|
536 | |
---|
537 | if( abs(recvbuf[0]-rank) > 1.e-10 || abs(recvbuf[1]-size) > 1.e-10 ) |
---|
538 | { |
---|
539 | scatter_test = 0; //printf("%d %d id = %d\n", recvbuf[0], recvbuf[1], rank); |
---|
540 | } |
---|
541 | |
---|
542 | |
---|
543 | int scatter_result; |
---|
544 | MPI_Reduce(&scatter_test, &scatter_result, 1, MPI_INT, MPI_MIN, scatter_root, comm); |
---|
545 | |
---|
546 | if(rank == scatter_root && scatter_result) printf("root = %d : \t test MPI_Scatter \t OK\n", scatter_root); |
---|
547 | if(rank == scatter_root && !scatter_result) printf("root = %d : \t test MPI_Scatter \t FAILED\n", scatter_root); |
---|
548 | } |
---|
549 | |
---|
550 | MPI_Barrier(comm); |
---|
551 | |
---|
552 | // TEST OF SCATTERV |
---|
553 | { |
---|
554 | |
---|
555 | int scatterv_root; |
---|
556 | |
---|
557 | if(rank == 0) scatterv_root = rand() % size; |
---|
558 | |
---|
559 | MPI_Bcast(&scatterv_root, 1, MPI_INT, 0, comm); |
---|
560 | |
---|
561 | std::vector<int>sendbuf(2*size, rank); |
---|
562 | std::vector<int>recvbuf(2, -1); |
---|
563 | std::vector<int>sendcounts(size, 2); |
---|
564 | std::vector<int>displs(size, 0); |
---|
565 | |
---|
566 | for(int i=0; i<size; i++) displs[i] = 2*(size-1-i); |
---|
567 | |
---|
568 | if(rank == scatterv_root) |
---|
569 | { |
---|
570 | for(int i=0; i<size; i++) |
---|
571 | { |
---|
572 | sendbuf[2*i] = i; |
---|
573 | sendbuf[2*i+1] = size; |
---|
574 | } |
---|
575 | } |
---|
576 | |
---|
577 | |
---|
578 | MPI_Scatterv(sendbuf.data(), sendcounts.data(), displs.data(), MPI_INT, recvbuf.data(), 2, MPI_INT, scatterv_root, comm); |
---|
579 | |
---|
580 | //printf("ID = %d : %d %d\n", rank, recvbuf[0], recvbuf[1]); |
---|
581 | |
---|
582 | int scatterv_test = 1; |
---|
583 | |
---|
584 | |
---|
585 | if( abs(recvbuf[0]-(size-1-rank)) > 1.e-10 || abs(recvbuf[1]-size) > 1.e-10 ) |
---|
586 | { |
---|
587 | scatterv_test = 0; printf("%d %d id = %d\n", recvbuf[0], recvbuf[1], rank); |
---|
588 | } |
---|
589 | |
---|
590 | |
---|
591 | int scatterv_result; |
---|
592 | MPI_Reduce(&scatterv_test, &scatterv_result, 1, MPI_INT, MPI_MIN, scatterv_root, comm); |
---|
593 | |
---|
594 | if(rank == scatterv_root && scatterv_result) printf("root = %d : \t test MPI_Scatterv \t OK\n", scatterv_root); |
---|
595 | if(rank == scatterv_root && !scatterv_result) printf("root = %d : \t test MPI_Scatterv \t FAILED\n", scatterv_root); |
---|
596 | } |
---|
597 | |
---|
598 | MPI_Barrier(comm); |
---|
599 | |
---|
600 | // TEST OF ALLTOALL |
---|
601 | { |
---|
602 | |
---|
603 | std::vector<int>sendbuf(size, rank); |
---|
604 | std::vector<int>recvbuf(size, -1); |
---|
605 | |
---|
606 | |
---|
607 | MPI_Alltoall(sendbuf.data(), 1, MPI_INT, recvbuf.data(), 1, MPI_INT, comm); |
---|
608 | |
---|
609 | int alltoall_result = 1; |
---|
610 | |
---|
611 | |
---|
612 | for(int i=0; i<size; i++) |
---|
613 | if( abs(recvbuf[i]-i) > 1.e-10 ) |
---|
614 | { |
---|
615 | alltoall_result = 0; printf("%d id = %d\n", recvbuf[i], rank); |
---|
616 | } |
---|
617 | |
---|
618 | if(rank == 0 && alltoall_result) printf(" \t test MPI_Alltoall \t OK\n"); |
---|
619 | if(rank == 0 && !alltoall_result) printf(" \t test MPI_Alltoall \t FAILED\n"); |
---|
620 | } |
---|
621 | |
---|
622 | // TEST OF SCAN |
---|
623 | { |
---|
624 | |
---|
625 | std::vector<int>sendbuf(2, rank); |
---|
626 | std::vector<int>recvbuf(2, -1); |
---|
627 | |
---|
628 | MPI_Op op = MPI_SUM; |
---|
629 | |
---|
630 | |
---|
631 | MPI_Scan(sendbuf.data(), recvbuf.data(), 2, MPI_INT, op, comm); |
---|
632 | |
---|
633 | int scan_test = 1; |
---|
634 | |
---|
635 | // printf(" ID=%d : %d %d \n", rank, recvbuf[0], recvbuf[1]); |
---|
636 | |
---|
637 | if((op == MPI_SUM && (abs(recvbuf[0]-rank*(rank+1)/2) > 1.e-10 || abs(recvbuf[1]-rank*(rank+1)/2) > 1.e-10) ) || |
---|
638 | (op == MPI_MIN && (abs(recvbuf[0]) > 1.e-10 || abs(recvbuf[1]) > 1.e-10) ) || |
---|
639 | (op == MPI_MAX && (abs(recvbuf[0] - rank) > 1.e-10 || abs(recvbuf[1] - rank) > 1.e-10) ) ) |
---|
640 | { |
---|
641 | scan_test = 0; //printf("%d id = %d\n", recvbuf[i], rank); |
---|
642 | } |
---|
643 | |
---|
644 | int scan_result; |
---|
645 | MPI_Reduce(&scan_test, &scan_result, 1, MPI_INT, MPI_MIN, 0, comm); |
---|
646 | |
---|
647 | if(rank == 0 && scan_result) printf(" \t test MPI_Scan \t\t OK\n"); |
---|
648 | if(rank == 0 && !scan_result) printf(" \t test MPI_Scan \t\t FAILED\n"); |
---|
649 | } |
---|
650 | |
---|
651 | |
---|
652 | // TEST OF EXSCAN |
---|
653 | { |
---|
654 | |
---|
655 | std::vector<int>sendbuf(2, rank); |
---|
656 | std::vector<int>recvbuf(2, -1); |
---|
657 | |
---|
658 | MPI_Op op = MPI_SUM; |
---|
659 | |
---|
660 | |
---|
661 | MPI_Exscan(sendbuf.data(), recvbuf.data(), 2, MPI_INT, op, comm); |
---|
662 | |
---|
663 | int exscan_test = 1; |
---|
664 | |
---|
665 | // printf(" ID=%d : %d %d \n", rank, recvbuf[0], recvbuf[1]); |
---|
666 | |
---|
667 | if(rank >0) |
---|
668 | if((op == MPI_SUM && (abs(recvbuf[0]-rank*(rank-1)/2) > 1.e-10 || abs(recvbuf[1]-rank*(rank-1)/2) > 1.e-10) ) || |
---|
669 | (op == MPI_MIN && (abs(recvbuf[0] ) > 1.e-10 || abs(recvbuf[1]) > 1.e-10) ) || |
---|
670 | (op == MPI_MAX && (abs(recvbuf[0] - rank+1) > 1.e-10 || abs(recvbuf[1] - rank+1) > 1.e-10) ) ) |
---|
671 | { |
---|
672 | exscan_test = 0; //printf("%d id = %d\n", recvbuf[i], rank); |
---|
673 | } |
---|
674 | |
---|
675 | int exscan_result; |
---|
676 | MPI_Reduce(&exscan_test, &exscan_result, 1, MPI_INT, MPI_MIN, 0, comm); |
---|
677 | |
---|
678 | if(rank == 0 && exscan_result) printf(" \t test MPI_Exscan \t OK\n"); |
---|
679 | if(rank == 0 && !exscan_result) printf(" \t test MPI_Exscan \t FAILED\n"); |
---|
680 | } |
---|
681 | |
---|
682 | |
---|
683 | |
---|
684 | // TEST OF COMM_SPLIT |
---|
685 | { |
---|
686 | |
---|
687 | MPI_Barrier(comm); |
---|
688 | int rank, size; |
---|
689 | MPI_Comm_rank(comm, &rank); |
---|
690 | MPI_Comm_size(comm, &size); |
---|
691 | |
---|
692 | int color = rand()%3; |
---|
693 | int key = rand()%5; |
---|
694 | |
---|
695 | int color2 = rand()%3; |
---|
696 | int key2 = rand()%5; |
---|
697 | |
---|
698 | MPI_Comm split_comm; |
---|
699 | MPI_Comm_split(comm, color, key, &split_comm); |
---|
700 | |
---|
701 | |
---|
702 | MPI_Comm split_comm2; |
---|
703 | MPI_Comm_split(comm, color2, key2, &split_comm2); |
---|
704 | |
---|
705 | |
---|
706 | |
---|
707 | int split_rank, split_size; |
---|
708 | MPI_Comm_rank(split_comm, &split_rank); |
---|
709 | MPI_Comm_size(split_comm, &split_size); |
---|
710 | |
---|
711 | #ifdef _Memory_check |
---|
712 | printf("rank = %d, color = %d, key = %d, split_rank = %d, local_rank=%d\n", rank, color, key, split_rank, split_comm->ep_comm_ptr->size_rank_info[1].first); |
---|
713 | #endif |
---|
714 | |
---|
715 | MPI_Barrier(comm); |
---|
716 | MPI_Barrier(comm); |
---|
717 | MPI_Barrier(comm); |
---|
718 | |
---|
719 | if(rank == 0) printf(" \t MPI_Comm_split \t OK\n"); |
---|
720 | |
---|
721 | MPI_Barrier(comm); |
---|
722 | MPI_Barrier(comm); |
---|
723 | |
---|
724 | int bcast_buf_size=100; |
---|
725 | |
---|
726 | std::vector<int> bcast_buf(bcast_buf_size, 0); |
---|
727 | |
---|
728 | if(split_rank==0) bcast_buf.assign(bcast_buf_size, (color+1)*split_size); |
---|
729 | |
---|
730 | MPI_Bcast(bcast_buf.data(), bcast_buf_size, MPI_INT, 0, split_comm); |
---|
731 | |
---|
732 | int bcast_test = 0; |
---|
733 | for(int i=0; i<bcast_buf_size; i++) |
---|
734 | { |
---|
735 | if(bcast_buf[i] != (color+1)*split_size) |
---|
736 | bcast_test = 1; |
---|
737 | } |
---|
738 | |
---|
739 | int bcast_result; |
---|
740 | |
---|
741 | MPI_Reduce(&bcast_test, &bcast_result, 1, MPI_INT, MPI_SUM, 0, comm); |
---|
742 | |
---|
743 | MPI_Barrier(split_comm); |
---|
744 | MPI_Comm_free(&split_comm); |
---|
745 | |
---|
746 | MPI_Barrier(split_comm2); |
---|
747 | MPI_Comm_free(&split_comm2); |
---|
748 | |
---|
749 | |
---|
750 | |
---|
751 | if(bcast_result == 0 && rank == 0) printf(" \t test MPI_Bcast for split comm\t OK\n"); |
---|
752 | if(bcast_result != 0 && rank == 0) printf(" \t test MPI_Bcast for split comm\t FAILED %d\n", bcast_result); |
---|
753 | |
---|
754 | } |
---|
755 | |
---|
756 | MPI_Barrier(comm); |
---|
757 | MPI_Barrier(comm); |
---|
758 | MPI_Barrier(comm); |
---|
759 | |
---|
760 | // TESE OF INTERCOMM_CREATE |
---|
761 | { |
---|
762 | MPI_Barrier(comm); |
---|
763 | |
---|
764 | int rank, size; |
---|
765 | MPI_Comm_rank(comm, &rank); |
---|
766 | MPI_Comm_size(comm, &size); |
---|
767 | |
---|
768 | int config = 1; |
---|
769 | if(config=1) |
---|
770 | { |
---|
771 | assert(size == 16 && omp_get_num_threads()==4); |
---|
772 | } |
---|
773 | |
---|
774 | int tab_color[16] = {2, 2, 2, 1, 2, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1}; // used for config 1 |
---|
775 | int tab_key[16] = {2, 1, 4, 1, 0, 4, 3, 3, 4, 3, 4, 4, 4, 0, 0, 2}; // used for config 1 |
---|
776 | |
---|
777 | int color = tab_color[rank]; |
---|
778 | int key = tab_key[rank]; |
---|
779 | |
---|
780 | |
---|
781 | MPI_Comm split_comm; |
---|
782 | MPI_Comm_split(comm, color, key, &split_comm); |
---|
783 | |
---|
784 | |
---|
785 | int split_rank, split_size; |
---|
786 | MPI_Comm_rank(split_comm, &split_rank); |
---|
787 | MPI_Comm_size(split_comm, &split_size); |
---|
788 | |
---|
789 | |
---|
790 | |
---|
791 | |
---|
792 | MPI_Barrier(comm); |
---|
793 | MPI_Barrier(comm); |
---|
794 | |
---|
795 | if(rank == 0) printf(" \t MPI_Comm_split \t OK\n"); |
---|
796 | |
---|
797 | MPI_Barrier(comm); |
---|
798 | MPI_Barrier(comm); |
---|
799 | |
---|
800 | |
---|
801 | |
---|
802 | |
---|
803 | |
---|
804 | int local_leader = 0; |
---|
805 | int remote_leader = color==2? 13: 4; // used for config 1 |
---|
806 | |
---|
807 | MPI_Comm peer_comm = comm; |
---|
808 | |
---|
809 | MPI_Comm inter_comm; |
---|
810 | MPI_Intercomm_create(split_comm, local_leader, peer_comm, remote_leader, 99, &inter_comm); |
---|
811 | |
---|
812 | int inter_rank, inter_size, remote_size; |
---|
813 | MPI_Comm_rank(inter_comm, &inter_rank); |
---|
814 | MPI_Comm_size(inter_comm, &inter_size); |
---|
815 | MPI_Comm_remote_size(inter_comm, &remote_size); |
---|
816 | |
---|
817 | |
---|
818 | MPI_Barrier(comm); |
---|
819 | MPI_Barrier(comm); |
---|
820 | |
---|
821 | if(rank == 0) printf(" \t MPI_Intercomm_create \t OK\n"); |
---|
822 | |
---|
823 | printf("rank = %d, split_rank = %d, split_size = %d, inter_rank=%d, inter_size=%d, remote_size=%d\n", rank, split_rank, split_size, inter_rank, inter_size, remote_size); |
---|
824 | |
---|
825 | |
---|
826 | MPI_Barrier(comm); |
---|
827 | MPI_Barrier(comm); |
---|
828 | |
---|
829 | |
---|
830 | if(color==2 && split_rank==0) |
---|
831 | { |
---|
832 | double sendbuf[9]={1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9}; |
---|
833 | MPI_Request send_request; |
---|
834 | MPI_Status send_status; |
---|
835 | MPI_Isend(sendbuf, 9, MPI_DOUBLE, 0, 10, inter_comm, &send_request); |
---|
836 | MPI_Wait(&send_request, &send_status); |
---|
837 | } |
---|
838 | |
---|
839 | if(color==1 && split_rank==0) |
---|
840 | { |
---|
841 | double recvbuf[9]; |
---|
842 | MPI_Request recv_request; |
---|
843 | MPI_Status recv_status; |
---|
844 | MPI_Irecv(recvbuf, 9, MPI_DOUBLE, 0, 10, inter_comm, &recv_request); |
---|
845 | MPI_Wait(&recv_request, &recv_status); |
---|
846 | for(int i=0; i<9; i++) |
---|
847 | { |
---|
848 | printf("recvbuf[%d] = %lf\n", i, recvbuf[i]); |
---|
849 | } |
---|
850 | } |
---|
851 | |
---|
852 | MPI_Barrier(comm); |
---|
853 | MPI_Barrier(comm); |
---|
854 | |
---|
855 | if(rank == 0) printf(" \t Test iP2P for intercomm \t OK\n"); |
---|
856 | |
---|
857 | |
---|
858 | |
---|
859 | |
---|
860 | MPI_Barrier(comm); |
---|
861 | MPI_Barrier(comm); |
---|
862 | |
---|
863 | MPI_Barrier(split_comm); |
---|
864 | MPI_Comm_free(&split_comm); |
---|
865 | |
---|
866 | |
---|
867 | MPI_Barrier(comm); |
---|
868 | MPI_Barrier(comm); |
---|
869 | |
---|
870 | MPI_Comm inter_comm_dup; |
---|
871 | MPI_Comm_dup(inter_comm, &inter_comm_dup); |
---|
872 | |
---|
873 | int inter_comm_dup_size; |
---|
874 | int inter_comm_dup_remote_size; |
---|
875 | MPI_Comm_size(inter_comm_dup, &inter_comm_dup_size); |
---|
876 | |
---|
877 | MPI_Comm_remote_size(inter_comm_dup, &inter_comm_dup_remote_size); |
---|
878 | |
---|
879 | bool high = inter_comm_dup_size>inter_comm_dup_remote_size; |
---|
880 | |
---|
881 | |
---|
882 | printf("rank = %d, high = %d, inter_comm_dup_size = %d, inter_comm_dup_remote_size = %d\n", rank, high, inter_comm_dup_size, inter_comm_dup_remote_size); |
---|
883 | |
---|
884 | MPI_Comm inter_comm_dup_merged; |
---|
885 | |
---|
886 | MPI_Intercomm_merge(inter_comm_dup, high, &inter_comm_dup_merged); |
---|
887 | |
---|
888 | int inter_comm_dup_merged_rank; |
---|
889 | MPI_Comm_rank(inter_comm_dup_merged, &inter_comm_dup_merged_rank); |
---|
890 | |
---|
891 | int inter_comm_dup_merged_size; |
---|
892 | MPI_Comm_size(inter_comm_dup_merged, &inter_comm_dup_merged_size); |
---|
893 | |
---|
894 | //printf("rank = %d, inter_comm_dup_merged_rank = %d, inter_comm_dup_merged_size = %d\n", rank, inter_comm_dup_merged_rank, inter_comm_dup_merged_size); |
---|
895 | |
---|
896 | |
---|
897 | MPI_Barrier(inter_comm_dup_merged); |
---|
898 | |
---|
899 | // TEST OF GATHER |
---|
900 | { |
---|
901 | |
---|
902 | |
---|
903 | int gather_root = 99; |
---|
904 | |
---|
905 | if(inter_comm_dup_merged_rank == 0) |
---|
906 | { |
---|
907 | gather_root = rand() % inter_comm_dup_merged_size; |
---|
908 | } |
---|
909 | |
---|
910 | MPI_Bcast(&gather_root, 1, MPI_INT, 0, inter_comm_dup_merged); |
---|
911 | |
---|
912 | double sendbuf[2]; |
---|
913 | sendbuf[0] = inter_comm_dup_merged_rank * 1.0; |
---|
914 | sendbuf[1] = inter_comm_dup_merged_size * (-1.0); |
---|
915 | |
---|
916 | std::vector<double>recvbuf(2*inter_comm_dup_merged_size, 0); |
---|
917 | |
---|
918 | MPI_Gather(sendbuf, 2, MPI_DOUBLE, recvbuf.data(), 2, MPI_DOUBLE, gather_root, inter_comm_dup_merged); |
---|
919 | |
---|
920 | bool gather_result = true; |
---|
921 | |
---|
922 | if(inter_comm_dup_merged_rank == gather_root) |
---|
923 | { |
---|
924 | for(int i=0; i<inter_comm_dup_merged_size; i++) |
---|
925 | { |
---|
926 | if(abs(recvbuf[2*i] - i) > 1.e-10 || abs(recvbuf[2*i+1] + inter_comm_dup_merged_size) > 1.e-10) |
---|
927 | { |
---|
928 | gather_result = false; |
---|
929 | break; |
---|
930 | } |
---|
931 | } |
---|
932 | |
---|
933 | if(gather_result) printf("root = %d : \t test MPI_Gather for merged comm\t OK \n", gather_root); |
---|
934 | else printf("root = %d : \t test MPI_Gather for merged comm\t FAILED\n", gather_root); |
---|
935 | } |
---|
936 | } |
---|
937 | |
---|
938 | MPI_Barrier(inter_comm_dup_merged); |
---|
939 | |
---|
940 | // TEST OF ALLREDUCE |
---|
941 | { |
---|
942 | |
---|
943 | int sendbuf[2]; |
---|
944 | sendbuf[0] = inter_comm_dup_merged_rank; |
---|
945 | sendbuf[1] = -inter_comm_dup_merged_size; |
---|
946 | |
---|
947 | std::vector<int>recvbuf(2, 0); |
---|
948 | |
---|
949 | MPI_Op op = MPI_MIN; |
---|
950 | |
---|
951 | MPI_Allreduce(sendbuf, recvbuf.data(), 2, MPI_INT, op, inter_comm_dup_merged); |
---|
952 | |
---|
953 | |
---|
954 | int allreduce_test = 1; |
---|
955 | |
---|
956 | |
---|
957 | if((op == MPI_SUM && (abs(recvbuf[0]-(inter_comm_dup_merged_size-1)*inter_comm_dup_merged_size/2) > 1.e-10 || abs(recvbuf[1] + inter_comm_dup_merged_size * inter_comm_dup_merged_size) > 1.e-10) ) || |
---|
958 | (op == MPI_MAX && (abs(recvbuf[0]-(inter_comm_dup_merged_size-1)) > 1.e-10 || abs(recvbuf[1] + inter_comm_dup_merged_size) > 1.e-10) ) || |
---|
959 | (op == MPI_MIN && (abs(recvbuf[0]) > 1.e-10 || abs(recvbuf[1] + inter_comm_dup_merged_size) > 1.e-10) ) ) |
---|
960 | { |
---|
961 | allreduce_test = 0; printf("%d %d\n", recvbuf[0], recvbuf[1]); |
---|
962 | } |
---|
963 | |
---|
964 | |
---|
965 | int allreduce_result; |
---|
966 | MPI_Reduce(&allreduce_test, &allreduce_result, 1, MPI_INT, MPI_MIN, 0, inter_comm_dup_merged); |
---|
967 | |
---|
968 | if(inter_comm_dup_merged_rank == 0 && allreduce_result) printf(" \t test MPI_Allreduce for merged comm \t OK\n"); |
---|
969 | if(inter_comm_dup_merged_rank == 0 && !allreduce_result) printf(" \t test MPI_Allreduce for merged comm \t FAILED\n"); |
---|
970 | |
---|
971 | } |
---|
972 | |
---|
973 | MPI_Barrier(inter_comm_dup_merged); |
---|
974 | |
---|
975 | // TEST OF EXSCAN |
---|
976 | { |
---|
977 | |
---|
978 | std::vector<int>sendbuf(2, inter_comm_dup_merged_rank); |
---|
979 | std::vector<int>recvbuf(2, -1); |
---|
980 | |
---|
981 | MPI_Op op = MPI_SUM; |
---|
982 | |
---|
983 | |
---|
984 | MPI_Exscan(sendbuf.data(), recvbuf.data(), 2, MPI_INT, op, inter_comm_dup_merged); |
---|
985 | |
---|
986 | int exscan_test = 1; |
---|
987 | |
---|
988 | if(inter_comm_dup_merged_rank >0) |
---|
989 | if((op == MPI_SUM && (abs(recvbuf[0]-inter_comm_dup_merged_rank*(inter_comm_dup_merged_rank-1)/2) > 1.e-10 || abs(recvbuf[1]-inter_comm_dup_merged_rank*(inter_comm_dup_merged_rank-1)/2) > 1.e-10) ) || |
---|
990 | (op == MPI_MIN && (abs(recvbuf[0] ) > 1.e-10 || abs(recvbuf[1]) > 1.e-10) ) || |
---|
991 | (op == MPI_MAX && (abs(recvbuf[0] - inter_comm_dup_merged_rank+1) > 1.e-10 || abs(recvbuf[1] - inter_comm_dup_merged_rank+1) > 1.e-10) ) ) |
---|
992 | { |
---|
993 | exscan_test = 0; |
---|
994 | } |
---|
995 | |
---|
996 | int exscan_result; |
---|
997 | MPI_Reduce(&exscan_test, &exscan_result, 1, MPI_INT, MPI_MIN, 0, inter_comm_dup_merged); |
---|
998 | |
---|
999 | if(inter_comm_dup_merged_rank == 0 && exscan_result) printf(" \t test MPI_Exscan for merged comm \t OK\n"); |
---|
1000 | if(inter_comm_dup_merged_rank == 0 && !exscan_result) printf(" \t test MPI_Exscan for merged comm \t FAILED %d\n", exscan_result); |
---|
1001 | } |
---|
1002 | |
---|
1003 | MPI_Barrier(inter_comm_dup_merged); |
---|
1004 | |
---|
1005 | // TEST OF SCATTERV |
---|
1006 | { |
---|
1007 | |
---|
1008 | int scatterv_root; |
---|
1009 | |
---|
1010 | if(inter_comm_dup_merged_rank == 0) scatterv_root = rand() % inter_comm_dup_merged_size; |
---|
1011 | |
---|
1012 | MPI_Bcast(&scatterv_root, 1, MPI_INT, 0, inter_comm_dup_merged); |
---|
1013 | |
---|
1014 | std::vector<int>sendbuf(2*inter_comm_dup_merged_size, inter_comm_dup_merged_rank); |
---|
1015 | std::vector<int>recvbuf(2, -1); |
---|
1016 | std::vector<int>sendcounts(inter_comm_dup_merged_size, 2); |
---|
1017 | std::vector<int>displs(inter_comm_dup_merged_size, 0); |
---|
1018 | |
---|
1019 | for(int i=0; i<inter_comm_dup_merged_size; i++) displs[i] = 2*(inter_comm_dup_merged_size-1-i); |
---|
1020 | |
---|
1021 | if(inter_comm_dup_merged_rank == scatterv_root) |
---|
1022 | { |
---|
1023 | for(int i=0; i<inter_comm_dup_merged_size; i++) |
---|
1024 | { |
---|
1025 | sendbuf[2*i] = i; |
---|
1026 | sendbuf[2*i+1] = inter_comm_dup_merged_size; |
---|
1027 | } |
---|
1028 | } |
---|
1029 | |
---|
1030 | |
---|
1031 | MPI_Scatterv(sendbuf.data(), sendcounts.data(), displs.data(), MPI_INT, recvbuf.data(), 2, MPI_INT, scatterv_root, inter_comm_dup_merged); |
---|
1032 | |
---|
1033 | int scatterv_test = 1; |
---|
1034 | |
---|
1035 | |
---|
1036 | if( abs(recvbuf[0]-(inter_comm_dup_merged_size-1-inter_comm_dup_merged_rank)) > 1.e-10 || abs(recvbuf[1]-inter_comm_dup_merged_size) > 1.e-10 ) |
---|
1037 | { |
---|
1038 | scatterv_test = 0; printf("%d %d id = %d\n", recvbuf[0], recvbuf[1], inter_comm_dup_merged_rank); |
---|
1039 | } |
---|
1040 | |
---|
1041 | |
---|
1042 | int scatterv_result; |
---|
1043 | MPI_Reduce(&scatterv_test, &scatterv_result, 1, MPI_INT, MPI_MIN, scatterv_root, inter_comm_dup_merged); |
---|
1044 | |
---|
1045 | if(inter_comm_dup_merged_rank == scatterv_root && scatterv_result) printf("root = %d : \t test MPI_Scatterv for merged comm \t OK\n", scatterv_root); |
---|
1046 | if(inter_comm_dup_merged_rank == scatterv_root && !scatterv_result) printf("root = %d : \t test MPI_Scatterv for merged comm \t FAILED\n", scatterv_root); |
---|
1047 | } |
---|
1048 | |
---|
1049 | MPI_Barrier(inter_comm_dup_merged); |
---|
1050 | |
---|
1051 | // TEST OF TESTALL |
---|
1052 | { |
---|
1053 | int merged_size, merged_rank; |
---|
1054 | MPI_Comm_rank(inter_comm_dup_merged, &merged_rank); |
---|
1055 | MPI_Comm_size(inter_comm_dup_merged, &merged_size); |
---|
1056 | |
---|
1057 | int left = merged_rank>0? merged_rank-1 : merged_size-1; |
---|
1058 | int right = (merged_rank+1) % merged_size; |
---|
1059 | |
---|
1060 | int N=10000; |
---|
1061 | int *left_buf = new int[N]; |
---|
1062 | int *right_buf = new int[N]; |
---|
1063 | |
---|
1064 | for(int i=0; i<N; i++) |
---|
1065 | { |
---|
1066 | right_buf[i] = merged_rank*100000+i; |
---|
1067 | } |
---|
1068 | |
---|
1069 | MPI_Request request[2]; |
---|
1070 | MPI_Status status[2]; |
---|
1071 | |
---|
1072 | MPI_Irecv(left_buf, N, MPI_INT, left, 0, inter_comm_dup_merged, &request[0]); |
---|
1073 | MPI_Isend(right_buf, N, MPI_INT, right, 0, inter_comm_dup_merged, &request[1]); |
---|
1074 | |
---|
1075 | |
---|
1076 | int flag; |
---|
1077 | MPI_Testall(2, request, &flag, status); |
---|
1078 | |
---|
1079 | printf("rank = %d, flag = %d\n", merged_rank, flag); |
---|
1080 | |
---|
1081 | while(!flag) MPI_Testall(2, request, &flag, status); |
---|
1082 | |
---|
1083 | int recv_count; |
---|
1084 | MPI_Get_count(&status[0], MPI_INT, &recv_count); |
---|
1085 | |
---|
1086 | MPI_Barrier(inter_comm_dup_merged); |
---|
1087 | |
---|
1088 | printf("rank = %d, recv_count = %d, left_buf[5-10] = %d\t%d\t%d\t%d\t%d\t%d\n", merged_rank, recv_count, left_buf[5], left_buf[6], left_buf[7], left_buf[8], left_buf[9], left_buf[10]); |
---|
1089 | |
---|
1090 | delete[] left_buf; |
---|
1091 | delete[] right_buf; |
---|
1092 | } |
---|
1093 | |
---|
1094 | MPI_Barrier(inter_comm_dup_merged); |
---|
1095 | |
---|
1096 | // TEST WAITALL |
---|
1097 | { |
---|
1098 | int merged_rank; |
---|
1099 | int merged_size; |
---|
1100 | |
---|
1101 | MPI_Comm_rank(inter_comm_dup_merged, &merged_rank); |
---|
1102 | MPI_Comm_size(inter_comm_dup_merged, &merged_size); |
---|
1103 | |
---|
1104 | int left = merged_rank>0? merged_rank-1 : merged_size-1; |
---|
1105 | int right = (merged_rank+1)%merged_size; |
---|
1106 | |
---|
1107 | printf("merged_rank = %d, left = %d, right = %d\n", merged_rank, left, right); |
---|
1108 | |
---|
1109 | int NN=10000; |
---|
1110 | |
---|
1111 | double *left_buf = new double[NN]; |
---|
1112 | double *right_buf = new double[NN]; |
---|
1113 | |
---|
1114 | for(int i=0; i<NN; i++) |
---|
1115 | { |
---|
1116 | right_buf[i] = merged_rank*1000000 + i; |
---|
1117 | } |
---|
1118 | |
---|
1119 | MPI_Request request[2]; |
---|
1120 | MPI_Status status[2]; |
---|
1121 | |
---|
1122 | MPI_Irecv(left_buf, NN, MPI_DOUBLE, left, 0, inter_comm_dup_merged, &request[0]); |
---|
1123 | MPI_Isend(right_buf, NN, MPI_DOUBLE, right, 0, inter_comm_dup_merged, &request[1]); |
---|
1124 | |
---|
1125 | |
---|
1126 | MPI_Waitall(2, request, status); |
---|
1127 | |
---|
1128 | printf("merged_rank = %d, left_buf[0-4] = %lf\t%lf\t%lf\t%lf\t%lf\n", merged_rank, left_buf[0], left_buf[1], left_buf[2], left_buf[3], left_buf[4]); |
---|
1129 | |
---|
1130 | |
---|
1131 | delete left_buf; |
---|
1132 | delete right_buf; |
---|
1133 | } |
---|
1134 | |
---|
1135 | MPI_Barrier(comm); |
---|
1136 | MPI_Barrier(comm); |
---|
1137 | |
---|
1138 | |
---|
1139 | MPI_Comm_free(&inter_comm_dup); |
---|
1140 | |
---|
1141 | MPI_Barrier(comm); |
---|
1142 | MPI_Barrier(comm); |
---|
1143 | |
---|
1144 | |
---|
1145 | MPI_Comm_free(&inter_comm_dup_merged); |
---|
1146 | |
---|
1147 | |
---|
1148 | |
---|
1149 | MPI_Barrier(comm); |
---|
1150 | MPI_Barrier(comm); |
---|
1151 | |
---|
1152 | MPI_Barrier(inter_comm); |
---|
1153 | MPI_Comm_free(&inter_comm); |
---|
1154 | |
---|
1155 | } |
---|
1156 | |
---|
1157 | // TESE OF INTERCOMM_CREATE |
---|
1158 | { |
---|
1159 | MPI_Barrier(comm); |
---|
1160 | |
---|
1161 | int rank, size; |
---|
1162 | MPI_Comm_rank(comm, &rank); |
---|
1163 | MPI_Comm_size(comm, &size); |
---|
1164 | |
---|
1165 | int config = 1; |
---|
1166 | if(config=1) |
---|
1167 | { |
---|
1168 | assert(size == 16 && omp_get_num_threads()==4); |
---|
1169 | } |
---|
1170 | |
---|
1171 | int tab_color[16] = {2, 2, 2, 1, 2, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1}; // used for config 1 |
---|
1172 | int tab_key[16] = {2, 1, 4, 1, 0, 4, 3, 3, 4, 3, 4, 4, 4, 0, 0, 2}; // used for config 1 |
---|
1173 | |
---|
1174 | int color = tab_color[rank]; |
---|
1175 | int key = tab_key[rank]; |
---|
1176 | |
---|
1177 | |
---|
1178 | MPI_Comm split_comm; |
---|
1179 | MPI_Comm_split(comm, color, key, &split_comm); |
---|
1180 | |
---|
1181 | |
---|
1182 | int split_rank, split_size; |
---|
1183 | MPI_Comm_rank(split_comm, &split_rank); |
---|
1184 | MPI_Comm_size(split_comm, &split_size); |
---|
1185 | |
---|
1186 | |
---|
1187 | |
---|
1188 | |
---|
1189 | MPI_Barrier(comm); |
---|
1190 | MPI_Barrier(comm); |
---|
1191 | |
---|
1192 | if(rank == 0) printf(" \t MPI_Comm_split \t OK\n"); |
---|
1193 | |
---|
1194 | MPI_Barrier(comm); |
---|
1195 | MPI_Barrier(comm); |
---|
1196 | |
---|
1197 | |
---|
1198 | int local_leader = 5; |
---|
1199 | int remote_leader = color==2? 5: 2; // used for config 1 |
---|
1200 | |
---|
1201 | MPI_Comm peer_comm = comm; |
---|
1202 | |
---|
1203 | MPI_Comm inter_comm; |
---|
1204 | MPI_Intercomm_create2(split_comm, local_leader, peer_comm, remote_leader, 99, &inter_comm); |
---|
1205 | |
---|
1206 | int inter_rank; |
---|
1207 | int inter_size; |
---|
1208 | MPI_Comm_rank(inter_comm, &inter_rank); |
---|
1209 | MPI_Comm_size(inter_comm, &inter_size); |
---|
1210 | |
---|
1211 | printf("split_rank = %d, inter_rank = %d / %d\n", split_rank, inter_rank, inter_size); |
---|
1212 | |
---|
1213 | MPI_Barrier(split_comm); |
---|
1214 | MPI_Comm_free(&split_comm); |
---|
1215 | |
---|
1216 | MPI_Barrier2(inter_comm); |
---|
1217 | MPI_Comm_free2(&inter_comm); |
---|
1218 | |
---|
1219 | } |
---|
1220 | |
---|
1221 | MPI_Barrier(comm); |
---|
1222 | MPI_Comm_free(&comm); |
---|
1223 | } |
---|
1224 | |
---|
1225 | /* |
---|
1226 | int num_threads; |
---|
1227 | if(mpi_rank < mpi_size-2) |
---|
1228 | { |
---|
1229 | printf("Proc %d is client\n", mpi_rank); |
---|
1230 | num_threads = 2;//+mpi_rank; |
---|
1231 | } |
---|
1232 | else |
---|
1233 | { |
---|
1234 | printf("Proc %d is server\n", mpi_rank); |
---|
1235 | num_threads = 1; |
---|
1236 | } |
---|
1237 | |
---|
1238 | omp_set_num_threads(num_threads); |
---|
1239 | |
---|
1240 | #pragma omp parallel default(shared) firstprivate(num_threads) |
---|
1241 | { |
---|
1242 | int num_ep = num_threads; |
---|
1243 | MPI_Info info; |
---|
1244 | |
---|
1245 | //printf("omp_get_thread_num() = %d, omp_get_num_threads() = %d, num_threads = %d\n", omp_get_thread_num(), omp_get_num_threads(), num_threads); |
---|
1246 | MPI_Comm *ep_comm; |
---|
1247 | #pragma omp master |
---|
1248 | { |
---|
1249 | MPI_Comm *ep_comm; |
---|
1250 | MPI_Comm_create_endpoints(MPI_COMM_WORLD->mpi_comm, num_ep, info, ep_comm); |
---|
1251 | passage = ep_comm; |
---|
1252 | } |
---|
1253 | |
---|
1254 | #pragma omp barrier |
---|
1255 | |
---|
1256 | |
---|
1257 | MPI_Comm comm; // this should act as EP_COMM_WORLD |
---|
1258 | comm = passage[omp_get_thread_num()]; |
---|
1259 | |
---|
1260 | int rank, size; |
---|
1261 | MPI_Comm_rank(comm, &rank); |
---|
1262 | MPI_Comm_size(comm, &size); |
---|
1263 | |
---|
1264 | |
---|
1265 | |
---|
1266 | bool isClient = false; |
---|
1267 | bool isServer = false; |
---|
1268 | |
---|
1269 | if(omp_get_num_threads()>1) isClient = true; |
---|
1270 | else isServer = true; |
---|
1271 | |
---|
1272 | printf("mpi_rank = %d, ep_rank = %d, isClient = %d\n", mpi_rank, rank, isClient); |
---|
1273 | |
---|
1274 | MPI_Win ep_win; |
---|
1275 | MPI_Aint buf_size=1; |
---|
1276 | int buf = rank; |
---|
1277 | int local_buf = rank; |
---|
1278 | int result_buf = -1; |
---|
1279 | MPI_Win_create(&buf, buf_size, sizeof(int), info, comm, &ep_win); |
---|
1280 | MPI_Barrier(comm); |
---|
1281 | |
---|
1282 | // MPI_Win_fence(MPI_MODE_NOPRECEDE, ep_win); |
---|
1283 | |
---|
1284 | MPI_Barrier(comm); |
---|
1285 | sleep(0.2); |
---|
1286 | MPI_Barrier(comm); |
---|
1287 | |
---|
1288 | MPI_Win_fence(0, ep_win); |
---|
1289 | |
---|
1290 | if(rank == 0) |
---|
1291 | { |
---|
1292 | local_buf = 99; |
---|
1293 | MPI_Aint displs=0; |
---|
1294 | MPI_Put(&local_buf, 1, MPI_INT, size-1, displs, 1, MPI_INT, ep_win); |
---|
1295 | } |
---|
1296 | |
---|
1297 | if(rank == size-2) |
---|
1298 | { |
---|
1299 | MPI_Aint displs(0); |
---|
1300 | MPI_Get(&local_buf, 1, MPI_INT, 2, displs, 1, MPI_INT, ep_win); |
---|
1301 | } |
---|
1302 | |
---|
1303 | MPI_Win_fence(0, ep_win); |
---|
1304 | |
---|
1305 | if(rank == 1) |
---|
1306 | { |
---|
1307 | MPI_Aint displs=0; |
---|
1308 | MPI_Accumulate(&local_buf, 1, MPI_INT, size-1, displs, 1, MPI_INT, MPI_REPLACE, ep_win); |
---|
1309 | } |
---|
1310 | |
---|
1311 | |
---|
1312 | MPI_Barrier(comm); |
---|
1313 | |
---|
1314 | MPI_Win_fence(0, ep_win); |
---|
1315 | |
---|
1316 | if(rank == 2) |
---|
1317 | { |
---|
1318 | MPI_Aint displs = 0; |
---|
1319 | MPI_Get_accumulate(&local_buf, 1, MPI_INT, &result_buf, |
---|
1320 | 1, MPI_INT, size-2, displs, |
---|
1321 | 1, MPI_INT, MPI_SUM, ep_win); |
---|
1322 | } |
---|
1323 | |
---|
1324 | MPI_Win_fence(0, ep_win); |
---|
1325 | |
---|
1326 | if(rank == 6) |
---|
1327 | { |
---|
1328 | MPI_Aint displs = 0; |
---|
1329 | MPI_Fetch_and_op(&local_buf, &result_buf, MPI_INT, size-1, displs, |
---|
1330 | MPI_SUM, ep_win); |
---|
1331 | } |
---|
1332 | |
---|
1333 | MPI_Win_fence(0, ep_win); |
---|
1334 | |
---|
1335 | if(rank == 7) |
---|
1336 | { |
---|
1337 | MPI_Aint displs = 0; |
---|
1338 | MPI_Compare_and_swap(&local_buf, &buf, &result_buf, MPI_INT, size-1, displs, ep_win); |
---|
1339 | } |
---|
1340 | |
---|
1341 | MPI_Win_fence(0, ep_win); |
---|
1342 | |
---|
1343 | //::MPI_Compare_and_swap(origin_addr, compare_addr, result_addr, to_mpi_type(datatype), target_mpi_rank, to_mpi_aint(target_disp), to_mpi_win(win.server_win[target_local_rank])); |
---|
1344 | |
---|
1345 | MPI_Win ep_win_allocated; |
---|
1346 | int* baseptr = new int[10]; |
---|
1347 | MPI_Aint base_size = 4; |
---|
1348 | |
---|
1349 | MPI_Win_allocate (base_size, sizeof(int), info, comm, baseptr, &ep_win_allocated); |
---|
1350 | |
---|
1351 | MPI_Win_fence(0, ep_win_allocated); |
---|
1352 | |
---|
1353 | MPI_Win_free(&ep_win_allocated); |
---|
1354 | delete[] baseptr; |
---|
1355 | |
---|
1356 | MPI_Win_free(&ep_win); |
---|
1357 | printf("rank = %d, buf = %d, local_buf = %d, result_buf = %d\n", rank, buf, local_buf, result_buf); |
---|
1358 | |
---|
1359 | MPI_Comm_free(&comm); |
---|
1360 | |
---|
1361 | } |
---|
1362 | */ |
---|
1363 | MPI_Finalize(); |
---|
1364 | |
---|
1365 | } |
---|