Advertisement
noctual

топология и коммуникаторы

Dec 15th, 2021
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include "mpi.h"
  6.  
  7. using namespace std;
  8.  
  9. bool isPrimeNum(int num) {
  10.     for (int i = 2; i < num; i++) {
  11.         if (num % i == 0) {
  12.             return true;
  13.         }
  14.     }
  15.     return false;
  16. }
  17.  
  18. int main(int argc, char* argv[]) {
  19.     MPI_Init(&argc, &argv);
  20.  
  21.     // Get world rank
  22.     int rankWorld;
  23.     MPI_Comm_rank(MPI_COMM_WORLD, &rankWorld);
  24.  
  25.     //
  26.     int ranksWorldCount;
  27.     MPI_Comm_size(MPI_COMM_WORLD, &ranksWorldCount);
  28.  
  29.     // Get the group from the default communicator
  30.     MPI_Group groupWorld;
  31.     MPI_Comm_group(MPI_COMM_WORLD, &groupWorld);
  32.  
  33.     //
  34.     vector<int> groupPrimeNumProcesses, groupOtherProcesses;
  35.     for (int i = 0; i < ranksWorldCount; i++) {
  36.         if (!isPrimeNum(i)) {
  37.             groupPrimeNumProcesses.push_back(i);
  38.         }
  39.         else {
  40.             groupOtherProcesses.push_back(i);
  41.         }
  42.     }
  43.  
  44.     // *** CART ***
  45.     int ranksPrimeNumCount = groupPrimeNumProcesses.size();
  46.  
  47.     MPI_Group groupPrimeNum;
  48.     MPI_Group_incl(groupWorld, ranksPrimeNumCount, &groupPrimeNumProcesses[0], &groupPrimeNum); // MPI_COMM_NULL
  49.  
  50.     MPI_Comm communicatorPrimeNum;
  51.     MPI_Comm_create(MPI_COMM_WORLD, groupPrimeNum, &communicatorPrimeNum);
  52.  
  53.     if (communicatorPrimeNum != MPI_COMM_NULL)
  54.     {
  55.         // Get PrimeNum communicator rank
  56.         int rankPrimeNum;
  57.         MPI_Comm_rank(communicatorPrimeNum, &rankPrimeNum);
  58.  
  59.         const int ndims = 1;
  60.         int dim_size[ndims] = { ranksPrimeNumCount };
  61.         int periods[ndims] = { 1 };
  62.  
  63.         MPI_Comm communicatorCart;
  64.         MPI_Cart_create(communicatorPrimeNum, ndims, dim_size, periods, 0, &communicatorCart);
  65.  
  66.         int rank_from, rank_to;
  67.         MPI_Cart_shift(communicatorCart, 0, 1, &rank_from, &rank_to);
  68.  
  69.         double A = rand() % 500;
  70.         double B;
  71.  
  72.         MPI_Status status;
  73.         MPI_Sendrecv(&A, 1, MPI_DOUBLE, rank_to, 123, &B, 1, MPI_DOUBLE, rank_from, 123, communicatorCart, &status);
  74.  
  75.         printf("[Cart] Rank global = %i, from Rank %i to Rank %i\n", rankWorld, rank_from, rank_to);
  76.  
  77.         MPI_Comm_free(&communicatorCart);
  78.         MPI_Comm_free(&communicatorPrimeNum);
  79.     }
  80.  
  81.     // MPI_Comm_free(&communicatorPrimeNum);
  82.  
  83.     MPI_Group_free(&groupPrimeNum);
  84.     // *** CART END ***
  85.  
  86.     // *** GRAPH ***
  87.     int ranksOtherCount = groupOtherProcesses.size();
  88.  
  89.     MPI_Group groupOther;
  90.     MPI_Group_incl(groupWorld, ranksOtherCount, &groupOtherProcesses[0], &groupOther); // MPI_COMM_NULL
  91.  
  92.     MPI_Comm communicatorOther;
  93.     MPI_Comm_create(MPI_COMM_WORLD, groupOther, &communicatorOther);
  94.  
  95.     if (communicatorOther != MPI_COMM_NULL)
  96.     {
  97.         int* index = new int[ranksOtherCount];
  98.         for (int k = 0; k < ranksOtherCount; k++) {
  99.             index[k] = ranksOtherCount - 1 + k;
  100.         }
  101.         int* edges = new int[2 * ranksOtherCount - 2];
  102.         for (int k = 0; k < ranksOtherCount - 1; k++) {
  103.             edges[k] = k + 1;
  104.             edges[ranksOtherCount - 1 + k] = 0;
  105.         }
  106.  
  107.         MPI_Comm Gr_Comm;
  108.         MPI_Graph_create(communicatorOther, ranksOtherCount, index, edges, false, &Gr_Comm);
  109.  
  110.         int Rank;
  111.         MPI_Comm_rank(Gr_Comm, &Rank);
  112.  
  113.         int Num;
  114.         MPI_Graph_neighbors_count(Gr_Comm, Rank, &Num);
  115.  
  116.         int* Neighbors = new int[Num];
  117.         MPI_Graph_neighbors(Gr_Comm, Rank, Num, Neighbors);
  118.  
  119.         for (int k = 0; k < Num; k++) {
  120.             int rankIn;
  121.             MPI_Status St;
  122.             MPI_Sendrecv(&Rank, 1, MPI_INT, Neighbors[k], 5, &rankIn, 1, MPI_INT, Neighbors[k], 5, Gr_Comm, &St);
  123.             printf("[Graph] Rank global = %i, from Rank %i to Rank %i\n", rankWorld, Rank, Neighbors[k]);
  124.         }
  125.  
  126.         MPI_Comm_free(&Gr_Comm);
  127.         MPI_Comm_free(&communicatorOther);
  128.         delete[] Neighbors;
  129.         delete[] edges;
  130.         delete[] index;
  131.     }
  132.  
  133.     MPI_Group_free(&groupOther);
  134.     // *** GRAPH END ***
  135.  
  136.     MPI_Group_free(&groupWorld);
  137.  
  138.     MPI_Finalize();
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement