alpaka
Abstraction Library for Parallel Kernel Acceleration
Traits.hpp
Go to the documentation of this file.
1 /* Copyright 2022 Benjamin Worpitz, Bernhard Manfred Gruber
2  * SPDX-License-Identifier: MPL-2.0
3  */
4 
5 #pragma once
6 
7 #include "alpaka/core/Common.hpp"
9 #include "alpaka/idx/Traits.hpp"
10 #include "alpaka/vec/Vec.hpp"
11 
12 #include <type_traits>
13 
14 namespace alpaka
15 {
16  //! The offset traits.
17  namespace trait
18  {
19  //! The x offset get trait.
20  //!
21  //! If not specialized explicitly it returns 0.
22  template<typename TIdx, typename TOffsets, typename TSfinae = void>
23  struct [[deprecated("Specialize GetOffsets instead")]] GetOffset
24  {
26  {
27  return static_cast<Idx<TOffsets>>(0);
28  } // namespace trait
29  }; // namespace alpaka
30 
31  //! The GetOffsets trait for getting the offsets of an object as an alpaka::Vec.
32  template<typename TExtent, typename TSfinae = void>
33  struct GetOffsets;
34  } // namespace trait
35 
36  //! \return The offset in the given dimension.
38  template<std::size_t Tidx, typename TOffsets>
39  [[deprecated("use getOffsets(offsets)[Tidx] instead")]] ALPAKA_FN_HOST_ACC auto getOffset(TOffsets const& offsets)
40  -> Idx<TOffsets>
41  {
42 #if BOOST_COMP_CLANG || BOOST_COMP_GNUC
43 # pragma GCC diagnostic push
44 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
45 #endif
46  return trait::GetOffset<DimInt<Tidx>, TOffsets>::getOffset(offsets);
47 #if BOOST_COMP_CLANG || BOOST_COMP_GNUC
48 # pragma GCC diagnostic pop
49 #endif
50  }
51 
52  //! \return The extents of the given object.
54  template<typename T>
55  ALPAKA_FN_HOST_ACC auto getOffsets(T const& object) -> Vec<Dim<T>, Idx<T>>
56  {
57  return trait::GetOffsets<T>{}(object);
58  }
59 
60  //! \tparam T has to specialize GetOffsets.
61  //! \return The offset vector.
63  template<typename T>
64  ALPAKA_FN_HOST_ACC constexpr auto getOffsetVec(T const& object = {}) -> Vec<Dim<T>, Idx<T>>
65  {
66  return getOffsets(object);
67  }
68 
69  //! \tparam T has to specialize GetOffsets.
70  //! \return The offset vector but only the last TDim elements.
72  template<typename TDim, typename T>
73  ALPAKA_FN_HOST_ACC constexpr auto getOffsetVecEnd(T const& object = {}) -> Vec<TDim, Idx<T>>
74  {
75  static_assert(TDim::value <= Dim<T>::value, "Cannot get more items than the offsets hold");
76 
77  auto const o = getOffsets(object);
78  Vec<TDim, Idx<T>> v;
79  for(unsigned i = 0; i < TDim::value; i++)
80  v[i] = o[(Dim<T>::value - TDim::value) + i];
81  return v;
82  }
83 
84  //! \return The offset in x dimension.
86  template<typename TOffsets>
87  ALPAKA_FN_HOST_ACC auto getOffsetX(TOffsets const& offsets = TOffsets()) -> Idx<TOffsets>
88  {
89  return getOffsets(offsets)[Dim<TOffsets>::value - 1u];
90  }
91 
92  //! \return The offset in y dimension.
94  template<typename TOffsets>
95  ALPAKA_FN_HOST_ACC auto getOffsetY(TOffsets const& offsets = TOffsets()) -> Idx<TOffsets>
96  {
97  return getOffsets(offsets)[Dim<TOffsets>::value - 2u];
98  }
99 
100  //! \return The offset in z dimension.
102  template<typename TOffsets>
103  ALPAKA_FN_HOST_ACC auto getOffsetZ(TOffsets const& offsets = TOffsets()) -> Idx<TOffsets>
104  {
105  return getOffsets(offsets)[Dim<TOffsets>::value - 3u];
106  }
107 
108  namespace trait
109  {
110  //! The Vec offset get trait specialization.
111  template<typename TDim, typename TVal>
112  struct GetOffsets<Vec<TDim, TVal>>
113  {
115  ALPAKA_FN_HOST_ACC constexpr auto operator()(Vec<TDim, TVal> const& offsets) const -> Vec<TDim, TVal>
116  {
117  return offsets;
118  }
119  };
120 
121  //! The unsigned integral x offset get trait specialization.
122  template<typename TIntegral>
123  struct GetOffsets<TIntegral, std::enable_if_t<std::is_integral_v<TIntegral>>>
124  {
126  ALPAKA_FN_HOST_ACC constexpr auto operator()(TIntegral const& i) const
127  {
128  return Vec{i};
129  }
130  };
131  } // namespace trait
132 } // namespace alpaka
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
The alpaka accelerator library.
typename trait::IdxType< T >::type Idx
Definition: Traits.hpp:29
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getOffsetX(TOffsets const &offsets=TOffsets()) -> Idx< TOffsets >
Definition: Traits.hpp:87
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getOffsetZ(TOffsets const &offsets=TOffsets()) -> Idx< TOffsets >
Definition: Traits.hpp:103
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getOffset(TOffsets const &offsets) -> Idx< TOffsets >
Definition: Traits.hpp:39
ALPAKA_NO_HOST_ACC_WARNING constexpr ALPAKA_FN_HOST_ACC auto getOffsetVec(T const &object={}) -> Vec< Dim< T >, Idx< T >>
Definition: Traits.hpp:64
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getOffsetY(TOffsets const &offsets=TOffsets()) -> Idx< TOffsets >
Definition: Traits.hpp:95
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getOffsets(T const &object) -> Vec< Dim< T >, Idx< T >>
Definition: Traits.hpp:55
ALPAKA_NO_HOST_ACC_WARNING constexpr ALPAKA_FN_HOST_ACC auto getOffsetVecEnd(T const &object={}) -> Vec< TDim, Idx< T >>
Definition: Traits.hpp:73
ALPAKA_FN_HOST_ACC Vec(TFirstIndex &&, TRestIndices &&...) -> Vec< DimInt< 1+sizeof...(TRestIndices)>, std::decay_t< TFirstIndex >>
typename trait::DimType< T >::type Dim
The dimension type trait alias template to remove the ::type.
Definition: Traits.hpp:19
The x offset get trait.
Definition: Traits.hpp:24
ALPAKA_NO_HOST_ACC_WARNING static ALPAKA_FN_HOST_ACC auto getOffset(TOffsets const &) -> Idx< TOffsets >
Definition: Traits.hpp:25
ALPAKA_NO_HOST_ACC_WARNING constexpr ALPAKA_FN_HOST_ACC auto operator()(TIntegral const &i) const
Definition: Traits.hpp:126
ALPAKA_NO_HOST_ACC_WARNING constexpr ALPAKA_FN_HOST_ACC auto operator()(Vec< TDim, TVal > const &offsets) const -> Vec< TDim, TVal >
Definition: Traits.hpp:115
The GetOffsets trait for getting the offsets of an object as an alpaka::Vec.
Definition: Traits.hpp:33