hinagawa

7_2

Dec 7th, 2020 (edited)
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 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 world_rank;
  13.     int world_size;
  14.  
  15.     const int N = 4;
  16.     MPI_Init(&argc, &argv);
  17.     MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  18.     MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  19.  
  20.     int proc = world_rank / 3;
  21.  
  22.     MPI_Comm my_comm;
  23.     MPI_Comm_split(MPI_COMM_WORLD, proc, world_rank % 3, &my_comm);
  24.  
  25.     int rank = -1;
  26.     int size = -1;
  27.  
  28.     if (my_comm != MPI_COMM_NULL) {
  29.         MPI_Comm_rank(my_comm, &rank);
  30.     }
  31.  
  32.     if (my_comm != MPI_COMM_NULL) {
  33.         MPI_Comm_size(my_comm, &size);
  34.     }
  35.  
  36.     int array[N] = { proc, proc, proc, proc };
  37.  
  38.     int com[N * 3];
  39.  
  40.     MPI_Gather(&array, 4, MPI_INT, &com, 4, MPI_INT, 0, my_comm);
  41.  
  42.     if (rank == 0) {
  43.  
  44.         cout << "Process " << proc << " com = ";
  45.  
  46.         for (int i = 0; i < 12; ++i) {
  47.  
  48.             cout << com[i] << " ";
  49.  
  50.         }
  51.  
  52.         cout << endl;
  53.  
  54.     }
  55.  
  56.     int tag = -1;
  57.     int rlead = -1;
  58.  
  59.     if (proc == 0) {
  60.         tag = 333;
  61.         rlead = 3;
  62.     }
  63.  
  64.     if (proc == 1) {
  65.         tag = 333;
  66.         rlead = 0;
  67.     }
  68.  
  69.     if (proc == 2) {
  70.         tag = 999;
  71.         rlead = 9;
  72.     }
  73.  
  74.     if (proc == 3) {
  75.         tag = 999;
  76.         rlead = 6;
  77.     }
  78.  
  79.     MPI_Comm intercomm;
  80.  
  81.     MPI_Intercomm_create(my_comm, 0, MPI_COMM_WORLD, rlead, tag, &intercomm);
  82.  
  83.     if ((proc == 0 || proc == 2) && rank == 0) {
  84.  
  85.         MPI_Send(&com, 12, MPI_INT, 0, proc, intercomm);
  86.  
  87.     }
  88.  
  89.     if ((proc == 1 || proc == 3) && rank == 0) {
  90.  
  91.         int buf[12];
  92.  
  93.         MPI_Recv(&buf, 12, MPI_INT, MPI_ANY_SOURCE, proc - 1, intercomm, MPI_STATUS_IGNORE);
  94.  
  95.         cout << "From " << proc << " buf = ";
  96.  
  97.         for (int i = 0; i < 12; ++i) {
  98.  
  99.             cout << buf[i] << " ";
  100.  
  101.         }
  102.  
  103.     }
  104.  
  105.     if (my_comm != MPI_COMM_NULL) MPI_Comm_free(&my_comm);
  106.  
  107.     MPI_Finalize();
  108.     cout<<endl;
  109.     return 0;
  110. }
Add Comment
Please, Sign In to add comment