16 #ifndef CUDA_API_WRAPPERS_POINTER_HPP_ 17 #define CUDA_API_WRAPPERS_POINTER_HPP_ 41 enum type_t : ::std::underlying_type<CUmemorytype>::type {
42 host_ = CU_MEMORYTYPE_HOST,
43 device_ = CU_MEMORYTYPE_DEVICE,
44 array = CU_MEMORYTYPE_ARRAY,
45 unified_ = CU_MEMORYTYPE_UNIFIED,
46 managed_ = CU_MEMORYTYPE_UNIFIED,
47 non_cuda = ~(::std::underlying_type<CUmemorytype>::type{0})
56 void get_attributes(
unsigned num_attributes,
pointer::attribute_t* attributes,
void** value_ptrs,
const void* ptr);
58 template <attribute_t attribute>
struct attribute_value {};
59 template <>
struct attribute_value<CU_POINTER_ATTRIBUTE_CONTEXT> {
using type =
context::handle_t;};
60 template <>
struct attribute_value<CU_POINTER_ATTRIBUTE_MEMORY_TYPE> {
using type =
memory::type_t;};
61 template <>
struct attribute_value<CU_POINTER_ATTRIBUTE_DEVICE_POINTER> {
using type =
void*;};
62 template <>
struct attribute_value<CU_POINTER_ATTRIBUTE_HOST_POINTER> {
using type =
void*;};
63 template <>
struct attribute_value<CU_POINTER_ATTRIBUTE_P2P_TOKENS> {
using type =
struct CUDA_POINTER_ATTRIBUTE_P2P_TOKEN;};
64 template <>
struct attribute_value<CU_POINTER_ATTRIBUTE_SYNC_MEMOPS> {
using type = int;};
65 template <>
struct attribute_value<CU_POINTER_ATTRIBUTE_BUFFER_ID> {
using type =
unsigned long long;};
66 template <>
struct attribute_value<CU_POINTER_ATTRIBUTE_IS_MANAGED> {
using type = int;};
67 #if CUDA_VERSION >= 9020 68 template <>
struct attribute_value<CU_POINTER_ATTRIBUTE_DEVICE_ORDINAL> {
using type =
cuda::device::id_t;};
69 #if CUDA_VERSION >= 10020 70 template <>
struct attribute_value<CU_POINTER_ATTRIBUTE_RANGE_START_ADDR> {
using type =
void*;};
71 template <>
struct attribute_value<CU_POINTER_ATTRIBUTE_RANGE_SIZE> {
using type =
size_t;};
72 template <>
struct attribute_value<CU_POINTER_ATTRIBUTE_MAPPED> {
using type = int;};
73 template <>
struct attribute_value<CU_POINTER_ATTRIBUTE_IS_LEGACY_CUDA_IPC_CAPABLE> {
using type = int;};
74 template <>
struct attribute_value<CU_POINTER_ATTRIBUTE_ALLOWED_HANDLE_TYPES> {
using type = uint64_t;};
75 #if CUDA_VERSION >= 11030 76 template <>
struct attribute_value<CU_POINTER_ATTRIBUTE_MEMPOOL_HANDLE> {
using type =
pool::handle_t;};
77 #endif // CUDA_VERSION >= 11030 78 #endif // CUDA_VERSION >= 10020 79 #endif // CUDA_VERSION >= 9020 81 template <CUpo
inter_attribute attribute>
82 using attribute_value_t =
typename attribute_value<attribute>::type;
84 template<attribute_t attribute>
85 struct status_and_attribute_value {
87 attribute_value_t<attribute> value;
90 template<attribute_t attribute>
91 status_and_attribute_value<attribute> get_attribute_with_status(
const void *ptr);
93 template <attribute_t attribute>
94 attribute_value_t<attribute> get_attribute(
const void* ptr);
98 return pointer::detail_::get_attribute<CU_POINTER_ATTRIBUTE_CONTEXT>(ptr);
114 auto result = pointer::detail_::get_attribute_with_status<CU_POINTER_ATTRIBUTE_MEMORY_TYPE>(ptr);
117 return (result.status == status::named_t::invalid_value) ?
118 memory::type_t::non_cuda : result.value;
130 template <
typename T>
138 T*
get()
const {
return ptr_; }
140 operator T*()
const {
return ptr_; }
144 template <po
inter::attribute_t attribute>
145 pointer::detail_::attribute_value_t<attribute> get_attribute()
const 147 return pointer::detail_::get_attribute<attribute>(ptr_);
172 return pointer::detail_::get_attribute<CU_POINTER_ATTRIBUTE_DEVICE_POINTER>(ptr_);
184 return pointer::detail_::get_attribute<CU_POINTER_ATTRIBUTE_HOST_POINTER>(ptr_);
187 #if CUDA_VERSION >= 10020 191 auto range_start = pointer::detail_::get_attribute<CU_POINTER_ATTRIBUTE_RANGE_START_ADDR>(ptr_);
192 auto range_size = pointer::detail_::get_attribute<CU_POINTER_ATTRIBUTE_RANGE_SIZE>(ptr_);
211 CU_POINTER_ATTRIBUTE_MEMORY_TYPE,
212 CU_POINTER_ATTRIBUTE_HOST_POINTER,
213 CU_POINTER_ATTRIBUTE_DEVICE_POINTER
218 void* value_ptrs[] = { &memory_type, &host_ptr, &device_ptr };
219 pointer::detail_::get_attributes(3, attributes, value_ptrs, ptr_);
222 assert(host_ptr == ptr_ or device_ptr == ptr_);
224 return { ptr_ == host_ptr ? device_ptr : host_ptr };
252 #endif // CUDA_API_WRAPPERS_POINTER_HPP_ range::handle_t range_start(const CharT *description, range::type_t type=range::type_t::unspecified, color_t color=color_t::LightRed())
Mark the beginning of a range on the profiler timeline, giving it also a color and some descriptive t...
Definition: profiling.hpp:203
Wrapper class for a CUDA context.
Definition: context.hpp:244
Definitions and functionality wrapping CUDA APIs.
Definition: array.hpp:22
detail_::region_helper< memory::region_t > region_t
A child class of the generic region_t with some managed-memory-specific functionality.
Definition: memory.hpp:1960
CUcontext handle_t
Raw CUDA driver handle for a context; see {context_t}.
Definition: types.hpp:878
pointer_t(T *ptr) noexcept
Wrap a raw pointer in this class.
Definition: pointer.hpp:229
CUdevice id_t
Numeric ID of a CUDA device used by the CUDA Runtime API.
Definition: types.hpp:850
memory::type_t type_of(const void *ptr)
Determine the type of memory at a given address vis-a-vis the CUDA ecosystem: Was it allocated by the...
Definition: pointer.hpp:112
context_t context_of(const void *ptr)
Obtain (a non-owning wrapper for) the CUDA context with which a memory address is associated (e...
Definition: pointer.hpp:50
CUpointer_attribute attribute_t
Raw CUDA driver choice type for attributes of pointers.
Definition: types.hpp:662
::std::size_t size_t
A size type for use throughout the wrappers library (except when specific API functions limit the siz...
Definition: types.hpp:81
pointer_t other_side_of_region_pair() const
Definition: pointer.hpp:208
A convenience wrapper around a raw pointer "known" to the CUDA runtime and which thus has various kin...
Definition: pointer.hpp:131
CUarray handle_t
Raw CUDA driver handle for arrays (of any dimension)
Definition: array.hpp:34
Facilities for exception-based handling of Runtime and Driver API errors, including a basic exception...
Fundamental CUDA-related constants and enumerations, not dependent on any more complex abstractions...
T * get_for_device() const
Definition: pointer.hpp:170
type_t
The CUDA execution ecosystem involves different memory spaces in their relation to a GPU device or th...
Definition: pointer.hpp:41
T * get_for_host() const
Definition: pointer.hpp:182
Wrapper class for a CUDA device.
Definition: device.hpp:135
Fundamental CUDA-related type definitions.
CUresult status_t
Indicates either the result (success or error index) of a CUDA Runtime or Driver API call...
Definition: types.hpp:77