Advertisement
VladSmirN

parall_lab_2

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