-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.cpp
367 lines (316 loc) · 10.8 KB
/
main.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
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
361
362
363
364
365
366
367
/* Copyright (c) Hilmi Yildirim 2010,2011.
The software is provided on an as is basis for research purposes.
There is no additional support offered, nor are the author(s)
or their institutions liable under any circumstances.
*/
#include "Graph.h"
#include "GraphUtil.h"
#include "Grail.h"
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <cstring>
#include "exception_list.h"
#include "exception_list_incremental_base.h"
#include "exception_list_incremental_plus.h"
#include "utils.h"
bool SKIPSCC = false;
bool BIDIRECTIONAL = false;
int LABELINGTYPE = 0;
bool UseExceptions = false;
bool UsePositiveCut = false;
bool POOL = false;
int POOLSIZE = 100;
int DIM = 2;
int query_num = 100000;
char* filename = NULL;
char* testfilename = NULL;
bool debug = false;
bool LEVEL_FILTER = false;
bool LEVEL_FILTER_I = false;
float labeling_time, query_time, query_timepart,exceptionlist_time;
int alg_type = 1;
void handle(int sig) {
char *alg_name;
switch(alg_type){
case 1: alg_name= "GRAIL"; break;
case 2: alg_name= "GRAILLF"; break;
case 3: alg_name= "GRAILBI"; break;
case 6: alg_name= "GRAILBILF"; break;
case -1: alg_name= "GRAIL*"; break;
case -2: alg_name= "GRAIL*LF"; break;
case -3: alg_name= "GRAIL*BI"; break;
case -6: alg_name= "GRAIL*BILF"; break;
}
cout << "COMPAR: " << alg_name << DIM << "\t" << labeling_time << "\t" << "TOut" << "\t" << endl;
exit(1);
}
static void usage() {
cout << "\nUsage:\n"
" grail [-h] <filename> -test <testfilename> [-dim <DIM>] [-skip] [-ex] [-ltype <labelingtype>] [-t <alg_type>]\n"
"Description:\n"
" -h Print the help message.\n"
" <filename> is the name of the input graph in gra format.\n"
" -test Set the queryfile which contains a line of <source> <target> <reachability> for each query. \n"
" -dim Set the number of traversals to be performed. The default value is 5.\n"
" -ex Use exception lists method instead of pruning. Default is not using exception lists.\n"
" -skip Skip the phase converting the graph into a dag. Use only when the input is already a DAG.\n"
" -t <alg_type> alg_type can take 8 different values. Default value is 1.\n"
" \t\t\t 1 : Basic Search (used in the original VLDB'10 paper) \n"
" \t\t\t 2 : Basic Search + Level Filter \n"
" \t\t\t 3 : Bidirectional Search \n"
" \t\t\t 6 : Bidirectional Search + Level Filter \n"
" \t\t\t -1 : Positive Cut + Basic Search\n"
" \t\t\t -2 : Positive Cut + Basic Search + Level Filter (usually provides the best query times) \n"
" \t\t\t -3 : Positive Cut + Bidirectional Search \n"
" \t\t\t -6 : Positive Cut + Bidirectional Search + Level Filter\n"
" -ltype <labeling_type> labeling_type can take 6 different values. Default value is 0.\n"
" \t\t\t 0 : Completely randomized traversals. \n"
" \t\t\t 1 : Randomized Reverse Pairs Traversals (usually provides the best quality) \n"
" \t\t\t 2 : Heuristicly Guided Traversal: Maximum Volume First \n"
" \t\t\t 3 : Heuristicly Guided Traversal: Maximum of Minimum Interval First \n"
" \t\t\t 4 : Heuristicly Guided Traversal: Maximum Adjusted Volume First \n"
" \t\t\t 5 : Heuristicly Guided Traversal: Maximum of Adjusted Minimum Volume First \n"
<< endl;
}
static void parse_args(int argc, char *argv[]){
if (argc == 1) {
usage();
exit(0);
}
int i = 1;
while (i < argc) {
if (strcmp("-h", argv[i]) == 0) {
usage();
exit(0);
}
if (strcmp("-d", argv[i]) == 0) {
i++;
debug = true;
}
if (strcmp("-n", argv[i]) == 0) {
i++;
query_num = atoi(argv[i++]);
}
else if (strcmp("-dim", argv[i]) == 0) {
i++;
DIM = atoi(argv[i++]);
}
else if (strcmp("-lfi", argv[i]) == 0) {
LEVEL_FILTER_I = true;
i++;
}
else if (strcmp("-lf", argv[i]) == 0) {
LEVEL_FILTER = true;
alg_type *=2;
i++;
}else if (strcmp("-test", argv[i]) == 0) {
i++;
testfilename = argv[i++];
}else if(strcmp("-skip", argv[i])== 0) {
i++;
SKIPSCC = true;
}else if(strcmp("-ltype", argv[i])== 0) {
i++;
LABELINGTYPE = atoi(argv[i++]);
}else if(strcmp("-t", argv[i])== 0) {
i++;
alg_type = atoi(argv[i++]);
}else if(strcmp("-ex", argv[i])== 0) {
i++;
UseExceptions = true;
}else if(strcmp("-pp", argv[i])== 0) {
i++;
UsePositiveCut = true;
alg_type *=-1;
}else if(strcmp("-bi", argv[i])== 0) {
i++;
BIDIRECTIONAL = true;
alg_type *=3;
}else if(strcmp("-pool", argv[i])== 0) {
i++;
POOL = true;
POOLSIZE = atoi(argv[i++]);
}
else {
filename = argv[i++];
}
}
if(!testfilename){
cout << "Please provide a test file : -test <testfilename> " << endl;
exit(0);
}
}
int main(int argc, char* argv[]) {
signal(SIGALRM, handle);
parse_args(argc,argv);
/*
Read Graph from the input file
*/
ifstream infile(filename);
if (!infile) {
cout << "Error: Cannot open " << filename << endl;
return -1;
}
Graph g(infile);
cout << "#vertex size:" << g.num_vertices() << "\t#edges size:" << g.num_edges() << endl;
int s, t;
int left = 0;
int gsize = g.num_vertices();
bool r;
struct timeval after_time, before_time, after_timepart, before_timepart;
int *sccmap;
if(!SKIPSCC){
sccmap = new int[gsize]; // store pair of orignal vertex and corresponding vertex in merged graph
vector<int> reverse_topo_sort;
// merge strongly connected component
cout << "merging strongly connected component..." << endl;
gettimeofday(&before_time, NULL);
GraphUtil::mergeSCC(g, sccmap, reverse_topo_sort);
gettimeofday(&after_time, NULL);
query_time = (after_time.tv_sec - before_time.tv_sec)*1000.0 +
(after_time.tv_usec - before_time.tv_usec)*1.0/1000.0;
cout << "merging time:" << query_time << " (ms)" << endl;
cout << "#DAG vertex size:" << g.num_vertices() << "\t#DAG edges size:" << g.num_edges() << endl;
// g.printGraph();
// ofstream outSCC("scc.out");
// g.writeGraph(outSCC);
// outSCC.close();
}
GraphUtil::topo_leveler(g);
// prepare queries
srand48(time(NULL));
cout << "preparing queries..." << endl;
vector<int> src;
vector<int> trg;
vector<int> labels;
vector<int>::iterator sit, tit, lit;
int success=0,fail=0;
int label;
if(testfilename==NULL){
while (left < query_num) {
s = lrand48() % gsize;
t = lrand48() % gsize;
if (s == t) continue;
src.push_back(s);
trg.push_back(t);
++left;
}
}else{
std::ifstream fstr(testfilename);
while(!fstr.eof()) {
fstr >> s >> t >> label;
src.push_back(s);
trg.push_back(t);
labels.push_back(label);
}
}
cout << "queries are ready" << endl;
gettimeofday(&before_time, NULL);
int dimension ;
Grail grail(g,DIM,LABELINGTYPE,POOL,POOLSIZE);
grail.set_level_filter(LEVEL_FILTER);
gettimeofday(&after_time, NULL);
labeling_time = (after_time.tv_sec - before_time.tv_sec)*1000.0 +
(after_time.tv_usec - before_time.tv_usec)*1.0/1000.0;
cout << "#construction time:" << labeling_time << " (ms)" << endl;
ExceptionList * el = NULL;
exceptionlist_time = 0;
if(UseExceptions){
gettimeofday(&before_time, NULL);
// el = new ExceptionListIncrementalPlus(g,DIM,LEVEL_FILTER); // filtering by levels doesnot help inside exceptionlists construction
// cout << "UP TO HERE OK" << endl;
el = new ExceptionListIncrementalPlus(g,DIM,0);
gettimeofday(&after_time, NULL);
exceptionlist_time = (after_time.tv_sec - before_time.tv_sec)*1000.0 +
(after_time.tv_usec - before_time.tv_usec)*1.0/1000.0;
cout << "exceptionlist time:" << exceptionlist_time << " (ms)" << endl;
}
// process queries
cout << "process queries..." << endl;
gettimeofday(&before_time, NULL);
gettimeofday(&before_timepart, NULL);
int seenpositive = 0;
int source, target;
int reachable = 0, nonreachable =0;
for (sit = src.begin(), tit = trg.begin(), lit = labels.begin();sit != src.end(); ++sit, ++tit, ++lit) {
if(!SKIPSCC){
s = sccmap[*sit];
t = sccmap[*tit];
}else{
s = *sit;
t = *tit;
}
// if(grail.bidirectionalReach(s,t,el) != grail.reach(s,t,el)){
// cout << "Conflict 1 " << s <<" " << t << endl;
// }
// if(grail.bidirectionalReachPP(s,t,el) != grail.reachPP(s,t,el)){
// cout << "Conflict 2 " << s <<" " << t << endl;
// }
switch(alg_type){
case 1: r = grail.reach(s,t,el); break;
case 2: r = grail.reach_lf(s,t,el); break;
case 3: r = grail.bidirectionalReach(s,t,el); break;
case 6: r = grail.bidirectionalReach_lf(s,t,el); break;
case -1: r = grail.reachPP(s,t,el); break;
case -2: r = grail.reachPP_lf(s,t,el); break;
case -3: r = grail.bidirectionalReachPP(s,t,el); break;
case -6: r = grail.bidirectionalReachPP_lf(s,t,el); break;
}
if(r==true) {
reachable++;
if(*lit == 0) {
// cout << "False positive pair = " << s << " " << t << " " << *lit << endl;
// cout << "Levels : " << s << "->" << g[s].top_level << " " << t << "->" << g[t].top_level << endl;
fail++;
} else {
success++;
}
}
else {
nonreachable++;
if(*lit == 1) {
// cout << "False negative pair = " << s << " " << t << " " << *lit << endl;
fail++;
}
else success++;
}
}
cout << "Success Rate " << success << "/" << success+fail << endl;
gettimeofday(&after_time, NULL);
query_time = (after_time.tv_sec - before_time.tv_sec)*1000.0 +
(after_time.tv_usec - before_time.tv_usec)*1.0/1000.0;
cout << "#total query running time:" << query_time << " (ms)" << endl;
cout.setf(ios::fixed);
cout.precision(2);
cout << "GRAIL REPORT " << endl;
cout << "filename = " << filename << "\t testfilename = " << (testfilename ? testfilename:"Random") << "\t DIM = " << DIM <<endl;
cout << "Labeling_time = " << labeling_time + exceptionlist_time << "\t Query_Time = " << query_time << "\t Index_Size = " << gsize*DIM*2 << "\t Mem_Usage = " << print_mem_usage() << " MB"<< endl;
char *alg_name;
switch(alg_type){
case 1: alg_name= "GRAIL"; break;
case 2: alg_name= "GRAILLF"; LEVEL_FILTER=true; break;
case 3: alg_name= "GRAILBI"; break;
case 6: alg_name= "GRAILBILF"; LEVEL_FILTER=true; break;
case -1: alg_name= "GRAIL*"; break;
case -2: alg_name= "GRAIL*LF"; LEVEL_FILTER=true; break;
case -3: alg_name= "GRAIL*BI"; break;
case -6: alg_name= "GRAIL*BILF"; LEVEL_FILTER=true; break;
}
if(grail.PositiveCut==0)
grail.PositiveCut = 1;
int totalIndexSize;
if(alg_type < 0){
totalIndexSize=gsize*DIM*3;
}else{
totalIndexSize=gsize*DIM*2;
}
if(LEVEL_FILTER){
totalIndexSize+=gsize;
}
if(UseExceptions){
totalIndexSize+=el->Size();
}
cout << "COMPAR: " << alg_name << DIM << "\t" << labeling_time + exceptionlist_time << "\t" << query_time << "\t" << totalIndexSize << "\t" << print_mem_usage() << "\t" << grail.TotalCall << "\t" << grail.PositiveCut << "\t" << grail.NegativeCut << "\t" << reachable << "\t AvgCut:" << (grail.TotalDepth/grail.PositiveCut) << endl;
}