alpaka
Abstraction Library for Parallel Kernel Acceleration
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 
7 #include "alpaka/core/Assert.hpp"
8 #include "alpaka/core/Common.hpp"
9 #include "alpaka/dev/Traits.hpp"
10 #include "alpaka/dim/Traits.hpp"
11 #include "alpaka/extent/Traits.hpp"
12 #include "alpaka/idx/Traits.hpp"
16 #include "alpaka/offset/Traits.hpp"
17 #include "alpaka/vec/Vec.hpp"
18 
19 #include <type_traits>
20 #include <utility>
21 
22 namespace 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  {
95 #if BOOST_COMP_GNUC
96 # pragma GCC diagnostic push
97  // "cast from 'std::uint8_t*' to 'TElem*' increases required alignment of target type"
98 # pragma GCC diagnostic ignored "-Wcast-align"
99 #endif
100  return reinterpret_cast<TElem*>(
101  reinterpret_cast<std::uint8_t*>(alpaka::getPtrNative(m_viewParentView))
103 #if BOOST_COMP_GNUC
104 # pragma GCC diagnostic pop
105 #endif
106  }
107 
109  Vec<TDim, TIdx> m_extentElements; // The extent of this view.
110  Vec<TDim, TIdx> m_offsetsElements; // The offset relative to the parent view.
111  TElem* m_nativePtr;
112  };
113 
114  // Trait specializations for ViewSubView.
115  namespace trait
116  {
117  //! The ViewSubView device type trait specialization.
118  template<typename TElem, typename TDim, typename TDev, typename TIdx>
119  struct DevType<ViewSubView<TDev, TElem, TDim, TIdx>>
120  {
122  };
123 
124  //! The ViewSubView device get trait specialization.
125  template<typename TElem, typename TDim, typename TDev, typename TIdx>
126  struct GetDev<ViewSubView<TDev, TElem, TDim, TIdx>>
127  {
129  {
130  return alpaka::getDev(view.m_viewParentView);
131  }
132  };
133 
134  //! The ViewSubView dimension getter trait specialization.
135  template<typename TElem, typename TDim, typename TDev, typename TIdx>
136  struct DimType<ViewSubView<TDev, TElem, TDim, TIdx>>
137  {
138  using type = TDim;
139  };
140 
141  //! The ViewSubView memory element type get trait specialization.
142  template<typename TElem, typename TDim, typename TDev, typename TIdx>
143  struct ElemType<ViewSubView<TDev, TElem, TDim, TIdx>>
144  {
145  using type = TElem;
146  };
147 
148  //! The ViewSubView width get trait specialization.
149  template<typename TElem, typename TDim, typename TDev, typename TIdx>
150  struct GetExtents<ViewSubView<TDev, TElem, TDim, TIdx>>
151  {
153  {
154  return view.m_extentElements;
155  }
156  };
157 
158  //! The ViewSubView native pointer get trait specialization.
159  template<typename TElem, typename TDim, typename TDev, typename TIdx>
160  struct GetPtrNative<ViewSubView<TDev, TElem, TDim, TIdx>>
161  {
162  ALPAKA_FN_HOST static auto getPtrNative(ViewSubView<TDev, TElem, TDim, TIdx> const& view) -> TElem const*
163  {
164  return view.m_nativePtr;
165  }
166 
168  {
169  return view.m_nativePtr;
170  }
171  };
172 
173  //! The ViewSubView pitch get trait specialization.
174  template<typename TDev, typename TElem, typename TDim, typename TIdx>
175  struct GetPitchesInBytes<ViewSubView<TDev, TElem, TDim, TIdx>>
176  {
178  {
179  return getPitchesInBytes(view.m_viewParentView);
180  }
181  };
182 
183  //! The ViewSubView x offset get trait specialization.
184  template<typename TElem, typename TDim, typename TDev, typename TIdx>
185  struct GetOffsets<ViewSubView<TDev, TElem, TDim, TIdx>>
186  {
188  {
189  return offset.m_offsetsElements;
190  }
191  };
192 
193  //! The ViewSubView idx type trait specialization.
194  template<typename TElem, typename TDim, typename TDev, typename TIdx>
195  struct IdxType<ViewSubView<TDev, TElem, TDim, TIdx>>
196  {
197  using type = TIdx;
198  };
199 
200  //! The CPU device CreateSubView trait default implementation
201  template<typename TDev, typename TSfinae>
203  {
204  template<typename TView, typename TExtent, typename TOffsets>
205  static auto createSubView(
206  TView& view,
207  TExtent const& extentElements,
208  TOffsets const& relativeOffsetsElements)
209  {
210  using Dim = alpaka::Dim<TExtent>;
211  using Idx = alpaka::Idx<TExtent>;
212  using Elem = typename trait::ElemType<TView>::type;
213  return ViewSubView<TDev, Elem, Dim, Idx>(view, extentElements, relativeOffsetsElements);
214  }
215  };
216  } // namespace trait
217 } // 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.
Definition: ViewSubView.hpp:27
ViewSubView(TView &view)
Definition: ViewSubView.hpp:87
ViewPlainPtr< Dev, TElem, TDim, TIdx > m_viewParentView
Vec< TDim, TIdx > m_extentElements
ALPAKA_FN_HOST auto computeNativePtr()
Definition: ViewSubView.hpp:93
ViewSubView(TQualifiedView &view, TExtent const &extentElements, TOffsets const &relativeOffsetsElements=TOffsets())
Constructor.
Definition: ViewSubView.hpp:38
ViewSubView(TView const &view)
Definition: ViewSubView.hpp:80
Vec< TDim, TIdx > m_offsetsElements
#define ALPAKA_FN_HOST
Definition: Common.hpp:40
constexpr auto offset
Definition: Extent.hpp:34
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_ACC auto all(TWarp const &warp, std::int32_t predicate) -> std::int32_t
Evaluates predicate for all active threads of the warp and returns non-zero if and only if predicate ...
Definition: Traits.hpp:114
The alpaka accelerator library.
typename trait::IdxType< T >::type Idx
Definition: Traits.hpp:29
typename trait::DevType< T >::type Dev
The device type trait alias template to remove the ::type.
Definition: Traits.hpp:56
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 getPitchesInBytes(TView const &view) -> Vec< Dim< TView >, Idx< TView >>
Definition: Traits.hpp:196
ALPAKA_FN_HOST auto getPtrNative(TView const &view) -> Elem< TView > const *
Gets the native pointer of the memory view.
Definition: Traits.hpp:136
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_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 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