Guest User

Untitled

a guest
May 20th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 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.     int g;
  29.     int *ta = new int [numprocs];
  30.     int * kopia = new int[numprocs];
  31.  
  32.     int tura=1;
  33.     while(tura<=4){
  34.  
  35.         int c=0;
  36.         g=Glosuj(numprocs,ta,myid);
  37.         cout<<"Proces "<<myid<<" glosuje na "<<g<<endl;
  38.  
  39.         if(myid==0){
  40.             if(tura==1)for(int u=0;u<numprocs;u++)ta[u]=1;
  41.  
  42.             /***kopia tablicy*/
  43.             for(int m=0;m<numprocs;m++){
  44.                 kopia[m]=ta[m];
  45.             }
  46.             /*koniec kopii*/
  47.  
  48.             ta[g]++;
  49.             for(int y=1;y<numprocs;y++){
  50.                 MPI_Recv(&g,1,MPI_INT,y,50,MPI_COMM_WORLD,&status);
  51.                 ta[g]++;
  52.             }
  53.             //sprawdzanie z kopią (poprzednimi wartosciami tablicy)
  54.             for(int m=0;m<numprocs;m++){
  55.                 if(kopia[m]==ta[m])ta[m]=1;
  56.             }
  57.             //koniec sprawdzania
  58.             cout<<"Wyniki tury nr."<<tura<<":";
  59.             for(int u=0;u<numprocs;u++)cout<<ta[u]-1<<" ";
  60.             cout<<endl;
  61.             MPI_Bcast(&ta,numprocs,MPI_INT,0,MPI_COMM_WORLD);
  62.         }
  63.         if(myid!=0){
  64.                 MPI_Send(&g,1,MPI_INT,0,50,MPI_COMM_WORLD);
  65.         }
  66.         tura++;
  67.     }
  68.     MPI_Finalize();
  69. }
Add Comment
Please, Sign In to add comment