-
Notifications
You must be signed in to change notification settings - Fork 335
add C implementation of MST #5044
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
38bd0ad
add C implementation of MST
jnke2016 e123916
update return method
jnke2016 3214b14
add plc implementation
jnke2016 979a987
add c tests
jnke2016 7d7c48b
update CAPI tests
jnke2016 a7641b9
add plc API for MST
jnke2016 deaf7aa
update python implementation of MST
jnke2016 8714798
Merge remote-tracking branch 'upstream/branch-25.06' into branch-25.0…
jnke2016 6b511dd
fix copyright and style
jnke2016 814782c
undo changes to MsBFS tests
jnke2016 ec77468
update copyright and add fixme
jnke2016 6943507
update docstring example
jnke2016 c536262
add default value for expensive check
jnke2016 e669aa5
update docstring
jnke2016 710860a
Merge remote-tracking branch 'upstream/branch-25.06' into branch-25.0…
jnke2016 72a6ec1
Merge remote-tracking branch 'upstream/branch-25.06' into branch-25.0…
jnke2016 3a1a36f
update the edge property view
jnke2016 46e7bf1
fix style check
jnke2016 f50ab22
Merge remote-tracking branch 'upstream/branch-25.06' into branch-25.0…
jnke2016 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 hidden or 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 hidden or 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 hidden or 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,63 @@ | ||
/* | ||
* Copyright (c) 2025, 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/error.h> | ||
#include <cugraph_c/graph.h> | ||
#include <cugraph_c/graph_functions.h> | ||
#include <cugraph_c/random.h> | ||
#include <cugraph_c/resource_handle.h> | ||
|
||
/** @defgroup layout Layout algorithms | ||
*/ | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/** | ||
* @brief Opaque layout output | ||
*/ | ||
typedef struct { | ||
int32_t align_; | ||
} cugraph_layout_result_t; | ||
|
||
/** | ||
* @brief Minimum Spanning Tree | ||
* | ||
* NOTE: This currently wraps the legacy minimum implementation and is only | ||
* available in Single GPU implementation. | ||
* | ||
* @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] do_expensive_check | ||
* A flag to run expensive checks for input arguments (if set to true) | ||
* @param [out] result Opaque object containing the extracted subgraph | ||
* @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_minimum_spanning_tree(const cugraph_resource_handle_t* handle, | ||
cugraph_graph_t* graph, | ||
bool_t do_expensive_check, | ||
cugraph_induced_subgraph_result_t** result, | ||
cugraph_error_t** error); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
This file contains hidden or 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,169 @@ | ||
/* | ||
* Copyright (c) 2025, 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 "c_api/abstract_functor.hpp" | ||
#include "c_api/capi_helper.hpp" | ||
#include "c_api/graph.hpp" | ||
#include "c_api/induced_subgraph_result.hpp" | ||
#include "c_api/random.hpp" | ||
#include "c_api/resource_handle.hpp" | ||
#include "c_api/utils.hpp" | ||
|
||
#include <cugraph_c/algorithms.h> | ||
|
||
#include <cugraph/algorithms.hpp> | ||
#include <cugraph/detail/utility_wrappers.hpp> | ||
#include <cugraph/graph_functions.hpp> | ||
|
||
#include <optional> | ||
|
||
namespace { | ||
|
||
struct minimum_spanning_tree_functor : public cugraph::c_api::abstract_functor { | ||
raft::handle_t const& handle_; | ||
cugraph::c_api::cugraph_graph_t* graph_{nullptr}; | ||
bool do_expensive_check_{}; | ||
cugraph::c_api::cugraph_induced_subgraph_result_t* result_{}; | ||
; | ||
|
||
minimum_spanning_tree_functor(::cugraph_resource_handle_t const* handle, | ||
::cugraph_graph_t* graph, | ||
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)), | ||
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 if constexpr (multi_gpu) { | ||
unsupported(); | ||
} else if constexpr (!std::is_same_v<edge_t, int32_t>) { | ||
unsupported(); | ||
} else { | ||
auto graph = | ||
reinterpret_cast<cugraph::graph_t<vertex_t, edge_t, false, false>*>(graph_->graph_); | ||
|
||
auto edge_weights = | ||
reinterpret_cast<cugraph::edge_property_t<edge_t, weight_t>*>(graph_->edge_weights_); | ||
|
||
auto number_map = reinterpret_cast<rmm::device_uvector<vertex_t>*>(graph_->number_map_); | ||
|
||
auto graph_view = graph->view(); | ||
auto edge_partition_view = graph_view.local_edge_partition_view(); | ||
|
||
rmm::device_uvector<weight_t> tmp_weights(0, handle_.get_stream()); | ||
if (edge_weights == nullptr) { | ||
tmp_weights.resize(edge_partition_view.indices().size(), handle_.get_stream()); | ||
cugraph::detail::scalar_fill(handle_, tmp_weights.data(), tmp_weights.size(), weight_t{1}); | ||
} | ||
|
||
cugraph::legacy::GraphCSRView<vertex_t, edge_t, weight_t> legacy_csr_graph_view( | ||
const_cast<edge_t*>(edge_partition_view.offsets().data()), | ||
const_cast<vertex_t*>(edge_partition_view.indices().data()), | ||
(edge_weights == nullptr) | ||
? tmp_weights.data() | ||
: const_cast<weight_t*>(edge_weights->view().value_firsts().front()), | ||
edge_partition_view.offsets().size() - 1, | ||
edge_partition_view.indices().size()); | ||
|
||
auto result_legacy_coo_graph = | ||
cugraph::minimum_spanning_tree<vertex_t, edge_t, weight_t>(handle_, legacy_csr_graph_view); | ||
|
||
const size_t num_edges = result_legacy_coo_graph->view().number_of_edges; | ||
|
||
// FIXME: Add new constructor for cugraph_type_erased_host_array_t that takes an | ||
// rmm::device_buffer with the goa of skipping copies | ||
|
||
rmm::device_uvector<vertex_t> result_src(num_edges, handle_.get_stream()); | ||
raft::copy(result_src.data(), | ||
result_legacy_coo_graph->view().src_indices, | ||
result_src.size(), | ||
handle_.get_stream()); | ||
|
||
rmm::device_uvector<vertex_t> result_dst(num_edges, handle_.get_stream()); | ||
raft::copy(result_dst.data(), | ||
result_legacy_coo_graph->view().dst_indices, | ||
result_dst.size(), | ||
handle_.get_stream()); | ||
|
||
std::optional<rmm::device_uvector<weight_t>> result_wgt{std::nullopt}; | ||
|
||
result_wgt = rmm::device_uvector<weight_t>{num_edges, handle_.get_stream()}; | ||
raft::copy(result_wgt->data(), | ||
result_legacy_coo_graph->view().edge_data, | ||
result_wgt->size(), | ||
handle_.get_stream()); | ||
|
||
cugraph::unrenumber_int_vertices<vertex_t, multi_gpu>( | ||
handle_, | ||
result_src.data(), | ||
result_src.size(), | ||
number_map->data(), | ||
graph_view.vertex_partition_range_lasts(), | ||
do_expensive_check_); | ||
|
||
cugraph::unrenumber_int_vertices<vertex_t, multi_gpu>( | ||
handle_, | ||
result_dst.data(), | ||
result_dst.size(), | ||
number_map->data(), | ||
graph_view.vertex_partition_range_lasts(), | ||
do_expensive_check_); | ||
|
||
rmm::device_uvector<size_t> edge_offsets(2, handle_.get_stream()); | ||
std::vector<size_t> h_edge_offsets{{0, num_edges}}; | ||
raft::update_device( | ||
edge_offsets.data(), h_edge_offsets.data(), h_edge_offsets.size(), handle_.get_stream()); | ||
|
||
// FIXME: Add support for edge_id and edge_type_id. | ||
result_ = new cugraph::c_api::cugraph_induced_subgraph_result_t{ | ||
new cugraph::c_api::cugraph_type_erased_device_array_t(result_src, graph_->vertex_type_), | ||
new cugraph::c_api::cugraph_type_erased_device_array_t(result_dst, graph_->vertex_type_), | ||
result_wgt ? new cugraph::c_api::cugraph_type_erased_device_array_t(*result_wgt, | ||
graph_->weight_type_) | ||
: NULL, | ||
NULL, | ||
NULL, | ||
new cugraph::c_api::cugraph_type_erased_device_array_t(edge_offsets, | ||
cugraph_data_type_id_t::SIZE_T)}; | ||
} | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
extern "C" cugraph_error_code_t cugraph_minimum_spanning_tree( | ||
const cugraph_resource_handle_t* handle, | ||
cugraph_graph_t* graph, | ||
bool_t do_expensive_check, | ||
cugraph_induced_subgraph_result_t** result, | ||
cugraph_error_t** error) | ||
{ | ||
minimum_spanning_tree_functor functor(handle, graph, do_expensive_check); | ||
|
||
return cugraph::c_api::run_algorithm(graph, functor, result, error); | ||
} |
This file contains hidden or 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
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.
Could you add a FIXME here.
These copies could be unnecessary.
src_indices
,dst_indices
andedge_data
within the result_legacy_coo_graph are alreadyrmm::device_buffer
instances. If we made a new constructor forcugraph_type_erased_host_array_t
that took anrmm::device_buffer
we could skip these copies.We can do that optimization later.