FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
graphchi-cpp/example_apps/connectedcomponents.cpp at master · GraphChi/graphchi-cpp · GitHub
GraphChi
graphchi-cpp
Repository navigation
Code
Issues
25
(25)
Pull requests
8
(8)
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
graphchi-cpp
/
example_apps
/
connectedcomponents.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
189 lines (158 loc) · 6.51 KB
Breadcrumbs
graphchi-cpp
/
example_apps
/
connectedcomponents.cpp
Copy path
File metadata and controls
189 lines (158 loc) · 6.51 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
*
* @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
*
* Application for computing the connected components of a graph.
* The algorithm is simple: on first iteration each vertex sends its
* id to neighboring vertices. On subsequent iterations, each vertex chooses
* the smallest id of its neighbors and broadcasts its (new) label to
* its neighbors. The algorithm terminates when no vertex changes label.
*
* @section REMARKS
*
* This application is interesting demonstration of the asyncronous capabilities
* of GraphChi, improving the convergence considerably. Consider
* a chain graph 0->1->2->...->n. First, vertex 0 will write its value to its edges,
* which will be observed by vertex 1 immediatelly, changing its label to 0. Nexgt,
* vertex 2 changes its value to 0, and so on. This all happens in one iteration.
* A subtle issue is that as any pair of vertices a<->b share an edge, they will
* overwrite each others value. However, because they will be never run in parallel
* (due to deterministic parallellism of graphchi), this does not compromise correctness.
*
* @author Aapo Kyrola
*/
#
include
<
cmath
>
#
include
<
string
>
#
include
"
graphchi_basic_includes.hpp
"
#
include
"
util/labelanalysis.hpp
"
using
namespace
graphchi
;
int
iterationcount =
0
;
bool
scheduler =
false
;
/*
*
* Type definitions. Remember to create suitable graph shards using the
* Sharder-program.
*/
typedef
vid_t
VertexDataType;
//
vid_t is the vertex id type
typedef
vid_t
EdgeDataType;
/*
*
* GraphChi programs need to subclass GraphChiProgram<vertex-type, edge-type>
* class. The main logic is usually in the update function.
*/
struct
ConnectedComponentsProgram
:
public
GraphChiProgram
<VertexDataType, EdgeDataType> {
bool
converged;
/*
*
* Vertex update function.
* On first iteration ,each vertex chooses a label = the vertex id.
* On subsequent iterations, each vertex chooses the minimum of the neighbor's
* label (and itself).
*/
void
update
(graphchi_vertex<VertexDataType, EdgeDataType> &vertex, graphchi_context &gcontext) {
if
(scheduler) gcontext.
scheduler
->
remove_tasks
(vertex.
id
(), vertex.
id
());
if
(gcontext.
iteration
==
0
) {
vertex.
set_data
(vertex.
id
());
if
(scheduler) gcontext.
scheduler
->
add_task
(vertex.
id
());
}
/*
On subsequent iterations, find the minimum label of my neighbors
*/
vid_t
curmin = vertex.
get_data
();
for
(
int
i=
0
; i < vertex.
num_edges
(); i++) {
vid_t
nblabel = vertex.
edge
(i)->
get_data
();
if
(gcontext.
iteration
==
0
) nblabel = vertex.
edge
(i)->
vertex_id
();
//
Note!
curmin =
std::min
(nblabel, curmin);
}
/*
Set my label
*/
vertex.
set_data
(curmin);
/*
*
* Broadcast new label to neighbors by writing the value
* to the incident edges.
* Note: on first iteration, write only to out-edges to avoid
* overwriting data (this is kind of a subtle point)
*/
vid_t
label = vertex.
get_data
();
if
(gcontext.
iteration
>
0
) {
for
(
int
i=
0
; i < vertex.
num_edges
(); i++) {
if
(label < vertex.
edge
(i)->
get_data
()) {
vertex.
edge
(i)->
set_data
(label);
/*
Schedule neighbor for update
*/
if
(scheduler) gcontext.
scheduler
->
add_task
(vertex.
edge
(i)->
vertex_id
(),
true
);
converged =
false
;
}
}
}
else
if
(gcontext.
iteration
==
0
) {
for
(
int
i=
0
; i < vertex.
num_outedges
(); i++) {
vertex.
outedge
(i)->
set_data
(label);
}
}
}
/*
*
* Called before an iteration starts.
*/
void
before_iteration
(
int
iteration, graphchi_context &info) {
iterationcount++;
converged = iteration >
0
;
}
/*
*
* Called after an iteration has finished.
*/
void
after_iteration
(
int
iteration, graphchi_context &ginfo) {
if
(converged) {
std::cout <<
"
Converged!
"
<< std::endl;
ginfo.
set_last_iteration
(iteration);
}
}
/*
*
* Called before an execution interval is started.
*/
void
before_exec_interval
(
vid_t
window_st,
vid_t
window_en, graphchi_context &ginfo) {
}
/*
*
* Called after an execution interval has finished.
*/
void
after_exec_interval
(
vid_t
window_st,
vid_t
window_en, graphchi_context &ginfo) {
}
};
int
main
(
int
argc,
const
char
** argv) {
/*
GraphChi initialization will read the command line
arguments and the configuration file.
*/
graphchi_init
(argc, argv);
/*
Metrics object for keeping track of performance counters
and other information. Currently required.
*/
metrics
m
(
"
connected-components
"
);
/*
Basic arguments for application
*/
std::string filename =
get_option_string
(
"
file
"
);
//
Base filename
int
niters =
get_option_int
(
"
niters
"
,
1000
);
//
Number of iterations (max)
scheduler =
get_option_int
(
"
scheduler
"
,
false
);
/*
Process input file - if not already preprocessed
*/
int
nshards = (
int
) convert_if_notexists<EdgeDataType>(filename,
get_option_string
(
"
nshards
"
,
"
auto
"
));
if
(
get_option_int
(
"
onlyresult
"
,
0
) ==
0
) {
/*
Run
*/
ConnectedComponentsProgram program;
graphchi_engine<VertexDataType, EdgeDataType>
engine
(filename, nshards, scheduler, m);
engine.
run
(program, niters);
}
/*
Run analysis of the connected components (output is written to a file)
*/
m.
start_time
(
"
label-analysis
"
);
analyze_labels<
vid_t
>(filename);
m.
stop_time
(
"
label-analysis
"
);
/*
Report execution metrics
*/
metrics_report
(m);
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL