Advertisement
Margoshinka

parallel1

Jan 12th, 2023
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <mpi.h>
  2. #include <stdio.h>
  3. #include <string>
  4. #include <iostream>
  5.  
  6.  
  7.  
  8. using namespace std;
  9. int main(int argc, char** argv) {
  10.    
  11.     // Initialize the MPI environment
  12.     MPI_Init(&argc, &argv);
  13.  
  14.     // Get the number of processes
  15.     int world_size;
  16.     MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  17.  
  18.     // Get the rank of the process
  19.     int world_rank;
  20.     MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  21.     char processor_name[MPI_MAX_PROCESSOR_NAME];
  22.     int name_len;
  23.     MPI_Get_processor_name(processor_name, &name_len);
  24.  
  25.     //// Print off a hello world message
  26.     //printf("Hello world from processor %s, rank %d out of %d processors\n",
  27.     //    processor_name, world_rank, world_size);
  28.  
  29.  
  30.     switch (world_rank) {
  31.     case 0: {
  32.         int n = 123;
  33.         MPI_Send(&n, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
  34.         printf("The rank of the sending process: %d, The value being sent: %d\n", world_rank,n);
  35.         break;
  36.     }
  37.     case 1: {
  38.         int rec=2;
  39.        
  40.         cout << "Variable before receiving:" << rec << endl;
  41.         MPI_Recv(&rec, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  42.         printf("The rank of the recipient process: %d, Variable after receiving: %d\n", world_rank, rec);
  43.         break;
  44.     }
  45.     default: {
  46.         break;
  47.     }
  48.     }
  49.  
  50.     // Finalize the MPI environment.
  51.     MPI_Finalize();
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement