Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <mpi.h>
- #include <stdio.h>
- #include <cstdlib>
- int main(int argc, char** argv)
- {
- int n,m,*summ = nullptr;
- int size;
- int **a = nullptr,*local = nullptr,*v = nullptr,*v2 = nullptr, *vect;
- scanf("%d",&n);
- MPI_Init(&argc, &argv);
- int world_size;
- MPI_Comm_size(MPI_COMM_WORLD, &world_size);
- int world_rank;
- MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
- v2 = new int[n];
- v = new int[n];
- if (world_rank == 0)
- {
- srand(time(0));
- a = new int*[n];
- vect = new int[n*n];
- int k = 0;
- for (int i = 0; i < n; i++)
- {
- v[i] = rand() % 20;
- a[i] = new int[n];
- for (int j = 0; j < n; j++)
- {
- a[i][j] = rand() % 20;
- vect[k++] = a[i][j];
- printf("%d ",a[i][j]);
- }
- printf("\n");
- }
- printf("\n");
- for (int i = 0; i < n; i++)
- printf("%d ",v[i]);
- printf("\n");
- }
- MPI_Bcast(&n,1,MPI_INT,0,MPI_COMM_WORLD);
- MPI_Bcast(v,n,MPI_INT,0,MPI_COMM_WORLD);
- local = new int[n * n / world_size];
- summ = new int[n / world_size];
- MPI_Scatter(vect,n * n / world_size,MPI_INT,local,n * n / world_size,MPI_INT,0,MPI_COMM_WORLD);
- int k = 0;
- for (int j = 0; j < n / world_size; j++)
- {
- summ[j] = 0;
- for (int i = 0; i < n; i++)
- {
- summ[j] += local[k] * v[i];
- k++;
- }
- }
- MPI_Gather(summ,n / world_size,MPI_INT,v2,n / world_size,MPI_INT,0,MPI_COMM_WORLD);
- if (world_rank == 0)
- printf("\n");
- if (world_rank == 0)
- {
- for (int i = 0;i < n;i++)
- printf("%d ",v2[i]);
- }
- MPI_Finalize();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement