-
Notifications
You must be signed in to change notification settings - Fork 329
Compilation optimizations - batch 1 #5112
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
rapids-bot
merged 1 commit into
rapidsai:branch-25.08
from
ChuckHastings:begin_variant_work
Jun 10, 2025
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,249 @@ | ||
/* | ||
* 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/edge_property.hpp> | ||
#include <cugraph/utilities/error.hpp> | ||
|
||
#include <raft/core/device_span.hpp> | ||
|
||
#include <rmm/device_uvector.hpp> | ||
|
||
#include <variant> | ||
|
||
namespace cugraph { | ||
|
||
using arithmetic_device_uvector_t = std::variant<std::monostate, | ||
rmm::device_uvector<float>, | ||
rmm::device_uvector<double>, | ||
rmm::device_uvector<int32_t>, | ||
rmm::device_uvector<int64_t>, | ||
rmm::device_uvector<size_t>>; | ||
using arithmetic_device_span_t = std::variant<std::monostate, | ||
raft::device_span<float>, | ||
raft::device_span<double>, | ||
raft::device_span<int32_t>, | ||
raft::device_span<int64_t>, | ||
raft::device_span<size_t>>; | ||
using const_arithmetic_device_span_t = std::variant<std::monostate, | ||
raft::device_span<float const>, | ||
raft::device_span<double const>, | ||
raft::device_span<int32_t const>, | ||
raft::device_span<int64_t const>, | ||
raft::device_span<size_t const>>; | ||
|
||
template <typename edge_t> | ||
using edge_arithmetic_property_view_t = | ||
std::variant<std::monostate, | ||
cugraph::edge_property_view_t<edge_t, float const*>, | ||
cugraph::edge_property_view_t<edge_t, double const*>, | ||
cugraph::edge_property_view_t<edge_t, int32_t const*>, | ||
cugraph::edge_property_view_t<edge_t, int64_t const*>, | ||
cugraph::edge_property_view_t<edge_t, size_t const*>>; | ||
|
||
template <typename edge_t> | ||
using edge_arithmetic_property_mutable_view_t = | ||
std::variant<std::monostate, | ||
cugraph::edge_property_view_t<edge_t, float*>, | ||
cugraph::edge_property_view_t<edge_t, double*>, | ||
cugraph::edge_property_view_t<edge_t, int32_t*>, | ||
cugraph::edge_property_view_t<edge_t, int64_t*>, | ||
cugraph::edge_property_view_t<edge_t, size_t*>>; | ||
|
||
template <typename func_t> | ||
auto variant_type_dispatch(arithmetic_device_uvector_t& property, func_t func) | ||
{ | ||
if (std::holds_alternative<rmm::device_uvector<float>>(property)) { | ||
auto& prop = std::get<rmm::device_uvector<float>>(property); | ||
return func(prop); | ||
} else if (std::holds_alternative<rmm::device_uvector<double>>(property)) { | ||
auto& prop = std::get<rmm::device_uvector<double>>(property); | ||
return func(prop); | ||
} else if (std::holds_alternative<rmm::device_uvector<int32_t>>(property)) { | ||
auto& prop = std::get<rmm::device_uvector<int32_t>>(property); | ||
return func(prop); | ||
} else if (std::holds_alternative<rmm::device_uvector<int64_t>>(property)) { | ||
auto& prop = std::get<rmm::device_uvector<int64_t>>(property); | ||
return func(prop); | ||
} else { | ||
CUGRAPH_EXPECTS(std::holds_alternative<rmm::device_uvector<size_t>>(property), | ||
"unsupported variant type -- shouldn't happen"); | ||
|
||
auto& prop = std::get<rmm::device_uvector<size_t>>(property); | ||
return func(prop); | ||
} | ||
} | ||
|
||
template <typename func_t> | ||
auto variant_type_dispatch(arithmetic_device_uvector_t const& property, func_t func) | ||
{ | ||
if (std::holds_alternative<rmm::device_uvector<float>>(property)) { | ||
auto& prop = std::get<rmm::device_uvector<float>>(property); | ||
return func(prop); | ||
} else if (std::holds_alternative<rmm::device_uvector<double>>(property)) { | ||
auto& prop = std::get<rmm::device_uvector<double>>(property); | ||
return func(prop); | ||
} else if (std::holds_alternative<rmm::device_uvector<int32_t>>(property)) { | ||
auto& prop = std::get<rmm::device_uvector<int32_t>>(property); | ||
return func(prop); | ||
} else if (std::holds_alternative<rmm::device_uvector<int64_t>>(property)) { | ||
auto& prop = std::get<rmm::device_uvector<int64_t>>(property); | ||
return func(prop); | ||
} else { | ||
CUGRAPH_EXPECTS(std::holds_alternative<rmm::device_uvector<size_t>>(property), | ||
"unsupported variant type -- shouldn't happen"); | ||
auto& prop = std::get<rmm::device_uvector<size_t>>(property); | ||
return func(prop); | ||
} | ||
} | ||
|
||
template <typename func_t> | ||
auto variant_type_dispatch(arithmetic_device_span_t& property, func_t func) | ||
{ | ||
if (std::holds_alternative<raft::device_span<float>>(property)) { | ||
auto& prop = std::get<raft::device_span<float>>(property); | ||
return func(prop); | ||
} else if (std::holds_alternative<raft::device_span<double>>(property)) { | ||
auto& prop = std::get<raft::device_span<double>>(property); | ||
return func(prop); | ||
} else if (std::holds_alternative<raft::device_span<int32_t>>(property)) { | ||
auto& prop = std::get<raft::device_span<int32_t>>(property); | ||
return func(prop); | ||
} else if (std::holds_alternative<raft::device_span<int64_t>>(property)) { | ||
auto& prop = std::get<raft::device_span<int64_t>>(property); | ||
return func(prop); | ||
} else { | ||
CUGRAPH_EXPECTS(std::holds_alternative<raft::device_span<size_t>>(property), | ||
"unsupported variant type -- shouldn't happen"); | ||
|
||
auto& prop = std::get<raft::device_span<size_t>>(property); | ||
return func(prop); | ||
} | ||
} | ||
|
||
template <typename func_t> | ||
auto variant_type_dispatch(const_arithmetic_device_span_t& property, func_t func) | ||
{ | ||
if (std::holds_alternative<raft::device_span<float const>>(property)) { | ||
auto& prop = std::get<raft::device_span<float const>>(property); | ||
return func(prop); | ||
} else if (std::holds_alternative<raft::device_span<double const>>(property)) { | ||
auto& prop = std::get<raft::device_span<double const>>(property); | ||
return func(prop); | ||
} else if (std::holds_alternative<raft::device_span<int32_t const>>(property)) { | ||
auto& prop = std::get<raft::device_span<int32_t const>>(property); | ||
return func(prop); | ||
} else if (std::holds_alternative<raft::device_span<int64_t const>>(property)) { | ||
auto& prop = std::get<raft::device_span<int64_t const>>(property); | ||
return func(prop); | ||
} else { | ||
CUGRAPH_EXPECTS(std::holds_alternative<raft::device_span<size_t const>>(property), | ||
"unsupported variant type -- shouldn't happen"); | ||
|
||
auto& prop = std::get<raft::device_span<size_t const>>(property); | ||
return func(prop); | ||
} | ||
} | ||
|
||
template <typename edge_t, typename func_t> | ||
auto variant_type_dispatch(edge_arithmetic_property_view_t<edge_t>& property, func_t func) | ||
{ | ||
if (std::holds_alternative<cugraph::edge_property_view_t<edge_t, float const*>>(property)) { | ||
auto& prop = std::get<cugraph::edge_property_view_t<edge_t, float const*>>(property); | ||
return func(prop); | ||
} else if (std::holds_alternative<cugraph::edge_property_view_t<edge_t, double const*>>( | ||
property)) { | ||
auto& prop = std::get<cugraph::edge_property_view_t<edge_t, double const*>>(property); | ||
return func(prop); | ||
} else if (std::holds_alternative<cugraph::edge_property_view_t<edge_t, int32_t const*>>( | ||
property)) { | ||
auto& prop = std::get<cugraph::edge_property_view_t<edge_t, int32_t const*>>(property); | ||
return func(prop); | ||
} else if (std::holds_alternative<cugraph::edge_property_view_t<edge_t, int64_t const*>>( | ||
property)) { | ||
auto& prop = std::get<cugraph::edge_property_view_t<edge_t, int64_t const*>>(property); | ||
return func(prop); | ||
} else { | ||
CUGRAPH_EXPECTS( | ||
(std::holds_alternative<cugraph::edge_property_view_t<edge_t, size_t const*>>(property)), | ||
"unsupported variant type -- shouldn't happen"); | ||
|
||
auto& prop = std::get<cugraph::edge_property_view_t<edge_t, size_t const*>>(property); | ||
return func(prop); | ||
} | ||
} | ||
|
||
template <typename edge_t, typename func_t> | ||
auto variant_type_dispatch(edge_arithmetic_property_mutable_view_t<edge_t>& property, func_t func) | ||
{ | ||
if (std::holds_alternative<cugraph::edge_property_view_t<edge_t, float*>>(property)) { | ||
auto& prop = std::get<cugraph::edge_property_view_t<edge_t, float*>>(property); | ||
return func(prop); | ||
} else if (std::holds_alternative<cugraph::edge_property_view_t<edge_t, double*>>(property)) { | ||
auto& prop = std::get<cugraph::edge_property_view_t<edge_t, double*>>(property); | ||
return func(prop); | ||
} else if (std::holds_alternative<cugraph::edge_property_view_t<edge_t, int32_t*>>(property)) { | ||
auto& prop = std::get<cugraph::edge_property_view_t<edge_t, int32_t*>>(property); | ||
return func(prop); | ||
} else if (std::holds_alternative<cugraph::edge_property_view_t<edge_t, int64_t*>>(property)) { | ||
auto& prop = std::get<cugraph::edge_property_view_t<edge_t, int64_t*>>(property); | ||
return func(prop); | ||
} else { | ||
CUGRAPH_EXPECTS( | ||
(std::holds_alternative<cugraph::edge_property_view_t<edge_t, size_t*>>(property)), | ||
"unsupported variant type -- shouldn't happen"); | ||
|
||
auto& prop = std::get<cugraph::edge_property_view_t<edge_t, size_t const*>>(property); | ||
return func(prop); | ||
} | ||
} | ||
|
||
struct sizeof_arithmetic_element { | ||
template <typename T> | ||
size_t operator()(rmm::device_uvector<T> const&) const | ||
{ | ||
return sizeof(T); | ||
} | ||
template <typename T> | ||
size_t operator()(raft::device_span<T> const&) const | ||
{ | ||
return sizeof(T); | ||
} | ||
template <typename T> | ||
size_t operator()(raft::device_span<T const> const&) const | ||
{ | ||
return sizeof(T); | ||
} | ||
}; | ||
|
||
inline arithmetic_device_span_t make_arithmetic_device_span(arithmetic_device_uvector_t& v) | ||
{ | ||
return variant_type_dispatch(v, [](auto& v) { | ||
using T = typename std::remove_reference<decltype(v)>::type::value_type; | ||
return static_cast<arithmetic_device_span_t>(raft::device_span<T>(v.data(), v.size())); | ||
}); | ||
} | ||
|
||
inline std::vector<arithmetic_device_span_t> make_arithmetic_device_span_vector( | ||
std::vector<arithmetic_device_uvector_t>& v) | ||
{ | ||
std::vector<arithmetic_device_span_t> results(v.size()); | ||
std::transform( | ||
v.begin(), v.end(), results.begin(), [](auto& c) { return make_arithmetic_device_span(c); }); | ||
return results; | ||
} | ||
|
||
} // namespace cugraph |
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.
Minor thing but isn't
arithmetic_device_span_t(raft::device_span<T>(v.data(), v.size()))
sufficient? And assigning a span to a variant of span might be more intuitive than statically casting a span to a variant of a span.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.
Yes, that would be better. I'll fix that in batch 2 of the changes.