|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, NVIDIA CORPORATION. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "c_api/abstract_functor.hpp" |
| 18 | +#include "c_api/capi_helper.hpp" |
| 19 | +#include "c_api/graph.hpp" |
| 20 | +#include "c_api/induced_subgraph_result.hpp" |
| 21 | +#include "c_api/random.hpp" |
| 22 | +#include "c_api/resource_handle.hpp" |
| 23 | +#include "c_api/utils.hpp" |
| 24 | + |
| 25 | +#include <cugraph_c/algorithms.h> |
| 26 | + |
| 27 | +#include <cugraph/algorithms.hpp> |
| 28 | +#include <cugraph/detail/utility_wrappers.hpp> |
| 29 | +#include <cugraph/graph_functions.hpp> |
| 30 | + |
| 31 | +#include <optional> |
| 32 | + |
| 33 | +namespace { |
| 34 | + |
| 35 | +struct minimum_spanning_tree_functor : public cugraph::c_api::abstract_functor { |
| 36 | + raft::handle_t const& handle_; |
| 37 | + cugraph::c_api::cugraph_graph_t* graph_{nullptr}; |
| 38 | + bool do_expensive_check_{}; |
| 39 | + cugraph::c_api::cugraph_induced_subgraph_result_t* result_{}; |
| 40 | + ; |
| 41 | + |
| 42 | + minimum_spanning_tree_functor(::cugraph_resource_handle_t const* handle, |
| 43 | + ::cugraph_graph_t* graph, |
| 44 | + bool do_expensive_check) |
| 45 | + : abstract_functor(), |
| 46 | + handle_(*reinterpret_cast<cugraph::c_api::cugraph_resource_handle_t const*>(handle)->handle_), |
| 47 | + graph_(reinterpret_cast<cugraph::c_api::cugraph_graph_t*>(graph)), |
| 48 | + do_expensive_check_(do_expensive_check) |
| 49 | + { |
| 50 | + } |
| 51 | + |
| 52 | + template <typename vertex_t, |
| 53 | + typename edge_t, |
| 54 | + typename weight_t, |
| 55 | + typename edge_type_type_t, |
| 56 | + bool store_transposed, |
| 57 | + bool multi_gpu> |
| 58 | + void operator()() |
| 59 | + { |
| 60 | + if constexpr (!cugraph::is_candidate<vertex_t, edge_t, weight_t>::value) { |
| 61 | + unsupported(); |
| 62 | + } else if constexpr (multi_gpu) { |
| 63 | + unsupported(); |
| 64 | + } else if constexpr (!std::is_same_v<edge_t, int32_t>) { |
| 65 | + unsupported(); |
| 66 | + } else { |
| 67 | + auto graph = |
| 68 | + reinterpret_cast<cugraph::graph_t<vertex_t, edge_t, false, false>*>(graph_->graph_); |
| 69 | + |
| 70 | + auto edge_weights = |
| 71 | + reinterpret_cast<cugraph::edge_property_t<edge_t, weight_t>*>(graph_->edge_weights_); |
| 72 | + |
| 73 | + auto number_map = reinterpret_cast<rmm::device_uvector<vertex_t>*>(graph_->number_map_); |
| 74 | + |
| 75 | + auto graph_view = graph->view(); |
| 76 | + auto edge_partition_view = graph_view.local_edge_partition_view(); |
| 77 | + |
| 78 | + rmm::device_uvector<weight_t> tmp_weights(0, handle_.get_stream()); |
| 79 | + if (edge_weights == nullptr) { |
| 80 | + tmp_weights.resize(edge_partition_view.indices().size(), handle_.get_stream()); |
| 81 | + cugraph::detail::scalar_fill(handle_, tmp_weights.data(), tmp_weights.size(), weight_t{1}); |
| 82 | + } |
| 83 | + |
| 84 | + cugraph::legacy::GraphCSRView<vertex_t, edge_t, weight_t> legacy_csr_graph_view( |
| 85 | + const_cast<edge_t*>(edge_partition_view.offsets().data()), |
| 86 | + const_cast<vertex_t*>(edge_partition_view.indices().data()), |
| 87 | + (edge_weights == nullptr) |
| 88 | + ? tmp_weights.data() |
| 89 | + : const_cast<weight_t*>(edge_weights->view().value_firsts().front()), |
| 90 | + edge_partition_view.offsets().size() - 1, |
| 91 | + edge_partition_view.indices().size()); |
| 92 | + |
| 93 | + auto result_legacy_coo_graph = |
| 94 | + cugraph::minimum_spanning_tree<vertex_t, edge_t, weight_t>(handle_, legacy_csr_graph_view); |
| 95 | + |
| 96 | + const size_t num_edges = result_legacy_coo_graph->view().number_of_edges; |
| 97 | + |
| 98 | + // FIXME: Add new constructor for cugraph_type_erased_host_array_t that takes an |
| 99 | + // rmm::device_buffer with the goa of skipping copies |
| 100 | + |
| 101 | + rmm::device_uvector<vertex_t> result_src(num_edges, handle_.get_stream()); |
| 102 | + raft::copy(result_src.data(), |
| 103 | + result_legacy_coo_graph->view().src_indices, |
| 104 | + result_src.size(), |
| 105 | + handle_.get_stream()); |
| 106 | + |
| 107 | + rmm::device_uvector<vertex_t> result_dst(num_edges, handle_.get_stream()); |
| 108 | + raft::copy(result_dst.data(), |
| 109 | + result_legacy_coo_graph->view().dst_indices, |
| 110 | + result_dst.size(), |
| 111 | + handle_.get_stream()); |
| 112 | + |
| 113 | + std::optional<rmm::device_uvector<weight_t>> result_wgt{std::nullopt}; |
| 114 | + |
| 115 | + result_wgt = rmm::device_uvector<weight_t>{num_edges, handle_.get_stream()}; |
| 116 | + raft::copy(result_wgt->data(), |
| 117 | + result_legacy_coo_graph->view().edge_data, |
| 118 | + result_wgt->size(), |
| 119 | + handle_.get_stream()); |
| 120 | + |
| 121 | + cugraph::unrenumber_int_vertices<vertex_t, multi_gpu>( |
| 122 | + handle_, |
| 123 | + result_src.data(), |
| 124 | + result_src.size(), |
| 125 | + number_map->data(), |
| 126 | + graph_view.vertex_partition_range_lasts(), |
| 127 | + do_expensive_check_); |
| 128 | + |
| 129 | + cugraph::unrenumber_int_vertices<vertex_t, multi_gpu>( |
| 130 | + handle_, |
| 131 | + result_dst.data(), |
| 132 | + result_dst.size(), |
| 133 | + number_map->data(), |
| 134 | + graph_view.vertex_partition_range_lasts(), |
| 135 | + do_expensive_check_); |
| 136 | + |
| 137 | + rmm::device_uvector<size_t> edge_offsets(2, handle_.get_stream()); |
| 138 | + std::vector<size_t> h_edge_offsets{{0, num_edges}}; |
| 139 | + raft::update_device( |
| 140 | + edge_offsets.data(), h_edge_offsets.data(), h_edge_offsets.size(), handle_.get_stream()); |
| 141 | + |
| 142 | + // FIXME: Add support for edge_id and edge_type_id. |
| 143 | + result_ = new cugraph::c_api::cugraph_induced_subgraph_result_t{ |
| 144 | + new cugraph::c_api::cugraph_type_erased_device_array_t(result_src, graph_->vertex_type_), |
| 145 | + new cugraph::c_api::cugraph_type_erased_device_array_t(result_dst, graph_->vertex_type_), |
| 146 | + result_wgt ? new cugraph::c_api::cugraph_type_erased_device_array_t(*result_wgt, |
| 147 | + graph_->weight_type_) |
| 148 | + : NULL, |
| 149 | + NULL, |
| 150 | + NULL, |
| 151 | + new cugraph::c_api::cugraph_type_erased_device_array_t(edge_offsets, |
| 152 | + cugraph_data_type_id_t::SIZE_T)}; |
| 153 | + } |
| 154 | + } |
| 155 | +}; |
| 156 | + |
| 157 | +} // namespace |
| 158 | + |
| 159 | +extern "C" cugraph_error_code_t cugraph_minimum_spanning_tree( |
| 160 | + const cugraph_resource_handle_t* handle, |
| 161 | + cugraph_graph_t* graph, |
| 162 | + bool_t do_expensive_check, |
| 163 | + cugraph_induced_subgraph_result_t** result, |
| 164 | + cugraph_error_t** error) |
| 165 | +{ |
| 166 | + minimum_spanning_tree_functor functor(handle, graph, do_expensive_check); |
| 167 | + |
| 168 | + return cugraph::c_api::run_algorithm(graph, functor, result, error); |
| 169 | +} |
0 commit comments