Advertisement
Margoshinka

round_robin

Jan 18th, 2023
1,204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 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 world_rank of the process
  14.     int world_rank;
  15.     MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  16.  
  17.     MPI_Request request;
  18.     MPI_Status st;
  19.     int msg;
  20.  
  21.     if (world_rank == 0) {
  22.         msg = 0;
  23.  
  24.         MPI_Send(&msg, 1, MPI_INT, world_size-1, 1, MPI_COMM_WORLD);
  25.         printf("Process %d sends %d to proc %d\n", world_rank, msg, world_size - 1);
  26.  
  27.         MPI_Recv(&msg, 1, MPI_INT, world_rank+1, 1, MPI_COMM_WORLD, &st);
  28.         printf("Process %d got %d from proc %d\n", world_rank, msg, world_size);
  29.  
  30.         msg++;
  31.         MPI_Send(&msg, 1, MPI_INT, world_size - 1, 1, MPI_COMM_WORLD);
  32.         printf("Process %d sends %d to proc %d\n", world_rank, msg, world_size - 1);
  33.  
  34.         MPI_Recv(&msg, 1, MPI_INT, world_rank + 1, 1, MPI_COMM_WORLD, &st);
  35.         printf("Process %d got %d from proc %d\n", world_rank, msg, world_size);
  36.  
  37.     }
  38.     if (world_rank > 0 && world_rank < (world_size - 1)) {
  39.         MPI_Recv(&msg, 1, MPI_INT, world_rank + 1, 1, MPI_COMM_WORLD, &st);
  40.         printf("Process %d got %d from proc %d\n", world_rank, msg, world_rank + 1);
  41.         msg++;
  42.         MPI_Send(&msg, 1, MPI_INT, world_rank - 1, 1, MPI_COMM_WORLD);
  43.         printf("Process %d sends %d to proc %d\n", world_rank, msg, world_rank - 1);
  44.  
  45.         MPI_Recv(&msg, 1, MPI_INT, world_rank + 1, 1, MPI_COMM_WORLD, &st);
  46.         printf("Process %d got %d from proc %d\n", world_rank, msg, world_rank + 1);
  47.         msg++;
  48.         MPI_Send(&msg, 1, MPI_INT, world_rank - 1, 1, MPI_COMM_WORLD);
  49.         printf("Process %d sends %d to proc %d\n", world_rank, msg, world_rank - 1);
  50.     }
  51.     if (world_rank == world_size - 1) {
  52.         MPI_Recv(&msg, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &st);
  53.        
  54.         printf("Process %d got %d from proc %d\n", world_rank, msg, 0);
  55.         msg++;
  56.         MPI_Send(&msg, 1, MPI_INT, world_rank - 1, 1, MPI_COMM_WORLD);
  57.         printf("Process %d sends %d to proc %d\n", world_rank, msg, world_rank - 1);
  58.  
  59.         MPI_Recv(&msg, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &st);
  60.  
  61.         printf("Process %d got %d from proc %d\n", world_rank, msg, 0);
  62.         msg++;
  63.         MPI_Send(&msg, 1, MPI_INT, world_rank - 1, 1, MPI_COMM_WORLD);
  64.         printf("Process %d sends %d to proc %d\n", world_rank, msg, world_rank - 1);
  65.     }
  66.  
  67.     // Finalize the MPI environment.
  68.     MPI_Finalize();
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement