alpaka
Abstraction Library for Parallel Kernel Acceleration
ViewPlainPtr.hpp
Go to the documentation of this file.
1 /* Copyright 2023 Benjamin Worpitz, Matthias Werner, RenĂ© Widera, Sergei Bastrakov, Bernhard Manfred Gruber,
2  * Jan Stephan, Andrea Bocci, Aurora Perego
3  * SPDX-License-Identifier: MPL-2.0
4  */
5 
6 #pragma once
7 
8 #include "alpaka/dev/DevCpu.hpp"
14 #include "alpaka/vec/Vec.hpp"
15 
16 #include <type_traits>
17 #include <utility>
18 
19 namespace alpaka
20 {
21  //! The memory view to wrap plain pointers.
22  template<typename TDev, typename TElem, typename TDim, typename TIdx>
23  struct ViewPlainPtr final : internal::ViewAccessOps<ViewPlainPtr<TDev, TElem, TDim, TIdx>>
24  {
25  static_assert(!std::is_const_v<TIdx>, "The idx type of the view can not be const!");
26 
27  template<typename TExtent>
28  ALPAKA_FN_HOST ViewPlainPtr(TElem* pMem, TDev dev, TExtent const& extent = TExtent())
29  : ViewPlainPtr(pMem, std::move(dev), extent, detail::calculatePitchesFromExtents<TElem>(extent))
30  {
31  }
32 
33  template<typename TExtent, typename TPitch>
34  ALPAKA_FN_HOST ViewPlainPtr(TElem* pMem, TDev dev, TExtent const& extent, TPitch pitchBytes)
35  : m_pMem(pMem)
36  , m_dev(std::move(dev))
37  , m_extentElements(extent)
38  , m_pitchBytes(static_cast<Vec<TDim, TIdx>>(pitchBytes))
39  {
40  }
41 
42  TElem* m_pMem;
43  TDev m_dev;
46  };
47 
48  // Trait specializations for ViewPlainPtr.
49  namespace trait
50  {
51  //! The ViewPlainPtr device type trait specialization.
52  template<typename TDev, typename TElem, typename TDim, typename TIdx>
53  struct DevType<ViewPlainPtr<TDev, TElem, TDim, TIdx>>
54  {
56  };
57 
58  //! The ViewPlainPtr device get trait specialization.
59  template<typename TDev, typename TElem, typename TDim, typename TIdx>
60  struct GetDev<ViewPlainPtr<TDev, TElem, TDim, TIdx>>
61  {
63  {
64  return view.m_dev;
65  }
66  };
67 
68  //! The ViewPlainPtr dimension getter trait.
69  template<typename TDev, typename TElem, typename TDim, typename TIdx>
70  struct DimType<ViewPlainPtr<TDev, TElem, TDim, TIdx>>
71  {
72  using type = TDim;
73  };
74 
75  //! The ViewPlainPtr memory element type get trait specialization.
76  template<typename TDev, typename TElem, typename TDim, typename TIdx>
77  struct ElemType<ViewPlainPtr<TDev, TElem, TDim, TIdx>>
78  {
79  using type = TElem;
80  };
81  } // namespace trait
82 
83  namespace trait
84  {
85  template<typename TDev, typename TElem, typename TDim, typename TIdx>
86  struct GetExtents<ViewPlainPtr<TDev, TElem, TDim, TIdx>>
87  {
89  {
90  return view.m_extentElements;
91  }
92  };
93 
94  //! The ViewPlainPtr native pointer get trait specialization.
95  template<typename TDev, typename TElem, typename TDim, typename TIdx>
96  struct GetPtrNative<ViewPlainPtr<TDev, TElem, TDim, TIdx>>
97  {
98  static auto getPtrNative(ViewPlainPtr<TDev, TElem, TDim, TIdx> const& view) -> TElem const*
99  {
100  return view.m_pMem;
101  }
102 
104  {
105  return view.m_pMem;
106  }
107  };
108 
109  template<typename TDev, typename TElem, typename TDim, typename TIdx>
110  struct GetPitchesInBytes<ViewPlainPtr<TDev, TElem, TDim, TIdx>>
111  {
113  {
114  return view.m_pitchBytes;
115  }
116  };
117 
118  //! The CPU device CreateViewPlainPtr trait specialization.
119  template<>
121  {
122  template<typename TElem, typename TExtent, typename TPitch>
123  static auto createViewPlainPtr(DevCpu const& dev, TElem* pMem, TExtent const& extent, TPitch pitch)
124  {
126  pMem,
127  dev,
128  extent,
129  pitch);
130  }
131  };
132 
133 #if defined(ALPAKA_ACC_GPU_CUDA_ENABLED) || defined(ALPAKA_ACC_GPU_HIP_ENABLED)
134  //! The CUDA/HIP RT device CreateViewPlainPtr trait specialization.
135  template<typename TApi>
137  {
138  template<typename TElem, typename TExtent, typename TPitch>
139  static auto createViewPlainPtr(
140  DevUniformCudaHipRt<TApi> const& dev,
141  TElem* pMem,
142  TExtent const& extent,
143  TPitch pitch)
144  {
145  return alpaka::
146  ViewPlainPtr<DevUniformCudaHipRt<TApi>, TElem, alpaka::Dim<TExtent>, alpaka::Idx<TExtent>>(
147  pMem,
148  dev,
149  extent,
150  pitch);
151  }
152  };
153 #endif
154 
155 #if defined(ALPAKA_ACC_SYCL_ENABLED)
156  //! The SYCL device CreateViewPlainPtr trait specialization.
157  template<typename TPlatform>
158  struct CreateViewPlainPtr<DevGenericSycl<TPlatform>>
159  {
160  template<typename TElem, typename TExtent, typename TPitch>
161  static auto createViewPlainPtr(
162  DevGenericSycl<TPlatform> const& dev,
163  TElem* pMem,
164  TExtent const& extent,
165  TPitch pitch)
166  {
167  return alpaka::
168  ViewPlainPtr<DevGenericSycl<TPlatform>, TElem, alpaka::Dim<TExtent>, alpaka::Idx<TExtent>>(
169  pMem,
170  dev,
171  extent,
172  pitch);
173  }
174  };
175 #endif
176  //! The ViewPlainPtr offset get trait specialization.
177  template<typename TDev, typename TElem, typename TDim, typename TIdx>
178  struct GetOffsets<ViewPlainPtr<TDev, TElem, TDim, TIdx>>
179  {
181  {
182  return Vec<TDim, TIdx>::zeros();
183  }
184  };
185 
186  //! The ViewPlainPtr idx type trait specialization.
187  template<typename TDev, typename TElem, typename TDim, typename TIdx>
188  struct IdxType<ViewPlainPtr<TDev, TElem, TDim, TIdx>>
189  {
190  using type = TIdx;
191  };
192  } // namespace trait
193 } // namespace alpaka
The CPU device handle.
Definition: DevCpu.hpp:56
The CUDA/HIP RT device handle.
A n-dimensional vector.
Definition: Vec.hpp:38
ALPAKA_NO_HOST_ACC_WARNING static constexpr ALPAKA_FN_HOST_ACC auto zeros() -> Vec< TDim, TVal >
Zero value constructor.
Definition: Vec.hpp:126
#define ALPAKA_FN_HOST
Definition: Common.hpp:40
constexpr ALPAKA_FN_HOST_ACC auto calculatePitchesFromExtents(Vec< TDim, TIdx > const &extent)
Calculate the pitches purely from the extents.
Definition: Traits.hpp:36
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
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.
ALPAKA_FN_HOST ViewPlainPtr(TElem *pMem, TDev dev, TExtent const &extent=TExtent())
Vec< TDim, TIdx > m_extentElements
ALPAKA_FN_HOST ViewPlainPtr(TElem *pMem, TDev dev, TExtent const &extent, TPitch pitchBytes)
Vec< TDim, TIdx > m_pitchBytes
static auto createViewPlainPtr(DevCpu const &dev, TElem *pMem, TExtent const &extent, TPitch pitch)
static auto createViewPlainPtr(DevUniformCudaHipRt< TApi > const &dev, TElem *pMem, TExtent const &extent, TPitch pitch)
The device memory view creation trait.
Definition: Traits.hpp:124
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 auto getDev(ViewPlainPtr< TDev, TElem, TDim, TIdx > const &view) -> alpaka::Dev< TDev >
The device get trait.
Definition: Traits.hpp:27
ALPAKA_FN_HOST auto operator()(ViewPlainPtr< 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()(ViewPlainPtr< TDev, TElem, TDim, TIdx > const &) const -> Vec< TDim, TIdx >
The GetOffsets trait for getting the offsets of an object as an alpaka::Vec.
Definition: Traits.hpp:33
ALPAKA_FN_HOST auto operator()(ViewPlainPtr< 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 auto getPtrNative(ViewPlainPtr< TDev, TElem, TDim, TIdx > const &view) -> TElem const *
static auto getPtrNative(ViewPlainPtr< TDev, TElem, TDim, TIdx > &view) -> TElem *
The native pointer get trait.
Definition: Traits.hpp:54
The idx type trait.
Definition: Traits.hpp:25