Advertisement
filip710

LV5Z1

Jan 20th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. // https://github.com/elahehrashedi/MPI_Matrix_Multiplication_Scatter_Gather/blob/master/matrix.c
  2.  
  3. #include <mpi.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.     int matrixSize, rank, size;
  10.  
  11.     double *sendA, *sendB, *finalC, *recA, *recC;
  12.  
  13.     MPI::Init(argc, argv);
  14.  
  15.     size = MPI::COMM_WORLD.Get_size();
  16.     rank = MPI::COMM_WORLD.Get_rank();
  17.  
  18.     if (rank == 0)
  19.     {
  20.         matrixSize = 10;
  21.         printf("rows is %d \n", matrixSize);
  22.  
  23.         sendA = new double[matrixSize * matrixSize];
  24.         sendB = new double[matrixSize * matrixSize];
  25.         finalC = new double[matrixSize * matrixSize];
  26.  
  27.         for (int i = 0; i < matrixSize; i++)
  28.             for (int j = 0; j < matrixSize; j++)
  29.             {
  30.                 sendA[i * matrixSize + j] = i + 1;
  31.             }
  32.         for (int i = 0; i < matrixSize; i++)
  33.             for (int j = 0; j < matrixSize; j++)
  34.             {
  35.                 sendB[i * matrixSize + j] = j + 1;
  36.             }
  37.     }
  38.  
  39.     MPI::COMM_WORLD.Barrier();
  40.     MPI::COMM_WORLD.Bcast(&matrixSize, 1, MPI::INT, 0);
  41.  
  42.     recA = new double[matrixSize / size * matrixSize];
  43.     if (sendB == NULL)
  44.     {
  45.         sendB = new double[matrixSize * matrixSize];
  46.     }
  47.     recC = new double[matrixSize / size * matrixSize];
  48.  
  49.     // now p0 scatter matrix sendA to all
  50.     MPI::COMM_WORLD.Scatter(sendA, matrixSize * matrixSize / size, MPI::DOUBLE, recA, matrixSize * matrixSize / size, MPI::DOUBLE, 0);
  51.  
  52.     //now p0 broadcast sendB to all others
  53.     MPI::COMM_WORLD.Bcast(sendB, matrixSize * matrixSize, MPI::DOUBLE, 0);
  54.  
  55.     // all do this part to calculate recC as multiplicated matrix
  56.     for (int i = 0; i < (matrixSize / size); i++)
  57.         for (int j = 0; j < matrixSize; j++)
  58.         {
  59.             recC[i * matrixSize + j] = 0;
  60.             for (int k = 0; k < matrixSize; k++)
  61.                 recC[i * matrixSize + j] += recA[i * matrixSize + k] * sendB[k * matrixSize + j];
  62.         }
  63.  
  64.     MPI::COMM_WORLD.Barrier();
  65.     MPI::COMM_WORLD.Gather(recC, matrixSize * matrixSize / size, MPI::DOUBLE, finalC, matrixSize * matrixSize / size, MPI::DOUBLE, 0);
  66.     MPI::COMM_WORLD.Barrier();
  67.     if (rank == 0)
  68.     {
  69.         for (int i = 0; i < matrixSize; i++)
  70.         {
  71.             for (int j = 0; j < matrixSize; j++)
  72.                 printf(" %.2f ", finalC[i * matrixSize + j]);
  73.             printf("\n");
  74.         }
  75.     }
  76.  
  77.     MPI::Finalize();
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement