Guest User

Untitled

a guest
May 20th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <mpi.h>
  3. #include <time.h>
  4. #include <vector>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. int glosuj(vector<int> &Numbers,int s)
  10. {
  11. int glos;
  12. do
  13. {
  14. srand((2^s-1)*time(NULL));
  15. int n= Numbers.size();
  16. glos = (rand()%n)+1;
  17. //srand((unsigned int)time((time_t *)NULL));
  18. //int n = Numbers.size();
  19. //glos=((rand()*s)%n)+1;
  20. }
  21. while(Numbers[glos]==0);
  22. return glos;
  23. }
  24.  
  25. void main(int argc, char *argv[])
  26. {
  27. int rozmiar,rank,size,g;
  28. int tura=1;
  29.  
  30. MPI_Status status;
  31. vector<int>glosy;
  32. vector<int>kandydaci;
  33.  
  34. MPI_Init(&argc, &argv);
  35.  
  36. MPI_Comm_size(MPI_COMM_WORLD, &size);
  37. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  38.  
  39. glosy.resize(size,0);
  40. kandydaci.resize(size,1);
  41.  
  42. if(rank==0)
  43. {
  44. cout << endl << "Zaczynamy ture" << endl;
  45. MPI_Bcast(&kandydaci, size, MPI_INT,0, MPI_COMM_WORLD);
  46. }
  47.  
  48. if(rank!=0)
  49. {
  50. g=glosuj(kandydaci,rank);
  51. cout << "Jestem watkiem: " << rank << " i glosowalem na " << g << endl;
  52. MPI_Send(&g,1,MPI_INT,0,1,MPI_COMM_WORLD);
  53. }
  54.  
  55. if(rank==0)
  56. {
  57. for (int j = 1 ; j < size ; j++)
  58. {
  59. MPI_Recv(&g,1,MPI_INT,j,1,MPI_COMM_WORLD,&status);
  60. glosy[g]++;
  61. }
  62. cout << "Wyniki tury:" << endl;
  63. for (int i = 0 ; i <glosy.size() ; i++)
  64. {
  65. cout << "Kandydat: " << i << " ma " << glosy[i] << " glosow" << endl;
  66. }
  67. }
  68.  
  69. /*for (int i = 0 ; i <karta.size() ; i++)
  70. {
  71. if (karta[i].liczbaglosow==0) karta[i].kandydatura=false;
  72. }*/
  73.  
  74.  
  75.  
  76. MPI_Finalize();
  77. }
Add Comment
Please, Sign In to add comment