alpaka
Abstraction Library for Parallel Kernel Acceleration
Traits.hpp
Go to the documentation of this file.
1 /* Copyright 2023 Benjamin Worpitz, Andrea Bocci, Jan Stephan, Bernhard Manfred Gruber
2  * SPDX-License-Identifier: MPL-2.0
3  */
4 
5 #pragma once
6 
7 #include "alpaka/core/Common.hpp"
10 #include "alpaka/idx/Traits.hpp"
11 #include "alpaka/meta/Fold.hpp"
12 #include "alpaka/vec/Vec.hpp"
13 
14 #include <functional>
15 #include <type_traits>
16 #include <utility>
17 
18 namespace alpaka
19 {
20  //! The extent traits.
21  namespace trait
22  {
23  //! The extent get trait.
24  //!
25  //! If not specialized explicitly it returns 1.
26  template<typename TIdxIntegralConst, typename TExtent, typename TSfinae = void>
27  struct [[deprecated("Specialize GetExtents instead")]] GetExtent
28  {
30  {
31  return static_cast<Idx<TExtent>>(1);
32  } // namespace trait
33  }; // namespace alpaka
34 
35  //! The GetExtents trait for getting the extents of an object as an alpaka::Vec.
36  template<typename TExtent, typename TSfinae = void>
37  struct GetExtents;
38  } // namespace trait
39 
40  //! \return The extent in the given dimension.
42  template<std::size_t Tidx, typename TExtent>
43  [[deprecated("use getExtents(extent)[Tidx] instead")]] ALPAKA_FN_HOST_ACC auto getExtent(
44  TExtent const& extent = TExtent()) -> Idx<TExtent>
45  {
46 #if BOOST_COMP_CLANG || BOOST_COMP_GNUC
47 # pragma GCC diagnostic push
48 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
49 #endif
50  return trait::GetExtent<DimInt<Tidx>, TExtent>::getExtent(extent);
51 #if BOOST_COMP_CLANG || BOOST_COMP_GNUC
52 # pragma GCC diagnostic pop
53 #endif
54  }
55 
56  //! \return The extents of the given object.
58  template<typename T>
59  ALPAKA_FN_HOST_ACC auto getExtents(T const& object) -> Vec<Dim<T>, Idx<T>>
60  {
61  return trait::GetExtents<T>{}(object);
62  }
63 
64  //! \tparam T has to specialize GetExtent.
65  //! \return The extents of the given object.
67  template<typename T>
68  [[deprecated("use getExtents() instead")]] ALPAKA_FN_HOST_ACC constexpr auto getExtentVec(T const& object = {})
69  -> Vec<Dim<T>, Idx<T>>
70  {
71  return getExtents(object);
72  }
73 
74  //! \tparam T has to specialize GetExtent.
75  //! \return The extent but only the last TDim elements.
77  template<typename TDim, typename T>
78  ALPAKA_FN_HOST_ACC constexpr auto getExtentVecEnd(T const& object = {}) -> Vec<TDim, Idx<T>>
79  {
80  static_assert(TDim::value <= Dim<T>::value, "Cannot get more items than the extent holds");
81 
82  [[maybe_unused]] auto const e = getExtents(object);
83  Vec<TDim, Idx<T>> v{};
84  if constexpr(TDim::value > 0)
85  {
86  for(unsigned i = 0; i < TDim::value; i++)
87  v[i] = e[(Dim<T>::value - TDim::value) + i];
88  }
89  return v;
90  }
91 
92  //! \return The width.
94  template<typename TExtent>
95  ALPAKA_FN_HOST_ACC auto getWidth(TExtent const& extent = TExtent()) -> Idx<TExtent>
96  {
97  if constexpr(Dim<TExtent>::value >= 1)
98  return getExtents(extent)[Dim<TExtent>::value - 1u];
99  else
100  return 1;
101 
102  ALPAKA_UNREACHABLE({});
103  }
104 
105  //! \return The height.
107  template<typename TExtent>
108  ALPAKA_FN_HOST_ACC auto getHeight(TExtent const& extent = TExtent()) -> Idx<TExtent>
109  {
110  if constexpr(Dim<TExtent>::value >= 2)
111  return getExtents(extent)[Dim<TExtent>::value - 2u];
112  else
113  return 1;
114 
115  ALPAKA_UNREACHABLE({});
116  }
117 
118  //! \return The depth.
120  template<typename TExtent>
121  ALPAKA_FN_HOST_ACC auto getDepth(TExtent const& extent = TExtent()) -> Idx<TExtent>
122  {
123  if constexpr(Dim<TExtent>::value >= 3)
124  return getExtents(extent)[Dim<TExtent>::value - 3u];
125  else
126  return 1;
127 
128  ALPAKA_UNREACHABLE({});
129  }
130 
131  //! \return The product of the extents of the given object.
133  template<typename T>
134  ALPAKA_FN_HOST_ACC auto getExtentProduct(T const& object) -> Idx<T>
135  {
136  return getExtents(object).prod();
137  }
138 
139  namespace trait
140  {
141  //! The Vec extent get trait specialization.
142  template<typename TDim, typename TVal>
143  struct GetExtents<Vec<TDim, TVal>>
144  {
146  ALPAKA_FN_HOST_ACC constexpr auto operator()(Vec<TDim, TVal> const& extent) const -> Vec<TDim, TVal>
147  {
148  return extent;
149  }
150  };
151 
152  template<typename Integral>
153  struct GetExtents<Integral, std::enable_if_t<std::is_integral_v<Integral>>>
154  {
156  ALPAKA_FN_HOST_ACC auto operator()(Integral i) const
157  {
158  return Vec{i};
159  }
160  };
161  } // namespace trait
162 } // namespace alpaka
#define ALPAKA_UNREACHABLE(...)
Before CUDA 11.5 nvcc is unable to correctly identify return statements in 'if constexpr' branches....
Definition: Unreachable.hpp:24
A n-dimensional vector.
Definition: Vec.hpp:38
#define ALPAKA_FN_HOST_ACC
Definition: Common.hpp:39
#define ALPAKA_NO_HOST_ACC_WARNING
Disable nvcc warning: 'calling a host function from host device function.' Usage: ALPAKA_NO_HOST_ACC_...
Definition: Common.hpp:82
constexpr double e
Definition: Traits.hpp:58
The alpaka accelerator library.
typename trait::IdxType< T >::type Idx
Definition: Traits.hpp:29
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getExtentProduct(T const &object) -> Idx< T >
Definition: Traits.hpp:134
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getHeight(TExtent const &extent=TExtent()) -> Idx< TExtent >
Definition: Traits.hpp:108
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getExtent(TExtent const &extent=TExtent()) -> Idx< TExtent >
Definition: Traits.hpp:43
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getExtents(T const &object) -> Vec< Dim< T >, Idx< T >>
Definition: Traits.hpp:59
ALPAKA_NO_HOST_ACC_WARNING constexpr ALPAKA_FN_HOST_ACC auto getExtentVec(T const &object={}) -> Vec< Dim< T >, Idx< T >>
Definition: Traits.hpp:68
ALPAKA_FN_HOST_ACC Vec(TFirstIndex &&, TRestIndices &&...) -> Vec< DimInt< 1+sizeof...(TRestIndices)>, std::decay_t< TFirstIndex >>
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getDepth(TExtent const &extent=TExtent()) -> Idx< TExtent >
Definition: Traits.hpp:121
typename trait::DimType< T >::type Dim
The dimension type trait alias template to remove the ::type.
Definition: Traits.hpp:19
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getWidth(TExtent const &extent=TExtent()) -> Idx< TExtent >
Definition: Traits.hpp:95
ALPAKA_NO_HOST_ACC_WARNING constexpr ALPAKA_FN_HOST_ACC auto getExtentVecEnd(T const &object={}) -> Vec< TDim, Idx< T >>
Definition: Traits.hpp:78
The extent get trait.
Definition: Traits.hpp:28
ALPAKA_NO_HOST_ACC_WARNING static ALPAKA_FN_HOST_ACC auto getExtent(TExtent const &) -> Idx< TExtent >
Definition: Traits.hpp:29
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto operator()(Integral i) const
Definition: Traits.hpp:156
ALPAKA_NO_HOST_ACC_WARNING constexpr ALPAKA_FN_HOST_ACC auto operator()(Vec< TDim, TVal > const &extent) const -> Vec< TDim, TVal >
Definition: Traits.hpp:146
The GetExtents trait for getting the extents of an object as an alpaka::Vec.
Definition: Traits.hpp:37