Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
cuda_accessor.hpp
Go to the documentation of this file.
1#pragma once
2
10#include <uni20/core/types.hpp>
12
13#include <cuda/std/complex>
14
15#include <cstddef>
16#include <memory>
17#include <type_traits>
18
19namespace uni20::cuda
20{
21
26template <class Real>
27 requires std::is_trivially_copyable_v<uni20::complex<Real>>
29 public:
31 using execution_type = ::cuda::std::complex<Real>;
32
33 UNI20_HOST_DEVICE constexpr CudaComplexReference(logical_type* storage, std::size_t offset) noexcept
34 : components_(reinterpret_cast<Real*>(storage) + 2 * offset)
35 {}
36
37 UNI20_HOST_DEVICE constexpr CudaComplexReference(CudaComplexReference const&) noexcept = default;
38
40 [[nodiscard]] UNI20_HOST_DEVICE constexpr operator execution_type() const noexcept
41 {
42 return execution_type{components_[0], components_[1]};
43 }
44
47 {
48 components_[0] = value.real();
49 components_[1] = value.imag();
50 return *this;
51 }
52
55 {
56 return *this = static_cast<execution_type>(value);
57 }
58
61 {
62 auto const* components = reinterpret_cast<Real const*>(&value);
63 components_[0] = components[0];
64 components_[1] = components[1];
65 return *this;
66 }
67
68 private:
69 Real* components_;
70};
71
77template <class ElementType>
78 requires std::is_trivially_copyable_v<ElementType>
80{
81 using element_type = ElementType;
85 using offset_type = std::size_t;
86
88 offset_type offset) const noexcept
89 {
90 return handle[offset];
91 }
92
94 offset_type offset) const noexcept
95 {
96 if (offset == 0) return handle;
97 return handle + offset;
98 }
99};
100
102template <class Real> struct CudaPointerAccessor<uni20::detail::standard_complex<Real>>
103{
106 using reference = CudaComplexReference<Real>;
107 using offset_policy = CudaPointerAccessor;
108 using offset_type = std::size_t;
109
111 offset_type offset) const noexcept
112 {
113 return reference{handle, offset};
114 }
115
117 offset_type offset) const noexcept
118 {
119 if (offset == 0) return handle;
120 return handle + offset;
121 }
122};
123
125template <class Real> struct CudaPointerAccessor<uni20::detail::standard_complex<Real> const>
126{
129 using reference = ::cuda::std::complex<Real>;
130 using offset_policy = CudaPointerAccessor;
131 using offset_type = std::size_t;
132
134 offset_type offset) const noexcept
135 {
136 auto const* components = reinterpret_cast<Real const*>(handle) + 2 * offset;
137 return reference{components[0], components[1]};
138 }
139
141 offset_type offset) const noexcept
142 {
143 if (offset == 0) return handle;
144 return handle + offset;
145 }
146};
147
150{
151 template <class Real>
152 [[nodiscard]] UNI20_HOST_DEVICE constexpr auto operator()(::cuda::std::complex<Real> value) const noexcept
153 -> ::cuda::std::complex<Real>
154 {
155 return ::cuda::std::conj(value);
156 }
157
158 template <class Real>
159 [[nodiscard]] UNI20_HOST_DEVICE constexpr auto operator()(CudaComplexReference<Real> value) const noexcept
160 -> ::cuda::std::complex<Real>
161 {
162 using execution_type = ::cuda::std::complex<Real>;
163 return ::cuda::std::conj(static_cast<execution_type>(value));
164 }
165};
166
168template <class Real>
169 requires std::is_trivially_copyable_v<uni20::complex<Real>>
190
192template <class ElementType>
193 requires(!std::is_const_v<ElementType>)
198
199} // namespace uni20::cuda
200
201namespace uni20
202{
203
204template <class Real> struct remove_proxy_reference<cuda::CudaComplexReference<Real>>
205{
207};
208
209template <class Real> struct logical_value<::cuda::std::complex<Real>>
210{
212};
213
215template <class ElementType>
216inline constexpr bool enable_accessor_in_domain<cuda::CudaPointerAccessor<ElementType>, cuda_access_domain> = true;
217
218template <class Real>
219inline constexpr bool enable_accessor_in_domain<cuda::CudaConjugatingPointerAccessor<Real>, cuda_access_domain> = true;
220
221} // namespace uni20
Assignable CUDA execution proxy over one persistent complex value.
Definition cuda_accessor.hpp:28
UNI20_HOST_DEVICE constexpr CudaComplexReference(logical_type *storage, std::size_t offset) noexcept
Definition cuda_accessor.hpp:33
UNI20_HOST_DEVICE constexpr CudaComplexReference & operator=(execution_type const &value) noexcept
Store a CUDA execution value componentwise.
Definition cuda_accessor.hpp:46
UNI20_HOST_DEVICE constexpr CudaComplexReference & operator=(CudaComplexReference const &value) noexcept
Copy the observed value rather than rebinding the proxy.
Definition cuda_accessor.hpp:54
uni20::complex< Real > logical_type
Definition cuda_accessor.hpp:30
UNI20_HOST_DEVICE constexpr CudaComplexReference(CudaComplexReference const &) noexcept=default
::cuda::std::complex< Real > execution_type
Definition cuda_accessor.hpp:31
UNI20_HOST_DEVICE constexpr CudaComplexReference & operator=(logical_type const &value) noexcept
Store a host logical value componentwise.
Definition cuda_accessor.hpp:60
Project compiler annotations shared by host and device code.
#define UNI20_HOST_DEVICE
Definition compiler_attributes.hpp:12
Concept for real scalar types (float, double, etc.).
Definition scalar_concepts.hpp:69
cuda::ResourceLease< HandleSlot > handle
Definition cublas/execution.cpp:33
locale_t value
Definition data_table.cpp:49
typename detail::complex_type< Real >::type complex
Project-level complex scalar spelling.
Definition types.hpp:66
Mdspan concept and accessor extensions for Uni20.
Definition cublas/execution.hpp:18
UNI20_HOST_DEVICE constexpr auto const_accessor(CudaPointerAccessor< ElementType > const &)
Produce a read-only CUDA pointer accessor without a generic lvalue adaptor.
Definition cuda_accessor.hpp:194
Definition async/async.hpp:26
Named CUDA-callable complex conjugation operation.
Definition cuda_accessor.hpp:150
UNI20_HOST_DEVICE constexpr auto operator()(CudaComplexReference< Real > value) const noexcept -> ::cuda::std::complex< Real >
Definition cuda_accessor.hpp:159
UNI20_HOST_DEVICE constexpr auto operator()(::cuda::std::complex< Real > value) const noexcept -> ::cuda::std::complex< Real >
Definition cuda_accessor.hpp:152
Read-only CUDA accessor that conjugates persistent complex storage.
Definition cuda_accessor.hpp:171
::cuda::std::complex< Real > reference
Definition cuda_accessor.hpp:174
element_type * data_handle_type
Definition cuda_accessor.hpp:173
UNI20_HOST_DEVICE constexpr data_handle_type offset(data_handle_type handle, offset_type offset) const noexcept
Definition cuda_accessor.hpp:184
uni20::complex< Real > const element_type
Definition cuda_accessor.hpp:172
UNI20_HOST_DEVICE constexpr reference access(data_handle_type handle, offset_type offset) const noexcept
Definition cuda_accessor.hpp:178
std::size_t offset_type
Definition cuda_accessor.hpp:176
Accessor for a CUDA mdspan whose device pointer has been leased.
Definition cuda_accessor.hpp:80
UNI20_HOST_DEVICE constexpr reference access(data_handle_type handle, offset_type offset) const noexcept
Definition cuda_accessor.hpp:87
UNI20_HOST_DEVICE constexpr data_handle_type offset(data_handle_type handle, offset_type offset) const noexcept
Definition cuda_accessor.hpp:93
element_type & reference
Definition cuda_accessor.hpp:83
std::size_t offset_type
Definition cuda_accessor.hpp:85
element_type * data_handle_type
Definition cuda_accessor.hpp:82
CudaPointerAccessor offset_policy
Definition cuda_accessor.hpp:84
ElementType element_type
Definition cuda_accessor.hpp:81
Execution domain in which CUDA device code may evaluate an accessor.
Definition mdspan/concepts.hpp:78
uni20::complex< Real > type
Definition cuda_accessor.hpp:211
Map an execution expression type to its backend-neutral logical value type.
Definition types.hpp:127
uni20::complex< Real > type
Definition cuda_accessor.hpp:206
Trait for extracting the element type from a proxy reference.
Definition types.hpp:102