cuda-api-wrappers
Thin C++-flavored wrappers for the CUDA Runtime API
pointer.hpp
Go to the documentation of this file.
1 
7 #pragma once
8 #ifndef MULTI_WRAPPER_IMPLS_POINTER_HPP_
9 #define MULTI_WRAPPER_IMPLS_POINTER_HPP_
10 
11 #include "../pointer.hpp"
12 #include "../device.hpp"
13 #include "../context.hpp"
14 
15 namespace cuda {
16 
17 namespace memory {
18 
19 namespace pointer {
20 
21 namespace detail_ {
22 
23 inline cuda::device::id_t device_id_of(const void *ptr)
24 {
25 #if CUDA_VERSION >= 9020
26  return pointer::detail_::get_attribute<CU_POINTER_ATTRIBUTE_DEVICE_ORDINAL>(ptr);
27 #else
28  auto context_handle = context_handle_of(ptr);
29  return context::detail_::get_device_id(context_handle);
30 #endif
31 }
32 
33 } // namespace detail_
34 
35 } // namespace pointer
36 
37 
38 template <typename T>
40 {
41  return cuda::device::get(pointer::detail_::device_id_of(ptr_));
42 }
43 
44 template <typename T>
46 {
47  return context_of(ptr_);
48 }
49 
50 inline context_t context_of(const void* ptr)
51 {
52 #if CUDA_VERSION >= 9020
53  pointer::attribute_t attributes[] = {
54  CU_POINTER_ATTRIBUTE_DEVICE_ORDINAL,
55  CU_POINTER_ATTRIBUTE_CONTEXT
56  };
57  cuda::device::id_t device_id;
58  context::handle_t context_handle;
59  void* value_ptrs[] = {&device_id, &context_handle};
60  pointer::detail_::get_attributes(2, attributes, value_ptrs, ptr);
61 #else
62  auto context_handle = pointer::detail_::context_handle_of(ptr);
63  auto device_id = context::detail_::get_device_id(context_handle);
64 #endif
65  return context::wrap(device_id, context_handle);
66 }
67 
68 } // namespace memory
69 
70 } // namespace cuda
71 
72 #endif // MULTI_WRAPPER_IMPLS_POINTER_HPP_
73 
Wrapper class for a CUDA context.
Definition: context.hpp:244
Definitions and functionality wrapping CUDA APIs.
Definition: array.hpp:22
CUcontext handle_t
Raw CUDA driver handle for a context; see {context_t}.
Definition: types.hpp:878
CUdevice id_t
Numeric ID of a CUDA device used by the CUDA Runtime API.
Definition: types.hpp:850
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
device_t device() const
Returns a proxy for the device into whose global memory the pointer points.
Definition: pointer.hpp:39
device_t get(id_t id)
Returns a proxy for the CUDA device with a given id.
Definition: device.hpp:837
context_t context() const
Returns a proxy for the context in which the memory area, into which the pointer points, was allocated.
Definition: pointer.hpp:45
Wrapper class for a CUDA device.
Definition: device.hpp:135