-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel.cpp
288 lines (252 loc) · 7.61 KB
/
parallel.cpp
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "graphs.hpp"
#include "bag.hpp"
#include <omp.h>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <cstring>
using namespace std;
/** Parallel implementation of turning undirected graph G to a directed graph, from class. */
void ParallelGraphAlgorithms::make_directed(Graph &G)
{
if (!G.directed())
{
// set flag
G.is_directed = 1;
// as the graph is now directed, we now use the In-neighbourhood adjacency list, so we create it.
for (unsigned int v : G.vertex_list())
G.In[v]; // create inlist
// for each vertex v
for (unsigned int v : G.vertex_list())
{
if (G.degree(v) > 0)
{
// for each neighbour of v, in parallel, get a reference to the neighbour
// and add the opposing edge in the in-neighbourhood
#pragma omp parallel for
for (unsigned int i = 0; i < G.Out[v]._map.bucket_count(); i++)
{
for (auto it = G.Out[v]._map.begin(i); it != G.Out[v]._map.end(i); it++)
{
#pragma omp critical
G.insert(G.In[it->first], v, 1);
}
}
}
}
}
}
void ParallelGraphAlgorithms::algorithm_B(Graph G, vector<unsigned int> &p)
{
/** Step 1. Use the make_directed function from class to turn G into a weighted graph.*/
make_directed(G);
/** Step 1.1. Initialise parent array according to the LU framework definition.
* Each vertex is initially its own parent. */
#pragma omp parallel for
for (unsigned int i = 0; i < G.Out.bucket_count(); i++)
{
for (auto it = G.Out.begin(i); it != G.Out.end(i); it++)
{
unsigned int v = it->first;
p[v] = v;
}
}
/** We loop continuously until the end condition has been met.*/
bool done(false);
while (!done)
{
done = true;
/** Step 2.1. Consider all arcs w->w in parallel **/
#pragma omp parallel for
for (unsigned int i = 0; i < G.Out.bucket_count(); i++)
{
for (auto itv = G.Out.begin(i); itv != G.Out.end(i); itv++)
{
/** get v's value*/
unsigned int v = itv->first;
/** determine v's parent using min rule with reducer*/
unsigned int parent = p[v];
#pragma omp parallel for reduction(min : parent)
for (unsigned int j = 0; j < G.Out[v]._map.bucket_count(); j++)
{
for (auto ite = G.Out[v]._map.begin(j); ite != G.Out[v]._map.end(j); ite++)
{
/** get w's value */
unsigned int w = ite->first;
if (v > w)
{
/** Set the parent using the min rule*/
parent = min(parent, w);
}
}
}
/** Finally, v's parent in p[v]*/
p[v] = parent;
}
}
/** Step 3.
* Since adding/removing edges from a graph while iterating through said edges is a bad idea
* (leads to losing pointer reference/ends up iterating through more edges than it should)
* I do this step in two passes:
* 3.0: Create 2 bitmaps where I will flag which edges should be added/deleted.
* 3.1: For each edge in parallel, flag for deletion if w=v.p. Else, flag for adding to the graph.
* 3.2: Iterate through the bitmaps in parallel and do the deletion/adding of edges without worrying about losing
* pointer references/iterating through recently added edges.
*
*/
/** Step 3.0: */
unsigned int n = G.order();
bool bitmap_todelete[n][n];
bool bitmap_toadd[n][n];
memset(bitmap_todelete, 0, n * n * sizeof(bool));
memset(bitmap_toadd, 0, n * n * sizeof(bool));
/** Step 3.1: */
// for each arc v->w in parallel
#pragma omp parallel for
for (unsigned int i = 0; i < G.Out.bucket_count(); i++)
{
for (auto itv = G.Out.begin(i); itv != G.Out.end(i); itv++)
{
unsigned int v = itv->first;
#pragma omp parallel for
for (unsigned int j = 0; j < G.Out[v]._map.bucket_count(); j++)
{
for (auto ite = G.Out[v]._map.begin(j); ite != G.Out[v]._map.end(j); ite++)
{
unsigned int w = ite->first;
bitmap_todelete[v][w] = 1; // mark for deletion
if (p[v] != w)
{
// mark for adding. doing this to avoid iterating through the newly added edges in this step.
bitmap_toadd[w][p[v]] = 1;
}
}
}
}
}
/** Step 3.2: */
// delete/add marked edges, in parallel
#pragma omp parallel for
for (unsigned int i = 0; i < n; i++)
{
#pragma omp parallel for
for (unsigned int j = 0; j < n; j++)
{
if (bitmap_todelete[i][j] == 1)
{
#pragma omp critical
G.remove_edge(i, j);
}
if (bitmap_toadd[i][j] == 1)
{
#pragma omp critical
G.add_edge(i, j);
}
}
}
/** Step 4. Finally, we consider all vertices v in parallel. If v!=v.p, add a new arc v->v.p.*/
#pragma omp parallel for
for (unsigned int i = 0; i < G.Out.bucket_count(); i++)
{
for (auto it = G.Out.begin(i); it != G.Out.end(i); it++)
{
unsigned int v = it->first;
if (p[v] != v)
{
#pragma omp critical
G.add_edge(p[v], v);
}
}
}
/** Step 5. Check the end condition. For each edge in parallel, if we find one that
* does not have v.p = w.p, with w.p being either v or w, we set the done flag to false,
* thus continuing the algorithm.
*/
#pragma omp parallel for
for (unsigned int i = 0; i < G.Out.bucket_count(); i++)
{
for (auto itv = G.Out.begin(i); itv != G.Out.end(i); itv++)
{
unsigned int v = itv->first;
#pragma omp parallel for
for (unsigned int j = 0; j < G.Out[v]._map.bucket_count(); j++)
{
for (auto ite = G.Out[v]._map.begin(j); ite != G.Out[v]._map.end(j); ite++)
{
unsigned int w = ite->first;
if (!(p[v] == p[w] && (p[v] == v || p[v] == w)))
{
done = false;
}
}
}
}
}
}
/** We're done! */
}
// --------------------------------------------------------------------
// --------------------------------------------------------------------
// Algorithm S done in the lab.
// Used for checking algorithm B's output against a source of truth.
bool ParallelGraphAlgorithms::shortcut(Graph& G, vector<unsigned int>& p, vector<unsigned int>& o){
bool changes(0);
#pragma omp parallel for
for(unsigned int i = 0; i < G.vertices.bucket_count(); i++){
for(auto it = G.vertices.begin(i); it != G.vertices.end(i); it++){
unsigned int v = *it;
o[v] = p[v];
}
}
#pragma omp parallel for
for(unsigned int i = 0; i < G.vertices.bucket_count(); i++){
for(auto it = G.vertices.begin(i); it != G.vertices.end(i); it++){
unsigned int v = *it;
p[v] = o[o[v]];
if(p[v] != o[v]) changes = 1;
}
}
return changes;
}
bool ParallelGraphAlgorithms::parent_connect(Graph& G, vector<unsigned int>& p, vector<unsigned int>& o){
bool changes(0);
/* CMT: atribuire in paralel */
#pragma omp parallel for
for(unsigned int i = 0; i < G.vertices.bucket_count(); i++){
for(auto itv = G.vertices.begin(i); itv != G.vertices.end(i); itv++){
unsigned int v = *itv;
o[v] = p[v];
}
}
#pragma omp parallel for
for(unsigned int i = 0; i < G.Out.bucket_count(); i++){
for(auto itv = G.Out.begin(i); itv != G.Out.end(i); itv++){
unsigned int v = itv->first;
#pragma omp parallel for
for(unsigned int j = 0; j < G.Out[v]._map.bucket_count(); j++){
for(auto ite = G.Out[v]._map.begin(j); ite != G.Out[v]._map.end(j); ite++){
unsigned int w = ite->first;
if(o[v] > o[w])
#pragma omp critical
p[o[v]] = min(p[o[v]],o[w]);
}
}
}
}
return changes;
}
void ParallelGraphAlgorithms::algorithm_S(Graph G, vector<unsigned int>& p, vector<unsigned int>& o){
#pragma omp parallel for
for(unsigned int i = 0; i < G.Out.bucket_count(); i++){
for(auto it = G.Out.begin(i); it != G.Out.end(i); it++){
unsigned int v = it->first;
p[v] = v;
}
}
bool not_done(1);
while(not_done){
not_done = parent_connect(G,p,o);
bool not_star(1);
while(not_star) not_star = shortcut(G,p,o);
}
}