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>
- using namespace std;
- int glosuj(vector<int> &Numbers,int s)
- {
- int glos;
- do
- {
- srand((2^s-1)*time(NULL));
- int n= Numbers.size();
- glos = (rand()%n)+1;
- //srand((unsigned int)time((time_t *)NULL));
- //int n = Numbers.size();
- //glos=((rand()*s)%n)+1;
- }
- while(Numbers[glos]==0);
- return glos;
- }
- void main(int argc, char *argv[])
- {
- int rozmiar,rank,size,g;
- int tura=1;
- MPI_Status status;
- vector<int>glosy;
- vector<int>kandydaci;
- MPI_Init(&argc, &argv);
- MPI_Comm_size(MPI_COMM_WORLD, &size);
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- glosy.resize(size,0);
- kandydaci.resize(size,1);
- if(rank==0)
- {
- cout << endl << "Zaczynamy ture" << endl;
- MPI_Bcast(&kandydaci, size, MPI_INT,0, MPI_COMM_WORLD);
- }
- if(rank!=0)
- {
- g=glosuj(kandydaci,rank);
- cout << "Jestem watkiem: " << rank << " i glosowalem na " << g << endl;
- MPI_Send(&g,1,MPI_INT,0,1,MPI_COMM_WORLD);
- }
- if(rank==0)
- {
- for (int j = 1 ; j < size ; j++)
- {
- MPI_Recv(&g,1,MPI_INT,j,1,MPI_COMM_WORLD,&status);
- glosy[g]++;
- }
- cout << "Wyniki tury:" << endl;
- for (int i = 0 ; i <glosy.size() ; i++)
- {
- cout << "Kandydat: " << i << " ma " << glosy[i] << " glosow" << endl;
- }
- }
- /*for (int i = 0 ; i <karta.size() ; i++)
- {
- if (karta[i].liczbaglosow==0) karta[i].kandydatura=false;
- }*/
- MPI_Finalize();
- }
Add Comment
Please, Sign In to add comment