-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cpp
262 lines (214 loc) · 6.73 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
#include <sys/resource.h>
#include <sys/time.h>
#include <pthread.h>
#include <unistd.h>
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <mpi.h>
#include <upcxx/upcxx.hpp>
#if defined(USE_UPX_RPC)
#include "maxematch_upx_rpc.hpp"
#else
#define USE_UPX_RMA 1
#include "maxematch_upx.hpp"
#endif
static std::string inputFileName;
static int me, nprocs;
static int ranksPerNode = 1;
static GraphElem nvRGG = 0;
static int generateGraph = 0;
static int randomEdgePercent = 0;
static bool randomNumberLCG = false;
static bool readBalanced = false;
static double threshold = 1.0E-6;
static bool isUnitEdgeWeight = true;
// parse command line parameters
static void parseCommandLine(const int argc, char * const argv[]);
int main(int argc, char *argv[])
{
double t0, t1, td, td0, td1;
#if defined(USE_UPX_RPC) || defined(USE_UPX_RMA)
upcxx::init(); // init UPC++
#endif
// init MPI, if necessary
int mpi_already_init;
MPI_Initialized(&mpi_already_init);
if (!mpi_already_init) MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &me);
MPI_Barrier(MPI_COMM_WORLD);
parseCommandLine(argc, argv);
Graph* g = nullptr;
if (me == 0) {
std::cout << "Using " << nprocs << " ranks" << std::endl;
}
td0 = MPI_Wtime();
if (generateGraph)
{
GenerateRGG gr(nvRGG);
g = gr.generate(randomNumberLCG, isUnitEdgeWeight, randomEdgePercent);
if (me == 0)
{
std::cout << "Generated Random Geometric Graph with d: " << gr.get_d() << std::endl;
const GraphElem nv = g->get_nv();
const GraphElem ne = g->get_ne();
std::cout << "Number of vertices: " << nv << std::endl;
std::cout << "Number of edges: " << ne << std::endl;
//std::cout << "Sparsity: "<< (double)((double)nv / (double)(nvRGG*nvRGG))*100.0 <<"%"<< std::endl;
std::cout << "Average degree: " << (ne / nv) << std::endl;
}
MPI_Barrier(MPI_COMM_WORLD);
}
else
{
BinaryEdgeList rm;
if (readBalanced == true)
g = rm.read_balanced(me, nprocs, ranksPerNode, inputFileName);
else
g = rm.read(me, nprocs, ranksPerNode, inputFileName);
}
g->print_dist_stats();
assert(g != nullptr);
MPI_Barrier(MPI_COMM_WORLD);
#ifdef DEBUG_PRINTF
assert(g);
#endif
td1 = MPI_Wtime();
td = td1 - td0;
double tdt = 0.0;
MPI_Reduce(&td, &tdt, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
if (me == 0)
{
if (!generateGraph)
std::cout << "Time to read input file and create distributed graph (in s): "
<< tdt << std::endl;
else
std::cout << "Time to generate distributed graph of "
<< nvRGG << " vertices (in s): " << tdt << std::endl;
}
// start graph matching
#if defined(USE_UPX_RPC)
if (me == 0)
std::cout << "MPI and UPCXX (RPC): ";
MaxEdgeMatchUPXRPC mt(g);
#else
if (me == 0)
std::cout << "MPI and UPCXX (RMA): ";
MaxEdgeMatchUPX mt(g);
// create dist_object<...> per process
upcxx::global_ptr<GraphElem> arr = upcxx::new_array<GraphElem>(
mt.get_nelems());
assert(arr);
upcxx::dist_object<upcxx::global_ptr<GraphElem>> dobj(arr);
mt.create_upx_ptr(dobj);
#endif
upcxx::barrier();
MPI_Barrier(MPI_COMM_WORLD);
// invoke matching
t0 = MPI_Wtime();
std::vector<EdgeTuple> const& M = mt();
#if defined(USE_UPX_RPC) || defined(USE_UPX_RMA)
upcxx::barrier();
#endif
MPI_Barrier(MPI_COMM_WORLD);
t1 = MPI_Wtime();
double p_tot = t1 - t0, t_tot = 0.0;
MPI_Reduce(&p_tot, &t_tot, 1, MPI_DOUBLE,
MPI_SUM, 0, MPI_COMM_WORLD);
if (me == 0)
std::cout << "Execution time (in s) for maximal edge matching: "
<< (double)(t_tot/(double)nprocs) << std::endl;
#if defined(CHECK_RESULTS)
mt.check_results();
#endif
#if defined(PRINT_RESULTS)
mt.print_M();
#endif
#if defined(USE_UPX_RPC) || defined(USE_UPX_RMA)
upcxx::barrier();
#endif
MPI_Barrier(MPI_COMM_WORLD);
#if defined(USE_UPX_RMA)
mt.destroy_upx_ptr();
upcxx::barrier();
#endif
mt.clear();
#if defined(USE_UPX_RPC) || defined(USE_UPX_RMA)
upcxx::barrier();
#endif
MPI_Barrier(MPI_COMM_WORLD);
// Finalize MPI only if we initted it
if (!mpi_already_init) MPI_Finalize();
#if defined(USE_UPX_RPC) || defined(USE_UPX_RMA)
upcxx::finalize(); // finalize UPC++
#endif
return 0;
}
void parseCommandLine(const int argc, char * const argv[])
{
int ret;
while ((ret = getopt(argc, argv, "f:br:t:n:wlp:")) != -1) {
switch (ret) {
case 'f':
inputFileName.assign(optarg);
break;
case 'b':
readBalanced = true;
break;
case 'r':
ranksPerNode = atoi(optarg);
break;
case 't':
threshold = atof(optarg);
break;
case 'n':
nvRGG = atol(optarg);
if (nvRGG > 0)
generateGraph = true;
break;
case 'w':
isUnitEdgeWeight = false;
break;
case 'l':
randomNumberLCG = true;
break;
case 'p':
randomEdgePercent = atoi(optarg);
break;
default:
assert(0 && "Should not reach here!!");
break;
}
}
if (me == 0 && (argc == 1)) {
std::cerr << "Must specify some options." << std::endl;
MPI_Abort(MPI_COMM_WORLD, -99);
}
if (me == 0 && !generateGraph && inputFileName.empty()) {
std::cerr << "Must specify a binary file name with -f or provide parameters for generating a graph." << std::endl;
MPI_Abort(MPI_COMM_WORLD, -99);
}
if (me == 0 && !generateGraph && randomNumberLCG) {
std::cerr << "Must specify -g for graph generation using LCG." << std::endl;
MPI_Abort(MPI_COMM_WORLD, -99);
}
if (me == 0 && !generateGraph && randomEdgePercent) {
std::cerr << "Must specify -g for graph generation first to add random edges to it." << std::endl;
MPI_Abort(MPI_COMM_WORLD, -99);
}
if (me == 0 && !generateGraph && !isUnitEdgeWeight) {
std::cerr << "Must specify -g for graph generation first before setting edge weights." << std::endl;
MPI_Abort(MPI_COMM_WORLD, -99);
}
if (me == 0 && generateGraph && ((randomEdgePercent < 0) || (randomEdgePercent >= 100))) {
std::cerr << "Invalid random edge percentage for generated graph!" << std::endl;
MPI_Abort(MPI_COMM_WORLD, -99);
}
if (me == 0 && readBalanced && generateGraph) {
std::cout << "Balanced graph distribution is only applicable to real-world graphs, and not applicable to synthetic graphs." << std::endl;
}
} // parseCommandLine