alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
ViewSubView.hpp
Go to the documentation of this file.
1/* Copyright 2022 Benjamin Worpitz, Matthias Werner, Bernhard Manfred Gruber
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
10#include "alpaka/dim/Traits.hpp"
12#include "alpaka/idx/Traits.hpp"
17#include "alpaka/vec/Vec.hpp"
18
19#include <type_traits>
20#include <utility>
21
22namespace alpaka
23{
24 //! A sub-view to a view.
25 template<typename TDev, typename TElem, typename TDim, typename TIdx>
26 class ViewSubView : public internal::ViewAccessOps<ViewSubView<TDev, TElem, TDim, TIdx>>
27 {
28 static_assert(!std::is_const_v<TIdx>, "The idx type of the view can not be const!");
29
30 using Dev = alpaka::Dev<TDev>;
31
32 public:
33 //! Constructor.
34 //! \param view The view this view is a sub-view of.
35 //! \param extentElements The extent in elements.
36 //! \param relativeOffsetsElements The offsets in elements.
37 template<typename TQualifiedView, typename TOffsets, typename TExtent>
39 TQualifiedView& view,
40 TExtent const& extentElements,
41 TOffsets const& relativeOffsetsElements = TOffsets())
43 , m_extentElements(getExtents(extentElements))
44 , m_offsetsElements(getOffsets(relativeOffsetsElements))
46 {
48
49 using View = std::remove_cv_t<TQualifiedView>;
50
51 static_assert(
52 std::is_same_v<Dev, alpaka::Dev<View>>,
53 "The dev type of TView and the Dev template parameter have to be identical!");
54
55 static_assert(
56 std::is_same_v<TIdx, alpaka::Idx<View>>,
57 "The idx type of TView and the TIdx template parameter have to be identical!");
58 static_assert(
59 std::is_same_v<TIdx, alpaka::Idx<TExtent>>,
60 "The idx type of TExtent and the TIdx template parameter have to be identical!");
61 static_assert(
62 std::is_same_v<TIdx, alpaka::Idx<TOffsets>>,
63 "The idx type of TOffsets and the TIdx template parameter have to be identical!");
64
65 static_assert(
66 std::is_same_v<TDim, alpaka::Dim<View>>,
67 "The dim type of TView and the TDim template parameter have to be identical!");
68 static_assert(
69 std::is_same_v<TDim, alpaka::Dim<TExtent>>,
70 "The dim type of TExtent and the TDim template parameter have to be identical!");
71 static_assert(
72 std::is_same_v<TDim, alpaka::Dim<TOffsets>>,
73 "The dim type of TOffsets and the TDim template parameter have to be identical!");
74
76 }
77
78 //! \param view The view this view is a sub-view of.
79 template<typename TView>
80 explicit ViewSubView(TView const& view) : ViewSubView(view, getExtents(view), Vec<TDim, TIdx>::zeros())
81 {
83 }
84
85 //! \param view The view this view is a sub-view of.
86 template<typename TView>
87 explicit ViewSubView(TView& view) : ViewSubView(view, getExtents(view), Vec<TDim, TIdx>::zeros())
88 {
90 }
91
92 public:
94 {
97 std::byte* ptr = reinterpret_cast<std::byte*>(base) + offset;
98 return reinterpret_cast<TElem*>(__builtin_assume_aligned(ptr, alignof(TElem)));
99 }
100
102 Vec<TDim, TIdx> m_extentElements; // The extent of this view.
103 Vec<TDim, TIdx> m_offsetsElements; // The offset relative to the parent view.
105 };
106
107 // Trait specializations for ViewSubView.
108 namespace trait
109 {
110 //! The ViewSubView device type trait specialization.
111 template<typename TElem, typename TDim, typename TDev, typename TIdx>
112 struct DevType<ViewSubView<TDev, TElem, TDim, TIdx>>
113 {
115 };
116
117 //! The ViewSubView device get trait specialization.
118 template<typename TElem, typename TDim, typename TDev, typename TIdx>
119 struct GetDev<ViewSubView<TDev, TElem, TDim, TIdx>>
120 {
122 {
123 return alpaka::getDev(view.m_viewParentView);
124 }
125 };
126
127 //! The ViewSubView dimension getter trait specialization.
128 template<typename TElem, typename TDim, typename TDev, typename TIdx>
129 struct DimType<ViewSubView<TDev, TElem, TDim, TIdx>>
130 {
131 using type = TDim;
132 };
133
134 //! The ViewSubView memory element type get trait specialization.
135 template<typename TElem, typename TDim, typename TDev, typename TIdx>
136 struct ElemType<ViewSubView<TDev, TElem, TDim, TIdx>>
137 {
138 using type = TElem;
139 };
140
141 //! The ViewSubView width get trait specialization.
142 template<typename TElem, typename TDim, typename TDev, typename TIdx>
143 struct GetExtents<ViewSubView<TDev, TElem, TDim, TIdx>>
144 {
146 {
147 return view.m_extentElements;
148 }
149 };
150
151 //! The ViewSubView native pointer get trait specialization.
152 template<typename TElem, typename TDim, typename TDev, typename TIdx>
153 struct GetPtrNative<ViewSubView<TDev, TElem, TDim, TIdx>>
154 {
155 ALPAKA_FN_HOST static auto getPtrNative(ViewSubView<TDev, TElem, TDim, TIdx> const& view) -> TElem const*
156 {
157 return view.m_nativePtr;
158 }
159
161 {
162 return view.m_nativePtr;
163 }
164 };
165
166 //! The ViewSubView pitch get trait specialization.
167 template<typename TDev, typename TElem, typename TDim, typename TIdx>
168 struct GetPitchesInBytes<ViewSubView<TDev, TElem, TDim, TIdx>>
169 {
174 };
175
176 //! The ViewSubView x offset get trait specialization.
177 template<typename TElem, typename TDim, typename TDev, typename TIdx>
178 struct GetOffsets<ViewSubView<TDev, TElem, TDim, TIdx>>
179 {
181 {
182 return offset.m_offsetsElements;
183 }
184 };
185
186 //! The ViewSubView idx type trait specialization.
187 template<typename TElem, typename TDim, typename TDev, typename TIdx>
188 struct IdxType<ViewSubView<TDev, TElem, TDim, TIdx>>
189 {
190 using type = TIdx;
191 };
192
193 //! The CPU device CreateSubView trait default implementation
194 template<typename TDev, typename TSfinae>
196 {
197 template<typename TView, typename TExtent, typename TOffsets>
198 static auto createSubView(
199 TView& view,
200 TExtent const& extentElements,
201 TOffsets const& relativeOffsetsElements)
202 {
205 using Elem = typename trait::ElemType<TView>::type;
206 return ViewSubView<TDev, Elem, Dim, Idx>(view, extentElements, relativeOffsetsElements);
207 }
208 };
209 } // namespace trait
210} // namespace alpaka
#define ALPAKA_ASSERT(...)
The assert can be explicit disabled by defining NDEBUG.
Definition Assert.hpp:13
#define ALPAKA_DEBUG_FULL_LOG_SCOPE
Definition Debug.hpp:62
A n-dimensional vector.
Definition Vec.hpp:38
A sub-view to a view.
ViewSubView(TView &view)
ViewPlainPtr< Dev, TElem, TDim, TIdx > m_viewParentView
Vec< TDim, TIdx > m_extentElements
ALPAKA_FN_HOST auto computeNativePtr()
ViewSubView(TQualifiedView &view, TExtent const &extentElements, TOffsets const &relativeOffsetsElements=TOffsets())
Constructor.
ViewSubView(TView const &view)
Vec< TDim, TIdx > m_offsetsElements
#define ALPAKA_FN_HOST
Definition Common.hpp:40
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:199
typename trait::DevType< T >::type Dev
The device type trait alias template to remove the ::type.
Definition Traits.hpp:56
ALPAKA_FN_HOST auto getPtrNative(TView const &view) -> Elem< TView > const *
Gets the native pointer of the memory view.
Definition Traits.hpp:139
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
ALPAKA_FN_HOST auto getDev(T const &t)
Definition Traits.hpp:68
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getOffsets(T const &object) -> Vec< Dim< T >, Idx< T > >
Definition Traits.hpp:55
typename trait::DimType< T >::type Dim
The dimension type trait alias template to remove the ::type.
Definition Traits.hpp:19
The memory view to wrap plain pointers.
The sub view creation trait.
static auto createSubView(TView &view, TExtent const &extentElements, TOffsets const &relativeOffsetsElements)
The device type trait.
Definition Traits.hpp:23
The dimension getter type trait.
Definition Traits.hpp:14
The element type trait.
Definition Traits.hpp:16
static ALPAKA_FN_HOST auto getDev(ViewSubView< TDev, TElem, TDim, TIdx > const &view) -> alpaka::Dev< TDev >
The device get trait.
Definition Traits.hpp:27
ALPAKA_FN_HOST auto operator()(ViewSubView< TDev, TElem, TDim, TIdx > const &view) const
The GetExtents trait for getting the extents of an object as an alpaka::Vec.
Definition Traits.hpp:37
ALPAKA_FN_HOST auto operator()(ViewSubView< TDev, TElem, TDim, TIdx > const &offset)
The GetOffsets trait for getting the offsets of an object as an alpaka::Vec.
Definition Traits.hpp:33
ALPAKA_FN_HOST auto operator()(ViewSubView< TDev, TElem, TDim, TIdx > const &view) const
Customization point for getPitchesInBytes. The default implementation uses the extent to calculate th...
Definition Traits.hpp:103
static ALPAKA_FN_HOST auto getPtrNative(ViewSubView< TDev, TElem, TDim, TIdx > const &view) -> TElem const *
static ALPAKA_FN_HOST auto getPtrNative(ViewSubView< TDev, TElem, TDim, TIdx > &view) -> TElem *
The native pointer get trait.
Definition Traits.hpp:54
The idx type trait.
Definition Traits.hpp:25