Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <mpi.h>
- #include <stdio.h>
- #include <iostream>
- int main(int argc, char** argv) {
- srand(time(0));
- int size;
- int *arr = nullptr, *vec = nullptr, *localArr = nullptr, *localArr2 = nullptr, *arrSums = nullptr;
- 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);
- if(world_rank == 0) {
- std::cout<< "Size: ";
- std::cin >> size;
- arr = new int[size*size];
- for(int i=0; i<size; i++) {
- for(int j=0; j<size; j++) {
- arr[i*size + j] = rand()%10;
- std::cout << arr[i*size + j] << " ";
- }
- std::cout << std::endl;
- }
- std::cout << std::endl;
- vec = new int[size];
- for(int i=0; i<size; i++) {
- vec[i] = rand()%10;
- std::cout << vec[i] << " ";
- }
- std::cout << std::endl;
- arrSums = new int[size];
- }
- MPI_Bcast(&size, 1, MPI_INT, 0, MPI_COMM_WORLD);
- std::cout << world_rank << ": " << size <<std::endl;
- MPI_Bcast(vec, size, MPI_INT, 0, MPI_COMM_WORLD);
- /*for(int i=0; i<size; i++) {
- std::cout << vec[i] << " ";
- }
- std::cout << std::endl;
- /*
- int countVar = (size * size) / world_size;
- localArr = new int[countVar];
- MPI_Scatter(arr, countVar, MPI_INT, localArr, countVar, MPI_INT, 0, MPI_COMM_WORLD);
- int sum(0);
- for (int i=0; i<countVar; i++) {
- sum += localArr[i] * vec[i%size];
- }
- MPI_Gather(&sum, 1, MPI_INT, arrSums, 1, MPI_INT, 0, MPI_COMM_WORLD);
- if(world_rank == 0) {
- for(int i=0; i<size; i++) {
- std::cout << arrSums[i] << " ";
- }
- }*/
- MPI_Finalize();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement