15 #include <source_location>
17 #include <type_traits>
21 #include <cuda_runtime_api.h>
30 #define RAPIDSMPF_CUDA_VERSION_AT_LEAST(version) (CUDART_VERSION >= version)
33 using Clock = std::chrono::high_resolution_clock;
35 using Duration = std::chrono::duration<double>;
37 using TimePoint = std::chrono::time_point<Clock, Duration>;
51 template <
typename MapType>
52 std::pair<typename MapType::key_type, typename MapType::mapped_type>
extract_item(
53 MapType& map,
typename MapType::const_iterator position
55 auto node = map.extract(position);
57 throw std::out_of_range(
"Invalid iterator passed to extract");
59 return {std::move(node.key()), std::move(node.mapped())};
72 template <
typename MapType>
73 std::pair<typename MapType::key_type, typename MapType::mapped_type>
extract_item(
74 MapType& map,
typename MapType::key_type
const& key
76 auto node = map.extract(key);
78 throw std::out_of_range(
"Invalid key passed to extract");
80 return {std::move(node.key()), std::move(node.mapped())};
94 template <
typename MapType>
96 MapType& map,
typename MapType::key_type
const& key
114 template <
typename MapType>
116 MapType& map,
typename MapType::const_iterator position
131 template <
typename MapType>
133 MapType& map,
typename MapType::key_type
const& key
150 template <
typename MapType>
152 MapType& map,
typename MapType::const_iterator position
167 template <
typename MapType>
169 using ValueType =
typename std::remove_reference_t<MapType>::mapped_type;
170 std::vector<ValueType> vec;
171 vec.reserve(map.size());
172 for (
auto&& [key, value] : map) {
173 vec.push_back(std::move(value));
193 template <
typename T>
195 return (y == 0) ? 0 : x / y;
210 template <std::
integral T>
212 return x / y + (x % y != 0 ? T{1} : T{0});
231 [[nodiscard]]
inline auto chunk_indices(std::size_t count, std::size_t num_chunks) {
232 std::size_t
const chunk_size =
ceil_div(count, num_chunks);
233 return std::views::iota(std::size_t{0}, num_chunks)
234 | std::views::transform([count, chunk_size](std::size_t k) {
235 return std::pair<std::size_t, std::size_t>{
236 std::min(k * chunk_size, count),
237 std::min((k + 1) * chunk_size, count)
243 #define RAPIDSMPF_CONCAT_DETAIL_(x, y) x##y
244 #define RAPIDSMPF_CONCAT(x, y) RAPIDSMPF_CONCAT_DETAIL_(x, y)
247 #define RAPIDSMPF_STRINGIFY_DETAIL_(x) #x
248 #define RAPIDSMPF_STRINGIFY(x) RAPIDSMPF_STRINGIFY_DETAIL_(x)
266 #define RAPIDSMPF_OVERLOAD_BY_ARG_COUNT(_1, _2, NAME, ...) NAME
279 template <
typename T>
285 template <
typename T>
287 return std::addressof(ptr);
291 template <
typename T>
297 template <
typename T>
305 template <
class... Ts>
307 using Ts::operator()...;
326 template <std::ranges::input_range R,
typename T,
typename Proj = std::
identity>
327 [[nodiscard]] constexpr
bool contains(R&& range, T
const& value, Proj proj = {}) {
328 for (
auto const& elem : range) {
329 if (std::invoke(proj, elem) == value) {
342 template <
typename T>
344 typename T::element_type;
345 requires std::same_as<T, std::shared_ptr<typename T::element_type>>
346 || std::same_as<T, std::weak_ptr<typename T::element_type>>;
366 template <SharedOrWeakPtr A, SharedOrWeakPtr B>
367 requires std::same_as<typename A::element_type, typename B::element_type>
368 [[nodiscard]] constexpr
bool owner_equal(A
const& a, B
const& b) noexcept {
369 return !a.owner_before(b) && !b.owner_before(a);
391 template <
typename To,
typename From>
392 requires std::is_arithmetic_v<To> && std::is_arithmetic_v<From>
394 From value, std::source_location
const& loc = std::source_location::current()
396 if constexpr (std::is_same_v<From, To>) {
399 }
else if constexpr (std::is_integral_v<From> && std::is_integral_v<To>) {
401 if (!std::in_range<To>(value)) {
402 throw std::overflow_error(
403 "RapidsMPF cast error at: " + std::string(loc.file_name()) +
":"
404 + std::to_string(loc.line())
405 +
", value out of range (value=" + std::to_string(value) +
")"
408 return static_cast<To
>(value);
411 return static_cast<To
>(value);
constexpr T * to_pointer(T *ptr) noexcept
Returns the raw pointer from a pointer, reference, or smart pointer.
RAPIDS Multi-Processor interfaces.
constexpr T ceil_div(T x, T y)
Computes the ceiling of the division of two integers.
std::chrono::duration< double > Duration
Alias for a duration type representing time in seconds as a double.
std::chrono::time_point< Clock, Duration > TimePoint
Alias for a time point with double precision in seconds.
MapType::mapped_type extract_value(MapType &map, typename MapType::key_type const &key)
Extracts the value associated with a specific key from a map, removing the key-value pair.
MapType::key_type extract_key(MapType &map, typename MapType::key_type const &key)
Extracts a key from a map, removing the key-value pair.
bool is_running_under_valgrind()
Checks whether the application is running under Valgrind.
std::pair< typename MapType::key_type, typename MapType::mapped_type > extract_item(MapType &map, typename MapType::const_iterator position)
Extracts a key-value pair from a map, removing it from the map.
auto chunk_indices(std::size_t count, std::size_t num_chunks)
Splits the index range [0, count) into exactly num_chunks contiguous chunks.
concept SharedOrWeakPtr
Satisfied by specializations of std::shared_ptr and std::weak_ptr.
constexpr T safe_div(T x, T y)
Performs safe division, returning 0 if the denominator is zero.
std::chrono::high_resolution_clock Clock
Alias for high-resolution clock from the chrono library.
requires constexpr std::same_as< typename A::element_type, typename B::element_type > bool owner_equal(A const &a, B const &b) noexcept
Backport of std::weak_ptr::owner_equal / std::owner_equal from C++26.
constexpr bool contains(R &&range, T const &value, Proj proj={})
Backport of std::ranges::contains from C++23 for C++20.
requires std::is_arithmetic_v< To > &&constexpr std::is_arithmetic_v< From > To safe_cast(From value, std::source_location const &loc=std::source_location::current())
Safely casts a numeric value to another type with overflow checking.
auto to_vector(MapType &&map)
Converts a map-like associative container to a vector by moving the values and discarding the keys.
Helper for overloaded lambdas using std::visit.