utils.hpp
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #include <chrono>
8 #include <cstring>
9 #include <future>
10 #include <optional>
11 #include <stdexcept>
12 #include <tuple>
13 #include <type_traits>
14 
15 #include <kvikio/error.hpp>
16 #include <kvikio/shim/cuda.hpp>
17 
18 namespace kvikio {
19 
20 std::size_t get_page_size();
21 
22 [[nodiscard]] off_t convert_size2off(std::size_t x);
23 
24 [[nodiscard]] ssize_t convert_size2ssize(std::size_t x);
25 
26 [[nodiscard]] CUdeviceptr convert_void2deviceptr(void const* devPtr);
27 
31 template <typename T, std::enable_if_t<std::is_integral_v<T>>* = nullptr>
32 [[nodiscard]] std::int64_t convert_to_64bit(T value)
33 {
34  if constexpr (std::numeric_limits<T>::max() > std::numeric_limits<std::int64_t>::max()) {
35  KVIKIO_EXPECT(value <= std::numeric_limits<std::int64_t>::max(),
36  "convert_to_64bit(x): x too large to fit std::int64_t",
37  std::overflow_error);
38  }
39  return std::int64_t(value);
40 }
41 
46 [[nodiscard]] inline std::uint64_t convert_to_64bit(std::uint64_t value) { return value; }
47 
51 template <typename T, std::enable_if_t<std::is_floating_point_v<T>>* = nullptr>
52 [[nodiscard]] double convert_to_64bit(T value)
53 {
54  return double(value);
55 }
56 
65 bool is_host_memory(void const* ptr);
66 
73 [[nodiscard]] int get_device_ordinal_from_pointer(CUdeviceptr dev_ptr);
74 
83 [[nodiscard]] KVIKIO_EXPORT CUcontext get_primary_cuda_context(int ordinal);
84 
91 [[nodiscard]] std::optional<CUcontext> get_context_associated_pointer(CUdeviceptr dev_ptr);
92 
99 [[nodiscard]] bool current_context_can_access_pointer(CUdeviceptr dev_ptr);
100 
117 [[nodiscard]] CUcontext get_context_from_pointer(void const* devPtr);
118 
123  private:
124  CUcontext _ctx;
125 
126  public:
127  PushAndPopContext(CUcontext ctx);
128  PushAndPopContext(PushAndPopContext const&) = delete;
129  PushAndPopContext& operator=(PushAndPopContext const&) = delete;
131  PushAndPopContext&& operator=(PushAndPopContext&&) = delete;
133 };
134 
135 // Find the base and offset of the memory allocation `devPtr` is in
136 std::tuple<void*, std::size_t, std::size_t> get_alloc_info(void const* devPtr,
137  CUcontext* ctx = nullptr);
138 
150 template <typename T>
151 std::future<std::decay_t<T>> make_ready_future(T&& t)
152 {
153  std::promise<std::decay_t<T>> p;
154  auto fut = p.get_future();
155  p.set_value(std::forward<T>(t));
156  return fut;
157 }
158 
170 template <typename T>
171 bool is_future_done(T const& future)
172 {
173  KVIKIO_EXPECT(future.valid(),
174  "The future object does not refer to a valid shared state.",
175  std::invalid_argument);
176  return future.wait_for(std::chrono::seconds(0)) != std::future_status::timeout;
177 }
178 
179 } // namespace kvikio
Push CUDA context on creation and pop it on destruction.
Definition: utils.hpp:122
#define KVIKIO_EXPECT(...)
Macro for checking pre-conditions or conditions that throws an exception when a condition is violated...
Definition: error.hpp:205
KvikIO namespace.
Definition: batch.hpp:16
CUcontext get_context_from_pointer(void const *devPtr)
Return a CUDA context that can be used with the given device pointer.
std::int64_t convert_to_64bit(T value)
Help function to convert value to 64 bit signed integer.
Definition: utils.hpp:32
bool current_context_can_access_pointer(CUdeviceptr dev_ptr)
Check if the current CUDA context can access the given device pointer.
bool is_future_done(T const &future)
Check the status of the future object. True indicates that the result is available in the future's sh...
Definition: utils.hpp:171
KVIKIO_EXPORT CUcontext get_primary_cuda_context(int ordinal)
Given a device ordinal, return the primary context of the device.
std::future< std::decay_t< T > > make_ready_future(T &&t)
Create a shared state in a future object that is immediately ready.
Definition: utils.hpp:151
bool is_host_memory(void const *ptr)
Check if ptr points to host memory (as opposed to device memory)
std::optional< CUcontext > get_context_associated_pointer(CUdeviceptr dev_ptr)
Return the CUDA context associated the given device pointer, if any.
int get_device_ordinal_from_pointer(CUdeviceptr dev_ptr)
Return the device owning the pointer.