-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
192 lines (159 loc) · 5.6 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
#include <sys/resource.h>
#include <sys/time.h>
#include <unistd.h>
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include "graph.hpp"
#include "louvain.hpp"
unsigned seed;
static std::string inputFileName;
static GraphElem nvRGG = 0;
static int generateGraph = 0;
static GraphWeight randomEdgePercent = 0.0;
static bool randomNumberLCG = false;
static bool isUnitEdgeWeight = false;
static GraphWeight threshold = 1.0E-6;
static void parseCommandLine(const int argc, char * const argv[]);
int main(int argc, char *argv[])
{
double t1, td, td0, td1, tot_time;
int max_threads, req_threads;
max_threads = omp_get_max_threads();
#pragma omp parallel
{
#pragma omp single
req_threads = omp_get_num_threads();
}
// command line options
parseCommandLine(argc, argv);
Graph* g = nullptr;
td0 = omp_get_wtime();
// generate graph only supports RGG as of now
if (generateGraph)
{
GenerateRGG gr(nvRGG);
g = gr.generate(randomNumberLCG, isUnitEdgeWeight, randomEdgePercent);
std::cout << "Generated Random Geometric Graph with d: " << gr.get_d() << std::endl;
}
else // read input graph
{
BinaryEdgeList rm;
g = rm.read(inputFileName, isUnitEdgeWeight);
std::cout << "Input file: " << inputFileName << std::endl;
}
g->print_stats();
assert(g != nullptr);
td1 = omp_get_wtime();
td = td1 - td0;
if (!generateGraph)
std::cout << "Time to read input file and create graph (in s): "
<< td << std::endl;
else
std::cout << "Time to generate graph of "
<< nvRGG << " vertices (in s): " << td << std::endl;
#ifdef USE_OMP_OFFLOAD
td0 = omp_get_wtime();
const GraphElem nv = g->get_nv();
const GraphElem ne = g->get_ne();
#pragma omp target update to(g->edge_indices_[0:nv+1], g->edge_list_[0:ne])
td1 = omp_get_wtime();
std::cout << "Host to device graph data transfer time (in s): " << (td1 - td0) << std::endl;
#endif
GraphWeight currMod = -1.0;
int iters = 0;
t1 = omp_get_wtime();
currMod = louvainMethod(*g, currMod, threshold, iters);
tot_time = omp_get_wtime() - t1;
if (!generateGraph) {
std::cout << "-------------------------------------------------------" << std::endl;
std::cout << "Maximum threads: " << max_threads << ", requested threads: " << req_threads << std::endl;
std::cout << "Input file: " << inputFileName << std::endl;
std::cout << "-------------------------------------------------------" << std::endl;
}
std::cout << "-------------------------------------------------------" << std::endl;
#ifdef USE_32_BIT_GRAPH
std::cout << "32-bit datatype" << std::endl;
#else
std::cout << "64-bit datatype" << std::endl;
#endif
std::cout << "-------------------------------------------------------" << std::endl;
std::cout << "Total time (in s): " << tot_time << std::endl;
std::cout << "Modularity, #Iterations: " << currMod << ", " << iters << std::endl;
std::cout << "MODS (final modularity * time): " << (currMod * tot_time) << std::endl;
std::cout << "-------------------------------------------------------" << std::endl;
return 0;
}
void parseCommandLine(const int argc, char * const argv[])
{
int ret;
optind = 1;
bool help_text = false;
if (argc == 1)
{
nvRGG = DEFAULT_NV;
generateGraph = (nvRGG > 0)? true : false;
}
else
{
while ((ret = getopt(argc, argv, "f:n:lt:p:uh")) != -1)
{
switch (ret) {
case 'f':
inputFileName.assign(optarg);
break;
case 'n':
nvRGG = atol(optarg);
if (nvRGG > 0)
generateGraph = true;
break;
case 't':
threshold = atof(optarg);
break;
case 'l':
randomNumberLCG = true;
break;
case 'u':
isUnitEdgeWeight = true;
std::cout << "Warning: graph edge weights will be 1.0." << std::endl;
break;
case 'p':
randomEdgePercent = atof(optarg);
break;
case 'h':
std::cout << "Sample usage [1] (use real-world file): ./louvain_omp [-f /path/to/binary/file.bin] (see README)" << std::endl;
std::cout << "Sample usage [2] (use synthetic graph): ./louvain_omp [-n <#vertices>] [-l] [-p <% extra edges>]" << std::endl;
help_text = true;
break;
default:
std::cout << "Please check the passed options." << std::endl;
break;
}
}
}
if (help_text)
std::exit(EXIT_SUCCESS);
if (!generateGraph && inputFileName.empty())
{
std::cerr << "Must specify a binary file name with -f or provide parameters for generating a graph." << std::endl;
std::abort();
}
if (!generateGraph && randomNumberLCG)
{
std::cerr << "Must specify -n <#vertices> for graph generation using LCG." << std::endl;
std::abort();
}
if (!generateGraph && (randomEdgePercent > 0.0))
{
std::cerr << "Must specify -n <#vertices> for graph generation first to add random edges to it." << std::endl;
std::abort();
}
if (generateGraph && ((randomEdgePercent < 0.0) || (randomEdgePercent >= 100.0)))
{
std::cerr << "Invalid random edge percentage for generated graph!" << std::endl;
std::abort();
}
} // parseCommandLine