Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <mpi.h>
- #include <time.h>
- #include <vector>
- #include <iostream>
- #include <stdlib.h>
- using namespace std;
- int glosuj(vector<int> _kandydaci,int s) //METODA LOSUJACA
- {
- int glos;
- int n = _kandydaci.size();
- do
- {
- glos = ((rand()+s)%n);
- }
- while(_kandydaci[glos]==0||glos==0); //JESLI PROCES ZAGLOSOWALBY NA KOGOS NA KOGO NIE MOZNA (NIE DZIALA!!!)
- return glos;
- }
- void main(int argc, char *argv[])
- {
- int rozmiar,rank,size,g;
- int tura=1;
- int wygrany = 0;
- int iloscwyel;
- int min;
- srand((unsigned int)time((time_t *)NULL));
- MPI_Status status;
- vector<int>glosy; //ILE KTO DOSTAL GLOSOW W DANEJ RUNDZIE
- vector<int>kandydaci; //NA KOGO MOZNA GLOSOWAC 1-MOZNA 0-NIET
- MPI_Init(&argc, &argv);
- MPI_Comm_size(MPI_COMM_WORLD, &size);
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- glosy.resize(size,0); //0 BO KAZDY MA LICZBE 0 GLOSOW
- kandydaci.resize(size,1); //1 BO NA KAZDEGO MOZNA GLOSOWAC
- do{
- if(rank==0)
- {
- MPI_Bcast(&kandydaci, size, MPI_INT,0, MPI_COMM_WORLD); //0 WYSYLA DO INNYCH TABLICE (NIE DZIALA!!!)
- }
- if(rank!=0)
- {
- g=glosuj(kandydaci,rank); //ROZNY OD 0 GLOSUJE
- //for (int i = 1 ; i < kandydaci.size() ; i++) cout << "[" << kandydaci[i] << "]";
- //cout << "Jestem watkiem: " << rank << " i glosowalem na " << g << endl;
- MPI_Send(&g,1,MPI_INT,0,1,MPI_COMM_WORLD); //ROZNY OD 0 WYSYLA SWOJ GLOS
- }
- if(rank==0)
- {
- for (int j = 1 ; j < size ; j++)
- {
- MPI_Recv(&g,1,MPI_INT,j,1,MPI_COMM_WORLD,&status); //0 ODBIERA GLOSY
- glosy[g]++; //0 WPISUJE INFO O GLOSACH DO TABLICY
- }
- cout << "Wyniki tury " << tura << endl;
- for (int i = 1 ; i <glosy.size() ; i++) //0 WYPISUJE WYNIKI TURY
- {
- cout << "Kandydat: " << i << " ma " << glosy[i] << " glosow" << endl;
- }
- }
- if(rank==0)
- {
- //ELIMINACJA==================================================
- min=1;
- for (int i = 1 ; i <glosy.size() ; i++)
- {
- if (glosy[i]<=min && glosy[i]>0)min=glosy[i]; //SZUKAM NAJNIZSZEJ WATOSCI GLOSOW (OPROCZ 0)
- if (glosy[i]==0) kandydaci[i]=0; // 0 ELIMINUJE TYCH CO DOSTALI ZERO GLOSOW
- }
- for (int i = 1 ; i <glosy.size() ; i++)
- {
- if (glosy[i]==min)kandydaci[i]=0; //ELIMINACJA TYCH CO MAJA NAJNIZSZA ILOSC GLOSOW
- }
- //SPRAWDZANIE=================================================
- iloscwyel=0;
- for (int i = 1 ; i <kandydaci.size() ; i++)
- {
- if(kandydaci[i]==0)
- {
- cout << "Kandydat: " << i << " nie bierze udzialu w nastepnej turze " << endl;
- iloscwyel++; //0 WYPISUJE ODRZUCONYCH DO NASTEPNEJ RUNDY I ZLICZA ICH
- }
- }
- if (iloscwyel >= (kandydaci.size()-2)) //0 SPRAWDZA CZY WSZYSCY NIE ZOSTALI WYELIMNOWANI
- {
- cout << "Koniec wyborow" << endl;
- for (int i = 1 ; i <kandydaci.size() ; i++)if (kandydaci[i]==1)cout << "Wygral: " << i << endl;
- wygrany = 1;
- MPI_Bcast(&wygrany, 1, MPI_INT,1, MPI_COMM_WORLD);//JESLI TAK WYSYLA WYNIK DLA INNYCH
- }
- for (int i = 0 ; i < glosy.size() ; i++)glosy[i]=0; //0 ZERUJE TABLICE PRZED KOLEJNYM GLOSOWANIEM
- }
- tura++;
- }
- while(wygrany==0);//ROB DOPOKI NIE MA WYGRANEGO
- MPI_Finalize();
- }
Add Comment
Please, Sign In to add comment