Repository navigation
Expand file tree
/
Copy pathBFS_election_graph.c
More file actions
227 lines (192 loc) · 5.92 KB
/
Copy pathBFS_election_graph.c
File metadata and controls
227 lines (192 loc) · 5.92 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Propagation contrôlée
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
// Fonction pour supprimer un élément d'un tableau
void removeElementFromArray(int array[], int *length, int element)
{
int i, pos = -1;
for (i = 0; i < *length; i++)
{
if (array[i] == element)
{
pos = i;
break;
}
}
// If the element was found
if (pos != -1)
{
for (i = pos; i < *length - 1; i++)
{
array[i] = array[i + 1];
}
(*length)--;
}
}
void addElementToArray(int array[], int *length, int element)
{
int i, j = 0;
array[*length] = element;
++(*length) ;
}
/*
Messages utilisés (chacun son id):
- 0: Traverse()
- 1: Retour()
- -1: Terminer()
- 2: Elu()
*/
// id : message id
/*void sendMessage(int id, int dest, MPI_Comm comm)
{
int buffer[] = {id};
MPI_Send(buffer, 1, MPI_INT, dest, 0, comm);
}*/
void sendMessage(int msg[2], int dest, MPI_Comm comm)
{
int* buffer = msg;
MPI_Send(buffer, 2, MPI_INT, dest, 0, comm);
}
int main(int argc, char *argv[])
{
int wsize, pi;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &wsize);
// Création du graphe
/*int index[5] = {1, 4, 7, 10, 12};
int edges[] = {1, 0, 2, 3, 1, 3, 4, 1, 2, 4, 2, 3};*/
int index[6] = {2, 4, 9, 12, 14, 16};
int edges[] = {1, 2, 0, 2, 0, 1, 3, 4, 5, 2, 4, 5, 2, 3, 2, 3};
MPI_Comm graph_comm;
MPI_Graph_create(MPI_COMM_WORLD, wsize, index, edges, 1, &graph_comm);
MPI_Comm_rank(graph_comm, &pi);
// Variables locales
int elu = pi;
int message[2];
int maxNeighbors = 3;
int neighborsCount;
int neighbors[maxNeighbors]; // Liste des voisins
int filsCount = 0;
int fils[maxNeighbors];
MPI_Graph_neighbors_count(graph_comm, pi, &neighborsCount);
MPI_Graph_neighbors(graph_comm, pi, maxNeighbors, neighbors);
int i;
int visited = 0; // Si ce noeud a était visité ou non
int pred = -1; // Le prédécesseur de ce noeud
MPI_Status status;
// A la récéption de INIT() par le processus 0
if (pi == 0)
{
visited = 1;
printf("Node 0: INIT.\n");
for (i = 0; i < neighborsCount; i++)
{
int msg_temp[2] = {0,-1};
sendMessage(msg_temp, neighbors[i], graph_comm);
}
}
while (1)
{
MPI_Recv(&message, 2, MPI_INT, MPI_ANY_SOURCE, 0, graph_comm, &status);
int pj = status.MPI_SOURCE;
// 1st arg = message id ; 2nd arg = count
// A la reception de Traverse() par pi depuis pj
if (message[0] == 0)
{
removeElementFromArray(neighbors, &neighborsCount, pj);
if (!visited)
{
visited = 1;
pred = pj;
printf("Node %d: My predecessor is %d\n", pi, pred);
if (neighborsCount == 0)
{
int msg_temp[] = {1,pi}; //envoi de retour au pred avec l'identifiant
sendMessage(msg_temp, pred, graph_comm);
//phase = 2;
// We can safely break the while in this case
break;
}
else
{
for (i = 0; i < neighborsCount; i++)
{
int msg_temp[] = {0,-1}; //envoi de traverse aux voisins
sendMessage(msg_temp, neighbors[i], graph_comm);
}
}
}
else
{
int msg_temp[2] = {1, -1}; //déjà visité, envoi de retour avec pi = nil
sendMessage(msg_temp, pj, graph_comm);
}
}
// A la reception de Retour(x) par pi depuis pj
if (message[0] == 1)
{
//count = count + message[1];
removeElementFromArray(neighbors, &neighborsCount, pj);
if (message[1] >=elu){
elu=message[1];
addElementToArray(fils, &filsCount,pj);
}
if (neighborsCount == 0)
{
if (pred == -1)
{
printf("Node %d: END.\n", pi);
printf("L'Elu est %d\n", elu);
for (int i=0; i<filsCount; i++){
int msg_temp[] = {2,elu};
sendMessage(msg_temp,fils[i], graph_comm);
}
}
else
{
int msg_temp[2] = {1,elu};
sendMessage(msg_temp, pred, graph_comm);
}
// We can safely break the while in both cases
break;
}
}
}
while(1){
MPI_Recv(&message, 2, MPI_INT, MPI_ANY_SOURCE, 0, graph_comm, &status);
int pj = status.MPI_SOURCE;
//reception de elu de pi depuis pj
if (message[0]==2){
elu=message[1];
if (filsCount==0){
int msg_temp[] = {3,-1};
sendMessage(msg_temp, pred,graph_comm);//message terminer
}
else {
for (int i=0; i<filsCount; i++){
int msg_temp[] = {2,elu};
sendMessage(msg_temp,fils[i], graph_comm);//message elu
}
}
}
//A la reception de Terminer()
if (message[0] == 3){
removeElementFromArray(fils, &filsCount, pj);
if (filsCount == 0)
{
if (pi == 0)
{
printf("Everyone was informed\n");
}
else{
int msg_temp[] = {3,-1};
sendMessage(msg_temp, pred, graph_comm);
}
}
break;
}
}
MPI_Finalize();
return 0;
}