alpaka
Abstraction Library for Parallel Kernel Acceleration
MapIdx.hpp
Go to the documentation of this file.
1 /* Copyright 2023 Axel Hübl, Benjamin Worpitz, Erik Zenker, Jan Stephan, Jeffrey Kelling, 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/vec/Traits.hpp"
10 #include "alpaka/vec/Vec.hpp"
11 
12 #include <type_traits>
13 
14 namespace alpaka
15 {
16  //! Maps an N-dimensional index to an N-dimensional position. At least one dimension must always be 1 or zero.
17  //!
18  //! \tparam TDimOut Dimension of the index vector to map to.
19  //! \param in The index vector to map from.
20  //! \param extent The extents of the input or output space, whichever has more than 1 dimensions.
22  std::size_t TDimOut,
23  std::size_t TDimIn,
24  std::size_t TDimExtents,
25  typename TElem>
26  ALPAKA_FN_HOST_ACC auto mapIdx(Vec<DimInt<TDimIn>, TElem> const& in, Vec<DimInt<TDimExtents>, TElem> const& extent)
27  -> Vec<DimInt<TDimOut>, TElem>
28  {
29  if constexpr(TDimOut == 0 || TDimIn == 0)
30  return Vec<DimInt<TDimOut>, TElem>::zeros();
31  else if constexpr(TDimOut == TDimIn)
32  return in;
33  else if constexpr(TDimOut == 1)
34  {
35  TElem out = in[0];
36  for(std::size_t d = 1; d < TDimIn; ++d)
37  out = static_cast<TElem>(out * extent[d] + in[d]);
38  return {out};
39  }
40  else if constexpr(TDimIn == 1)
41  {
42  auto flat = in.front();
43  Vec<DimInt<TDimOut>, TElem> out;
44  for(std::size_t d = TDimOut - 1u; d > 0; d--)
45  {
46  out[d] = static_cast<TElem>(flat % extent[d]);
47  flat /= extent[d];
48  }
49  out.front() = static_cast<TElem>(flat);
50  return out;
51  }
52  else
53  static_assert(!sizeof(TElem), "Not implemented");
54 
56  }
57 
58  //! Maps an N dimensional index to a N dimensional position based on the pitches of a view without padding or a
59  //! byte view. At least one dimension must always be 1 or zero.
60  //!
61  //! \tparam TDimOut Dimension of the index vector to map to.
62  //! \param in The index vector to map from.
63  //! \param pitches The pitches of the input or output space, whichever has more than 1 dimensions.
65  template<std::size_t TDimOut, std::size_t TDimIn, std::size_t TidxDimPitch, typename TElem>
67  Vec<DimInt<TDimIn>, TElem> const& in,
68  Vec<DimInt<TidxDimPitch>, TElem> const& pitches) -> Vec<DimInt<TDimOut>, TElem>
69  {
70  if constexpr(TDimOut == 0 || TDimIn == 0)
71  return Vec<DimInt<TDimOut>, TElem>::zeros();
72  else if constexpr(TDimOut == TDimIn)
73  return in;
74  else if constexpr(TDimOut == 1)
75  {
76  using DimMinusOne = DimInt<TDimIn - 1>;
77  return {in.back() + (subVecBegin<DimMinusOne>(pitches) * subVecBegin<DimMinusOne>(in)).sum()};
78  }
79  else if constexpr(TDimIn == 1)
80  {
81  auto result = Vec<DimInt<TDimOut>, TElem>::zeros();
82 
83  TElem out = in.front();
84  for(std::size_t d = 0; d < TDimOut - 1u; ++d)
85  {
86  result[d] = static_cast<TElem>(out / pitches[d]);
87  out %= pitches[d];
88  }
89  result.back() = out;
90 
91  return result;
92  }
93  else
94  static_assert(!sizeof(TElem), "Not implemented");
95 
97  }
98 } // 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
constexpr ALPAKA_FN_HOST_ACC auto front() -> TVal &
Definition: Vec.hpp:158
#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.
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto mapIdx(Vec< DimInt< TDimIn >, TElem > const &in, Vec< DimInt< TDimExtents >, TElem > const &extent) -> Vec< DimInt< TDimOut >, TElem >
Maps an N-dimensional index to an N-dimensional position. At least one dimension must always be 1 or ...
Definition: MapIdx.hpp:26
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto mapIdxPitchBytes(Vec< DimInt< TDimIn >, TElem > const &in, Vec< DimInt< TidxDimPitch >, TElem > const &pitches) -> Vec< DimInt< TDimOut >, TElem >
Maps an N dimensional index to a N dimensional position based on the pitches of a view without paddin...
Definition: MapIdx.hpp:66
std::integral_constant< std::size_t, N > DimInt