Skip to content
Navigation Menu
{{ message }}
forked from GraphChi/graphchi-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_objects.hpp
More file actions
386 lines (301 loc) · 11.3 KB
/
Copy pathgraph_objects.hpp
File metadata and controls
386 lines (301 loc) · 11.3 KB
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
* @file
* @author Aapo Kyrola <akyrola@cs.cmu.edu>
* @version 1.0
*
* @section LICENSE
*
* Copyright [2012] [Aapo Kyrola, Guy Blelloch, Carlos Guestrin / Carnegie Mellon University]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @section DESCRIPTION
*
* Vertex and Edge objects.
*/
#ifndef DEF_GRAPHCHI_OBJECTS
#define DEF_GRAPHCHI_OBJECTS
#include <vector>
#include <assert.h>
#include <omp.h>
#include <string.h>
#include "graphchi_types.hpp"
#include "util/qsort.hpp"
namespace graphchi {
/**
* GNU COMPILER HACK TO PREVENT WARNINGS "Unused variable", if
* the particular app being compiled does not use a function.
*/
#ifdef __GNUC__
#define VARIABLE_IS_NOT_USED __attribute__ ((unused))
#else
#define VARIABLE_IS_NOT_USED
#endif
template <typename EdgeDataType>
class graphchi_edge {
public:
vid_t vertexid; // Source or Target vertex id. Clear from context.
EdgeDataType * data_ptr;
graphchi_edge() {}
graphchi_edge(vid_t _vertexid, EdgeDataType * edata_ptr) : vertexid(_vertexid), data_ptr(edata_ptr) {
}
#ifndef DYNAMICEDATA
EdgeDataType get_data() {
return * data_ptr;
}
void set_data(EdgeDataType x) {
*data_ptr = x;
}
#else
EdgeDataType * get_vector() { // EdgeDataType is a chivector
return data_ptr;
}
#endif
/**
* Returns id of the endpoint of this edge.
*/
vid_t vertex_id() {
return vertexid;
}
} __attribute__((packed));
template <typename ET>
bool eptr_less(const graphchi_edge<ET> &a, const graphchi_edge<ET> &b) {
return a.vertexid < b.vertexid;
}
#ifdef SUPPORT_DELETIONS
/*
* Hacky support for edge deletions.
* Edges are deleted by setting the value of the edge to a special
* value that denotes it was deleted.
* In the future, a better system could be designed.
*/
// This is hacky...
static inline bool VARIABLE_IS_NOT_USED is_deleted_edge_value(int val);
static inline bool VARIABLE_IS_NOT_USED is_deleted_edge_value(bool val) {
return val;
}
static inline bool VARIABLE_IS_NOT_USED is_deleted_edge_value(int val);
static inline bool VARIABLE_IS_NOT_USED is_deleted_edge_value(int val) {
return 0xffffffff == (unsigned int)val;
}
static inline bool VARIABLE_IS_NOT_USED is_deleted_edge_value(vid_t val);
static inline bool VARIABLE_IS_NOT_USED is_deleted_edge_value(vid_t val) {
return 0xffffffffu == val;
}
static inline bool VARIABLE_IS_NOT_USED is_deleted_edge_value(float val);
static inline bool VARIABLE_IS_NOT_USED is_deleted_edge_value(float val) {
return !(val < 0 || val > 0);
}
static void VARIABLE_IS_NOT_USED remove_edgev(graphchi_edge<bool> * e);
static void VARIABLE_IS_NOT_USED remove_edgev(graphchi_edge<bool> * e) {
e->set_data(true);
}
static void VARIABLE_IS_NOT_USED remove_edgev(graphchi_edge<vid_t> * e);
static void VARIABLE_IS_NOT_USED remove_edgev(graphchi_edge<vid_t> * e) {
e->set_data(0xffffffff);
}
static void VARIABLE_IS_NOT_USED remove_edgev(graphchi_edge<int> * e);
static void VARIABLE_IS_NOT_USED remove_edgev(graphchi_edge<int> * e) {
e->set_data(0xffffffff);
}
#endif
template <typename VertexDataType, typename EdgeDataType>
class internal_graphchi_vertex {
public: // Todo, use friend
int inc;
volatile int outc;
vid_t vertexid;
protected:
graphchi_edge<EdgeDataType> * inedges_ptr;
graphchi_edge<EdgeDataType> * outedges_ptr;
public:
bool modified;
VertexDataType * dataptr;
/* Accessed directly by the engine */
bool scheduled;
bool parallel_safe;
#ifdef SUPPORT_DELETIONS
int deleted_inc;
int deleted_outc;
#endif
internal_graphchi_vertex() : inc(0), outc(0) {
#ifdef SUPPORT_DELETIONS
deleted_outc = deleted_inc = 0;
#endif
dataptr = NULL;
}
internal_graphchi_vertex(vid_t _id, graphchi_edge<EdgeDataType> * iptr,
graphchi_edge<EdgeDataType> * optr,
int indeg,
int outdeg) :
vertexid(_id), inedges_ptr(iptr), outedges_ptr(optr) {
inc = 0;
outc = 0;
scheduled = false;
modified = false;
parallel_safe = true;
dataptr = NULL;
#ifdef SUPPORT_DELETIONS
deleted_inc = 0;
deleted_outc = 0;
#endif
}
virtual ~internal_graphchi_vertex() {}
vid_t id() const {
return vertexid;
}
int num_inedges() const {
return inc;
}
int num_outedges() const {
return outc;
}
int num_edges() const {
return inc + outc;
}
// Optimization: as only memshard (not streaming shard) creates inedgers,
// we do not need atomic instructions here!
inline void add_inedge(vid_t src, EdgeDataType * ptr, bool special_edge) {
#ifdef SUPPORT_DELETIONS
if (inedges_ptr != NULL && is_deleted_edge_value(*ptr)) {
deleted_inc++;
return;
}
#endif
if (inedges_ptr != NULL)
inedges_ptr[inc] = graphchi_edge<EdgeDataType>(src, ptr);
inc++; // Note: do not move inside the brackets, since we need to still keep track of inc even if inedgeptr is null!
assert(src != vertexid);
/* if(inedges_ptr != NULL && inc > outedges_ptr - inedges_ptr) {
logstream(LOG_FATAL) << "Tried to add more in-edges as the stored in-degree of this vertex (" << src << "). Perhaps a preprocessing step had failed?" << std::endl;
assert(inc <= outedges_ptr - inedges_ptr);
} */ // Deleted, since does not work when we have separate in-edge and out-edge arrays
}
inline void add_outedge(vid_t dst, EdgeDataType * ptr, bool special_edge) {
#ifdef SUPPORT_DELETIONS
if (outedges_ptr != NULL && is_deleted_edge_value(*ptr)) {
deleted_outc++;
return;
}
#endif
int i = __sync_add_and_fetch(&outc, 1);
if (outedges_ptr != NULL) outedges_ptr[i-1] = graphchi_edge<EdgeDataType>(dst, ptr);
assert(dst != vertexid);
}
};
template <typename VertexDataType, typename EdgeDataType >
class graphchi_vertex : public internal_graphchi_vertex<VertexDataType, EdgeDataType> {
public:
graphchi_vertex() : internal_graphchi_vertex<VertexDataType, EdgeDataType>() { }
graphchi_vertex(vid_t _id,
graphchi_edge<EdgeDataType> * iptr,
graphchi_edge<EdgeDataType> * optr,
int indeg,
int outdeg) :
internal_graphchi_vertex<VertexDataType, EdgeDataType>(_id, iptr, optr, indeg, outdeg) {}
virtual ~graphchi_vertex() {}
/**
* Returns ith edge of a vertex, ignoring
* edge direction.
*/
graphchi_edge<EdgeDataType> * edge(int i) {
if (i < this->inc) return inedge(i);
else return outedge(i - this->inc);
}
graphchi_edge<EdgeDataType> * inedge(int i) {
assert(i >= 0 && i < this->inc);
return &this->inedges_ptr[i];
}
graphchi_edge<EdgeDataType> * outedge(int i) {
assert(i >= 0 && i < this->outc);
return &this->outedges_ptr[i];
}
graphchi_edge<EdgeDataType> * random_outedge() {
if (this->outc == 0) return NULL;
return outedge((int) (std::abs(random()) % this->outc));
}
/**
* Get the value of vertex
*/
#ifndef DYNAMICVERTEXDATA
VertexDataType get_data() {
return *(this->dataptr);
}
#else
// VertexDataType must be a chivector
VertexDataType * get_vector() {
this->modified = true; // Assume vector always modified... Temporaryh solution.
return this->dataptr;
}
#endif
/**
* Modify the vertex value. The new value will be
* stored on disk.
*/
virtual void set_data(VertexDataType d) {
*(this->dataptr) = d;
this->modified = true;
}
// TODO: rethink
static bool computational_edges() {
return false;
}
static bool read_outedges() {
return true;
}
/**
* Sorts all the edges. Note: this will destroy information
* about the in/out direction of an edge. Do use only if you
* ignore the edge direction.
*/
void VARIABLE_IS_NOT_USED sort_edges_indirect() {
// Check for deleted edges first...
if (this->inc != (this->outedges_ptr - this->inedges_ptr)) {
// Moving
memmove(&this->inedges_ptr[this->inc], this->outedges_ptr, this->outc * sizeof(graphchi_edge<EdgeDataType>));
this->outedges_ptr = &this->inedges_ptr[this->inc];
}
quickSort(this->inedges_ptr, (int) (this->inc + this->outc), eptr_less<EdgeDataType>);
}
#ifdef SUPPORT_DELETIONS
void VARIABLE_IS_NOT_USED remove_edge(int i) {
remove_edgev(edge(i));
}
void VARIABLE_IS_NOT_USED remove_inedge(int i) {
remove_edgev(inedge(i));
}
void VARIABLE_IS_NOT_USED remove_outedge(int i) {
remove_edgev(outedge(i));
}
#endif
};
/**
* Experimental code
*/
// If highest order bit is set, the edge is "special". This is used
// to indicate - in the neighborhood model - that neighbor's value is
// cached in memory.
#define HIGHMASK (1 + (2147483647 >> 1))
#define CLEARMASK (2147483647 >> 1)
inline vid_t translate_edge(vid_t rawid, bool &is_special) {
is_special = (rawid & HIGHMASK) != 0;
return rawid & CLEARMASK;
}
inline vid_t make_special(vid_t rawid) {
return rawid | HIGHMASK;
}
inline bool is_special(vid_t rawid) {
return (rawid & HIGHMASK) != 0;
}
} // Namespace
#endif
You can’t perform that action at this time.
