-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBC.C
237 lines (216 loc) · 7.2 KB
/
BC.C
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
// This code is part of the project "Ligra: A Lightweight Graph Processing
// Framework for Shared Memory", presented at Principles and Practice of
// Parallel Programming, 2013.
// Copyright (c) 2013 Julian Shun and Guy Blelloch
//
// This code has been extended in the project "GraphGrind: Addressing Load
// Imbalance of Graph Partitioning", presented at International Symposium
// on Supercomputing, 2017.
// Copyright (c) 2017 Jiawen Sun, Hans Vandierendonck, Dimitrios S. Nikolopoulos
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights (to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#define VERTEX 1
#if NUMA
#include "ligra-numa.h"
#else
#include "ligra.h"
#endif
typedef float fType;
struct BC_F
{
fType* NumPaths;
bool* Visited;
static const bool use_cache = true;
struct cache_t
{
fType value;
};
BC_F(fType* _NumPaths, bool* _Visited) :
NumPaths(_NumPaths), Visited(_Visited) {}
inline bool update(intT s, intT d) //Update function for forward phase
{
fType oldV = NumPaths[d];
NumPaths[d] += NumPaths[s];
return oldV == (fType)0;
}
inline bool updateAtomic (intT s, intT d) //atomic Update, basically an add
{
volatile fType oldV, newV;
do
{
oldV = NumPaths[d];
newV = oldV + NumPaths[s];
}
while(!CAS(&NumPaths[d],oldV,newV));
return oldV == (fType)0;
}
inline void create_cache(cache_t &cache, intT d)
{
cache.value = NumPaths[d];
}
inline bool update(cache_t &cache, intT s)
{
fType oldV = cache.value;
cache.value += NumPaths[s];
return oldV == (fType)0;
}
inline void commit_cache(cache_t &cache, intT d)
{
// Cache used only when vertex accessed sequentially
NumPaths[d] = cache.value;
}
inline bool cond (intT d)
{
return Visited[d] == false; //check if visited
}
};
struct BC_Back_F
{
fType* Dependencies;
bool* Visited;
BC_Back_F(fType* _Dependencies, bool* _Visited) :
Dependencies(_Dependencies), Visited(_Visited) {}
static const bool use_cache = true;//true;
struct cache_t
{
fType value;
};
inline bool update(intT s, intT d) //Update function for backwards phase
{
fType oldV = Dependencies[d];
Dependencies[d] += Dependencies[s];
return oldV == (fType)0;
}
inline bool updateAtomic (intT s, intT d) //atomic Update
{
volatile fType oldV, newV;
do
{
oldV = Dependencies[d];
newV = oldV + Dependencies[s];
}
while(!CAS(&Dependencies[d],oldV,newV));
return oldV == (fType)0;
}
inline void create_cache(cache_t &cache, intT d)
{
cache.value = Dependencies[d];
}
inline bool update(cache_t &cache, intT s)
{
fType oldV = cache.value;
cache.value += Dependencies[s];
return oldV == (fType)0;
}
inline void commit_cache(cache_t &cache, intT d)
{
Dependencies[d] = cache.value;
}
inline bool cond (intT d)
{
return Visited[d] == false; //check if visited
}
};
//vertex map function to mark visited vertexSubset
struct BC_Vertex_F
{
bool* Visited;
BC_Vertex_F(bool* _Visited) : Visited(_Visited) {}
inline bool operator() (intT i)
{
Visited[i] = true;
return true;
}
};
//vertex map function (used on backwards phase) to mark visited vertexSubset
//and add to Dependencies score
struct BC_Back_Vertex_F
{
bool* Visited;
fType* Dependencies, *inverseNumPaths;
BC_Back_Vertex_F(bool* _Visited, fType* _Dependencies, fType* _inverseNumPaths) :
Visited(_Visited), Dependencies(_Dependencies), inverseNumPaths(_inverseNumPaths) {}
inline bool operator() (intT i)
{
Visited[i] = true;
Dependencies[i] += inverseNumPaths[i];
return true;
}
};
template <class GraphType>
void Compute(GraphType &GA, long start)
{
typedef typename GraphType::vertex_type vertex; // Is determined by GraphType
const partitioner &part = GA.get_partitioner();
const int perNode = part.get_num_per_node_partitions();
intT n = GA.n;
intT m = GA.m;
mmap_ptr<fType> NumPaths;
NumPaths.part_allocate (part);
mmap_ptr<bool> Visited;
Visited.part_allocate (part);
loop(j,part,perNode,NumPaths[j]=0.0);
loop(j,part,perNode,Visited[j]=false);
NumPaths[start] = 1.0;
Visited[start] = true;
//vertexSubset Frontier(n,start);
partitioned_vertices Frontier=partitioned_vertices::create(n,start,GA.getWholeGraph().V[start].getOutDegree());
vector<partitioned_vertices> Levels;
Levels.push_back(Frontier);
intT round = 0;
timer t1,t2;
while(!Frontier.isEmpty()) //first phase
{
round++;
partitioned_vertices output=edgeMap(GA,Frontier,BC_F(NumPaths,Visited),m/20);
vertexMap(part,output, BC_Vertex_F(Visited)); //mark visited
Levels.push_back(output); //save frontier onto Levels
Frontier = output;
}
mmap_ptr<fType> Dependencies;
Dependencies.part_allocate (part);
loop(j,part,perNode,Dependencies[j]=0.0);
//invert numpaths
mmap_ptr<fType> inverseNumPaths;
inverseNumPaths = NumPaths;
loop(j,part,perNode,inverseNumPaths[j]=1/inverseNumPaths[j]);
Levels[round].del();
//reuse Visited
loop(j,part,perNode, Visited[j]=false);
Frontier = Levels[round-1];
vertexMap(part,Frontier,BC_Back_Vertex_F(Visited,Dependencies,inverseNumPaths));
//tranpose graph
GA.transpose();
for(intT r=round-2; r>=0; r--) //backwards phase
{
partitioned_vertices output=edgeMap(GA,Frontier,BC_Back_F(Dependencies,Visited), m/20);
output.del();
Frontier.del();
Frontier = Levels[r]; //gets frontier from Levels array
//vertex map to mark visited and update Dependencies scores
vertexMap(part,Frontier,BC_Back_Vertex_F(Visited,Dependencies,inverseNumPaths));
}
Frontier.del();
//Update dependencies scores
loop(j,part,perNode, Dependencies[j]=(Dependencies[j]-inverseNumPaths[j])/inverseNumPaths[j]);
inverseNumPaths.del(); //free(inverseNumPaths);
Visited.del(); //free(Visited);
Dependencies.del(); //free(Dependencies);
}