Repository navigation
Expand file tree
/
Copy pathDFS.c
More file actions
153 lines (131 loc) · 3.85 KB
/
Copy pathDFS.c
File metadata and controls
153 lines (131 loc) · 3.85 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
// Propagation contrôlée
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <time.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)--;
}
}
/*
Messages utilisés (chacun son id):
- 0: Traverse()
- 1: Retour()
*/
void sendMessage(int id, int dest, MPI_Comm comm)
{
int buffer[] = {id};
MPI_Send(buffer, 1, MPI_INT, dest, 0, comm);
}
int main(int argc, char *argv[])
{
srand(time(0));
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[8] = {2, 5, 9, 11, 14, 17, 19, 20};
int edges[] = {1, 2, 0, 2, 4, 0, 1, 3, 4, 2, 5, 1, 2, 5, 3, 4, 6, 5, 7, 6};
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 message;
int maxNeighbors = 4;
int neighborsCount;
int neighbors[maxNeighbors]; // Liste des voisins
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");
//choose a neighbor randomly
i = 0 + rand() % (neighborsCount - 0);
sendMessage(0, neighbors[i], graph_comm);
}
while (1)
{
MPI_Recv(&message, 1, MPI_INT, MPI_ANY_SOURCE, 0, graph_comm, &status);
int pj = status.MPI_SOURCE;
if (message == 0) // A la reception de Traverse() par pi depuis pj
{
removeElementFromArray(neighbors, &neighborsCount, pj);
if (!visited)
{
visited = 1;
pred = pj;
printf("Node %d: My predecessor is %d\n", pi, pred);
if (neighborsCount == 0)
{
sendMessage(1, pred, graph_comm);
// We can safely break the while in this case
break;
}
else
{
//choose a neighbor randomly
i = 0 + rand() % (neighborsCount - 0);
//send a traverse()
sendMessage(0, neighbors[i], graph_comm);
}
}
else
{
sendMessage(1, pj, graph_comm);
}
}
// A la reception de Retour() par pi depuis pj
if (message == 1)
{
removeElementFromArray(neighbors, &neighborsCount, pj);
if (neighborsCount == 0)
{
if (pred == -1)
{
printf("Node %d: END.\n", pi);
}
else
{
sendMessage(1, pred, graph_comm);
}
// We can safely break the while in both cases
break;
}
else
{
//choose a neighbor randomly
i = 0 + rand() % (neighborsCount - 0);
//send a traverse()
sendMessage(0, neighbors[i], graph_comm);
}
}
}
MPI_Finalize();
return 0;
}