Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. Vector<T>& mulVector(const Vector<T>& vector) const {
  2.  
  3.     T* tmp = this->array;
  4.     T* vec, res;
  5.    
  6.     char* argv[1]; argv[0] = nullptr;
  7.     int argc = 0;
  8.     MPI_Init(&argc, &argv);
  9.     unsigned amount, rank;
  10.    
  11.     MPI_Comm_size(MPI_COMM_WORLD, &amount);
  12.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  13.  
  14.     unsigned lines = size / amount;
  15.     if (rank == 0) {
  16.         T* vec = tmp;
  17.         for (unsigned it = 1; it != amount; ++it)
  18.             MPI_Send(tmp + it * lines * size, lines * size, MPI_DOUBLE, it, 1, MPI_COMM_WORLD);
  19.     }
  20.     else {
  21.         T* vec = tmp + rank * lines * size;
  22.         MPI_Recv(vec, lines * size, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  23.     }
  24.  
  25.     for (unsigned it = 0; it != lines; ++it)
  26.         for (unsigned j = 0; j != size; ++j)
  27.             res[it] += vec[it * size + j] * vector[j];
  28.  
  29.  
  30.     if (rank != 0) {
  31.         MPI_Send(res, lines, MPI_DOUBLE, 0, 2, MPI_COMM_WORLD);
  32.     }
  33.  
  34.     else {
  35.         T* final = T[size * size];
  36.         for (unsigned it = 1; it != amount; ++it)
  37.             MPI_Recv(final + it * lines * size, lines * size, MPI_DOUBLE, it, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  38.         Vector<T>& New = *(new Vector<T>(final, size));
  39.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement