Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. Vector& SquareMatrix::MPImulVector(const Vector& vector) const {
  2.  
  3.     double* arr = this->array;
  4.     double result[size];
  5.     double* buffer;
  6.     int amount, rank;
  7.  
  8.     MPI_Init(&argc, &argv);
  9.     MPI_Comm_size(MPI_COMM_WORLD, &amount);
  10.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  11.  
  12.     unsigned lines = size / amount;
  13.     unsigned remLines = size - amount * lines;
  14.     double res[lines];
  15.  
  16.     if (rank == 0) {
  17.  
  18.         buffer = arr;
  19.         for (unsigned it = 1; it < amount - 1; ++it)
  20.             MPI_Send(arr + it * lines * size, lines * size, MPI_DOUBLE, it, 0, MPI_COMM_WORLD);
  21.         if (amount > 1)
  22.             MPI_Send(arr + (amount - 1) * lines * size, (lines + remLines) * size, MPI_DOUBLE, amount - 1, 0, MPI_COMM_WORLD);
  23.     }
  24.  
  25.     else if (rank != amount - 1) {
  26.  
  27.         double buffer[lines * size];
  28.         MPI_Recv(buffer, lines * size, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  29.     }
  30.  
  31.     else {
  32.  
  33.         lines += remLines;
  34.  
  35.         double buffer[lines * size];
  36.         MPI_Recv(buffer, lines * size, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  37.     }
  38.  
  39.     for (unsigned it = 0; it != lines; ++it)
  40.         res[it] = 0;
  41.  
  42.     for (unsigned it = 0; it != lines; ++it)
  43.         for (unsigned j = 0; j != size; ++j)
  44.             res[it] += buffer[it * size + j] * vector[j];
  45.  
  46.     if (rank != 0) {
  47.         MPI_Send(res, lines, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD);
  48.     }
  49.  
  50.     else {
  51.  
  52.         for (unsigned it = 0; it != lines; ++it)
  53.             result[it] = res[it];
  54.  
  55.         for (unsigned it = 1; it < amount - 1; ++it)
  56.             MPI_Recv(result + it * lines, lines, MPI_DOUBLE, it, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  57.  
  58.         if (amount > 1)
  59.             MPI_Recv(result + (amount - 1) * lines, lines + remLines, MPI_DOUBLE, amount - 1, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  60.     }
  61.  
  62.     Vector& New = *(new Vector(result, size));
  63.     MPI_Finalize();
  64.     return New;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement