Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <mpi.h>
- #include <time.h>
- #include <vector>
- #include <iostream>
- using namespace std;
- int Glosuj(int LICZBA_EL,int * tab,int id){ //proces moze glosowac na siebie
- int los;
- //do{
- srand((2^id-1)*time(NULL));
- los = rand()% (LICZBA_EL);
- if(tab[los]==1) {
- srand((2^id-1)*time(NULL));
- los = rand()% (LICZBA_EL);
- }
- //}while(tab[los]==1);
- return los;
- }
- void main(int argc, char*argv[])
- {
- MPI_Init(&argc,&argv);
- int myid,numprocs;
- MPI_Comm_rank(MPI_COMM_WORLD,&myid);
- MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
- MPI_Status status;
- bool jedynki = false;
- int g;
- int *ta = new int [numprocs];
- int * kopia = new int[numprocs];
- int tura=1;
- while(tura<=4){
- int c=0;
- g=Glosuj(numprocs,ta,myid);
- cout<<"Proces "<<myid<<" glosuje na "<<g<<endl;
- if(myid==0){
- if(tura==1)for(int u=0;u<numprocs;u++)ta[u]=1;
- /***kopia tablicy*/
- for(int m=0;m<numprocs;m++){
- kopia[m]=ta[m];
- }
- /*koniec kopii*/
- ta[g]++;
- for(int y=1;y<numprocs;y++){
- MPI_Recv(&g,1,MPI_INT,y,50,MPI_COMM_WORLD,&status);
- ta[g]++;
- }
- //sprawdzanie z kopią (poprzednimi wartosciami tablicy)
- for(int m=0;m<numprocs;m++){
- if(kopia[m]==ta[m])ta[m]=1;
- }
- //koniec sprawdzania
- cout<<"Wyniki tury nr."<<tura<<":";
- for(int u=0;u<numprocs;u++)cout<<ta[u]-1<<" ";
- cout<<endl;
- MPI_Bcast(&ta,numprocs,MPI_INT,0,MPI_COMM_WORLD);
- }
- if(myid!=0){
- MPI_Send(&g,1,MPI_INT,0,50,MPI_COMM_WORLD);
- }
- tura++;
- }
- MPI_Finalize();
- }
Add Comment
Please, Sign In to add comment