alpaka
Abstraction Library for Parallel Kernel Acceleration
ViewConst.hpp
Go to the documentation of this file.
1 /* Copyright 2022 Bernhard Manfred Gruber
2  * SPDX-License-Identifier: MPL-2.0
3  */
4 
5 #pragma once
6 
7 #include "alpaka/core/Common.hpp"
8 #include "alpaka/dev/Traits.hpp"
9 #include "alpaka/dim/Traits.hpp"
10 #include "alpaka/extent/Traits.hpp"
11 #include "alpaka/idx/Traits.hpp"
14 #include "alpaka/offset/Traits.hpp"
15 
16 namespace alpaka
17 {
18  //! A non-modifiable wrapper around a view. This view acts as the wrapped view, but the underlying data is only
19  //! exposed const-qualified.
20  template<typename TView>
21  struct ViewConst : internal::ViewAccessOps<ViewConst<TView>>
22  {
23  static_assert(!std::is_const_v<TView>, "ViewConst must be instantiated with a non-const type");
24  static_assert(
25  !std::is_reference_v<TView>,
26  "This is not implemented"); // It might even be dangerous for ViewConst to store a reference to the wrapped
27  // view, as this decouples the wrapped view's lifetime.
28 
29  ALPAKA_FN_HOST ViewConst(TView const& view) : m_view(view)
30  {
31  }
32 
33  ALPAKA_FN_HOST ViewConst(TView&& view) : m_view(std::move(view))
34  {
35  }
36 
37  TView m_view;
38  };
39 
40  template<typename TView>
42 
43  namespace trait
44  {
45  template<typename TView>
46  struct DevType<ViewConst<TView>> : DevType<TView>
47  {
48  };
49 
50  template<typename TView>
51  struct GetDev<ViewConst<TView>>
52  {
53  ALPAKA_FN_HOST static auto getDev(ViewConst<TView> const& view)
54  {
55  return alpaka::getDev(view.m_view);
56  }
57  };
58 
59  template<typename TView>
60  struct DimType<ViewConst<TView>> : DimType<TView>
61  {
62  };
63 
64  template<typename TView>
65  struct ElemType<ViewConst<TView>>
66  {
67  // const qualify the element type of the inner view
68  using type = typename ElemType<TView>::type const;
69  };
70 
71  template<typename TView>
72  struct GetExtents<ViewConst<TView>>
73  {
74  ALPAKA_FN_HOST auto operator()(ViewConst<TView> const& view) const
75  {
76  return getExtents(view.m_view);
77  }
78  };
79 
80  template<typename TView>
81  struct GetPtrNative<ViewConst<TView>>
82  {
83  using TElem = typename ElemType<TView>::type;
84 
85  // const qualify the element type of the inner view
86  ALPAKA_FN_HOST static auto getPtrNative(ViewConst<TView> const& view) -> TElem const*
87  {
88  return alpaka::getPtrNative(view.m_view);
89  }
90  };
91 
92  template<typename TView>
94  {
95  ALPAKA_FN_HOST auto operator()(ViewConst<TView> const& view) const
96  {
97  return alpaka::getPitchesInBytes(view.m_view);
98  }
99  };
100 
101  template<typename TView>
102  struct GetOffsets<ViewConst<TView>>
103  {
105  {
106  return alpaka::getOffsets(view.m_view);
107  }
108  };
109 
110  template<typename TView>
111  struct IdxType<ViewConst<TView>> : IdxType<TView>
112  {
113  };
114  } // namespace trait
115 } // namespace alpaka
#define ALPAKA_FN_HOST
Definition: Common.hpp:40
The alpaka accelerator library.
ViewConst(TView) -> ViewConst< std::decay_t< TView >>
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
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
A non-modifiable wrapper around a view. This view acts as the wrapped view, but the underlying data i...
Definition: ViewConst.hpp:22
ALPAKA_FN_HOST ViewConst(TView const &view)
Definition: ViewConst.hpp:29
ALPAKA_FN_HOST ViewConst(TView &&view)
Definition: ViewConst.hpp:33
The device type trait.
Definition: Traits.hpp:23
The dimension getter type trait.
Definition: Traits.hpp:14
typename ElemType< TView >::type const type
Definition: ViewConst.hpp:68
The element type trait.
Definition: Traits.hpp:16
static ALPAKA_FN_HOST auto getDev(ViewConst< TView > const &view)
Definition: ViewConst.hpp:53
The device get trait.
Definition: Traits.hpp:27
ALPAKA_FN_HOST auto operator()(ViewConst< TView > const &view) const
Definition: ViewConst.hpp:74
The GetExtents trait for getting the extents of an object as an alpaka::Vec.
Definition: Traits.hpp:37
ALPAKA_FN_HOST auto operator()(ViewConst< TView > const &view) const
Definition: ViewConst.hpp:104
The GetOffsets trait for getting the offsets of an object as an alpaka::Vec.
Definition: Traits.hpp:33
ALPAKA_FN_HOST auto operator()(ViewConst< TView > const &view) const
Definition: ViewConst.hpp:95
Customization point for getPitchesInBytes. The default implementation uses the extent to calculate th...
Definition: Traits.hpp:103
static ALPAKA_FN_HOST auto getPtrNative(ViewConst< TView > const &view) -> TElem const *
Definition: ViewConst.hpp:86
typename ElemType< TView >::type TElem
Definition: ViewConst.hpp:83
The native pointer get trait.
Definition: Traits.hpp:54
The idx type trait.
Definition: Traits.hpp:25