Changeset 1382 for XIOS/dev/branch_openmp/extern/ep_dev/main.cpp
- Timestamp:
- 12/19/17 14:44:41 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
XIOS/dev/branch_openmp/extern/ep_dev/main.cpp
r1381 r1382 30 30 MPI_Comm_size(MPI_COMM_WORLD, &mpi_size); 31 31 32 33 34 35 36 37 38 //omp_set_num_threads(4); 39 40 41 42 #pragma omp parallel default(shared) 43 { 44 MPI_Comm_rank(MPI_COMM_WORLD , &mpi_rank); 45 46 int num_ep = omp_get_num_threads(); 47 MPI_Info info; 48 49 //printf("mpi_rank = %d, thread_num = %d\n", mpi_rank, omp_get_thread_num()); 50 51 MPI_Comm *ep_comm; 52 #pragma omp master 53 { 54 MPI_Comm *ep_comm; 55 MPI_Comm_create_endpoints(MPI_COMM_WORLD.mpi_comm, num_ep, info, ep_comm); 56 passage = ep_comm; 57 } 58 59 #pragma omp barrier 60 61 62 MPI_Comm comm; // this should act as EP_COMM_WORLD 63 comm = passage[omp_get_thread_num()]; 64 65 int rank, size; 66 MPI_Comm_rank(comm, &rank); 67 MPI_Comm_size(comm, &size); 68 69 // TIMING SYCHRONIZATION 70 { 71 int n=100000; 72 73 MPI_Barrier(comm); 74 75 high_resolution_clock::time_point t1 = high_resolution_clock::now(); 76 77 for(int i=0; i<n; i++) 78 MPI_Barrier_local(comm); 79 80 high_resolution_clock::time_point t2 = high_resolution_clock::now(); 81 duration<double> time_span = duration_cast<duration<double>>(t2 - t1); 82 #pragma omp master 83 std::cout << "proc "<< mpi_rank <<" ep_barrier "<< time_span.count() << " seconds."<<std::endl; 84 85 t1 = high_resolution_clock::now(); 86 87 for(int i=0; i<n; i++) 88 { 89 #pragma omp barrier 90 } 91 92 t2 = high_resolution_clock::now(); 93 time_span = duration_cast<duration<double>>(t2 - t1); 94 95 #pragma omp master 96 std::cout << "proc "<< mpi_rank <<" omp_barrier "<< time_span.count() << " seconds."<<std::endl; 97 98 t1 = high_resolution_clock::now(); 99 100 for(int i=0; i<n; i++) 101 { 102 //#pragma omp barrier 103 } 104 105 t2 = high_resolution_clock::now(); 106 time_span = duration_cast<duration<double>>(t2 - t1); 107 108 MPI_Barrier(comm); 109 110 #pragma omp master 111 std::cout << "proc "<< mpi_rank <<" for_loop "<< time_span.count() << " seconds."<<std::endl; 112 } 113 114 115 // TEST OF BCAST FROM A RANDOM ROOT 116 { 117 int bcast_root; 118 119 if(rank == 0) bcast_root = rand() % size; 120 121 MPI_Bcast(&bcast_root, 1, MPI_INT, 0, comm); 122 123 int sendbuf[2]; 124 125 sendbuf[0] = rank; 126 sendbuf[1] = size; 127 128 MPI_Bcast(sendbuf, 2, MPI_INT, bcast_root, comm); 129 130 int bcast_test = 0; 131 if(sendbuf[0] == bcast_root && sendbuf[1] == size) bcast_test = 1; 132 133 int bcast_result; 134 135 MPI_Reduce(&bcast_test, &bcast_result, 1, MPI_INT, MPI_MIN, bcast_root, comm); 136 137 if(bcast_result && rank == bcast_root) printf("root = %d : \t test MPI_Bcast \t OK\n", bcast_root); 138 if(!bcast_result && rank == bcast_root) printf("root = %d : \t test MPI_Bcast \t FAILED %d\n", bcast_root, bcast_result); 139 } 140 141 MPI_Barrier(comm); 142 143 // TEST OF GATHER FROM A RAMDOM ROOT 144 { 145 int gather_root; 146 147 if(rank == 0) gather_root = rand() % size; 148 149 MPI_Bcast(&gather_root, 1, MPI_INT, 0, comm); 150 151 double sendbuf[2]; 152 sendbuf[0] = rank * 1.0; 153 sendbuf[1] = size * (-1.0); 154 155 std::vector<double>recvbuf(2*size, 0); 156 157 MPI_Gather(sendbuf, 2, MPI_DOUBLE, recvbuf.data(), 2, MPI_DOUBLE, gather_root, comm); 158 159 bool gather_result = true; 160 161 if(rank == gather_root) 162 { 163 for(int i=0; i<size; i++) 164 { 165 if(abs(recvbuf[2*i] - i) > 1.e-10 || abs(recvbuf[2*i+1] + size) > 1.e-10) 166 { 167 gather_result = false; 168 break; 169 } 170 } 171 172 if(gather_result) printf("root = %d : \t test MPI_Gather \t OK \n", gather_root); 173 else printf("root = %d : \t test MPI_Gather \t FAILED\n", gather_root); 174 } 175 } 176 177 MPI_Barrier(comm); 178 179 // TEST OF GATHERV FROM A RAMDOM ROOT 180 { 181 int gatherv_root; 182 183 if(rank == 0) gatherv_root = rand() % size; 184 185 MPI_Bcast(&gatherv_root, 1, MPI_INT, 0, comm); 186 187 int sendbuf[2]; 188 sendbuf[0] = rank; 189 sendbuf[1] = -size; 190 191 std::vector<int>recvbuf(2*size, 0); 192 193 std::vector<int>recvcounts(size, 2); 194 std::vector<int>displs(size, 0); 195 196 for(int i=0; i<size; i++) displs[i] = 2*(size-1-i); 197 198 MPI_Gatherv(sendbuf, 2, MPI_INT, recvbuf.data(), recvcounts.data(), displs.data(), MPI_INT, gatherv_root, comm); 199 200 bool gatherv_result = true; 201 202 if(rank == gatherv_root) 203 { 204 for(int i=0; i<size; i++) 205 { 206 if(abs(recvbuf[2*i] - (size-1-i)) > 1.e-10 || abs(recvbuf[2*i+1] + size) > 1.e-10) 207 { 208 gatherv_result = false; printf("%lf %lf root = %d, i = %d\n", recvbuf[2*i], recvbuf[2*i+1], gatherv_root, i); 209 break; 210 } 211 } 212 213 //for(int i=0; i<size*2; i++) printf("%lf\t", recvbuf[i]); 214 //printf("\n"); 215 216 if(gatherv_result) printf("root = %d : \t test MPI_Gatherv \t OK\n", gatherv_root); 217 else printf("root = %d : \t test MPI_Gatherv \t FAILED\n", gatherv_root); 218 } 219 } 220 221 MPI_Barrier(comm); 222 223 // TEST OF ALLGATHER 224 { 225 double sendbuf[2]; 226 sendbuf[0] = rank * 1.0; 227 sendbuf[1] = size * (-1.0); 228 229 std::vector<double>recvbuf(2*size, 0); 230 231 MPI_Allgather(sendbuf, 2, MPI_DOUBLE, recvbuf.data(), 2, MPI_DOUBLE, comm); 232 233 int allgather_test = 1; 234 235 for(int i=0; i<size; i++) 236 { 237 if(abs(recvbuf[2*i] - i) > 1.e-10 || abs(recvbuf[2*i+1] + size) > 1.e-10) 238 { 239 allgather_test = 0; 240 break; 241 } 242 } 243 244 int allgather_result; 245 MPI_Reduce(&allgather_test, &allgather_result, 1, MPI_INT, MPI_MIN, 0, comm); 246 247 if(rank == 0 && allgather_result) printf(" \t test MPI_Allgather \t OK \n"); 248 if(rank == 0 && !allgather_result) printf(" \t test MPI_Allgather \t OK \n"); 249 250 } 251 252 MPI_Barrier(comm); 253 254 // TEST OF ALLGATHERV 255 { 256 int sendbuf[2]; 257 sendbuf[0] = rank; 258 sendbuf[1] = -size; 259 260 std::vector<int>recvbuf(2*size, 0); 261 262 std::vector<int>recvcounts(size, 2); 263 std::vector<int>displs(size, 0); 264 265 for(int i=0; i<size; i++) displs[i] = 2*(size-1-i); 266 267 MPI_Allgatherv(sendbuf, 2, MPI_INT, recvbuf.data(), recvcounts.data(), displs.data(), MPI_INT, comm); 268 269 int allgatherv_test = 1; 270 271 272 273 for(int i=0; i<size; i++) 274 { 275 if(abs(recvbuf[2*i] - (size-1-i)) > 1.e-10 || abs(recvbuf[2*i+1] + size) > 1.e-10) 276 { 277 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); 278 break; 279 } 280 } 281 282 283 int allgatherv_result; 284 MPI_Reduce(&allgatherv_test, &allgatherv_result, 1, MPI_INT, MPI_MIN, 0, comm); 285 286 if(rank == 0 && allgatherv_result) printf(" \t test MPI_Allgatherv \t OK \n"); 287 if(rank == 0 && !allgatherv_result) printf(" \t test MPI_Allgatherv \t FAILED %d\n", allgatherv_result); 288 289 } 290 291 MPI_Barrier(comm); 292 293 // TEST OF REDUCE 294 { 295 int reduce_root; 296 297 if(rank == 0) reduce_root = rand() % size; 298 299 MPI_Bcast(&reduce_root, 1, MPI_INT, 0, comm); 300 301 int sendbuf[2]; 302 sendbuf[0] = rank; 303 sendbuf[1] = -size; 304 305 std::vector<int>recvbuf(2, 0); 306 307 MPI_Op op = MPI_MIN; 308 309 MPI_Reduce(sendbuf, recvbuf.data(), 2, MPI_INT, op, reduce_root, comm); 310 311 312 bool reduce_result = true; 313 314 if(rank == reduce_root) 315 { 316 for(int i=0; i<2; i++) 317 { 318 if((op == MPI_SUM && (abs(recvbuf[0]-(size-1)*size/2) > 1.e-10 || abs(recvbuf[1] + size * size) > 1.e-10) ) || 319 (op == MPI_MAX && (abs(recvbuf[0]-(size-1)) > 1.e-10 || abs(recvbuf[1] + size) > 1.e-10) ) || 320 (op == MPI_MIN && (abs(recvbuf[0]) > 1.e-10 || abs(recvbuf[1] + size) > 1.e-10) ) ) 321 { 322 reduce_result = false; printf("%d %d root = %d, i = %d\n", recvbuf[0], recvbuf[1], reduce_root, i); 323 break; 324 } 325 } 326 } 327 328 if(rank == reduce_root && reduce_result) printf("root = %d : \t test MPI_Reduce \t OK\n", reduce_root); 329 if(rank == reduce_root && !reduce_result) printf("root = %d : \t test MPI_Reduce \t FAILED\n", reduce_root); 330 } 331 332 333 MPI_Barrier(comm); 334 335 // TEST OF ALLREDUCE 336 { 337 338 int sendbuf[2]; 339 sendbuf[0] = rank; 340 sendbuf[1] = -size; 341 342 std::vector<int>recvbuf(2, 0); 343 344 MPI_Op op = MPI_MIN; 345 346 MPI_Allreduce(sendbuf, recvbuf.data(), 2, MPI_INT, op, comm); 347 348 349 int allreduce_test = 1; 350 351 352 if((op == MPI_SUM && (abs(recvbuf[0]-(size-1)*size/2) > 1.e-10 || abs(recvbuf[1] + size * size) > 1.e-10) ) || 353 (op == MPI_MAX && (abs(recvbuf[0]-(size-1)) > 1.e-10 || abs(recvbuf[1] + size) > 1.e-10) ) || 354 (op == MPI_MIN && (abs(recvbuf[0]) > 1.e-10 || abs(recvbuf[1] + size) > 1.e-10) ) ) 355 { 356 allreduce_test = 0; printf("%d %d\n", recvbuf[0], recvbuf[1]); 357 } 358 359 360 int allreduce_result; 361 MPI_Reduce(&allreduce_test, &allreduce_result, 1, MPI_INT, MPI_MIN, 0, comm); 362 363 if(rank == 0 && allreduce_result) printf(" \t test MPI_Allreduce \t OK\n"); 364 if(rank == 0 && !allreduce_result) printf(" \t test MPI_Allreduce \t FAILED\n"); 365 } 366 367 368 MPI_Barrier(comm); 369 370 // TEST OF REDUCE_SCATTER 371 { 372 373 std::vector<int>sendbuf(2*size, rank); 374 std::vector<int>recvbuf(2, -1); 375 std::vector<int>recvcounts(size, 2); 376 377 MPI_Op op = MPI_MIN; 378 379 MPI_Reduce_scatter(sendbuf.data(), recvbuf.data(), recvcounts.data(), MPI_INT, op, comm); 380 381 382 int reduce_scatter_test = 1; 383 384 385 if((op == MPI_SUM && (abs(recvbuf[0]-(size-1)*size/2) > 1.e-10 || abs(recvbuf[0]-(size-1)*size/2) > 1.e-10) ) || 386 (op == MPI_MAX && (abs(recvbuf[0]-(size-1)) > 1.e-10 || abs(recvbuf[1]-(size-1)) > 1.e-10) ) || 387 (op == MPI_MIN && (abs(recvbuf[0]) > 1.e-10 || abs(recvbuf[1] ) > 1.e-10) ) ) 388 { 389 reduce_scatter_test = 0; //printf("%d %d id = %d\n", recvbuf[0], recvbuf[1], rank); 390 } 391 392 393 int reduce_scatter_result; 394 MPI_Reduce(&reduce_scatter_test, &reduce_scatter_result, 1, MPI_INT, MPI_MIN, 0, comm); 395 396 if(rank == 0 && reduce_scatter_result) printf(" \t test MPI_Reduce_scatter OK\n"); 397 if(rank == 0 && !reduce_scatter_result) printf(" \t test MPI_Reduce_scatter FAILED\n"); 398 } 399 400 MPI_Barrier(comm); 401 402 // TEST OF SCATTER 403 { 404 405 int scatter_root; 406 407 if(rank == 0) scatter_root = rand() % size; 408 409 MPI_Bcast(&scatter_root, 1, MPI_INT, 0, comm); 410 411 std::vector<int>sendbuf(2*size, rank); 412 std::vector<int>recvbuf(2, -1); 413 std::vector<int>recvcounts(size, 2); 414 415 if(rank == scatter_root) 416 { 417 for(int i=0; i<size; i++) 418 { 419 sendbuf[2*i] = i; 420 sendbuf[2*i+1] = size; 421 } 422 //for(int i=0; i<size*2; i++) printf("%d\t", sendbuf[i]); 423 } 424 425 426 MPI_Scatter(sendbuf.data(), 2, MPI_INT, recvbuf.data(), 2, MPI_INT, scatter_root, comm); 427 428 //printf("ID = %d : %d %d\n", rank, recvbuf[0], recvbuf[1]); 429 430 int scatter_test = 1; 431 432 433 if( abs(recvbuf[0]-rank) > 1.e-10 || abs(recvbuf[1]-size) > 1.e-10 ) 434 { 435 scatter_test = 0; //printf("%d %d id = %d\n", recvbuf[0], recvbuf[1], rank); 436 } 437 438 439 int scatter_result; 440 MPI_Reduce(&scatter_test, &scatter_result, 1, MPI_INT, MPI_MIN, scatter_root, comm); 441 442 if(rank == scatter_root && scatter_result) printf("root = %d : \t test MPI_Scatter \t OK\n", scatter_root); 443 if(rank == scatter_root && !scatter_result) printf("root = %d : \t test MPI_Scatter \t FAILED\n", scatter_root); 444 } 445 446 MPI_Barrier(comm); 447 448 // TEST OF SCATTERV 449 { 450 451 int scatterv_root; 452 453 if(rank == 0) scatterv_root = rand() % size; 454 455 MPI_Bcast(&scatterv_root, 1, MPI_INT, 0, comm); 456 457 std::vector<int>sendbuf(2*size, rank); 458 std::vector<int>recvbuf(2, -1); 459 std::vector<int>sendcounts(size, 2); 460 std::vector<int>displs(size, 0); 461 462 for(int i=0; i<size; i++) displs[i] = 2*(size-1-i); 463 464 if(rank == scatterv_root) 465 { 466 for(int i=0; i<size; i++) 467 { 468 sendbuf[2*i] = i; 469 sendbuf[2*i+1] = size; 470 } 471 } 472 473 474 MPI_Scatterv(sendbuf.data(), sendcounts.data(), displs.data(), MPI_INT, recvbuf.data(), 2, MPI_INT, scatterv_root, comm); 475 476 //printf("ID = %d : %d %d\n", rank, recvbuf[0], recvbuf[1]); 477 478 int scatterv_test = 1; 479 480 481 if( abs(recvbuf[0]-(size-1-rank)) > 1.e-10 || abs(recvbuf[1]-size) > 1.e-10 ) 482 { 483 scatterv_test = 0; printf("%d %d id = %d\n", recvbuf[0], recvbuf[1], rank); 484 } 485 486 487 int scatterv_result; 488 MPI_Reduce(&scatterv_test, &scatterv_result, 1, MPI_INT, MPI_MIN, scatterv_root, comm); 489 490 if(rank == scatterv_root && scatterv_result) printf("root = %d : \t test MPI_Scatterv \t OK\n", scatterv_root); 491 if(rank == scatterv_root && !scatterv_result) printf("root = %d : \t test MPI_Scatterv \t FAILED\n", scatterv_root); 492 } 493 494 MPI_Barrier(comm); 495 496 // TEST OF ALLTOALL 497 { 498 499 std::vector<int>sendbuf(size, rank); 500 std::vector<int>recvbuf(size, -1); 501 502 503 MPI_Alltoall(sendbuf.data(), 1, MPI_INT, recvbuf.data(), 1, MPI_INT, comm); 504 505 int alltoall_result = 1; 506 507 508 for(int i=0; i<size; i++) 509 if( abs(recvbuf[i]-i) > 1.e-10 ) 510 { 511 alltoall_result = 0; printf("%d id = %d\n", recvbuf[i], rank); 512 } 513 514 if(rank == 0 && alltoall_result) printf(" \t test MPI_Alltoall \t OK\n"); 515 if(rank == 0 && !alltoall_result) printf(" \t test MPI_Alltoall \t FAILED\n"); 516 } 517 518 // TEST OF SCAN 519 { 520 521 std::vector<int>sendbuf(2, rank); 522 std::vector<int>recvbuf(2, -1); 523 524 MPI_Op op = MPI_SUM; 525 526 527 MPI_Scan(sendbuf.data(), recvbuf.data(), 2, MPI_INT, op, comm); 528 529 int scan_test = 1; 530 531 // printf(" ID=%d : %d %d \n", rank, recvbuf[0], recvbuf[1]); 532 533 if((op == MPI_SUM && (abs(recvbuf[0]-rank*(rank+1)/2) > 1.e-10 || abs(recvbuf[1]-rank*(rank+1)/2) > 1.e-10) ) || 534 (op == MPI_MIN && (abs(recvbuf[0]) > 1.e-10 || abs(recvbuf[1]) > 1.e-10) ) || 535 (op == MPI_MAX && (abs(recvbuf[0] - rank) > 1.e-10 || abs(recvbuf[1] - rank) > 1.e-10) ) ) 536 { 537 scan_test = 0; //printf("%d id = %d\n", recvbuf[i], rank); 538 } 539 540 int scan_result; 541 MPI_Reduce(&scan_test, &scan_result, 1, MPI_INT, MPI_MIN, 0, comm); 542 543 if(rank == 0 && scan_result) printf(" \t test MPI_Scan \t\t OK\n"); 544 if(rank == 0 && !scan_result) printf(" \t test MPI_Scan \t\t FAILED\n"); 545 } 546 547 548 // TEST OF EXSCAN 549 { 550 551 std::vector<int>sendbuf(2, rank); 552 std::vector<int>recvbuf(2, -1); 553 554 MPI_Op op = MPI_SUM; 555 556 557 MPI_Exscan(sendbuf.data(), recvbuf.data(), 2, MPI_INT, op, comm); 558 559 int exscan_test = 1; 560 561 // printf(" ID=%d : %d %d \n", rank, recvbuf[0], recvbuf[1]); 562 563 if(rank >0) 564 if((op == MPI_SUM && (abs(recvbuf[0]-rank*(rank-1)/2) > 1.e-10 || abs(recvbuf[1]-rank*(rank-1)/2) > 1.e-10) ) || 565 (op == MPI_MIN && (abs(recvbuf[0] ) > 1.e-10 || abs(recvbuf[1]) > 1.e-10) ) || 566 (op == MPI_MAX && (abs(recvbuf[0] - rank+1) > 1.e-10 || abs(recvbuf[1] - rank+1) > 1.e-10) ) ) 567 { 568 exscan_test = 0; //printf("%d id = %d\n", recvbuf[i], rank); 569 } 570 571 int exscan_result; 572 MPI_Reduce(&exscan_test, &exscan_result, 1, MPI_INT, MPI_MIN, 0, comm); 573 574 if(rank == 0 && exscan_result) printf(" \t test MPI_Exscan \t OK\n"); 575 if(rank == 0 && !exscan_result) printf(" \t test MPI_Exscan \t FAILED\n"); 576 } 577 578 579 580 581 /* 582 MPI_Barrier(comm); 583 { 584 int rank, size; 585 MPI_Comm_rank(comm, &rank); 586 MPI_Comm_size(comm, &size); 587 588 //int color = rank%2; 589 int color, remote_leader; 590 if(rank<size-2) {color = 1; remote_leader = size-2;} 591 else {color = 0; remote_leader = 0;} 592 593 printf("rank = %d, color = %d, remote_leader = %d\n", rank, color, remote_leader); 594 595 MPI_Comm sub_comm; 596 MPI_Comm_split(comm, color, rank, &sub_comm); 597 598 599 600 int sub_rank; 601 MPI_Comm_rank(sub_comm, &sub_rank); 602 603 604 MPI_Barrier(comm); 605 if(rank == 0) printf("\tMPI_Comm_split OK\n"); 606 MPI_Barrier(comm); 607 608 MPI_Comm inter_comm; 609 //MPI_Intercomm_create(sub_comm, 0, comm, (color+1)%2, 99, &inter_comm); 610 MPI_Intercomm_create(sub_comm, 0, comm, remote_leader, 99, &inter_comm); 611 612 MPI_Barrier(comm); 613 if(rank == 0) printf("\tMPI_Intercomm_create OK\n"); 614 MPI_Barrier(comm); 615 616 617 618 int high=color; 619 MPI_Comm intra_comm; 620 MPI_Intercomm_merge(inter_comm, high, &intra_comm); 621 622 int intra_rank, intra_size; 623 MPI_Comm_rank(intra_comm, &intra_rank); 624 MPI_Comm_size(intra_comm, &intra_size); 625 626 MPI_Barrier(comm); 627 if(rank == 0) printf("\tMPI_Intercomm_merge OK\n"); 628 MPI_Barrier(comm); 629 } 630 631 //check_test_gatherv(comm); 632 633 // MPI_Barrier(comm); 634 // MPI_Comm_free(&sub_comm); 635 636 637 // MPI_Barrier(comm); 638 // MPI_Comm_free(&inter_comm); 639 */ 640 641 MPI_Barrier(comm); 642 MPI_Comm_free(&comm); 643 } 644 32 645 int num_threads; 33 646 if(mpi_rank < mpi_size-2) … … 102 715 } 103 716 104 105 106 107 108 omp_set_num_threads(4);109 110 111 112 #pragma omp parallel default(shared)113 {114 MPI_Comm_rank(MPI_COMM_WORLD , &mpi_rank);115 116 int num_ep = omp_get_num_threads();117 MPI_Info info;118 119 //printf("mpi_rank = %d, thread_num = %d\n", mpi_rank, omp_get_thread_num());120 121 MPI_Comm *ep_comm;122 #pragma omp master123 {124 MPI_Comm *ep_comm;125 MPI_Comm_create_endpoints(MPI_COMM_WORLD.mpi_comm, num_ep, info, ep_comm);126 passage = ep_comm;127 }128 129 #pragma omp barrier130 131 132 MPI_Comm comm; // this should act as EP_COMM_WORLD133 comm = passage[omp_get_thread_num()];134 135 int rank, size;136 MPI_Comm_rank(comm, &rank);137 MPI_Comm_size(comm, &size);138 139 // TIMING SYCHRONIZATION140 {141 int n=100000;142 143 high_resolution_clock::time_point t1 = high_resolution_clock::now();144 145 for(int i=0; i<n; i++)146 MPI_Barrier_local(comm);147 148 high_resolution_clock::time_point t2 = high_resolution_clock::now();149 duration<double> time_span = duration_cast<duration<double>>(t2 - t1);150 #pragma omp master151 std::cout << "proc "<< mpi_rank <<" ep_barrier "<< time_span.count() << " seconds."<<std::endl;152 153 t1 = high_resolution_clock::now();154 155 for(int i=0; i<n; i++)156 {157 #pragma omp barrier158 }159 160 t2 = high_resolution_clock::now();161 time_span = duration_cast<duration<double>>(t2 - t1);162 163 #pragma omp master164 std::cout << "proc "<< mpi_rank <<" omp_barrier "<< time_span.count() << " seconds."<<std::endl;165 166 t1 = high_resolution_clock::now();167 168 for(int i=0; i<n; i++)169 {170 //#pragma omp barrier171 }172 173 t2 = high_resolution_clock::now();174 time_span = duration_cast<duration<double>>(t2 - t1);175 176 MPI_Barrier(comm);177 178 #pragma omp master179 std::cout << "proc "<< mpi_rank <<" for_loop "<< time_span.count() << " seconds."<<std::endl;180 }181 182 183 // TEST OF BCAST FROM A RANDOM ROOT184 {185 int bcast_root;186 187 if(rank == 0) bcast_root = rand() % size;188 189 MPI_Bcast(&bcast_root, 1, MPI_INT, 0, comm);190 191 int sendbuf[2];192 193 sendbuf[0] = rank;194 sendbuf[1] = size;195 196 MPI_Bcast(sendbuf, 2, MPI_INT, bcast_root, comm);197 198 int bcast_test = 0;199 if(sendbuf[0] == bcast_root && sendbuf[1] == size) bcast_test = 1;200 201 int bcast_result;202 203 MPI_Reduce(&bcast_test, &bcast_result, 1, MPI_INT, MPI_MIN, bcast_root, comm);204 205 if(bcast_result && rank == bcast_root) printf("root = %d : \t test MPI_Bcast \t OK\n", bcast_root);206 if(!bcast_result && rank == bcast_root) printf("root = %d : \t test MPI_Bcast \t FAILED %d\n", bcast_root, bcast_result);207 }208 209 MPI_Barrier(comm);210 211 // TEST OF GATHER FROM A RAMDOM ROOT212 {213 int gather_root;214 215 if(rank == 0) gather_root = rand() % size;216 217 MPI_Bcast(&gather_root, 1, MPI_INT, 0, comm);218 219 double sendbuf[2];220 sendbuf[0] = rank * 1.0;221 sendbuf[1] = size * (-1.0);222 223 std::vector<double>recvbuf(2*size, 0);224 225 MPI_Gather(sendbuf, 2, MPI_DOUBLE, recvbuf.data(), 2, MPI_DOUBLE, gather_root, comm);226 227 bool gather_result = true;228 229 if(rank == gather_root)230 {231 for(int i=0; i<size; i++)232 {233 if(abs(recvbuf[2*i] - i) > 1.e-10 || abs(recvbuf[2*i+1] + size) > 1.e-10)234 {235 gather_result = false;236 break;237 }238 }239 240 if(gather_result) printf("root = %d : \t test MPI_Gather \t OK \n", gather_root);241 else printf("root = %d : \t test MPI_Gather \t FAILED\n", gather_root);242 }243 }244 245 MPI_Barrier(comm);246 247 // TEST OF GATHERV FROM A RAMDOM ROOT248 {249 int gatherv_root;250 251 if(rank == 0) gatherv_root = rand() % size;252 253 MPI_Bcast(&gatherv_root, 1, MPI_INT, 0, comm);254 255 int sendbuf[2];256 sendbuf[0] = rank;257 sendbuf[1] = -size;258 259 std::vector<int>recvbuf(2*size, 0);260 261 std::vector<int>recvcounts(size, 2);262 std::vector<int>displs(size, 0);263 264 for(int i=0; i<size; i++) displs[i] = 2*(size-1-i);265 266 MPI_Gatherv(sendbuf, 2, MPI_INT, recvbuf.data(), recvcounts.data(), displs.data(), MPI_INT, gatherv_root, comm);267 268 bool gatherv_result = true;269 270 if(rank == gatherv_root)271 {272 for(int i=0; i<size; i++)273 {274 if(abs(recvbuf[2*i] - (size-1-i)) > 1.e-10 || abs(recvbuf[2*i+1] + size) > 1.e-10)275 {276 gatherv_result = false; printf("%lf %lf root = %d, i = %d\n", recvbuf[2*i], recvbuf[2*i+1], gatherv_root, i);277 break;278 }279 }280 281 //for(int i=0; i<size*2; i++) printf("%lf\t", recvbuf[i]);282 //printf("\n");283 284 if(gatherv_result) printf("root = %d : \t test MPI_Gatherv \t OK\n", gatherv_root);285 else printf("root = %d : \t test MPI_Gatherv \t FAILED\n", gatherv_root);286 }287 }288 289 MPI_Barrier(comm);290 291 // TEST OF ALLGATHER292 {293 double sendbuf[2];294 sendbuf[0] = rank * 1.0;295 sendbuf[1] = size * (-1.0);296 297 std::vector<double>recvbuf(2*size, 0);298 299 MPI_Allgather(sendbuf, 2, MPI_DOUBLE, recvbuf.data(), 2, MPI_DOUBLE, comm);300 301 int allgather_test = 1;302 303 for(int i=0; i<size; i++)304 {305 if(abs(recvbuf[2*i] - i) > 1.e-10 || abs(recvbuf[2*i+1] + size) > 1.e-10)306 {307 allgather_test = 0;308 break;309 }310 }311 312 int allgather_result;313 MPI_Reduce(&allgather_test, &allgather_result, 1, MPI_INT, MPI_MIN, 0, comm);314 315 if(rank == 0 && allgather_result) printf(" \t test MPI_Allgather \t OK \n");316 if(rank == 0 && !allgather_result) printf(" \t test MPI_Allgather \t OK \n");317 318 }319 320 MPI_Barrier(comm);321 322 // TEST OF ALLGATHERV323 {324 int sendbuf[2];325 sendbuf[0] = rank;326 sendbuf[1] = -size;327 328 std::vector<int>recvbuf(2*size, 0);329 330 std::vector<int>recvcounts(size, 2);331 std::vector<int>displs(size, 0);332 333 for(int i=0; i<size; i++) displs[i] = 2*(size-1-i);334 335 MPI_Allgatherv(sendbuf, 2, MPI_INT, recvbuf.data(), recvcounts.data(), displs.data(), MPI_INT, comm);336 337 int allgatherv_test = 1;338 339 340 341 for(int i=0; i<size; i++)342 {343 if(abs(recvbuf[2*i] - (size-1-i)) > 1.e-10 || abs(recvbuf[2*i+1] + size) > 1.e-10)344 {345 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);346 break;347 }348 }349 350 351 int allgatherv_result;352 MPI_Reduce(&allgatherv_test, &allgatherv_result, 1, MPI_INT, MPI_MIN, 0, comm);353 354 if(rank == 0 && allgatherv_result) printf(" \t test MPI_Allgatherv \t OK \n");355 if(rank == 0 && !allgatherv_result) printf(" \t test MPI_Allgatherv \t FAILED %d\n", allgatherv_result);356 357 }358 359 MPI_Barrier(comm);360 361 // TEST OF REDUCE362 {363 int reduce_root;364 365 if(rank == 0) reduce_root = rand() % size;366 367 MPI_Bcast(&reduce_root, 1, MPI_INT, 0, comm);368 369 int sendbuf[2];370 sendbuf[0] = rank;371 sendbuf[1] = -size;372 373 std::vector<int>recvbuf(2, 0);374 375 MPI_Op op = MPI_MIN;376 377 MPI_Reduce(sendbuf, recvbuf.data(), 2, MPI_INT, op, reduce_root, comm);378 379 380 bool reduce_result = true;381 382 if(rank == reduce_root)383 {384 for(int i=0; i<2; i++)385 {386 if((op == MPI_SUM && (abs(recvbuf[0]-(size-1)*size/2) > 1.e-10 || abs(recvbuf[1] + size * size) > 1.e-10) ) ||387 (op == MPI_MAX && (abs(recvbuf[0]-(size-1)) > 1.e-10 || abs(recvbuf[1] + size) > 1.e-10) ) ||388 (op == MPI_MIN && (abs(recvbuf[0]) > 1.e-10 || abs(recvbuf[1] + size) > 1.e-10) ) )389 {390 reduce_result = false; printf("%d %d root = %d, i = %d\n", recvbuf[0], recvbuf[1], reduce_root, i);391 break;392 }393 }394 }395 396 if(rank == reduce_root && reduce_result) printf("root = %d : \t test MPI_Reduce \t OK\n", reduce_root);397 if(rank == reduce_root && !reduce_result) printf("root = %d : \t test MPI_Reduce \t FAILED\n", reduce_root);398 }399 400 401 MPI_Barrier(comm);402 403 // TEST OF ALLREDUCE404 {405 406 int sendbuf[2];407 sendbuf[0] = rank;408 sendbuf[1] = -size;409 410 std::vector<int>recvbuf(2, 0);411 412 MPI_Op op = MPI_MIN;413 414 MPI_Allreduce(sendbuf, recvbuf.data(), 2, MPI_INT, op, comm);415 416 417 int allreduce_test = 1;418 419 420 if((op == MPI_SUM && (abs(recvbuf[0]-(size-1)*size/2) > 1.e-10 || abs(recvbuf[1] + size * size) > 1.e-10) ) ||421 (op == MPI_MAX && (abs(recvbuf[0]-(size-1)) > 1.e-10 || abs(recvbuf[1] + size) > 1.e-10) ) ||422 (op == MPI_MIN && (abs(recvbuf[0]) > 1.e-10 || abs(recvbuf[1] + size) > 1.e-10) ) )423 {424 allreduce_test = 0; printf("%d %d\n", recvbuf[0], recvbuf[1]);425 }426 427 428 int allreduce_result;429 MPI_Reduce(&allreduce_test, &allreduce_result, 1, MPI_INT, MPI_MIN, 0, comm);430 431 if(rank == 0 && allreduce_result) printf(" \t test MPI_Allreduce \t OK\n");432 if(rank == 0 && !allreduce_result) printf(" \t test MPI_Allreduce \t FAILED\n");433 }434 435 436 MPI_Barrier(comm);437 438 // TEST OF REDUCE_SCATTER439 {440 441 std::vector<int>sendbuf(2*size, rank);442 std::vector<int>recvbuf(2, -1);443 std::vector<int>recvcounts(size, 2);444 445 MPI_Op op = MPI_MIN;446 447 MPI_Reduce_scatter(sendbuf.data(), recvbuf.data(), recvcounts.data(), MPI_INT, op, comm);448 449 450 int reduce_scatter_test = 1;451 452 453 if((op == MPI_SUM && (abs(recvbuf[0]-(size-1)*size/2) > 1.e-10 || abs(recvbuf[0]-(size-1)*size/2) > 1.e-10) ) ||454 (op == MPI_MAX && (abs(recvbuf[0]-(size-1)) > 1.e-10 || abs(recvbuf[1]-(size-1)) > 1.e-10) ) ||455 (op == MPI_MIN && (abs(recvbuf[0]) > 1.e-10 || abs(recvbuf[1] ) > 1.e-10) ) )456 {457 reduce_scatter_test = 0; //printf("%d %d id = %d\n", recvbuf[0], recvbuf[1], rank);458 }459 460 461 int reduce_scatter_result;462 MPI_Reduce(&reduce_scatter_test, &reduce_scatter_result, 1, MPI_INT, MPI_MIN, 0, comm);463 464 if(rank == 0 && reduce_scatter_result) printf(" \t test MPI_Reduce_scatter OK\n");465 if(rank == 0 && !reduce_scatter_result) printf(" \t test MPI_Reduce_scatter FAILED\n");466 }467 468 MPI_Barrier(comm);469 470 // TEST OF SCATTER471 {472 473 int scatter_root;474 475 if(rank == 0) scatter_root = rand() % size;476 477 MPI_Bcast(&scatter_root, 1, MPI_INT, 0, comm);478 479 std::vector<int>sendbuf(2*size, rank);480 std::vector<int>recvbuf(2, -1);481 std::vector<int>recvcounts(size, 2);482 483 if(rank == scatter_root)484 {485 for(int i=0; i<size; i++)486 {487 sendbuf[2*i] = i;488 sendbuf[2*i+1] = size;489 }490 //for(int i=0; i<size*2; i++) printf("%d\t", sendbuf[i]);491 }492 493 494 MPI_Scatter(sendbuf.data(), 2, MPI_INT, recvbuf.data(), 2, MPI_INT, scatter_root, comm);495 496 //printf("ID = %d : %d %d\n", rank, recvbuf[0], recvbuf[1]);497 498 int scatter_test = 1;499 500 501 if( abs(recvbuf[0]-rank) > 1.e-10 || abs(recvbuf[1]-size) > 1.e-10 )502 {503 scatter_test = 0; //printf("%d %d id = %d\n", recvbuf[0], recvbuf[1], rank);504 }505 506 507 int scatter_result;508 MPI_Reduce(&scatter_test, &scatter_result, 1, MPI_INT, MPI_MIN, scatter_root, comm);509 510 if(rank == scatter_root && scatter_result) printf("root = %d : \t test MPI_Scatter \t OK\n", scatter_root);511 if(rank == scatter_root && !scatter_result) printf("root = %d : \t test MPI_Scatter \t FAILED\n", scatter_root);512 }513 514 MPI_Barrier(comm);515 516 // TEST OF SCATTERV517 {518 519 int scatterv_root;520 521 if(rank == 0) scatterv_root = rand() % size;522 523 MPI_Bcast(&scatterv_root, 1, MPI_INT, 0, comm);524 525 std::vector<int>sendbuf(2*size, rank);526 std::vector<int>recvbuf(2, -1);527 std::vector<int>sendcounts(size, 2);528 std::vector<int>displs(size, 0);529 530 for(int i=0; i<size; i++) displs[i] = 2*(size-1-i);531 532 if(rank == scatterv_root)533 {534 for(int i=0; i<size; i++)535 {536 sendbuf[2*i] = i;537 sendbuf[2*i+1] = size;538 }539 }540 541 542 MPI_Scatterv(sendbuf.data(), sendcounts.data(), displs.data(), MPI_INT, recvbuf.data(), 2, MPI_INT, scatterv_root, comm);543 544 //printf("ID = %d : %d %d\n", rank, recvbuf[0], recvbuf[1]);545 546 int scatterv_test = 1;547 548 549 if( abs(recvbuf[0]-(size-1-rank)) > 1.e-10 || abs(recvbuf[1]-size) > 1.e-10 )550 {551 scatterv_test = 0; printf("%d %d id = %d\n", recvbuf[0], recvbuf[1], rank);552 }553 554 555 int scatterv_result;556 MPI_Reduce(&scatterv_test, &scatterv_result, 1, MPI_INT, MPI_MIN, scatterv_root, comm);557 558 if(rank == scatterv_root && scatterv_result) printf("root = %d : \t test MPI_Scatterv \t OK\n", scatterv_root);559 if(rank == scatterv_root && !scatterv_result) printf("root = %d : \t test MPI_Scatterv \t FAILED\n", scatterv_root);560 }561 562 MPI_Barrier(comm);563 564 // TEST OF ALLTOALL565 {566 567 std::vector<int>sendbuf(size, rank);568 std::vector<int>recvbuf(size, -1);569 570 571 MPI_Alltoall(sendbuf.data(), 1, MPI_INT, recvbuf.data(), 1, MPI_INT, comm);572 573 int alltoall_result = 1;574 575 576 for(int i=0; i<size; i++)577 if( abs(recvbuf[i]-i) > 1.e-10 )578 {579 alltoall_result = 0; printf("%d id = %d\n", recvbuf[i], rank);580 }581 582 if(rank == 0 && alltoall_result) printf(" \t test MPI_Alltoall \t OK\n");583 if(rank == 0 && !alltoall_result) printf(" \t test MPI_Alltoall \t FAILED\n");584 }585 586 // TEST OF SCAN587 {588 589 std::vector<int>sendbuf(2, rank);590 std::vector<int>recvbuf(2, -1);591 592 MPI_Op op = MPI_SUM;593 594 595 MPI_Scan(sendbuf.data(), recvbuf.data(), 2, MPI_INT, op, comm);596 597 int scan_test = 1;598 599 // printf(" ID=%d : %d %d \n", rank, recvbuf[0], recvbuf[1]);600 601 if((op == MPI_SUM && (abs(recvbuf[0]-rank*(rank+1)/2) > 1.e-10 || abs(recvbuf[1]-rank*(rank+1)/2) > 1.e-10) ) ||602 (op == MPI_MIN && (abs(recvbuf[0]) > 1.e-10 || abs(recvbuf[1]) > 1.e-10) ) ||603 (op == MPI_MAX && (abs(recvbuf[0] - rank) > 1.e-10 || abs(recvbuf[1] - rank) > 1.e-10) ) )604 {605 scan_test = 0; //printf("%d id = %d\n", recvbuf[i], rank);606 }607 608 int scan_result;609 MPI_Reduce(&scan_test, &scan_result, 1, MPI_INT, MPI_MIN, 0, comm);610 611 if(rank == 0 && scan_result) printf(" \t test MPI_Scan \t\t OK\n");612 if(rank == 0 && !scan_result) printf(" \t test MPI_Scan \t\t FAILED\n");613 }614 615 616 // TEST OF EXSCAN617 {618 619 std::vector<int>sendbuf(2, rank);620 std::vector<int>recvbuf(2, -1);621 622 MPI_Op op = MPI_SUM;623 624 625 MPI_Exscan(sendbuf.data(), recvbuf.data(), 2, MPI_INT, op, comm);626 627 int exscan_test = 1;628 629 // printf(" ID=%d : %d %d \n", rank, recvbuf[0], recvbuf[1]);630 631 if(rank >0)632 if((op == MPI_SUM && (abs(recvbuf[0]-rank*(rank-1)/2) > 1.e-10 || abs(recvbuf[1]-rank*(rank-1)/2) > 1.e-10) ) ||633 (op == MPI_MIN && (abs(recvbuf[0] ) > 1.e-10 || abs(recvbuf[1]) > 1.e-10) ) ||634 (op == MPI_MAX && (abs(recvbuf[0] - rank+1) > 1.e-10 || abs(recvbuf[1] - rank+1) > 1.e-10) ) )635 {636 exscan_test = 0; //printf("%d id = %d\n", recvbuf[i], rank);637 }638 639 int exscan_result;640 MPI_Reduce(&exscan_test, &exscan_result, 1, MPI_INT, MPI_MIN, 0, comm);641 642 if(rank == 0 && exscan_result) printf(" \t test MPI_Exscan \t OK\n");643 if(rank == 0 && !exscan_result) printf(" \t test MPI_Exscan \t FAILED\n");644 }645 646 647 648 649 /*650 MPI_Barrier(comm);651 {652 int rank, size;653 MPI_Comm_rank(comm, &rank);654 MPI_Comm_size(comm, &size);655 656 //int color = rank%2;657 int color, remote_leader;658 if(rank<size-2) {color = 1; remote_leader = size-2;}659 else {color = 0; remote_leader = 0;}660 661 printf("rank = %d, color = %d, remote_leader = %d\n", rank, color, remote_leader);662 663 MPI_Comm sub_comm;664 MPI_Comm_split(comm, color, rank, &sub_comm);665 666 667 668 int sub_rank;669 MPI_Comm_rank(sub_comm, &sub_rank);670 671 672 MPI_Barrier(comm);673 if(rank == 0) printf("\tMPI_Comm_split OK\n");674 MPI_Barrier(comm);675 676 MPI_Comm inter_comm;677 //MPI_Intercomm_create(sub_comm, 0, comm, (color+1)%2, 99, &inter_comm);678 MPI_Intercomm_create(sub_comm, 0, comm, remote_leader, 99, &inter_comm);679 680 MPI_Barrier(comm);681 if(rank == 0) printf("\tMPI_Intercomm_create OK\n");682 MPI_Barrier(comm);683 684 685 686 int high=color;687 MPI_Comm intra_comm;688 MPI_Intercomm_merge(inter_comm, high, &intra_comm);689 690 int intra_rank, intra_size;691 MPI_Comm_rank(intra_comm, &intra_rank);692 MPI_Comm_size(intra_comm, &intra_size);693 694 MPI_Barrier(comm);695 if(rank == 0) printf("\tMPI_Intercomm_merge OK\n");696 MPI_Barrier(comm);697 }698 699 //check_test_gatherv(comm);700 701 // MPI_Barrier(comm);702 // MPI_Comm_free(&sub_comm);703 704 705 // MPI_Barrier(comm);706 // MPI_Comm_free(&inter_comm);707 */708 709 MPI_Barrier(comm);710 MPI_Comm_free(&comm);711 }712 713 717 MPI_Finalize(); 714 718
Note: See TracChangeset
for help on using the changeset viewer.