Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // kolo.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "mpi.h"
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- int main()
- {
- int size;
- int rank;
- MPI_Init(NULL, NULL);
- MPI_Comm_size(MPI_COMM_WORLD,&size);
- MPI_Comm_rank(MPI_COMM_WORLD,&rank);
- int* tab;
- int nRank;
- int Iloczyn=1;
- if(rank==0)
- {
- cout<<"Podaj nRank:"<<endl;
- cin>>nRank;
- int n=size*nRank;
- tab=new int[n];
- srand(time(NULL));
- for(int i=0;i<n;i++)
- {
- tab[i]=rand()%10;
- }
- }
- //rozeslanie rozmiaru;
- MPI_Bcast(&nRank,1,MPI_INT,0,MPI_COMM_WORLD);
- //dzielenie tab;
- int* tabRank=new int[nRank];
- MPI_Scatter(tab,nRank,MPI_INT,tabRank,nRank,MPI_INT,0,MPI_COMM_WORLD);
- //liczenie iloczynu
- int nIloczyn=1;
- for(int i=0;i<nRank;i++)
- {
- nIloczyn=nIloczyn*tabRank[i];
- }
- cout<<rank<<" iloczyn: "<<nIloczyn<<endl;
- //zliczanie iloczynu
- MPI_Reduce(&nIloczyn,&Iloczyn,1,MPI_INT,MPI_PROD,0,MPI_COMM_WORLD);
- if(rank==0)
- {
- cout<<"Iloczyn calosci: "<<Iloczyn<<endl;
- }
- delete[] tab;
- MPI_Finalize();
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement