-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstinger_algorithm.cpp
More file actions
227 lines (208 loc) · 7.18 KB
/
stinger_algorithm.cpp
File metadata and controls
227 lines (208 loc) · 7.18 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
#include "stinger_algorithm.h"
#include <vector>
#include <iostream>
#include <memory>
#include <string>
#include <sstream>
#include <stdint.h>
#include <cmath>
#include <algorithm>
extern "C" {
#include <stinger_core/stinger.h>
#include <stinger_alg/bfs.h>
#include <stinger_alg/betweenness.h>
#include <stinger_alg/clustering.h>
#include <stinger_alg/static_components.h>
#include <stinger_alg/kcore.h>
#include <stinger_alg/pagerank.h>
}
#include <stinger_alg/streaming_algorithm.h>
#include <stinger_alg/dynamic_betweenness.h>
#include <stinger_alg/dynamic_bfs.h>
#include <stinger_alg/dynamic_static_components.h>
#include <stinger_alg/dynamic_clustering.h>
#include <stinger_alg/dynamic_kcore.h>
#include <stinger_alg/dynamic_pagerank.h>
#include <stinger_alg/dynamic_pagerank_updating.h>
#include <stinger_alg/dynamic_simple_communities.h>
#include <stinger_alg/dynamic_simple_communities_updating.h>
#include <stinger_alg/dynamic_streaming_connected_components.h>
#include <stinger_net/stinger_alg.h>
using std::cerr;
using std::shared_ptr;
using std::make_shared;
using std::string;
using std::stringstream;
using std::vector;
using namespace gt::stinger;
// Implementation of StingerAlgorithm
const vector<string>
StingerAlgorithm::supported_algs = {
"bc",
"bfs",
"cc",
"clustering",
"simple_communities",
"simple_communities_updating",
"streaming_cc",
"kcore",
"pagerank",
"pagerank_updating"
};
shared_ptr<IDynamicGraphAlgorithm> createImplementation(string name)
{
if (name == "bc") {
return make_shared<BetweennessCentrality>(128, 0.5, 1);
} else if (name == "bfs") {
return make_shared<BreadthFirstSearch>();
} else if (name == "cc") {
return make_shared<ConnectedComponents>();
} else if (name == "clustering") {
return make_shared<ClusteringCoefficients>();
} else if (name == "simple_communities") {
return make_shared<SimpleCommunities>();
} else if (name == "simple_communities_updating") {
return make_shared<SimpleCommunitiesUpdating>(false);
} else if (name == "streaming_cc") {
return make_shared<StreamingConnectedComponents>();
} else if (name == "kcore") {
return make_shared<KCore>();
} else if (name == "pagerank") {
return make_shared<PageRank>("", false, true, EPSILON_DEFAULT, DAMPINGFACTOR_DEFAULT, MAXITER_DEFAULT);
} else if (name == "pagerank_updating") {
return make_shared<PageRankUpdating>("dprheld", 0.85, 0, 1.0);
} else {
cerr << "Algorithm " << name << " not implemented!\n";
exit(-1);
}
}
StingerAlgorithm::StingerAlgorithm(stinger_t * S, string name) :
// Save the name
name(name),
// Create the implementation
impl(createImplementation(name)),
// Zero-initialize the server data
server_data{},
// Allocate data for the algorithm
alg_data(impl->getDataPerVertex() * S->max_nv)
{
// Initialize the "server" data about this algorithm
strcpy(server_data.alg_name, impl->getName().c_str());
server_data.stinger = S;
server_data.alg_data_per_vertex = impl->getDataPerVertex();
server_data.alg_data = alg_data.data();
server_data.enabled = true;
}
void
StingerAlgorithm::observeInsertions(vector<stinger_edge_update> &recentInsertions)
{
server_data.num_insertions = recentInsertions.size();
server_data.insertions = recentInsertions.data();
}
void
StingerAlgorithm::observeDeletions(vector<stinger_edge_update> &recentDeletions)
{
server_data.num_deletions = recentDeletions.size();
server_data.deletions = recentDeletions.data();
}
void
StingerAlgorithm::observeVertexCount(int64_t nv)
{
server_data.max_active_vertex = nv;
}
void
StingerAlgorithm::setSources(const vector<int64_t> &sources)
{
if (auto b = std::dynamic_pointer_cast<BreadthFirstSearch>(impl)) {
b->setSources(sources);
} else if (auto b = std::dynamic_pointer_cast<BetweennessCentrality>(impl)) {
b->setSources(sources);
}
}
void
StingerAlgorithm::onInit(){ impl->onInit(&server_data); }
void
StingerAlgorithm::onPre(){ impl->onPre(&server_data); }
void
StingerAlgorithm::onPost(){ impl->onPost(&server_data); }
// Helper functions to split strings
// http://stackoverflow.com/a/236803/1877086
static void split(const string &s, char delim, vector<string> &elems) {
stringstream ss(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
}
static vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
}
int64_t *
StingerAlgorithm::get_data_ptr()
{
// Some algorithms provide multiple result arrays
// Decide which one we want
string desc;
if (name == "bc") { desc = "bc";
} else if (name == "bfs") { desc = "level";
} else if (name == "cc") { desc = "component_label";
} else if (name == "clustering") { desc = "coeff";
} else if (name == "simple_communities") { desc = "community_label";
} else if (name == "simple_communities_updating") { desc = "community_label";
} else if (name == "streaming_cc") { desc = "component_label";
} else if (name == "kcore") { desc = "kcore";
} else if (name == "pagerank") { desc = "pagerank";
} else if (name == "pagerank_updating") { desc = "dprheld";
} else {
cerr << "Algorithm " << name << " not implemented!\n";
exit(-1);
}
// Find the position of the result array we want within alg_data
// Example: Suppose data description is equal to "ll foo bar", and we want data for "bar"
// We find the index of "bar" to be 3
// We skip past the elements of "foo" (max_nv * sizeof(l))
// Then return the pointer
int64_t max_nv = server_data.stinger->max_nv;
auto tokens = split(impl->getDataDescription(), ' ');
auto pos = std::find(tokens.begin(), tokens.end(), desc);
if (pos == tokens.end()) { return nullptr; }
// Index of the character that describes the length of our data
size_t loc = pos - tokens.begin() - 1;
string spec = tokens[0];
// This function expects an array of 64-bit type, can't handle smaller types yet
assert(spec[loc] == 'l' || spec[loc] == 'd');
uint8_t * data = alg_data.data();
for (int i = 0; i < loc; ++i)
{
switch (spec[i]) {
case 'f': { data += (sizeof(float) * max_nv); } break;
case 'd': { data += (sizeof(double) * max_nv); } break;
case 'i': { data += (sizeof(int32_t) * max_nv); } break;
case 'l': { data += (sizeof(int64_t) * max_nv); } break;
case 'b': { data += (sizeof(uint8_t) * max_nv); } break;
}
}
return reinterpret_cast<int64_t*>(data);
}
void
StingerAlgorithm::setData(const DynoGraph::Range<int64_t> &data)
{
uint64_t max_nv = server_data.stinger->max_nv;
int64_t * ptr = get_data_ptr();
#pragma omp parallel for
for (uint64_t i = 0; i < max_nv; ++i) {
ptr[i] = data[i];
}
}
void
StingerAlgorithm::getData(DynoGraph::Range<int64_t> &data)
{
uint64_t max_nv = server_data.stinger->max_nv;
const int64_t * ptr = get_data_ptr();
#pragma omp parallel for
for (uint64_t i = 0; i < max_nv; ++i) {
data[i] = ptr[i];
}
}