-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscan_xp_common.cu
360 lines (318 loc) · 10.6 KB
/
scan_xp_common.cu
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "scan_xp.h"
#include <cmath>
#include <chrono>
#include <thread>
#include "util/stat.h"
#include "util/log.h"
#include "util/util.h"
#include "util/pretty_print.h"
using namespace std::chrono;
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort = true) {
if (code != cudaSuccess) {
fprintf(stderr, "GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort)
exit(code);
}
}
SCAN_XP::SCAN_XP(int thread_num, int min_u, double epsilon, char *dir) :
thread_num_(thread_num), min_u_(min_u), epsilon_(epsilon), g(Graph(dir)), uf_ptr(nullptr) {
log_info("thread num: %d", thread_num_);log_info("graph, n: %s, m: %s", FormatWithCommas(g.nodemax).c_str(), FormatWithCommas(g.edgemax).c_str());
uf_ptr = new UnionFind(g.nodemax);
// pre-processing
#pragma omp parallel for num_threads(thread_num_) schedule(dynamic, 100000)
for (auto i = 0u; i < g.nodemax; i++) {
g.core_count[i] = 0;
g.label[i] = UNCLASSIFIED;
for (auto n = g.node_off[i]; n < g.node_off[i + 1]; n++) {
g.common_node_num[n] = 2;
}
}
}
SCAN_XP::~SCAN_XP() {
delete uf_ptr;
}
uint32_t SCAN_XP::BinarySearch(int *array, uint32_t offset_beg, uint32_t offset_end, int val) {
auto mid = static_cast<uint32_t>((static_cast<unsigned long>(offset_beg) + offset_end) / 2);
if (array[mid] == val) {
return mid;
}
return val < array[mid] ? BinarySearch(array, offset_beg, mid, val) : BinarySearch(array, mid + 1, offset_end, val);
}
int SCAN_XP::FindSrc(Graph *g, int u, uint32_t edge_idx) {
if (edge_idx >= g->node_off[u + 1]) {
// update last_u, preferring galloping instead of binary search because not large range here
u = GallopingSearch(g->node_off, static_cast<uint32_t>(u) + 1, g->nodemax + 1, edge_idx);
// 1) first > , 2) has neighbor
if (g->node_off[u] > edge_idx) {
while (g->degree[u - 1] == 1) {
u--;
}
u--;
} else {
// g->node_off[u] == i
while (g->degree[u] == 1) {
u++;
}
}
}
return u;
}
void SCAN_XP::ClusterCore() {
#pragma omp parallel for num_threads(thread_num_) schedule(dynamic, 2000)
for (auto i = 0u; i < g.nodemax; i++) {
if (g.label[i] == CORE) {
for (auto edge_idx = g.node_off[i]; edge_idx < g.node_off[i + 1]; edge_idx++) {
// yche: fix bug, only union when g.edge_dst[edge_idx] is a core vertex
if (g.label[g.edge_dst[edge_idx]] == CORE && g.similarity[edge_idx]) {
uf_ptr->UnionThreadSafe(i, g.edge_dst[edge_idx]);
}
}
}
}
}
bool SCAN_XP::CheckHub(Graph *g, UnionFind *uf, int a) {
set<int> c;
for (auto i = g->node_off[a]; i < g->node_off[a + 1]; i++) {
if (g->label[g->edge_dst[i]] != CORE)
continue;
c.insert((*uf).FindRoot(g->edge_dst[i]));
}
return c.size() >= 2;
}
void SCAN_XP::LabelNonCore() {
int core_num = 0u;
#pragma omp parallel for num_threads(thread_num_) schedule(dynamic, 1000), reduction(+:core_num)
for (auto i = 0u; i < g.nodemax; i++) {
if (g.label[i] == CORE) {
core_num++;
continue;
}
if (CheckHub(&g, uf_ptr, i)) {
g.label[i] = HUB;
}
}log_info("Core: %s", FormatWithCommas(core_num).c_str());
}
void SCAN_XP::PostProcess() {
int cluster_num;
int hub_num = 0u;
int out_num = 0u;
set<int> c;
for (auto i = 0u; i < g.nodemax; i++) {
if (g.label[i] == CORE) {
c.emplace(uf_ptr->FindRoot(i));
}
}
cluster_num = static_cast<int>(c.size());
#pragma omp parallel for num_threads(thread_num_) reduction(+:hub_num, out_num)
for (auto i = 0u; i < g.nodemax; i++) {
if (g.label[i] == HUB) {
hub_num++;
} else if (g.label[i] == UNCLASSIFIED) {
out_num++;
}
}log_info("Cluster: %s, Hub: %s, Outlier: %s", FormatWithCommas(cluster_num).c_str(),
FormatWithCommas(hub_num).c_str(), FormatWithCommas(out_num).c_str());
}
void SCAN_XP::MarkClusterMinEleAsId(UnionFind *union_find_ptr) {
auto start = high_resolution_clock::now();
g.cluster_dict = vector<int>(g.nodemax);
std::fill(g.cluster_dict.begin(), g.cluster_dict.end(), g.nodemax);
#pragma omp parallel for num_threads(thread_num_)
for (auto i = 0u; i < g.nodemax; i++) {
if (g.label[i] == CORE) {
int x = union_find_ptr->FindRoot(i);
int cluster_min_ele;
do {
cluster_min_ele = g.cluster_dict[x];
if (i >= g.cluster_dict[x]) {
break;
}
} while (!__sync_bool_compare_and_swap(&(g.cluster_dict[x]), cluster_min_ele, i));
}
}
auto end = high_resolution_clock::now();
log_info("Step4 - cluster id initialization cost: %.3lf s, Mem Usage: %s KB",
duration_cast<milliseconds>(end - start).count() / 1000.0, FormatWithCommas(getValue()).c_str());
}
void SCAN_XP::PrepareResultOutput() {
// prepare output
MarkClusterMinEleAsId(uf_ptr);
auto start = high_resolution_clock::now();
#pragma omp parallel num_threads(thread_num_)
{
vector<pair<int, int>> local_non_core_cluster;
#pragma omp for nowait
for (auto i = 0u; i < g.nodemax; i++) {
if (g.label[i] == CORE) {
for (auto j = g.node_off[i]; j < g.node_off[i + 1]; j++) {
auto v = g.edge_dst[j];
if (g.label[v] != CORE && g.similarity[j]) {
local_non_core_cluster.emplace_back(g.cluster_dict[uf_ptr->FindRoot(i)], v);
}
}
}
}
#pragma omp critical
{
for (auto ele : local_non_core_cluster) {
g.noncore_cluster.emplace_back(ele);
}
};
};
auto end = high_resolution_clock::now();
log_info("Step4 - prepare results: %.3lf s, Mem Usage: %s KB",
duration_cast<milliseconds>(end - start).count() / 1000.0, FormatWithCommas(getValue()).c_str());
auto epsilon_str = to_string(epsilon_);
epsilon_str.erase(epsilon_str.find_last_not_of("0u") + 1);
start = high_resolution_clock::now();
g.Output(epsilon_str.c_str(), to_string(min_u_).c_str(), uf_ptr);
end = high_resolution_clock::now();
log_info("Step4 - output to the disk cost: %.3lf s, Mem Usage: %s KB",
duration_cast<milliseconds>(end - start).count() / 1000.0, FormatWithCommas(getValue()).c_str());
}
bool CheckSubstring(std::string firstString, std::string secondString) {
if (secondString.size() > firstString.size())
return false;
for (int i = 0; i < firstString.size(); i++) {
int j = 0;
if (firstString[i] == secondString[j]) {
while (firstString[i] == secondString[j] && j < secondString.size()) {
j++;
i++;
}
if (j == secondString.size())
return true;
}
}
return false;
}
void PostComputeCoreChecking(SCAN_XP* scanxp, Graph* g, int min_u_, double epsilon_) {
{
auto start = high_resolution_clock::now();
#pragma omp parallel for num_threads(scanxp->thread_num_) schedule(dynamic, 60000)
for (auto i = 0u; i < g->edgemax; i++) {
// remove edge_src optimization, assuming task scheduling in FIFO-queue-mode
static thread_local auto u = 0;
u = scanxp->FindSrc(g, u, i);
auto v = g->edge_dst[i];
if (u > v) {
uint32_t offset = g->common_node_num[i];
g->common_node_num[i] = g->common_node_num[offset];
}
}
auto end = high_resolution_clock::now();
log_info("bin-search cost : %.3lf s, Mem Usage: %s KB",
duration_cast<milliseconds>(end - start).count() / 1000.0, FormatWithCommas(getValue()).c_str());
}
// 5th: compute similarities and check core status
{
auto start = high_resolution_clock::now();
#pragma omp parallel for num_threads(scanxp->thread_num_)
for (auto i = 0u; i < g->edgemax; i++) {
static thread_local auto u = 0;
u = scanxp->FindSrc(g, u, i);
long double du = g->node_off[u + 1] - g->node_off[u] + 1;
long double dv = g->node_off[g->edge_dst[i] + 1] - g->node_off[g->edge_dst[i]] + 1;
auto sim_value = static_cast<double>((long double) g->common_node_num[i] / sqrt(du * dv));
if (sim_value >= epsilon_) {
//#pragma omp atomic // comment out assuming no overlap because of large range
g->core_count[u]++;
g->similarity[i] = true;
} else {
g->similarity[i] = false;
}
}
#pragma omp parallel for num_threads(scanxp->thread_num_)
for (auto i = 0u; i < g->nodemax; i++) {
if (g->core_count[i] >= min_u_) {
g->label[i] = CORE;
};
}
auto end = high_resolution_clock::now();
log_info("core-checking sim-core-comp cost : %.3lf s, Mem Usage: %s KB",
duration_cast<milliseconds>(end - start).count() / 1000.0, FormatWithCommas(getValue()).c_str());
}
}
void Verify(SCAN_XP* scanxp, Graph* g) {
double s1 = omp_get_wtime();
log_info("Verify with BitVec");
// iterate through vertices
#pragma omp parallel for num_threads(scanxp->thread_num_) schedule(dynamic, 6000)
for (auto i = 0u; i < g->edgemax; i++) {
// remove edge_src optimization, assuming task scheduling in FIFO-queue-mode
static thread_local auto u = 0;
u = scanxp->FindSrc(g, u, i);
auto v = g->edge_dst[i];
#if defined(BIT_VEC)
static thread_local auto last_u = -1;
static thread_local auto bits_vec = vector<bool>(g->nodemax, false);
if (last_u != u) {
// clear previous
if (last_u != -1)
{
for (auto offset = g->node_off[last_u]; offset < g->node_off[last_u + 1]; offset++) {
bits_vec[g->edge_dst[offset]] = false;
}
}
// set new ones
for (auto offset = g->node_off[u]; offset < g->node_off[u + 1]; offset++) {
bits_vec[g->edge_dst[offset]] = true;
}
last_u = u;
}
#endif
if (u < v) {
#ifdef BIT_VEC
if(g->common_node_num[i]!=2+ComputeCNHashBitVec(g, u, v, bits_vec))
{
#pragma omp critical
{
log_info("Verify Fail, Fix Bug Now!");
log_info("%d, %d, cur: %d, exp: %d", u, v, g->common_node_num[i],
2 + ComputeCNHashBitVec(g, u, v, bits_vec));
vector<int> u_nei;
for (auto i = g->node_off[u]; i < g->node_off[u + 1]; i++) {
u_nei.push_back(g->edge_dst[i]);
}
cout << u_nei << endl;
u_nei.clear();
for (auto i = g->node_off[v]; i < g->node_off[v + 1]; i++) {
u_nei.push_back(g->edge_dst[i]);
}
cout << u_nei << endl;
exit(-1);
}
}
#else
log_info("err");
exit(-1);
#endif
}
}
double e1 = omp_get_wtime();
log_info("Verification Time Cost: %.3lf s, Mem Usage: %s KB", e1 - s1, FormatWithCommas(getValue()).c_str());log_info("Verify Pass, Correct!");
}
void SCAN_XP::Execute() {
//step1 CheckCore
double s1 = omp_get_wtime();
CheckCore(&g);
double e1 = omp_get_wtime();
log_info("Step1 - CheckCore: %.3lf s, Mem Usage: %s KB", e1 - s1, FormatWithCommas(getValue()).c_str());
//#ifdef VERIFY_WITH_CPU
Verify(this, &g);
//#endif
//step2 ClusterCore
double s2 = omp_get_wtime();
ClusterCore();
double e2 = omp_get_wtime();
log_info("Step2 - ClusterCore: %.3lf s, Mem Usage: %s KB", e2 - s2, FormatWithCommas(getValue()).c_str());
//step3 LabelNonCore
double s3 = omp_get_wtime();
LabelNonCore();
double e3 = omp_get_wtime();
log_info("Step3 - LabelNonCore: %.3lf s, Mem Usage: %s KB", e3 - s3, FormatWithCommas(getValue()).c_str());
// post-processing, prepare result and output
PostProcess();
PrepareResultOutput();
}