Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <mpi.h>
  2. #include <stdio.h>
  3. #include <cstdlib>
  4.  
  5. int main(int argc, char** argv)
  6. {
  7.    
  8.     int n,m,*summ = nullptr;
  9.     int size;
  10.     int **a = nullptr,*local = nullptr,*v = nullptr,*v2 = nullptr, *vect;
  11.     scanf("%d",&n);
  12.     MPI_Init(&argc,  &argv);
  13.     int world_size;
  14.     MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  15.     int world_rank;
  16.     MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  17.     v2 = new int[n];
  18.     v = new int[n];
  19.     if (world_rank == 0)
  20.     {
  21.         srand(time(0));
  22.         a = new int*[n];
  23.         vect = new int[n*n];
  24.         int k = 0;
  25.         for (int i = 0; i < n; i++)
  26.         {
  27.             v[i] = rand() % 20;
  28.             a[i] = new int[n];
  29.             for (int j = 0; j < n; j++)
  30.             {
  31.                     a[i][j] = rand() % 20;
  32.                     vect[k++] = a[i][j];
  33.                     printf("%d ",a[i][j]);
  34.             }
  35.             printf("\n");
  36.         }
  37.         printf("\n");
  38.         for (int i = 0; i < n; i++)
  39.             printf("%d ",v[i]);
  40.         printf("\n");
  41.     }
  42.     MPI_Bcast(&n,1,MPI_INT,0,MPI_COMM_WORLD);
  43.     MPI_Bcast(v,n,MPI_INT,0,MPI_COMM_WORLD);
  44.     local = new int[n * n / world_size];
  45.     summ = new int[n / world_size];
  46.     MPI_Scatter(vect,n * n / world_size,MPI_INT,local,n * n / world_size,MPI_INT,0,MPI_COMM_WORLD);
  47.     int k = 0;
  48.     for (int j = 0; j < n / world_size; j++)
  49.     {
  50.         summ[j] = 0;
  51.         for (int i = 0; i < n; i++)
  52.         {
  53.             summ[j] += local[k] * v[i];
  54.             k++;
  55.         }
  56.     }
  57.     MPI_Gather(summ,n / world_size,MPI_INT,v2,n / world_size,MPI_INT,0,MPI_COMM_WORLD);
  58.     if (world_rank == 0)
  59.         printf("\n");
  60.     if (world_rank == 0)
  61.     {
  62.         for (int i = 0;i < n;i++)
  63.             printf("%d ",v2[i]);
  64.     }
  65.     MPI_Finalize();
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement