Skip to content
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

Initial implementation of the Leiden C API #3165

Merged
merged 6 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ add_library(cugraph_c
src/c_api/core_result.cpp
src/c_api/extract_ego.cpp
src/c_api/k_core.cpp
src/c_api/heirarchical_clustering_result.cpp
src/c_api/induced_subgraph.cpp
src/c_api/induced_subgraph_helper.cu
src/c_api/graph_helper.cu
Expand All @@ -405,6 +406,7 @@ add_library(cugraph_c
src/c_api/random_walks.cpp
src/c_api/random.cpp
src/c_api/similarity.cpp
src/c_api/leiden.cpp
src/c_api/louvain.cpp
src/c_api/triangle_count.cpp
src/c_api/uniform_neighbor_sampling.cpp
Expand Down
28 changes: 27 additions & 1 deletion cpp/include/cugraph_c/community_algorithms.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, NVIDIA CORPORATION.
* Copyright (c) 2022-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -111,6 +111,32 @@ cugraph_error_code_t cugraph_louvain(const cugraph_resource_handle_t* handle,
cugraph_heirarchical_clustering_result_t** result,
cugraph_error_t** error);

/**
* @brief Compute Leiden
*
* @param [in] handle Handle for accessing resources
* @param [in] graph Pointer to graph. NOTE: Graph might be modified if the storage
* needs to be transposed
* @param [in] max_level Maximum level in hierarchy
* @param [in] resolution Resolution parameter (gamma) in modularity formula.
* This changes the size of the communities. Higher resolutions
* lead to more smaller communities, lower resolutions lead to
* fewer larger communities.
* @param [in] do_expensive_check
* A flag to run expensive checks for input arguments (if set to true)
* @param [out] result Output from the Leiden call
* @param [out] error Pointer to an error object storing details of any error. Will
* be populated if error code is not CUGRAPH_SUCCESS
* @return error code
*/
cugraph_error_code_t cugraph_leiden(const cugraph_resource_handle_t* handle,
cugraph_graph_t* graph,
size_t max_level,
double resolution,
bool_t do_expensive_check,
cugraph_heirarchical_clustering_result_t** result,
cugraph_error_t** error);

/**
* @brief Get heirarchical clustering vertices
*/
Expand Down
57 changes: 57 additions & 0 deletions cpp/src/c_api/heirarchical_clustering_result.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2022-2023, 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_c/community_algorithms.h>

#include <c_api/heirarchical_clustering_result.hpp>

extern "C" cugraph_type_erased_device_array_view_t*
cugraph_heirarchical_clustering_result_get_vertices(
cugraph_heirarchical_clustering_result_t* result)
{
auto internal_pointer =
reinterpret_cast<cugraph::c_api::cugraph_heirarchical_clustering_result_t*>(result);
return reinterpret_cast<cugraph_type_erased_device_array_view_t*>(
internal_pointer->vertices_->view());
}

extern "C" cugraph_type_erased_device_array_view_t*
cugraph_heirarchical_clustering_result_get_clusters(
cugraph_heirarchical_clustering_result_t* result)
{
auto internal_pointer =
reinterpret_cast<cugraph::c_api::cugraph_heirarchical_clustering_result_t*>(result);
return reinterpret_cast<cugraph_type_erased_device_array_view_t*>(
internal_pointer->clusters_->view());
}

extern "C" double cugraph_heirarchical_clustering_result_get_modularity(
cugraph_heirarchical_clustering_result_t* result)
{
auto internal_pointer =
reinterpret_cast<cugraph::c_api::cugraph_heirarchical_clustering_result_t*>(result);
return internal_pointer->modularity;
}

extern "C" void cugraph_heirarchical_clustering_result_free(
cugraph_heirarchical_clustering_result_t* result)
{
auto internal_pointer =
reinterpret_cast<cugraph::c_api::cugraph_heirarchical_clustering_result_t*>(result);
delete internal_pointer->vertices_;
delete internal_pointer->clusters_;
delete internal_pointer;
}
32 changes: 32 additions & 0 deletions cpp/src/c_api/heirarchical_clustering_result.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2023, 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_c/algorithms.h>

#include <c_api/array.hpp>

namespace cugraph {
namespace c_api {

struct cugraph_heirarchical_clustering_result_t {
double modularity{0};
cugraph_type_erased_device_array_t* vertices_{nullptr};
cugraph_type_erased_device_array_t* clusters_{nullptr};
};

} // namespace c_api
} // namespace cugraph
126 changes: 126 additions & 0 deletions cpp/src/c_api/leiden.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright (c) 2023, 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_c/community_algorithms.h>

#include <c_api/abstract_functor.hpp>
#include <c_api/graph.hpp>
#include <c_api/heirarchical_clustering_result.hpp>
#include <c_api/resource_handle.hpp>
#include <c_api/utils.hpp>

#include <cugraph/algorithms.hpp>
#include <cugraph/detail/shuffle_wrappers.hpp>
#include <cugraph/detail/utility_wrappers.hpp>
#include <cugraph/graph_functions.hpp>

#include <optional>

namespace {

struct leiden_functor : public cugraph::c_api::abstract_functor {
raft::handle_t const& handle_;
cugraph::c_api::cugraph_graph_t* graph_;
size_t max_level_;
double resolution_;
bool do_expensive_check_;
cugraph::c_api::cugraph_heirarchical_clustering_result_t* result_{};

leiden_functor(::cugraph_resource_handle_t const* handle,
::cugraph_graph_t* graph,
size_t max_level,
double resolution,
bool do_expensive_check)
: abstract_functor(),
handle_(*reinterpret_cast<cugraph::c_api::cugraph_resource_handle_t const*>(handle)->handle_),
graph_(reinterpret_cast<cugraph::c_api::cugraph_graph_t*>(graph)),
max_level_(max_level),
resolution_(resolution),
do_expensive_check_(do_expensive_check)
{
}

template <typename vertex_t,
typename edge_t,
typename weight_t,
typename edge_type_type_t,
bool store_transposed,
bool multi_gpu>
void operator()()
{
if constexpr (!cugraph::is_candidate<vertex_t, edge_t, weight_t>::value) {
unsupported();
} else {
// leiden expects store_transposed == false
if constexpr (store_transposed) {
error_code_ = cugraph::c_api::
transpose_storage<vertex_t, edge_t, weight_t, store_transposed, multi_gpu>(
handle_, graph_, error_.get());
if (error_code_ != CUGRAPH_SUCCESS) return;
}

auto graph =
reinterpret_cast<cugraph::graph_t<vertex_t, edge_t, false, multi_gpu>*>(graph_->graph_);

auto graph_view = graph->view();

auto edge_weights = reinterpret_cast<
cugraph::edge_property_t<cugraph::graph_view_t<vertex_t, edge_t, false, multi_gpu>,
weight_t>*>(graph_->edge_weights_);

auto number_map = reinterpret_cast<rmm::device_uvector<vertex_t>*>(graph_->number_map_);

rmm::device_uvector<vertex_t> clusters(graph_view.local_vertex_partition_range_size(),
handle_.get_stream());

#if 0
auto [level, modularity] = cugraph::leiden(
handle_,
graph_view,
(edge_weights != nullptr) ? std::make_optional(edge_weights->view()) : std::nullopt,
clusters.data(),
max_level_,
static_cast<weight_t>(resolution_));

rmm::device_uvector<vertex_t> vertices(graph_view.local_vertex_partition_range_size(),
handle_.get_stream());
raft::copy(vertices.data(), number_map->data(), vertices.size(), handle_.get_stream());

result_ = new cugraph::c_api::cugraph_heirarchical_clustering_result_t{
modularity,
new cugraph::c_api::cugraph_type_erased_device_array_t(vertices, graph_->vertex_type_),
new cugraph::c_api::cugraph_type_erased_device_array_t(clusters, graph_->vertex_type_)};
#else
CUGRAPH_FAIL("NOT IMPLEMENTED YET");
#endif
}
}
};

} // namespace

extern "C" cugraph_error_code_t cugraph_leiden(const cugraph_resource_handle_t* handle,
cugraph_graph_t* graph,
size_t max_level,
double resolution,
bool_t do_expensive_check,
cugraph_heirarchical_clustering_result_t** result,
cugraph_error_t** error)
{
leiden_functor functor(handle, graph, max_level, resolution, do_expensive_check);

return cugraph::c_api::run_algorithm(graph, functor, result, error);
}
55 changes: 3 additions & 52 deletions cpp/src/c_api/louvain.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, NVIDIA CORPORATION.
* Copyright (c) 2022-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,10 +14,11 @@
* limitations under the License.
*/

#include <cugraph_c/algorithms.h>
#include <cugraph_c/community_algorithms.h>

#include <c_api/abstract_functor.hpp>
#include <c_api/graph.hpp>
#include <c_api/heirarchical_clustering_result.hpp>
#include <c_api/resource_handle.hpp>
#include <c_api/utils.hpp>

Expand All @@ -28,18 +29,6 @@

#include <optional>

namespace cugraph {
namespace c_api {

struct cugraph_heirarchical_clustering_result_t {
double modularity{0};
cugraph_type_erased_device_array_t* vertices_{nullptr};
cugraph_type_erased_device_array_t* clusters_{nullptr};
};

} // namespace c_api
} // namespace cugraph

namespace {

struct louvain_functor : public cugraph::c_api::abstract_functor {
Expand Down Expand Up @@ -119,44 +108,6 @@ struct louvain_functor : public cugraph::c_api::abstract_functor {

} // namespace

extern "C" cugraph_type_erased_device_array_view_t*
cugraph_heirarchical_clustering_result_get_vertices(
cugraph_heirarchical_clustering_result_t* result)
{
auto internal_pointer =
reinterpret_cast<cugraph::c_api::cugraph_heirarchical_clustering_result_t*>(result);
return reinterpret_cast<cugraph_type_erased_device_array_view_t*>(
internal_pointer->vertices_->view());
}

extern "C" cugraph_type_erased_device_array_view_t*
cugraph_heirarchical_clustering_result_get_clusters(
cugraph_heirarchical_clustering_result_t* result)
{
auto internal_pointer =
reinterpret_cast<cugraph::c_api::cugraph_heirarchical_clustering_result_t*>(result);
return reinterpret_cast<cugraph_type_erased_device_array_view_t*>(
internal_pointer->clusters_->view());
}

extern "C" double cugraph_heirarchical_clustering_result_get_modularity(
cugraph_heirarchical_clustering_result_t* result)
{
auto internal_pointer =
reinterpret_cast<cugraph::c_api::cugraph_heirarchical_clustering_result_t*>(result);
return internal_pointer->modularity;
}

extern "C" void cugraph_heirarchical_clustering_result_free(
cugraph_heirarchical_clustering_result_t* result)
{
auto internal_pointer =
reinterpret_cast<cugraph::c_api::cugraph_heirarchical_clustering_result_t*>(result);
delete internal_pointer->vertices_;
delete internal_pointer->clusters_;
delete internal_pointer;
}

extern "C" cugraph_error_code_t cugraph_louvain(const cugraph_resource_handle_t* handle,
cugraph_graph_t* graph,
size_t max_level,
Expand Down
2 changes: 2 additions & 0 deletions cpp/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ if(BUILD_CUGRAPH_MG_TESTS)
ConfigureCTestMG(MG_CAPI_RANDOM_WALKS_TEST c_api/mg_random_walks_test.c c_api/mg_test_utils.cpp)
ConfigureCTestMG(MG_CAPI_TRIANGLE_COUNT_TEST c_api/mg_triangle_count_test.c c_api/mg_test_utils.cpp)
ConfigureCTestMG(MG_CAPI_LOUVAIN_TEST c_api/mg_louvain_test.c c_api/mg_test_utils.cpp)
ConfigureCTestMG(MG_CAPI_LEIDEN_TEST c_api/mg_leiden_test.c c_api/mg_test_utils.cpp)
ConfigureCTestMG(MG_CAPI_CORE_NUMBER_TEST c_api/mg_core_number_test.c c_api/mg_test_utils.cpp)
ConfigureCTestMG(MG_CAPI_SIMILARITY_TEST c_api/mg_similarity_test.c c_api/mg_test_utils.cpp)
ConfigureCTestMG(MG_CAPI_K_CORE_TEST c_api/mg_k_core_test.c c_api/mg_test_utils.cpp)
Expand Down Expand Up @@ -641,6 +642,7 @@ ConfigureCTest(CAPI_UNIFORM_NEIGHBOR_SAMPLE_TEST c_api/uniform_neighbor_sample_t
ConfigureCTest(CAPI_RANDOM_WALKS_TEST c_api/sg_random_walks_test.c)
ConfigureCTest(CAPI_TRIANGLE_COUNT_TEST c_api/triangle_count_test.c)
ConfigureCTest(CAPI_LOUVAIN_TEST c_api/louvain_test.c)
ConfigureCTest(CAPI_LEIDEN_TEST c_api/leiden_test.c)
ConfigureCTest(CAPI_CORE_NUMBER_TEST c_api/core_number_test.c)
ConfigureCTest(CAPI_SIMILARITY_TEST c_api/similarity_test.c)
ConfigureCTest(CAPI_K_CORE_TEST c_api/k_core_test.c)
Expand Down
Loading