-
Notifications
You must be signed in to change notification settings - Fork 10
/
distgraph.hpp
287 lines (246 loc) · 9.6 KB
/
distgraph.hpp
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
// ***********************************************************************
//
// Vite: A C++ library for distributed-memory graph clustering
// using MPI+OpenMP
//
// Daniel Chavarria ([email protected])
// Antonino Tumeo ([email protected])
// Mahantesh Halappanavar ([email protected])
// Pacific Northwest National Laboratory
//
// Hao Lu ([email protected])
// Sayan Ghosh ([email protected])
// Ananth Kalyanaraman ([email protected])
// Washington State University
//
// ***********************************************************************
//
// Copyright (2017) Battelle Memorial Institute
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// ************************************************************************
#ifndef __DISTGRAPH_H
#define __DISTGRAPH_H
///
#include <cassert>
#include <cmath>
#include <algorithm>
#include "graph.hpp"
// TODO FIXME purge this entire class
// and just have one graph class like
// miniVite
#define PI (3.14159)
#define SR_UP_TAG 100
#define SR_DOWN_TAG 101
#define SR_SIZES_UP_TAG 102
#define SR_SIZES_DOWN_TAG 103
#define SR_X_UP_TAG 104
#define SR_X_DOWN_TAG 105
#define SR_Y_UP_TAG 106
#define SR_Y_DOWN_TAG 107
typedef std::vector<GraphElem> PartRanges;
class DistGraph {
protected:
GraphElem totalNumVertices;
GraphElem totalNumEdges;
Graph *localGraph;
public:
DistGraph(const GraphElem tnv, const GraphElem tne);
DistGraph(const DistGraph &othis);
~DistGraph();
GraphElem getTotalNumVertices() const;
GraphElem getTotalNumEdges() const;
void createLocalGraph(const GraphElem lnv, const GraphElem lne,
const PartRanges *oparts = NULL);
Graph &getLocalGraph();
const Graph &getLocalGraph() const;
GraphElem getBase(const int me) const;
GraphElem getBound(const int me) const;
GraphElem localToGlobal(GraphElem idx, const int me) const;
GraphElem globalToLocal(GraphElem idx, const int me) const;
int getOwner(const GraphElem v) const;
PartRanges *parts;
void setNumEdges(GraphElem numEdges);
void printStats();
GraphElem getNumGhosts(const int me) const;
protected:
DistGraph();
DistGraph &operator = (const DistGraph &othis);
};
void balanceEdges(int nprocs, std::string& fileName, std::vector<GraphElem>& mbins);
void loadDistGraphMPIIO(int me, int nprocs, int ranks_per_node,
DistGraph *&dg, std::string& fileName);
void loadDistGraphMPIIOBalanced(int me, int nprocs, int ranks_per_node,
DistGraph *&dg, std::string& fileName);
// graph generation
void generateInMemGraph(int rank, int nprocs, DistGraph *&dg, GraphElem nv, GraphWeight randomEdgePercent, std::string fileOut);
DistGraph* generateRGG(int rank, int nprocs, GraphElem nv, GraphWeight rn, GraphWeight randomEdgePercent, std::string fileOut);
void writeGraph(int me, int nprocs, DistGraph *&dg, std::vector<GraphElem>& edgeCount, std::string &fileName);
inline DistGraph::DistGraph()
: totalNumVertices(0), totalNumEdges(0), localGraph(NULL), parts(NULL)
{
} // DistGraph
inline DistGraph::DistGraph(const GraphElem tnv, const GraphElem tne)
: totalNumVertices(tnv), totalNumEdges(tne), localGraph(NULL), parts(NULL)
{
} // DistGraph
inline DistGraph::DistGraph(const DistGraph &othis)
: totalNumVertices(othis.totalNumVertices), totalNumEdges(othis.totalNumEdges),
localGraph(new Graph(*othis.localGraph)), parts(NULL)
{ parts = new PartRanges(*othis.parts); } // DistGraph
inline DistGraph::~DistGraph()
{
if (localGraph)
delete localGraph;
delete parts;
} // ~DistGraph
inline GraphElem DistGraph::getTotalNumVertices() const
{ return totalNumVertices; } // getTotalNumVertices
inline GraphElem DistGraph::getTotalNumEdges() const
{ return totalNumEdges; } // getTotalNumEdges
// print statistics about edge distribution
inline void DistGraph::printStats()
{
int me, size;
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &me);
Graph &g = this->getLocalGraph(); // local graph
long lne = (long)g.getNumEdges(); // local #edges
long nv = (long)this->getTotalNumVertices(); // global #vertices
// TODO FIXME currently totalNumEdges variable stores local
// number of edges and not total, keep a separate variable
//const GraphElem ne = this->getTotalNumEdges(); // global #edges
long ne = 0;
MPI_Allreduce(&lne, &ne, 1, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
long sumdeg = 0, maxdeg = 0, mindeg = 0;
MPI_Reduce(&lne, &sumdeg, 1, MPI_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
MPI_Reduce(&lne, &maxdeg, 1, MPI_LONG, MPI_MAX, 0, MPI_COMM_WORLD);
MPI_Reduce(&lne, &mindeg, 1, MPI_LONG, MPI_MIN, 0, MPI_COMM_WORLD);
long my_sq = lne*lne;
long sum_sq = 0;
MPI_Reduce(&my_sq, &sum_sq, 1, MPI_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
double average = (double) sumdeg / size;
double avg_sq = (double) sum_sq / size;
double var = avg_sq - (average*average);
double stddev = sqrt(var);
MPI_Barrier(MPI_COMM_WORLD);
if (me == 0)
{
std::cout << std::endl;
std::cout << "-------------------------------------------------------" << std::endl;
std::cout << "Graph edge distribution characteristics" << std::endl;
std::cout << "-------------------------------------------------------" << std::endl;
std::cout << "Number of vertices: " << nv << std::endl;
std::cout << "Number of edges: " << ne << std::endl;
std::cout << "Maximum number of edges: " << maxdeg << std::endl;
std::cout << "Average number of edges: " << average << std::endl;
std::cout << "Minimum number of edges: " << mindeg << std::endl;
std::cout << "Expected value of X^2: " << avg_sq << std::endl;
std::cout << "Variance: " << var << std::endl;
std::cout << "Standard deviation: " << stddev << std::endl;
std::cout << "-------------------------------------------------------" << std::endl;
}
}
inline void DistGraph::createLocalGraph(const GraphElem lnv, const GraphElem lne,
const PartRanges *oparts)
{
#ifdef DEBUG_PRINTF
assert(!localGraph);
#endif
localGraph = new Graph(lnv, lne);
parts = new PartRanges(*oparts);
} // createLocalGraph
inline Graph &DistGraph::getLocalGraph()
{
#ifdef DEBUG_PRINTF
assert(localGraph);
#endif
return *localGraph;
} // getLocalGraph
inline const Graph &DistGraph::getLocalGraph() const
{
#ifdef DEBUG_PRINTF
assert(localGraph);
#endif
return *localGraph;
} // getLocalGraph
inline GraphElem DistGraph::getBase(const int me) const
{
#ifdef DEBUG_PRINTF
return parts->at(me);
#else
return parts->operator[](me);
#endif
} // getBase
inline GraphElem DistGraph::getBound(const int me) const
{
#ifdef DEBUG_PRINTF
return parts->at(me + 1);
#else
return parts->operator[](me + 1);
#endif
} // getBound
inline GraphElem DistGraph::localToGlobal(GraphElem idx, const int me) const
{
return (idx + getBase(me));
} // localToGlobal
inline GraphElem DistGraph::globalToLocal(GraphElem idx, const int me) const
{
return (idx - getBase(me));
} // globalToLocal
inline void DistGraph::setNumEdges(const GraphElem numEdges)
{ this->totalNumEdges=numEdges; }
inline int DistGraph::getOwner(const GraphElem v) const
{
#ifdef DEBUG_PRINTF
assert((v >= 0) && (v < totalNumVertices));
#endif
const PartRanges::const_iterator iter = std::upper_bound(parts->begin(), parts->end(), v);
#ifdef DEBUG_PRINTF
assert(iter != parts->end());
#endif
return (iter - parts->begin() -1);
} // getOwner
inline GraphElem DistGraph::getNumGhosts(const int me) const
{
const Graph &g = this->getLocalGraph();
GraphElem numGhosts = 0;
for (GraphElem i = 0; i < g.getNumVertices(); i++) {
const GraphElem lb = g.edgeListIndexes[i], ub = g.edgeListIndexes[i + 1];
for (GraphElem j = lb; j < ub; j++) {
const Edge &edge = g.getEdge(j);
if (this->getOwner(edge.tail) != me)
numGhosts += 1;
}
}
return numGhosts;
} // return number of ghost vertices
#endif // __DISTGRAPH_H