-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLeaderElection.cpp
More file actions
125 lines (93 loc) · 2.8 KB
/
LeaderElection.cpp
File metadata and controls
125 lines (93 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include<mpi.h>
#include<iostream>
#include<vector>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include<unistd.h>
using namespace std;
int main()
{
int myrank,nprocs,namelen;
int initiator=0;
char processorName[10];
MPI_Init(NULL,NULL);
MPI_Comm_size(MPI_COMM_WORLD,&nprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
MPI_Get_processor_name(processorName, &namelen);
//to find the maximum rank
int max=0;
// to initiate the random every time
// srand(time(NULL));
int *array = new int[nprocs];
// int array[nprocs];
for(int i=0;i<nprocs;++i)
{
array[i]=-1;
}
int root=0;
if(myrank==0)
{
while(initiator==0)
{
srand(time(NULL));
initiator=rand()%nprocs;
}
cout<<"The inititaor for all the processses will be "<<initiator<<endl;
}
MPI_Bcast(&initiator,1,MPI_INT,0,MPI_COMM_WORLD);
if(myrank == initiator)
{
cout<<"\nThe initiator is(IN THE INITIATOR) "<<initiator<<endl;
array[myrank] = myrank;
//send to the process which is 1 bigger means make a ring
MPI_Send(array,nprocs,MPI_INT,(myrank+1)%nprocs,999,MPI_COMM_WORLD);
cout<<"\n\t\t"<<myrank <<" is waiting for the last process "<<(myrank-1)%nprocs<<" running on "<<processorName<<endl<<endl;
MPI_Recv(array, nprocs, MPI_INT, (myrank-1)%nprocs, 999, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
cout<<"\n\t\tThe initiator received the message \n"<<endl;
cout <<"My rank is "<< myrank <<" running on "<<processorName<< endl<<endl;
cout<<"The array in the rank "<<myrank<<" is"<<endl;
for(int j=0;j<nprocs;++j)
{
cout<<array[j]<<" ";
}
cout<<endl;
max = 0;
for(int i=0;i<nprocs;++i)
{
if(max<array[i])
max = array[i];
}
cout<<"Winner is "<<max<<endl;
//Broadcasting winner of the election
}
else if(myrank!=initiator){
if(myrank == 0)
{
MPI_Recv(array, nprocs, MPI_INT, (nprocs-1)%nprocs, 999, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
cout<<" My rank is "<<myrank<<" and I have received from "<<(nprocs-1)%nprocs<<" running on "<<processorName<<endl;
}
else
{
MPI_Recv(array, nprocs, MPI_INT, (myrank-1)%nprocs, 999, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
cout<<" My rank is "<<myrank<<" and I have received from "<<(myrank-1)%nprocs<<" running on "<<processorName<<endl;
}
cout<<"The array in the rank "<<myrank<<" is"<<endl;
for(int i=0;i<nprocs;++i)
{
cout << array[i] << " ";
}
sleep(1);
srand(time(NULL));
int random= rand()%2+1;
//cout << "\nRandom value is : " << random << endl;
//Means he wants to participitate 2 or yes and 1 for no
if(random==2)
array[myrank] = myrank;
cout<<"\nSending array to the rank "<<(myrank+1)%nprocs<<endl<<endl;
MPI_Send(array,nprocs,MPI_INT,(myrank+1)%nprocs,999,MPI_COMM_WORLD);
}
MPI_Bcast(&max,1,MPI_INT,max,MPI_COMM_WORLD);
cout<<myrank<<" received thank you from winner \n";
MPI_Finalize();
}