hinagawa

7_1

Dec 7th, 2020 (edited)
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <mpi.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5.  
  6. using namespace std;
  7.  
  8.  
  9.  
  10. int main(int argc, char** argv) {
  11.  
  12.     int newGroupOfProcesses[5] = { 8, 3, 9, 1, 6 };
  13.  
  14.     int rank;
  15.     int world_size;
  16.     MPI_Init(&argc, &argv);
  17.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  18.  
  19.     MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  20.  
  21.     MPI_Status status;
  22.  
  23.     MPI_Group groupFirst;
  24.     MPI_Group groupSecond;
  25.     MPI_Comm_group(MPI_COMM_WORLD, &groupFirst);
  26.     MPI_Group_incl(groupFirst, 5, newGroupOfProcesses, &groupSecond);
  27.  
  28.     MPI_Comm comm;
  29.     MPI_Comm_create(MPI_COMM_WORLD, groupSecond, &comm);
  30.  
  31.     int current_rank = -1;
  32.  
  33.     int a[10];
  34.  
  35.     if (comm != MPI_COMM_NULL) {
  36.         MPI_Comm_rank(comm, &current_rank);
  37.     }
  38.  
  39.     if (current_rank == 0 && comm != MPI_COMM_NULL) {
  40.  
  41.         for (int i = 0; i < 10; i++) {
  42.  
  43.             a[i] = rand() % 10;
  44.  
  45.         }
  46.  
  47.         MPI_Bcast(&a, 10, MPI_INT, 0, comm);
  48.  
  49.     }
  50.  
  51.     if (current_rank != -1 && comm != MPI_COMM_NULL) {
  52.  
  53.         MPI_Bcast(&a, 10, MPI_INT, 0, comm);
  54.  
  55.         cout << "Process number " << current_rank << " with array: " << endl;
  56.  
  57.         for (int i = 0; i < 10; i++) {
  58.  
  59.             cout << a[i] << " ";
  60.  
  61.         }
  62.  
  63.         cout << endl;
  64.  
  65.         if (current_rank == 4) {
  66.  
  67.             MPI_Send(a, 10, MPI_INT, 0, 0, MPI_COMM_WORLD);
  68.  
  69.         }
  70.  
  71.     }
  72.  
  73.     if (rank == 0) {
  74.  
  75.         MPI_Recv(a, 10, MPI_INT, 6, 0, MPI_COMM_WORLD, &status);
  76.  
  77.         cout << "Array of results " << endl;
  78.  
  79.         for (int i = 0; i < 10; i++) {
  80.  
  81.             cout << a[i] << " ";
  82.  
  83.         }
  84.  
  85.         cout << endl;
  86.  
  87.     }
  88.  
  89.     if (comm != MPI_COMM_NULL) {
  90.  
  91.         MPI_Comm_free(&comm);
  92.  
  93.     }
  94.  
  95.     MPI_Group_free(&groupSecond);
  96.     MPI_Finalize();
  97.     return 0;
  98. }
Add Comment
Please, Sign In to add comment