-
Notifications
You must be signed in to change notification settings - Fork 304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement eigenvector centrality #2287
Merged
rapids-bot
merged 7 commits into
rapidsai:branch-22.06
from
ChuckHastings:fea_implement_eigenvector_centrality
May 20, 2022
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f438a37
add eigenvector centrality implementation
ChuckHastings 39e4126
Merge branch 'branch-22.06' into fea_implement_eigenvector_centrality
ChuckHastings 9676970
Change eigenvector API to be more functional
ChuckHastings 6a50ee2
update C API tests with new C++ API for eigenvector centrality
ChuckHastings 7ed2c4b
fix clang-format issues
ChuckHastings a9a2579
fix clang-format issues
ChuckHastings f86f15e
delete unnecessary include
ChuckHastings File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ __pycache__ | |
.lock | ||
*.swp | ||
*.pytest_cache | ||
*~ | ||
DartConfiguration.tcl | ||
.DS_Store | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
/* | ||
* Copyright (c) 2022, 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. | ||
*/ | ||
#pragma once | ||
|
||
#include <cugraph/algorithms.hpp> | ||
#include <cugraph/detail/utility_wrappers.hpp> | ||
#include <cugraph/graph_view.hpp> | ||
#include <cugraph/prims/count_if_e.cuh> | ||
#include <cugraph/prims/count_if_v.cuh> | ||
#include <cugraph/prims/edge_partition_src_dst_property.cuh> | ||
#include <cugraph/prims/per_v_transform_reduce_incoming_outgoing_e.cuh> | ||
#include <cugraph/prims/reduce_v.cuh> | ||
#include <cugraph/prims/transform_reduce_v.cuh> | ||
#include <cugraph/prims/update_edge_partition_src_dst_property.cuh> | ||
#include <cugraph/utilities/error.hpp> | ||
|
||
#include <raft/handle.hpp> | ||
#include <rmm/exec_policy.hpp> | ||
|
||
#include <thrust/fill.h> | ||
#include <thrust/for_each.h> | ||
#include <thrust/iterator/constant_iterator.h> | ||
#include <thrust/iterator/zip_iterator.h> | ||
#include <thrust/transform.h> | ||
#include <thrust/tuple.h> | ||
|
||
namespace cugraph { | ||
namespace detail { | ||
|
||
template <typename vertex_t, typename edge_t, typename weight_t, bool multi_gpu> | ||
rmm::device_uvector<weight_t> eigenvector_centrality( | ||
raft::handle_t const& handle, | ||
graph_view_t<vertex_t, edge_t, weight_t, true, multi_gpu> const& pull_graph_view, | ||
std::optional<raft::device_span<weight_t const>> initial_centralities, | ||
weight_t epsilon, | ||
size_t max_iterations, | ||
bool do_expensive_check) | ||
{ | ||
using GraphViewType = graph_view_t<vertex_t, edge_t, weight_t, true, multi_gpu>; | ||
auto const num_vertices = pull_graph_view.number_of_vertices(); | ||
if (num_vertices == 0) { return rmm::device_uvector<weight_t>(0, handle.get_stream()); } | ||
|
||
if (do_expensive_check) { | ||
if (pull_graph_view.is_weighted()) { | ||
auto num_nonpositive_edge_weights = | ||
count_if_e(handle, | ||
pull_graph_view, | ||
dummy_property_t<vertex_t>{}.device_view(), | ||
dummy_property_t<vertex_t>{}.device_view(), | ||
[] __device__(vertex_t, vertex_t, weight_t w, auto, auto) { return w <= 0.0; }); | ||
CUGRAPH_EXPECTS(num_nonpositive_edge_weights == 0, | ||
"Invalid input argument: input graph should have postive edge weights."); | ||
} | ||
} | ||
|
||
rmm::device_uvector<weight_t> centralities(pull_graph_view.local_vertex_partition_range_size(), | ||
handle.get_stream()); | ||
if (initial_centralities) { | ||
thrust::copy(handle.get_thrust_policy(), | ||
initial_centralities->begin(), | ||
initial_centralities->end(), | ||
centralities.begin()); | ||
} else { | ||
thrust::fill(handle.get_thrust_policy(), | ||
centralities.begin(), | ||
centralities.end(), | ||
weight_t{1.0} / static_cast<weight_t>(num_vertices)); | ||
} | ||
|
||
// Power iteration | ||
rmm::device_uvector<weight_t> old_centralities(centralities.size(), handle.get_stream()); | ||
|
||
edge_partition_src_property_t<GraphViewType, weight_t> edge_partition_src_centralities( | ||
handle, pull_graph_view); | ||
|
||
size_t iter{0}; | ||
while (true) { | ||
thrust::copy(handle.get_thrust_policy(), | ||
centralities.begin(), | ||
centralities.end(), | ||
old_centralities.data()); | ||
|
||
update_edge_partition_src_property( | ||
handle, pull_graph_view, centralities.begin(), edge_partition_src_centralities); | ||
|
||
per_v_transform_reduce_incoming_e( | ||
handle, | ||
pull_graph_view, | ||
edge_partition_src_centralities.device_view(), | ||
dummy_property_t<vertex_t>{}.device_view(), | ||
[] __device__(vertex_t, vertex_t, weight_t w, auto src_val, auto) { return src_val * w; }, | ||
weight_t{0}, | ||
centralities.begin()); | ||
|
||
// Normalize the centralities | ||
auto hypotenuse = sqrt(transform_reduce_v( | ||
handle, | ||
pull_graph_view, | ||
centralities.begin(), | ||
[] __device__(auto, auto val) { return val * val; }, | ||
weight_t{0.0})); | ||
|
||
thrust::transform(handle.get_thrust_policy(), | ||
centralities.begin(), | ||
centralities.end(), | ||
centralities.begin(), | ||
[hypotenuse] __device__(auto val) { return val / hypotenuse; }); | ||
|
||
auto diff_sum = transform_reduce_v( | ||
handle, | ||
pull_graph_view, | ||
thrust::make_zip_iterator(thrust::make_tuple(centralities.begin(), old_centralities.data())), | ||
[] __device__(auto, auto val) { return std::abs(thrust::get<0>(val) - thrust::get<1>(val)); }, | ||
weight_t{0.0}); | ||
|
||
iter++; | ||
|
||
if (diff_sum < (pull_graph_view.number_of_vertices() * epsilon)) { | ||
break; | ||
} else if (iter >= max_iterations) { | ||
CUGRAPH_FAIL("Eigenvector Centrality failed to converge."); | ||
} | ||
} | ||
|
||
return centralities; | ||
} | ||
|
||
} // namespace detail | ||
|
||
template <typename vertex_t, typename edge_t, typename weight_t, bool multi_gpu> | ||
rmm::device_uvector<weight_t> eigenvector_centrality( | ||
raft::handle_t const& handle, | ||
graph_view_t<vertex_t, edge_t, weight_t, true, multi_gpu> const& graph_view, | ||
std::optional<raft::device_span<weight_t const>> initial_centralities, | ||
weight_t epsilon, | ||
size_t max_iterations, | ||
bool do_expensive_check) | ||
{ | ||
static_assert(std::is_integral<vertex_t>::value, | ||
"GraphViewType::vertex_type should be integral."); | ||
static_assert(std::is_floating_point<weight_t>::value, | ||
"weight_t should be a floating-point type."); | ||
|
||
CUGRAPH_EXPECTS(epsilon >= 0.0, "Invalid input argument: epsilon should be non-negative."); | ||
if (initial_centralities) | ||
CUGRAPH_EXPECTS(initial_centralities->size() == | ||
static_cast<size_t>(graph_view.local_vertex_partition_range_size()), | ||
"Centralities should be same size as vertex range"); | ||
|
||
return detail::eigenvector_centrality( | ||
handle, graph_view, initial_centralities, epsilon, max_iterations, do_expensive_check); | ||
} | ||
|
||
} // namespace cugraph |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright (c) 2022, 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 <centrality/eigenvector_centrality_impl.cuh> | ||
|
||
namespace cugraph { | ||
|
||
// MG instantiation | ||
template rmm::device_uvector<float> eigenvector_centrality( | ||
raft::handle_t const& handle, | ||
graph_view_t<int32_t, int32_t, float, true, true> const& graph_view, | ||
std::optional<raft::device_span<float const>> initial_centralities, | ||
float epsilon, | ||
size_t max_iterations, | ||
bool do_expensive_check); | ||
|
||
template rmm::device_uvector<float> eigenvector_centrality( | ||
raft::handle_t const& handle, | ||
graph_view_t<int32_t, int64_t, float, true, true> const& graph_view, | ||
std::optional<raft::device_span<float const>> initial_centralities, | ||
float epsilon, | ||
size_t max_iterations, | ||
bool do_expensive_check); | ||
|
||
template rmm::device_uvector<float> eigenvector_centrality( | ||
raft::handle_t const& handle, | ||
graph_view_t<int64_t, int64_t, float, true, true> const& graph_view, | ||
std::optional<raft::device_span<float const>> initial_centralities, | ||
float epsilon, | ||
size_t max_iterations, | ||
bool do_expensive_check); | ||
|
||
template rmm::device_uvector<double> eigenvector_centrality( | ||
raft::handle_t const& handle, | ||
graph_view_t<int32_t, int32_t, double, true, true> const& graph_view, | ||
std::optional<raft::device_span<double const>> initial_centralities, | ||
double epsilon, | ||
size_t max_iterations, | ||
bool do_expensive_check); | ||
|
||
template rmm::device_uvector<double> eigenvector_centrality( | ||
raft::handle_t const& handle, | ||
graph_view_t<int32_t, int64_t, double, true, true> const& graph_view, | ||
std::optional<raft::device_span<double const>> initial_centralities, | ||
double epsilon, | ||
size_t max_iterations, | ||
bool do_expensive_check); | ||
|
||
template rmm::device_uvector<double> eigenvector_centrality( | ||
raft::handle_t const& handle, | ||
graph_view_t<int64_t, int64_t, double, true, true> const& graph_view, | ||
std::optional<raft::device_span<double const>> initial_centralities, | ||
double epsilon, | ||
size_t max_iterations, | ||
bool do_expensive_check); | ||
|
||
} // namespace cugraph |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably not, copy/paste. I'll check all the headers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't forget to delete this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done