cuda-api-wrappers
Thin C++-flavored wrappers for the CUDA Runtime API
unique_span.hpp
Go to the documentation of this file.
1 
6 #pragma once
7 #ifndef MULTI_WRAPPER_IMPLS_UNIQUE_SPAN_HPP_
8 #define MULTI_WRAPPER_IMPLS_UNIQUE_SPAN_HPP_
9 
10 #include "../detail/unique_span.hpp"
11 #include "../current_device.hpp"
12 #include "../current_context.hpp"
13 #include "../primary_context.hpp"
14 #include "../memory.hpp"
15 #include "../types.hpp"
16 #include "../device.hpp"
17 
18 namespace cuda {
19 
20 namespace memory {
21 
22 namespace device {
23 
24 template <typename T>
25 unique_span<T> make_unique_span(const context_t& context, size_t num_elements)
26 {
27  return detail_::make_unique_span<T>(context.handle(), num_elements);
28 }
29 
40 template <typename T>
41 unique_span<T> make_unique_span(const device_t& device, size_t num_elements)
42 {
43  auto pc = device.primary_context();
44  CAW_SET_SCOPE_CONTEXT(pc.handle());
45  return make_unique_span<T>(pc, num_elements);
46 }
47 
61 template <typename T>
62 unique_span<T> make_unique_span(size_t num_elements)
63 {
64  auto current_device_id = cuda::device::current::detail_::get_id();
65  auto pc = cuda::device::primary_context::detail_::leaky_get(current_device_id);
66  return make_unique_span<T>(pc, num_elements);
67 }
68 
69 } // namespace device
70 
71 namespace managed {
72 
73 template <typename T>
74 unique_span<T> make_unique_span(
75  const context_t& context,
76  size_t size,
77  initial_visibility_t initial_visibility)
78 {
79  CAW_SET_SCOPE_CONTEXT(context.handle());
80  switch (initial_visibility) {
81  case initial_visibility_t::to_all_devices:
82  return detail_::make_unique_span<T, initial_visibility_t::to_all_devices>(context.handle(), size);
83  case initial_visibility_t::to_supporters_of_concurrent_managed_access:
84  return detail_::make_unique_span<T, initial_visibility_t::to_supporters_of_concurrent_managed_access>(context.handle(), size);
85  default:
86  throw ::std::logic_error("Library not yet updated to support additional initial visibility values");
87  }
88 }
89 
90 template <typename T>
91 unique_span<T> make_unique_span(
92  const device_t& device,
93  size_t size,
94  initial_visibility_t initial_visibility)
95 {
96  auto pc = device.primary_context();
97  return make_unique_span<T>(pc, size, initial_visibility);
98 }
99 
100 template <typename T>
101 unique_span<T> make_unique_span(
102  size_t size,
103  initial_visibility_t initial_visibility)
104 {
105  auto current_device_id = cuda::device::current::detail_::get_id();
106  auto pc = cuda::device::primary_context::detail_::leaky_get(current_device_id);
107  return make_unique_span<T>(pc, size, initial_visibility);
108 }
109 
110 } // namespace managed
111 
112 } // namespace memory
113 
114 } // namespace cuda
115 
116 #endif // MULTI_WRAPPER_IMPLS_UNIQUE_SPAN_HPP_
117 
Wrapper class for a CUDA context.
Definition: context.hpp:244
Definitions and functionality wrapping CUDA APIs.
Definition: array.hpp:22
unique_span< T > make_unique_span(const context_t &context, size_t size)
Allocate memory for a consecutive sequence of typed elements in device-global memory.
Definition: unique_span.hpp:25
device::primary_context_t primary_context(bool hold_pc_refcount_unit=false) const
Produce a proxy for the device&#39;s primary context - the one used by runtime API calls.
Definition: device.hpp:152
Wrapper class for a CUDA device.
Definition: device.hpp:135
initial_visibility_t
The choices of which categories CUDA devices must a managed memory region be visible to...
Definition: types.hpp:753