Margoshinka

send isend

Jan 25th, 2023
1,312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include <mpi.h>
  2. #include <stdio.h>
  3. #include <string>
  4.  
  5. int main(int argc, char** argv) {
  6.     // Initialize the MPI environment
  7.     MPI_Init(&argc, &argv);
  8.  
  9.     // Get the number of processes
  10.     int world_size;
  11.     MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  12.  
  13.     // Get the rank of the process
  14.     int world_rank;
  15.     MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  16.  
  17.     const int ITERATION_NUMBER = 1000;
  18.  
  19.     switch (world_rank) {
  20.     case 0: {
  21.         double time = MPI_Wtime();
  22.  
  23.      
  24.         for (int i = 0; i < ITERATION_NUMBER; i++)
  25.             MPI_Send(&i, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
  26.         time = MPI_Wtime() - time;
  27.         printf("Send: %.10f\n", time / ITERATION_NUMBER * 1E6);
  28.  
  29.  
  30.         MPI_Request requests[ITERATION_NUMBER];
  31.         MPI_Status statuses[ITERATION_NUMBER];
  32.         time = MPI_Wtime();
  33.         for (int i = 0; i < ITERATION_NUMBER; i++)
  34.             MPI_Isend(&i, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, &requests[i]);
  35.         MPI_Waitall(ITERATION_NUMBER, requests, statuses);
  36.         time = MPI_Wtime() - time;
  37.         printf("Isend: %.10f\n", time / ITERATION_NUMBER * 1E6);
  38.  
  39.        
  40.    
  41.  
  42.         break;
  43.     }
  44.     case 1: {
  45.         MPI_Status status;
  46.         int rec;
  47.         for (int i = 0; i < ITERATION_NUMBER; i++) {
  48.             MPI_Recv(&rec, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
  49.         }
  50.         break;
  51.     }
  52.     default: {
  53.         break;
  54.     }
  55.     }
  56.  
  57.     // Finalize the MPI environment.
  58.     MPI_Finalize();
  59. }
Advertisement
Add Comment
Please, Sign In to add comment