-
Notifications
You must be signed in to change notification settings - Fork 310
/
katz_centrality.cu
346 lines (307 loc) · 14.8 KB
/
katz_centrality.cu
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
/*
* Copyright (c) 2020-2021, NVIDIA CORPORATION.
*
* 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.
*/
#include <cugraph/algorithms.hpp>
#include <cugraph/experimental/graph_view.hpp>
#include <cugraph/prims/copy_to_adj_matrix_row_col.cuh>
#include <cugraph/prims/copy_v_transform_reduce_in_out_nbr.cuh>
#include <cugraph/prims/count_if_v.cuh>
#include <cugraph/prims/transform_reduce_v.cuh>
#include <cugraph/utilities/error.hpp>
#include <rmm/thrust_rmm_allocator.h>
#include <raft/handle.hpp>
#include <thrust/fill.h>
#include <thrust/iterator/constant_iterator.h>
#include <thrust/iterator/zip_iterator.h>
#include <thrust/transform.h>
#include <thrust/tuple.h>
namespace cugraph {
namespace experimental {
namespace detail {
template <typename GraphViewType, typename result_t>
void katz_centrality(raft::handle_t const& handle,
GraphViewType const& pull_graph_view,
result_t const* betas,
result_t* katz_centralities,
result_t alpha,
result_t beta, // relevant only if betas == nullptr
result_t epsilon,
size_t max_iterations,
bool has_initial_guess,
bool normalize,
bool do_expensive_check)
{
using vertex_t = typename GraphViewType::vertex_type;
using weight_t = typename GraphViewType::weight_type;
static_assert(std::is_integral<vertex_t>::value,
"GraphViewType::vertex_type should be integral.");
static_assert(std::is_floating_point<result_t>::value,
"result_t should be a floating-point type.");
static_assert(GraphViewType::is_adj_matrix_transposed,
"GraphViewType should support the pull model.");
auto const num_vertices = pull_graph_view.get_number_of_vertices();
if (num_vertices == 0) { return; }
// 1. check input arguments
CUGRAPH_EXPECTS((alpha >= 0.0) && (alpha <= 1.0),
"Invalid input argument: alpha should be in [0.0, 1.0].");
CUGRAPH_EXPECTS(epsilon >= 0.0, "Invalid input argument: epsilon should be non-negative.");
if (do_expensive_check) {
// FIXME: should I check for betas?
if (has_initial_guess) {
auto num_negative_values = count_if_v(
handle, pull_graph_view, katz_centralities, [] __device__(auto val) { return val < 0.0; });
CUGRAPH_EXPECTS(num_negative_values == 0,
"Invalid input argument: initial guess values should be non-negative.");
}
}
// 2. initialize katz centrality values
if (!has_initial_guess) {
thrust::fill(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()),
katz_centralities,
katz_centralities + pull_graph_view.get_number_of_local_vertices(),
result_t{0.0});
}
// 3. katz centrality iteration
// old katz centrality values
rmm::device_uvector<result_t> tmp_katz_centralities(
pull_graph_view.get_number_of_local_vertices(), handle.get_stream());
rmm::device_uvector<result_t> adj_matrix_row_katz_centralities(
pull_graph_view.get_number_of_local_adj_matrix_partition_rows(), handle.get_stream());
auto new_katz_centralities = katz_centralities;
auto old_katz_centralities = tmp_katz_centralities.data();
size_t iter{0};
while (true) {
std::swap(new_katz_centralities, old_katz_centralities);
copy_to_adj_matrix_row(
handle, pull_graph_view, old_katz_centralities, adj_matrix_row_katz_centralities.begin());
copy_v_transform_reduce_in_nbr(
handle,
pull_graph_view,
adj_matrix_row_katz_centralities.begin(),
thrust::make_constant_iterator(0) /* dummy */,
[alpha] __device__(vertex_t src, vertex_t dst, weight_t w, auto src_val, auto dst_val) {
return static_cast<result_t>(alpha * src_val * w);
},
betas != nullptr ? result_t{0.0} : beta,
new_katz_centralities);
if (betas != nullptr) {
auto val_first = thrust::make_zip_iterator(thrust::make_tuple(new_katz_centralities, betas));
thrust::transform(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()),
val_first,
val_first + pull_graph_view.get_number_of_local_vertices(),
new_katz_centralities,
[] __device__(auto val) {
auto const katz_centrality = thrust::get<0>(val);
auto const beta = thrust::get<1>(val);
return katz_centrality + beta;
});
}
auto diff_sum = transform_reduce_v(
handle,
pull_graph_view,
thrust::make_zip_iterator(thrust::make_tuple(new_katz_centralities, old_katz_centralities)),
[] __device__(auto val) { return std::abs(thrust::get<0>(val) - thrust::get<1>(val)); },
result_t{0.0});
iter++;
if (diff_sum < epsilon) {
break;
} else if (iter >= max_iterations) {
CUGRAPH_FAIL("Katz Centrality failed to converge.");
}
}
if (new_katz_centralities != katz_centralities) {
thrust::copy(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()),
new_katz_centralities,
new_katz_centralities + pull_graph_view.get_number_of_local_vertices(),
katz_centralities);
}
if (normalize) {
auto l2_norm = transform_reduce_v(
handle,
pull_graph_view,
katz_centralities,
[] __device__(auto val) { return val * val; },
result_t{0.0});
l2_norm = std::sqrt(l2_norm);
CUGRAPH_EXPECTS(l2_norm > 0.0,
"L2 norm of the computed Katz Centrality values should be positive.");
thrust::transform(rmm::exec_policy(handle.get_stream())->on(handle.get_stream()),
katz_centralities,
katz_centralities + pull_graph_view.get_number_of_local_vertices(),
katz_centralities,
[l2_norm] __device__(auto val) { return val / l2_norm; });
}
}
} // namespace detail
template <typename vertex_t, typename edge_t, typename weight_t, typename result_t, bool multi_gpu>
void katz_centrality(raft::handle_t const& handle,
graph_view_t<vertex_t, edge_t, weight_t, true, multi_gpu> const& graph_view,
result_t const* betas,
result_t* katz_centralities,
result_t alpha,
result_t beta, // relevant only if beta == nullptr
result_t epsilon,
size_t max_iterations,
bool has_initial_guess,
bool normalize,
bool do_expensive_check)
{
detail::katz_centrality(handle,
graph_view,
betas,
katz_centralities,
alpha,
beta,
epsilon,
max_iterations,
has_initial_guess,
normalize,
do_expensive_check);
}
// explicit instantiation
template void katz_centrality(raft::handle_t const& handle,
graph_view_t<int32_t, int32_t, float, true, true> const& graph_view,
float const* betas,
float* katz_centralities,
float alpha,
float beta,
float epsilon,
size_t max_iterations,
bool has_initial_guess,
bool normalize,
bool do_expensive_check);
template void katz_centrality(raft::handle_t const& handle,
graph_view_t<int32_t, int32_t, double, true, true> const& graph_view,
double const* betas,
double* katz_centralities,
double alpha,
double beta,
double epsilon,
size_t max_iterations,
bool has_initial_guess,
bool normalize,
bool do_expensive_check);
template void katz_centrality(raft::handle_t const& handle,
graph_view_t<int32_t, int64_t, float, true, true> const& graph_view,
float const* betas,
float* katz_centralities,
float alpha,
float beta,
float epsilon,
size_t max_iterations,
bool has_initial_guess,
bool normalize,
bool do_expensive_check);
template void katz_centrality(raft::handle_t const& handle,
graph_view_t<int32_t, int64_t, double, true, true> const& graph_view,
double const* betas,
double* katz_centralities,
double alpha,
double beta,
double epsilon,
size_t max_iterations,
bool has_initial_guess,
bool normalize,
bool do_expensive_check);
template void katz_centrality(raft::handle_t const& handle,
graph_view_t<int64_t, int64_t, float, true, true> const& graph_view,
float const* betas,
float* katz_centralities,
float alpha,
float beta,
float epsilon,
size_t max_iterations,
bool has_initial_guess,
bool normalize,
bool do_expensive_check);
template void katz_centrality(raft::handle_t const& handle,
graph_view_t<int64_t, int64_t, double, true, true> const& graph_view,
double const* betas,
double* katz_centralities,
double alpha,
double beta,
double epsilon,
size_t max_iterations,
bool has_initial_guess,
bool normalize,
bool do_expensive_check);
template void katz_centrality(raft::handle_t const& handle,
graph_view_t<int32_t, int32_t, float, true, false> const& graph_view,
float const* betas,
float* katz_centralities,
float alpha,
float beta,
float epsilon,
size_t max_iterations,
bool has_initial_guess,
bool normalize,
bool do_expensive_check);
template void katz_centrality(raft::handle_t const& handle,
graph_view_t<int32_t, int32_t, double, true, false> const& graph_view,
double const* betas,
double* katz_centralities,
double alpha,
double beta,
double epsilon,
size_t max_iterations,
bool has_initial_guess,
bool normalize,
bool do_expensive_check);
template void katz_centrality(raft::handle_t const& handle,
graph_view_t<int32_t, int64_t, float, true, false> const& graph_view,
float const* betas,
float* katz_centralities,
float alpha,
float beta,
float epsilon,
size_t max_iterations,
bool has_initial_guess,
bool normalize,
bool do_expensive_check);
template void katz_centrality(raft::handle_t const& handle,
graph_view_t<int32_t, int64_t, double, true, false> const& graph_view,
double const* betas,
double* katz_centralities,
double alpha,
double beta,
double epsilon,
size_t max_iterations,
bool has_initial_guess,
bool normalize,
bool do_expensive_check);
template void katz_centrality(raft::handle_t const& handle,
graph_view_t<int64_t, int64_t, float, true, false> const& graph_view,
float const* betas,
float* katz_centralities,
float alpha,
float beta,
float epsilon,
size_t max_iterations,
bool has_initial_guess,
bool normalize,
bool do_expensive_check);
template void katz_centrality(raft::handle_t const& handle,
graph_view_t<int64_t, int64_t, double, true, false> const& graph_view,
double const* betas,
double* katz_centralities,
double alpha,
double beta,
double epsilon,
size_t max_iterations,
bool has_initial_guess,
bool normalize,
bool do_expensive_check);
} // namespace experimental
} // namespace cugraph