alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
ViewAccessOps.hpp
Go to the documentation of this file.
1/* Copyright 2023 Andrea Bocci, Bernhard Manfred Gruber, Jan Stephan
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7#include "alpaka/acc/Tag.hpp"
11
12#include <cstdint>
13#include <sstream>
14#include <stdexcept>
15#include <type_traits>
16#include <utility>
17
18namespace alpaka
19{
20 class DevCpu;
21} // namespace alpaka
22
23namespace alpaka::internal
24{
25
26 template<typename TView>
27 concept ViewType = requires {
28 typename Idx<TView>;
29 typename Dim<TView>;
30 {
31 getPtrNative(std::declval<TView>())
32 };
33 {
34 getPitchesInBytes(std::declval<TView>())
35 };
36 {
37 getExtents(std::declval<TView>())
38 };
39 };
40
41 template<ViewType TView>
43 {
44 private:
45 using value_type = Elem<TView>;
46 using pointer = value_type*;
47 using const_pointer = value_type const*;
48 using reference = value_type&;
49 using const_reference = value_type const&;
50 using Idx = alpaka::Idx<TView>;
51 using Dim = alpaka::Dim<TView>;
52
53 public:
54 [[nodiscard]] ALPAKA_FN_HOST auto data() -> pointer
55 {
56 return getPtrNative(*static_cast<TView*>(this));
57 }
58
59 [[nodiscard]] ALPAKA_FN_HOST auto data() const -> const_pointer
60 {
61 return getPtrNative(*static_cast<TView const*>(this));
62 }
63 };
64
65 template<ViewType TView>
67 {
68 private:
69 using value_type = Elem<TView>;
70 using pointer = value_type*;
71 using const_pointer = value_type const*;
72 using reference = value_type&;
73 using const_reference = value_type const&;
74 using Idx = alpaka::Idx<TView>;
75 using Dim = alpaka::Dim<TView>;
76
77 public:
78 [[nodiscard]] ALPAKA_FN_HOST auto data() -> pointer
79 {
80 return getPtrNative(*static_cast<TView*>(this));
81 }
82
83 [[nodiscard]] ALPAKA_FN_HOST auto data() const -> const_pointer
84 {
85 return getPtrNative(*static_cast<TView const*>(this));
86 }
87
88 ALPAKA_FN_HOST auto operator*() -> reference
89 {
90 static_assert(Dim::value == 0, "operator* is only valid for Buffers and Views of dimension 0");
91 return *data();
92 }
93
94 ALPAKA_FN_HOST auto operator*() const -> const_reference
95 {
96 static_assert(Dim::value == 0, "operator* is only valid for Buffers and Views of dimension 0");
97 return *data();
98 }
99
100 ALPAKA_FN_HOST auto operator->() -> pointer
101 {
102 static_assert(Dim::value == 0, "operator-> is only valid for Buffers and Views of dimension 0");
103 return data();
104 }
105
106 ALPAKA_FN_HOST auto operator->() const -> const_pointer
107 {
108 static_assert(Dim::value == 0, "operator-> is only valid for Buffers and Views of dimension 0");
109 return data();
110 }
111
112 ALPAKA_FN_HOST auto operator[](Idx i) -> reference
113 {
114 static_assert(Dim::value == 1, "operator[i] is only valid for Buffers and Views of dimension 1");
115 return data()[i];
116 }
117
118 ALPAKA_FN_HOST auto operator[](Idx i) const -> const_reference
119 {
120 static_assert(Dim::value == 1, "operator[i] is only valid for Buffers and Views of dimension 1");
121 return data()[i];
122 }
123
124 private:
125 template<typename TIdx>
126 [[nodiscard]] ALPAKA_FN_HOST auto ptr_at([[maybe_unused]] Vec<Dim, TIdx> index) const -> const_pointer
127 {
128 static_assert(
129 std::is_convertible_v<TIdx, Idx>,
130 "the index type must be convertible to the index of the Buffer or View");
131
132 auto ptr = reinterpret_cast<std::uintptr_t>(data());
133 if constexpr(Dim::value > 0)
134 {
135 ptr += static_cast<std::uintptr_t>(
136 (getPitchesInBytes(*static_cast<TView const*>(this)) * castVec<Idx>(index)).sum());
137 }
138 return reinterpret_cast<const_pointer>(ptr);
139 }
140
141 public:
142 template<typename TIdx>
143 ALPAKA_FN_HOST auto operator[](Vec<Dim, TIdx> index) -> reference
144 {
145 return *const_cast<pointer>(ptr_at(index));
146 }
147
148 template<typename TIdx>
149 ALPAKA_FN_HOST auto operator[](Vec<Dim, TIdx> index) const -> const_reference
150 {
151 return *ptr_at(index);
152 }
153
154 template<typename TIdx>
155 ALPAKA_FN_HOST auto at(Vec<Dim, TIdx> index) -> reference
156 {
157 auto extent = getExtents(*static_cast<TView*>(this));
158 if(!(index < extent).all())
159 {
160 std::stringstream msg;
161 msg << "index " << index << " is outside of the Buffer or View extent " << extent;
162 throw std::out_of_range(msg.str());
163 }
164 return *const_cast<pointer>(ptr_at(index));
165 }
166
167 template<typename TIdx>
168 [[nodiscard]] ALPAKA_FN_HOST auto at(Vec<Dim, TIdx> index) const -> const_reference
169 {
170 auto extent = getExtents(*static_cast<TView const*>(this));
171 if(!(index < extent).all())
172 {
173 std::stringstream msg;
174 msg << "index " << index << " is outside of the Buffer or View extent " << extent;
175 throw std::out_of_range(msg.str());
176 }
177 return *ptr_at(index);
178 }
179 };
180
181 template<typename TDev>
183 {
184 template<ViewType TView>
186 };
187
188 template<>
190 {
191 template<ViewType TView>
193 };
194
195#ifdef ALPAKA_ACC_SYCL_ENABLED
196 template<>
197 struct ViewAccessor<alpaka::DevGenericSycl<alpaka::TagCpuSycl>>
198 {
199 template<ViewType TView>
201 };
202#endif
203
204 template<typename TDev, ViewType TView>
205 using ViewAccessorType = typename ViewAccessor<TDev>::template AccessorType<TView>;
206
207} // namespace alpaka::internal
The CPU device handle.
Definition DevCpu.hpp:56
A n-dimensional vector.
Definition Vec.hpp:38
#define ALPAKA_FN_HOST
Definition Common.hpp:40
typename ViewAccessor< TDev >::template AccessorType< TView > ViewAccessorType
The alpaka accelerator library.
typename trait::IdxType< T >::type Idx
Definition Traits.hpp:29
ALPAKA_FN_HOST auto getPitchesInBytes(TView const &view) -> Vec< Dim< TView >, Idx< TView > >
Definition Traits.hpp:225
ALPAKA_FN_HOST auto getPtrNative(TView const &view) -> Elem< TView > const *
Gets the native pointer of the memory view.
Definition Traits.hpp:165
std::remove_volatile_t< typename trait::ElemType< TView >::type > Elem
The element type trait alias template to remove the ::type.
Definition Traits.hpp:21
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getExtents(T const &object) -> Vec< Dim< T >, Idx< T > >
Definition Traits.hpp:59
typename trait::DimType< T >::type Dim
The dimension type trait alias template to remove the ::type.
Definition Traits.hpp:19
ALPAKA_FN_HOST auto data() const -> const_pointer
ALPAKA_FN_HOST auto data() -> pointer
ALPAKA_FN_HOST auto at(Vec< Dim, TIdx > index) const -> const_reference
ALPAKA_FN_HOST auto operator[](Vec< Dim, TIdx > index) -> reference
ALPAKA_FN_HOST auto data() -> pointer
ALPAKA_FN_HOST auto at(Vec< Dim, TIdx > index) -> reference
ALPAKA_FN_HOST auto operator[](Idx i) -> reference
ALPAKA_FN_HOST auto operator*() const -> const_reference
ALPAKA_FN_HOST auto operator*() -> reference
ALPAKA_FN_HOST auto operator->() const -> const_pointer
ALPAKA_FN_HOST auto operator[](Vec< Dim, TIdx > index) const -> const_reference
ALPAKA_FN_HOST auto operator[](Idx i) const -> const_reference
ALPAKA_FN_HOST auto operator->() -> pointer
ALPAKA_FN_HOST auto data() const -> const_pointer
DeviceViewAccessor< TView > AccessorType