Cawa245

Untitled

Dec 14th, 2020
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. #include <cstdio>
  2. #include "mpi.h"
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7.  
  8.  
  9. int main(int* argc, char** argv)
  10. {
  11.  
  12.     int vecResult [10];
  13.     int vec[10];
  14.     int matrix[10][10];
  15.  
  16.     MPI_Init(argc, &argv);
  17.     int world_rank, world_size;;
  18.     MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  19.     MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  20.  
  21.    
  22.     for (auto i = 0; i < 10; i++) vecResult[i] = 0;
  23.  
  24.     const int arrayCount = sizeof matrix / sizeof matrix[0];
  25.     const int sizeBlock = sizeof matrix[0] / sizeof matrix[0][0];
  26.  
  27.  
  28.     int* len = static_cast<int*>(malloc(sizeof(int) * world_size));
  29.     int* index = static_cast<int*>(malloc(sizeof(int) * world_size));
  30.  
  31.     int* vecLen = static_cast<int*>(malloc(sizeof(int) * world_size));
  32.     int* vecIndex = static_cast<int*>(malloc(sizeof(int) * world_size));
  33.  
  34.     if (arrayCount % world_size == 0)
  35.     {
  36.         const auto l = arrayCount * sizeBlock / world_size;
  37.  
  38.         for (auto i = 0; i < world_size; i++)
  39.         {
  40.             len[i] = l;
  41.             index[i] = (i)*l;
  42.             vecLen[i] = arrayCount / world_size;
  43.             vecIndex[i] = i * (arrayCount / world_size);
  44.         }
  45.     }
  46.     else
  47.     {
  48.         auto k = 0;
  49.         auto c = arrayCount / world_size;
  50.         for (auto i = 0; i < arrayCount % world_size; ++i)
  51.         {
  52.             len[i] = sizeBlock * (c + 1);
  53.             index[i] = k;
  54.             vecLen[i] = c + 1;
  55.             vecIndex[i] = i * (c + 1);
  56.             k += len[i];
  57.         }
  58.  
  59.         for (auto i = arrayCount % world_size; i < world_size; i++)
  60.         {
  61.             len[i] = sizeBlock * (c);
  62.             index[i] = k;
  63.             k += len[i];
  64.             vecLen[i] = c;
  65.             vecIndex[i] = i * (c);
  66.         }
  67.     }
  68.  
  69.     if (world_rank == 0)
  70.     {
  71.         for (int i = 0; i < arrayCount; ++i)
  72.         {
  73.  
  74.             for (int j = 0; j < sizeBlock; ++j)
  75.             {
  76.                 matrix[i][j] = rand() % 10;
  77.             }
  78.            
  79.             vec[i] = rand() % 10;
  80.         }
  81.     }
  82.  
  83.    
  84.  
  85.     const auto countOfRow = (len[world_rank]) / sizeBlock;
  86.     auto* const localMatrix = static_cast<int*>(malloc(sizeof(int) * countOfRow * sizeBlock));
  87.     auto* const localVec = static_cast<int*>(malloc(sizeof(int) * countOfRow));
  88.    
  89.     MPI_Scatterv(matrix, len, index, MPI_INT, localMatrix, len[world_rank], MPI_INT, 0, MPI_COMM_WORLD);
  90.     MPI_Scatterv(vec, vecLen, vecIndex, MPI_INT, localVec, len[world_rank], MPI_INT, 0, MPI_COMM_WORLD);
  91.  
  92.     int* localSum = static_cast<int*>(malloc(sizeof(int) * countOfRow));
  93.     for (int i = 0; i < countOfRow; ++i)
  94.     {
  95.         localSum[i] = 0;
  96.         for (int j = 0; j < sizeBlock; ++j)
  97.             localSum[i] += *(localMatrix + i * sizeBlock + j) * *(localVec + j);
  98.  
  99.     }
  100.  
  101.     const auto localIndex = vecIndex[world_rank];
  102.     auto* localArray = static_cast<int*>(malloc(sizeof(int) * sizeBlock));
  103.     for (int i = 0; i < sizeBlock; ++i) localArray[i] = 0;
  104.     for (int i = 0; i < countOfRow; ++i) localArray[localIndex + i] = localSum[i];
  105.  
  106.  
  107.    
  108.     MPI_Reduce(localArray, vecResult, sizeBlock, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  109.     //Вывод и конец
  110.     if (world_rank == 0)
  111.         for (int i = 0; i < arrayCount; ++i)
  112.             printf("%d \n", vecResult[i]);
  113.     MPI_Finalize();
  114. }
Advertisement
Add Comment
Please, Sign In to add comment