Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. // kolo.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "mpi.h"
  6. #include <iostream>
  7. #include <cstdlib>
  8. #include <ctime>
  9.  
  10. using namespace std;
  11.  
  12. int main()
  13. {
  14.  
  15. int size;
  16. int rank;
  17. MPI_Init(NULL, NULL);
  18. MPI_Comm_size(MPI_COMM_WORLD,&size);
  19. MPI_Comm_rank(MPI_COMM_WORLD,&rank);
  20.  
  21. int* tab;
  22. int nRank;
  23. int Iloczyn=1;
  24.  
  25. if(rank==0)
  26. {
  27. cout<<"Podaj nRank:"<<endl;
  28. cin>>nRank;
  29. int n=size*nRank;
  30. tab=new int[n];
  31. srand(time(NULL));
  32. for(int i=0;i<n;i++)
  33. {
  34. tab[i]=rand()%10;
  35. }
  36. }
  37. //rozeslanie rozmiaru;
  38. MPI_Bcast(&nRank,1,MPI_INT,0,MPI_COMM_WORLD);
  39.  
  40. //dzielenie tab;
  41. int* tabRank=new int[nRank];
  42. MPI_Scatter(tab,nRank,MPI_INT,tabRank,nRank,MPI_INT,0,MPI_COMM_WORLD);
  43.  
  44. //liczenie iloczynu
  45. int nIloczyn=1;
  46. for(int i=0;i<nRank;i++)
  47. {
  48. nIloczyn=nIloczyn*tabRank[i];
  49. }
  50. cout<<rank<<" iloczyn: "<<nIloczyn<<endl;
  51.  
  52. //zliczanie iloczynu
  53. MPI_Reduce(&nIloczyn,&Iloczyn,1,MPI_INT,MPI_PROD,0,MPI_COMM_WORLD);
  54.  
  55. if(rank==0)
  56. {
  57. cout<<"Iloczyn calosci: "<<Iloczyn<<endl;
  58. }
  59. delete[] tab;
  60. MPI_Finalize();
  61.  
  62. system("PAUSE");
  63.  
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement