alpaka
Abstraction Library for Parallel Kernel Acceleration
NdLoop.hpp
Go to the documentation of this file.
1 /* Copyright 2022 Axel Huebl, Benjamin Worpitz, Jan Stephan, 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/dim/Traits.hpp"
9 #include "alpaka/vec/Vec.hpp"
10 
11 #include <utility>
12 
13 namespace alpaka::meta
14 {
15  namespace detail
16  {
18  template<typename TIndex, typename TExtentVec, typename TFnObj>
20  std::index_sequence<>,
21  TIndex& idx,
22  TExtentVec const&,
23  TFnObj const& f)
24  {
25  f(idx);
26  }
27 
29  template<std::size_t Tdim0, std::size_t... Tdims, typename TIndex, typename TExtentVec, typename TFnObj>
31  std::index_sequence<Tdim0, Tdims...>,
32  TIndex& idx,
33  TExtentVec const& extent,
34  TFnObj const& f)
35  {
36  static_assert(Dim<TIndex>::value > 0u, "The dimension given to ndLoop has to be larger than zero!");
37  static_assert(
39  "The dimensions of the iteration vector and the extent vector have to be identical!");
40  static_assert(Dim<TIndex>::value > Tdim0, "The current dimension has to be in the range [0,dim-1]!");
41 
42  for(idx[Tdim0] = 0u; idx[Tdim0] < extent[Tdim0]; ++idx[Tdim0])
43  {
44  ndLoopImpl(std::index_sequence<Tdims...>{}, idx, extent, f);
45  }
46  }
47  } // namespace detail
48 
49  //! Loops over an n-dimensional iteration index variable calling f(idx, args...) for each iteration.
50  //! The loops are nested in the order given by the index_sequence with the first element being the outermost
51  //! and the last index the innermost loop.
52  //!
53  //! \param indexSequence A sequence of indices being a permutation of the values [0, dim-1].
54  //! \param extent N-dimensional loop extent.
55  //! \param f The function called at each iteration.
57  template<typename TExtentVec, typename TFnObj, std::size_t... Tdims>
59  [[maybe_unused]] std::index_sequence<Tdims...> indexSequence,
60  TExtentVec const& extent,
61  TFnObj const& f) -> void
62  {
63  static_assert(
64  IntegerSequenceValuesInRange<std::index_sequence<Tdims...>, std::size_t, 0, Dim<TExtentVec>::value>::value,
65  "The values in the index_sequence have to be in the range [0,dim-1]!");
66  static_assert(
67  IntegerSequenceValuesUnique<std::index_sequence<Tdims...>>::value,
68  "The values in the index_sequence have to be unique!");
69 
70  auto idx = Vec<Dim<TExtentVec>, Idx<TExtentVec>>::zeros();
71  detail::ndLoopImpl(std::index_sequence<Tdims...>{}, idx, extent, f);
72  }
73 
74  //! Loops over an n-dimensional iteration index variable calling f(idx, args...) for each iteration.
75  //! The loops are nested from index zero outmost to index (dim-1) innermost.
76  //!
77  //! \param extent N-dimensional loop extent.
78  //! \param f The function called at each iteration.
80  template<typename TExtentVec, typename TFnObj>
81  ALPAKA_FN_HOST_ACC auto ndLoopIncIdx(TExtentVec const& extent, TFnObj const& f) -> void
82  {
83  ndLoop(std::make_index_sequence<Dim<TExtentVec>::value>(), extent, f);
84  }
85 } // namespace alpaka::meta
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
ALPAKA_NO_HOST_ACC_WARNING constexpr ALPAKA_FN_HOST_ACC void ndLoopImpl(std::index_sequence<>, TIndex &idx, TExtentVec const &, TFnObj const &f)
Definition: NdLoop.hpp:19
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto ndLoop([[maybe_unused]] std::index_sequence< Tdims... > indexSequence, TExtentVec const &extent, TFnObj const &f) -> void
Loops over an n-dimensional iteration index variable calling f(idx, args...) for each iteration....
Definition: NdLoop.hpp:58
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto ndLoopIncIdx(TExtentVec const &extent, TFnObj const &f) -> void
Loops over an n-dimensional iteration index variable calling f(idx, args...) for each iteration....
Definition: NdLoop.hpp:81
typename trait::IdxType< T >::type Idx
Definition: Traits.hpp:29
typename trait::DimType< T >::type Dim
The dimension type trait alias template to remove the ::type.
Definition: Traits.hpp:19
Checks if the values in the index sequence are within the given range.
Checks if the values in the index sequence are unique.