utils.hpp
1 /*
2  * Copyright (c) 2021-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 #pragma once
17 
18 #include <chrono>
19 #include <cstring>
20 #include <future>
21 #include <optional>
22 #include <stdexcept>
23 #include <tuple>
24 #include <type_traits>
25 
26 #include <kvikio/error.hpp>
27 #include <kvikio/shim/cuda.hpp>
28 
29 namespace kvikio {
30 
31 std::size_t get_page_size();
32 
33 [[nodiscard]] off_t convert_size2off(std::size_t x);
34 
35 [[nodiscard]] ssize_t convert_size2ssize(std::size_t x);
36 
37 [[nodiscard]] CUdeviceptr convert_void2deviceptr(void const* devPtr);
38 
42 template <typename T, std::enable_if_t<std::is_integral_v<T>>* = nullptr>
43 [[nodiscard]] std::int64_t convert_to_64bit(T value)
44 {
45  if constexpr (std::numeric_limits<T>::max() > std::numeric_limits<std::int64_t>::max()) {
46  KVIKIO_EXPECT(value <= std::numeric_limits<std::int64_t>::max(),
47  "convert_to_64bit(x): x too large to fit std::int64_t",
48  std::overflow_error);
49  }
50  return std::int64_t(value);
51 }
52 
57 [[nodiscard]] inline std::uint64_t convert_to_64bit(std::uint64_t value) { return value; }
58 
62 template <typename T, std::enable_if_t<std::is_floating_point_v<T>>* = nullptr>
63 [[nodiscard]] double convert_to_64bit(T value)
64 {
65  return double(value);
66 }
67 
76 bool is_host_memory(void const* ptr);
77 
84 [[nodiscard]] int get_device_ordinal_from_pointer(CUdeviceptr dev_ptr);
85 
94 [[nodiscard]] KVIKIO_EXPORT CUcontext get_primary_cuda_context(int ordinal);
95 
102 [[nodiscard]] std::optional<CUcontext> get_context_associated_pointer(CUdeviceptr dev_ptr);
103 
110 [[nodiscard]] bool current_context_can_access_pointer(CUdeviceptr dev_ptr);
111 
128 [[nodiscard]] CUcontext get_context_from_pointer(void const* devPtr);
129 
134  private:
135  CUcontext _ctx;
136 
137  public:
138  PushAndPopContext(CUcontext ctx);
139  PushAndPopContext(PushAndPopContext const&) = delete;
140  PushAndPopContext& operator=(PushAndPopContext const&) = delete;
142  PushAndPopContext&& operator=(PushAndPopContext&&) = delete;
144 };
145 
146 // Find the base and offset of the memory allocation `devPtr` is in
147 std::tuple<void*, std::size_t, std::size_t> get_alloc_info(void const* devPtr,
148  CUcontext* ctx = nullptr);
149 
161 template <typename T>
162 std::future<std::decay_t<T>> make_ready_future(T&& t)
163 {
164  std::promise<std::decay_t<T>> p;
165  auto fut = p.get_future();
166  p.set_value(std::forward<T>(t));
167  return fut;
168 }
169 
181 template <typename T>
182 bool is_future_done(T const& future)
183 {
184  KVIKIO_EXPECT(future.valid(),
185  "The future object does not refer to a valid shared state.",
186  std::invalid_argument);
187  return future.wait_for(std::chrono::seconds(0)) != std::future_status::timeout;
188 }
189 
190 } // namespace kvikio
Push CUDA context on creation and pop it on destruction.
Definition: utils.hpp:133
#define KVIKIO_EXPECT(...)
Macro for checking pre-conditions or conditions that throws an exception when a condition is violated...
Definition: error.hpp:216
KvikIO namespace.
Definition: batch.hpp:27
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:43
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:182
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:162
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.