Guest User

Untitled

a guest
May 20th, 2018
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <mpi.h>
  2. #include <time.h>
  3. #include <vector>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7. int Glosuj(int LICZBA_EL,int * tab,int id){ //proces moze glosowac na siebie
  8.     int los;
  9.     //do{
  10.         srand((2^id-1)*time(NULL));
  11.         los = rand()% (LICZBA_EL);
  12.         if(tab[los]==1) {
  13.                 srand((2^id-1)*time(NULL));
  14.                 los = rand()% (LICZBA_EL);
  15.         }
  16.     //}while(tab[los]==1);
  17.  
  18.     return los;
  19. }
  20. void main(int argc, char*argv[])
  21. {
  22.     MPI_Init(&argc,&argv);
  23.     int myid,numprocs;
  24.     MPI_Comm_rank(MPI_COMM_WORLD,&myid);
  25.     MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
  26.     MPI_Status status;
  27.     bool jedynki = false;
  28.  
  29.  
  30.     int g;
  31.     int *ta;
  32.     ta = new int [numprocs];
  33.    
  34.     int tura=1;
  35.     while(tura<=4){
  36.  
  37.         int c=0;
  38.         g=Glosuj(numprocs,ta,myid);
  39.         cout<<"Proces "<<myid<<" glosuje na "<<g<<endl;
  40.  
  41.         if(myid==0){
  42.             if(tura==1)for(int u=0;u<numprocs;u++)ta[u]=1;
  43.             ta[g]++;
  44.             for(int y=1;y<numprocs;y++){
  45.                 MPI_Recv(&g,1,MPI_INT,y,50,MPI_COMM_WORLD,&status);
  46.                 ta[g]++;
  47.             }
  48.             //cout<<"*********************"<<endl;
  49.             cout<<"Wyniki tury nr."<<tura<<":";
  50.             for(int u=0;u<numprocs;u++)cout<<ta[u]-1<<" ";
  51.             cout<<endl;
  52.             //cout<<"*********************"<<endl;
  53.  
  54.             MPI_Bcast(&ta,numprocs,MPI_INT,0,MPI_COMM_WORLD);
  55.         }
  56.         if(myid!=0){
  57.                 MPI_Send(&g,1,MPI_INT,0,50,MPI_COMM_WORLD);
  58.         }
  59.         tura++;
  60.     }
  61.     MPI_Finalize();
  62. }
Add Comment
Please, Sign In to add comment