Advertisement
noctual

Коллективная

Nov 15th, 2021
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <iostream>
  2. #include "mpi.h"
  3.  
  4. int main(int argc, char* argv[]) {
  5.     MPI_Init(&argc, &argv);
  6.  
  7.     double starttime, endtime;
  8.     int world_rank, count_ranks, total;
  9.     MPI_Comm_size(MPI_COMM_WORLD, &count_ranks);
  10.     MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  11.     /*
  12.     // scan
  13.     starttime = MPI_Wtime();
  14.  
  15.     MPI_Scan(&world_rank, &total, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
  16.  
  17.     endtime = MPI_Wtime();
  18.  
  19.     printf("[MPI process %d library] Total = %d.\n", world_rank, total);
  20.     printf("[MPI process %d library] Time = %g ms\n", world_rank, (endtime - starttime));
  21.     */
  22.     // custom
  23.     starttime = MPI_Wtime();
  24.  
  25.     if (world_rank == 0) {
  26.         total = world_rank;
  27.         MPI_Send(&total, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
  28.     }
  29.     else if (world_rank < count_ranks - 1) {
  30.         MPI_Recv(&total, 1, MPI_INT, (world_rank - 1), 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  31.         total += world_rank;
  32.         MPI_Send(&total, 1, MPI_INT, (world_rank + 1), 0, MPI_COMM_WORLD);
  33.     }
  34.     else {
  35.         MPI_Recv(&total, 1, MPI_INT, (world_rank - 1), 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  36.         total += world_rank;
  37.     }
  38.  
  39.     endtime = MPI_Wtime();
  40.  
  41.     printf("[MPI process %d] Total = %d.\n", world_rank, total);
  42.     printf("[MPI process %d] Time = %g ms\n", world_rank, (endtime - starttime));
  43.    
  44.     MPI_Finalize();
  45.     return 0;
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement