Advertisement
Margoshinka

bsend

Feb 1st, 2023
796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 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.         // Send
  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.         // Bsend
  30.         int buffer_attached_size = ITERATION_NUMBER * (MPI_BSEND_OVERHEAD + sizeof(int));
  31.         char* buffer_attached = (char*)malloc(buffer_attached_size);
  32.         MPI_Buffer_attach(buffer_attached, buffer_attached_size);
  33.         time = MPI_Wtime();
  34.         for (int i = 0; i < ITERATION_NUMBER; i++)
  35.             MPI_Bsend(&i, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
  36.         time = MPI_Wtime() - time;
  37.         printf("Bsend: %.10f\n", time / ITERATION_NUMBER * 1E6);
  38.         MPI_Buffer_detach(buffer_attached, &buffer_attached_size);
  39.         free(buffer_attached);
  40.  
  41.  
  42.         // Isend
  43.         MPI_Request requests[ITERATION_NUMBER];
  44.         MPI_Status statuses[ITERATION_NUMBER];
  45.         time = MPI_Wtime();
  46.         for (int i = 0; i < ITERATION_NUMBER; i++)
  47.             MPI_Isend(&i, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, &requests[i]);
  48.         MPI_Waitall(ITERATION_NUMBER, requests, statuses);
  49.         time = MPI_Wtime() - time;
  50.         printf("Isend: %.10f\n", time / ITERATION_NUMBER * 1E6);
  51.  
  52.         break;
  53.     }
  54.     case 1: {
  55.         MPI_Status status;
  56.         MPI_Request requests[ITERATION_NUMBER];
  57.         MPI_Status statuses[ITERATION_NUMBER];
  58.         int rec[ITERATION_NUMBER];
  59.         for (int i = 0; i < ITERATION_NUMBER; i++) {
  60.             MPI_Recv(&rec[i], 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
  61.         }
  62.         for (int i = 0; i < ITERATION_NUMBER; i++) {
  63.             MPI_Recv(&rec[i], 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
  64.         }
  65.        
  66.         for (int i = 0; i < ITERATION_NUMBER; i++) {
  67.             MPI_Irecv(&rec[i], 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &requests[i]);
  68.            
  69.         }
  70.         MPI_Waitall(ITERATION_NUMBER, requests, statuses);
  71.         break;
  72.     }
  73.     default: {
  74.         break;
  75.     }
  76.     }
  77.  
  78.     // Finalize the MPI environment.
  79.     MPI_Finalize();
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement