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 = 0;
- int size;
- int **a = nullptr,*local = nullptr,*v = nullptr,*v2 = nullptr, *vect;
- 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];
- if (world_rank == 0)
- {
- srand(time(0));
- n = world_size;
- a = new int*[n];
- v = 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\n",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];
- MPI_Scatter(vect,n,MPI_INT,local,n,MPI_INT,0,MPI_COMM_WORLD);
- printf("pross %d\n",world_rank);
- for (int i = 0; i < n; i++)
- printf("%d ",local[i]);
- printf("\n");
- for (int i = 0; i < n; i++)
- {
- summ += local[i] * v[i];
- }
- printf("%d \n",summ);
- MPI_Gather(&summ,1,MPI_INT,v2,1,MPI_INT,0,MPI_COMM_WORLD);
- printf("success\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