Guest User

Untitled

a guest
May 20th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <mpi.h>
  3. #include <time.h>
  4. #include <vector>
  5. #include <iostream>
  6. #include <stdlib.h>
  7.  
  8. using namespace std;
  9.  
  10. int glosuj(vector<int> _kandydaci,int s) //METODA LOSUJACA
  11. {
  12. int glos;
  13. int n = _kandydaci.size();
  14. do
  15. {
  16. glos = ((rand()+s)%n);
  17. }
  18. while(_kandydaci[glos]==0||glos==0); //JESLI PROCES ZAGLOSOWALBY NA KOGOS NA KOGO NIE MOZNA (NIE DZIALA!!!)
  19. return glos;
  20. }
  21.  
  22. void main(int argc, char *argv[])
  23. {
  24. int rozmiar,rank,size,g;
  25. int tura=1;
  26. int wygrany = 0;
  27. int iloscwyel;
  28. int min;
  29. srand((unsigned int)time((time_t *)NULL));
  30.  
  31. MPI_Status status;
  32. vector<int>glosy; //ILE KTO DOSTAL GLOSOW W DANEJ RUNDZIE
  33. vector<int>kandydaci; //NA KOGO MOZNA GLOSOWAC 1-MOZNA 0-NIET
  34.  
  35. MPI_Init(&argc, &argv);
  36.  
  37. MPI_Comm_size(MPI_COMM_WORLD, &size);
  38. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  39.  
  40. glosy.resize(size,0); //0 BO KAZDY MA LICZBE 0 GLOSOW
  41. kandydaci.resize(size,1); //1 BO NA KAZDEGO MOZNA GLOSOWAC
  42.  
  43. do{
  44. if(rank==0)
  45. {
  46. MPI_Bcast(&kandydaci, size, MPI_INT,0, MPI_COMM_WORLD); //0 WYSYLA DO INNYCH TABLICE (NIE DZIALA!!!)
  47. }
  48.  
  49. if(rank!=0)
  50. {
  51. g=glosuj(kandydaci,rank); //ROZNY OD 0 GLOSUJE
  52. //for (int i = 1 ; i < kandydaci.size() ; i++) cout << "[" << kandydaci[i] << "]";
  53. //cout << "Jestem watkiem: " << rank << " i glosowalem na " << g << endl;
  54. MPI_Send(&g,1,MPI_INT,0,1,MPI_COMM_WORLD); //ROZNY OD 0 WYSYLA SWOJ GLOS
  55. }
  56.  
  57. if(rank==0)
  58. {
  59. for (int j = 1 ; j < size ; j++)
  60. {
  61. MPI_Recv(&g,1,MPI_INT,j,1,MPI_COMM_WORLD,&status); //0 ODBIERA GLOSY
  62. glosy[g]++; //0 WPISUJE INFO O GLOSACH DO TABLICY
  63. }
  64. cout << "Wyniki tury " << tura << endl;
  65. for (int i = 1 ; i <glosy.size() ; i++) //0 WYPISUJE WYNIKI TURY
  66. {
  67. cout << "Kandydat: " << i << " ma " << glosy[i] << " glosow" << endl;
  68. }
  69. }
  70.  
  71. if(rank==0)
  72. {
  73. //ELIMINACJA==================================================
  74. min=1;
  75. for (int i = 1 ; i <glosy.size() ; i++)
  76. {
  77. if (glosy[i]<=min && glosy[i]>0)min=glosy[i]; //SZUKAM NAJNIZSZEJ WATOSCI GLOSOW (OPROCZ 0)
  78. if (glosy[i]==0) kandydaci[i]=0; // 0 ELIMINUJE TYCH CO DOSTALI ZERO GLOSOW
  79. }
  80.  
  81. for (int i = 1 ; i <glosy.size() ; i++)
  82. {
  83. if (glosy[i]==min)kandydaci[i]=0; //ELIMINACJA TYCH CO MAJA NAJNIZSZA ILOSC GLOSOW
  84. }
  85. //SPRAWDZANIE=================================================
  86. iloscwyel=0;
  87. for (int i = 1 ; i <kandydaci.size() ; i++)
  88. {
  89. if(kandydaci[i]==0)
  90. {
  91. cout << "Kandydat: " << i << " nie bierze udzialu w nastepnej turze " << endl;
  92. iloscwyel++; //0 WYPISUJE ODRZUCONYCH DO NASTEPNEJ RUNDY I ZLICZA ICH
  93. }
  94. }
  95.  
  96. if (iloscwyel >= (kandydaci.size()-2)) //0 SPRAWDZA CZY WSZYSCY NIE ZOSTALI WYELIMNOWANI
  97. {
  98. cout << "Koniec wyborow" << endl;
  99. for (int i = 1 ; i <kandydaci.size() ; i++)if (kandydaci[i]==1)cout << "Wygral: " << i << endl;
  100. wygrany = 1;
  101. MPI_Bcast(&wygrany, 1, MPI_INT,1, MPI_COMM_WORLD);//JESLI TAK WYSYLA WYNIK DLA INNYCH
  102. }
  103.  
  104. for (int i = 0 ; i < glosy.size() ; i++)glosy[i]=0; //0 ZERUJE TABLICE PRZED KOLEJNYM GLOSOWANIEM
  105. }
  106. tura++;
  107. }
  108. while(wygrany==0);//ROB DOPOKI NIE MA WYGRANEGO
  109.  
  110. MPI_Finalize();
  111. }
Add Comment
Please, Sign In to add comment