alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
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/Vec.hpp"
10
11#include <utility>
12
13namespace alpaka::meta
14{
15 namespace detail
16 {
17 template<typename TIndex, typename TExtentVec, typename TFnObj>
18 constexpr void ndLoopImpl(std::index_sequence<>, TIndex& idx, TExtentVec const&, TFnObj const& f)
19 {
20 f(idx);
21 }
22
23 template<std::size_t Tdim0, std::size_t... Tdims, typename TIndex, typename TExtentVec, typename TFnObj>
24 constexpr void ndLoopImpl(
25 std::index_sequence<Tdim0, Tdims...>,
26 TIndex& idx,
27 TExtentVec const& extent,
28 TFnObj const& f)
29 {
30 static_assert(TIndex::dim() > 0u, "The dimension given to ndLoop has to be larger than zero!");
31 static_assert(
32 TIndex::dim() == TExtentVec::dim(),
33 "The dimensions of the iteration vector and the extent vector have to be identical!");
34 static_assert(TIndex::dim() > Tdim0, "The current dimension has to be in the range [0,dim-1]!");
35
36 for(idx[Tdim0] = 0u; idx[Tdim0] < extent[Tdim0]; ++idx[Tdim0])
37 {
38 ndLoopImpl(std::index_sequence<Tdims...>{}, idx, extent, f);
39 }
40 }
41 } // namespace detail
42
43 //! Loops over an n-dimensional iteration index variable calling f(idx, args...) for each iteration.
44 //! The loops are nested in the order given by the index_sequence with the first element being the outermost
45 //! and the last index the innermost loop.
46 //!
47 //! \param indexSequence A sequence of indices being a permutation of the values [0, dim-1].
48 //! \param extent N-dimensional loop extent.
49 //! \param f The function called at each iteration.
50 template<typename TExtentVec, typename TFnObj, std::size_t... Tdims>
51 auto ndLoop(
52 [[maybe_unused]] std::index_sequence<Tdims...> indexSequence,
53 TExtentVec& idx,
54 TExtentVec const& extent,
55 TFnObj const& f) -> void
56 {
57 static_assert(
58 IntegerSequenceValuesInRange<std::index_sequence<Tdims...>, std::size_t, 0, TExtentVec::dim()>::value,
59 "The values in the index_sequence have to be in the range [0,dim-1]!");
60 static_assert(
61 IntegerSequenceValuesUnique<std::index_sequence<Tdims...>>::value,
62 "The values in the index_sequence have to be unique!");
63
64 detail::ndLoopImpl(std::index_sequence<Tdims...>{}, idx, extent, f);
65 }
66
67 //! Loops over an n-dimensional iteration index variable calling f(idx, args...) for each iteration.
68 //! The loops are nested from index zero outmost to index (dim-1) innermost.
69 //!
70 //! \param extent N-dimensional loop extent.
71 //! \param f The function called at each iteration.
72 template<typename TExtentVec, typename TFnObj>
73 auto ndLoopIncIdx(TExtentVec& idx, TExtentVec const& extent, TFnObj const& f) -> void
74 {
75 idx = TExtentVec::fill(0);
76 ndLoop(std::make_index_sequence<TExtentVec::dim()>(), idx, extent, f);
77 }
78
79 template<typename TExtentVec, typename TFnObj>
80 auto ndLoopIncIdx(TExtentVec const& extent, TFnObj const& f) -> void
81 {
82 // TExtentVec could be a CVec therefore we need to make it writable
83 using IndexVector = typename TExtentVec::UniVec;
84 auto idx = IndexVector::fill(0);
85
86 ndLoop(std::make_index_sequence<TExtentVec::dim()>(), idx, IndexVector{extent}, f);
87 }
88} // namespace alpaka::meta
constexpr void ndLoopImpl(std::index_sequence<>, TIndex &idx, TExtentVec const &, TFnObj const &f)
Definition NdLoop.hpp:18
auto ndLoopIncIdx(TExtentVec &idx, 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:73
auto ndLoop(std::index_sequence< Tdims... > indexSequence, TExtentVec &idx, 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:51
Checks if the values in the index sequence are within the given range.
Checks if the values in the index sequence are unique.