Skip to content

Commit

Permalink
Merge pull request #3748 from rapidsai/branch-23.08
Browse files Browse the repository at this point in the history
Forward-merge branch-23.08 to branch-23.10
  • Loading branch information
GPUtester authored Jul 27, 2023
2 parents f84c27b + e4eb22b commit 8a85705
Show file tree
Hide file tree
Showing 26 changed files with 1,101 additions and 457 deletions.
10 changes: 10 additions & 0 deletions cpp/src/c_api/centrality_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,22 @@ extern "C" cugraph_type_erased_device_array_view_t* cugraph_edge_centrality_resu
internal_pointer->values_->view());
}

extern "C" cugraph_type_erased_device_array_view_t* cugraph_edge_centrality_result_get_edge_ids(
cugraph_edge_centrality_result_t* result)
{
auto internal_pointer =
reinterpret_cast<cugraph::c_api::cugraph_edge_centrality_result_t*>(result);
return reinterpret_cast<cugraph_type_erased_device_array_view_t*>(
internal_pointer->edge_ids_->view());
}

extern "C" void cugraph_edge_centrality_result_free(cugraph_edge_centrality_result_t* result)
{
auto internal_pointer =
reinterpret_cast<cugraph::c_api::cugraph_edge_centrality_result_t*>(result);
delete internal_pointer->src_ids_;
delete internal_pointer->dst_ids_;
delete internal_pointer->values_;
delete internal_pointer->edge_ids_;
delete internal_pointer;
}
29 changes: 29 additions & 0 deletions cpp/src/centrality/betweenness_centrality_impl.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,35 @@ edge_betweenness_centrality(
do_expensive_check);
}

std::optional<weight_t> scale_factor{std::nullopt};

if (normalized) {
weight_t n = static_cast<weight_t>(graph_view.number_of_vertices());
scale_factor = n * (n - 1);
} else if (graph_view.is_symmetric())
scale_factor = weight_t{2};

if (scale_factor) {
if (graph_view.number_of_vertices() > 1) {
if (static_cast<vertex_t>(num_sources) < graph_view.number_of_vertices()) {
(*scale_factor) *= static_cast<weight_t>(num_sources) /
static_cast<weight_t>(graph_view.number_of_vertices());
}

auto firsts = centralities.view().value_firsts();
auto counts = centralities.view().edge_counts();
auto mutable_firsts = centralities.mutable_view().value_firsts();
for (size_t k = 0; k < counts.size(); k++) {
thrust::transform(
handle.get_thrust_policy(),
firsts[k],
firsts[k] + counts[k],
mutable_firsts[k],
[sf = *scale_factor] __device__(auto centrality) { return centrality / sf; });
}
}
}

return centralities;
}

Expand Down
38 changes: 37 additions & 1 deletion cpp/tests/centrality/betweenness_centrality_reference.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,37 @@ void reference_rescale(result_t* result,
}
}

template <typename result_t>
void reference_edge_rescale(result_t* result,
bool directed,
bool normalize,
size_t const number_of_vertices,
size_t const number_of_edges,
size_t const number_of_sources)
{
result_t rescale_factor = static_cast<result_t>(1);
result_t casted_number_of_vertices = static_cast<result_t>(number_of_vertices);
result_t casted_number_of_sources = static_cast<result_t>(number_of_sources);

if (normalize) {
if (number_of_edges > 2) {
rescale_factor /= ((casted_number_of_vertices) * (casted_number_of_vertices - 1));
}
} else {
if (!directed) { rescale_factor /= static_cast<result_t>(2); }
}

if (rescale_factor != result_t{1}) {
if (number_of_sources > 0) {
rescale_factor *= (casted_number_of_vertices / casted_number_of_sources);
}

for (auto idx = 0; idx < number_of_edges; ++idx) {
result[idx] *= rescale_factor;
}
}
}

template <typename vertex_t, typename edge_t, typename weight_t>
std::vector<weight_t> betweenness_centrality_reference(
std::vector<edge_t> const& offsets,
Expand Down Expand Up @@ -213,7 +244,9 @@ std::vector<weight_t> edge_betweenness_centrality_reference(
std::vector<edge_t> const& offsets,
std::vector<vertex_t> const& indices,
std::optional<std::vector<weight_t>> const& wgt,
std::vector<vertex_t> const& seeds)
std::vector<vertex_t> const& seeds,
bool directed,
bool normalize)
{
std::vector<weight_t> result;
if (indices.size() > 0) {
Expand All @@ -234,6 +267,9 @@ std::vector<weight_t> edge_betweenness_centrality_reference(
ref_edge_accumulation(result, offsets, indices, S, pred, sigmas, deltas, s);
}
}

reference_edge_rescale(
result.data(), directed, normalize, offsets.size() - 1, indices.size(), seeds.size());
return result;
}
} // namespace
19 changes: 15 additions & 4 deletions cpp/tests/centrality/edge_betweenness_centrality_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ class Tests_EdgeBetweennessCentrality
auto h_seeds = cugraph::test::to_host(handle, d_seeds);

auto h_reference_centralities =
edge_betweenness_centrality_reference(h_offsets, h_indices, h_wgt, h_seeds);
edge_betweenness_centrality_reference(h_offsets,
h_indices,
h_wgt,
h_seeds,
!graph_view.is_symmetric(),
betweenness_usecase.normalized);

rmm::device_uvector<vertex_t> d_reference_src_vertex_ids(0, handle.get_stream());
rmm::device_uvector<vertex_t> d_reference_dst_vertex_ids(0, handle.get_stream());
Expand Down Expand Up @@ -183,7 +188,9 @@ INSTANTIATE_TEST_SUITE_P(
::testing::Combine(
// enable correctness checks
::testing::Values(EdgeBetweennessCentrality_Usecase{20, false, false, true},
EdgeBetweennessCentrality_Usecase{20, false, true, true}),
EdgeBetweennessCentrality_Usecase{20, false, true, true},
EdgeBetweennessCentrality_Usecase{20, true, false, true},
EdgeBetweennessCentrality_Usecase{20, true, true, true}),
::testing::Values(cugraph::test::File_Usecase("test/datasets/karate.mtx"),
cugraph::test::File_Usecase("test/datasets/web-Google.mtx"),
cugraph::test::File_Usecase("test/datasets/webbase-1M.mtx"))));
Expand All @@ -194,7 +201,9 @@ INSTANTIATE_TEST_SUITE_P(
// enable correctness checks
::testing::Combine(
::testing::Values(EdgeBetweennessCentrality_Usecase{50, false, false, true},
EdgeBetweennessCentrality_Usecase{50, false, true, true}),
EdgeBetweennessCentrality_Usecase{50, false, true, true},
EdgeBetweennessCentrality_Usecase{50, true, false, true},
EdgeBetweennessCentrality_Usecase{50, true, true, true}),
::testing::Values(cugraph::test::Rmat_Usecase(10, 16, 0.57, 0.19, 0.19, 0, true, false))));

INSTANTIATE_TEST_SUITE_P(
Expand All @@ -207,7 +216,9 @@ INSTANTIATE_TEST_SUITE_P(
// disable correctness checks for large graphs
::testing::Combine(
::testing::Values(EdgeBetweennessCentrality_Usecase{500, false, false, false},
EdgeBetweennessCentrality_Usecase{500, false, true, false}),
EdgeBetweennessCentrality_Usecase{500, false, true, false},
EdgeBetweennessCentrality_Usecase{500, true, false, false},
EdgeBetweennessCentrality_Usecase{500, true, true, false}),
::testing::Values(cugraph::test::Rmat_Usecase(20, 32, 0.57, 0.19, 0.19, 0, false, false))));

CUGRAPH_TEST_PROGRAM_MAIN()
1 change: 0 additions & 1 deletion python/cugraph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ endif()

rapids_cython_init()

add_subdirectory(cugraph/centrality)
add_subdirectory(cugraph/community)
add_subdirectory(cugraph/components)
add_subdirectory(cugraph/dask/comms)
Expand Down
25 changes: 0 additions & 25 deletions python/cugraph/cugraph/centrality/CMakeLists.txt

This file was deleted.

Loading

0 comments on commit 8a85705

Please sign in to comment.