Schnek
kokkos-iteration.hpp
1 /*
2  * kokkos-iteration.hpp
3  *
4  * Created on: 16 Dec 2022
5  * Author: Holger Schmitz
6  * Email: holger@notjustphysics.com
7  *
8  * Copyright 2012-2022 Holger Schmitz
9  *
10  * This file is part of Schnek.
11  *
12  * Schnek is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * Schnek is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with Schnek. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #ifndef SCHNEK_GRID_ITERATION_KOKKOSITERATION_HPP_
27 #define SCHNEK_GRID_ITERATION_KOKKOSITERATION_HPP_
28 
29 #include "../../config.hpp"
30 #include "../array.hpp"
31 
32 #ifdef SCHNEK_HAVE_KOKKOS
33 
34 #include <Kokkos_Core.hpp>
35 
36 namespace schnek {
37 
38  template<
39  size_t rank,
40  typename executionSpace = Kokkos::DefaultExecutionSpace
41  >
42  struct RangeKokkosIterationPolicy
43  {
55  template<
56  class RangeType,
57  typename Func
58  >
59  static void forEach(const RangeType& range, const Func &func);
60  };
61 
62  //=================================================================
63  //==================== RangeKokkosIterationPolicy =================
64  //=================================================================
65 
66  namespace internal {
67  template<typename Func, typename RangeType>
68  struct RangeKokkosIterationPolicyFunctor
69  {
70  Func func;
71 
72  template<typename... Indices>
73  SCHNEK_INLINE void operator()(Indices... ind) const
74  {
75  func(typename RangeType::LimitType{ind...});
76  }
77  };
78  }
79 
80  // specialization for 1d because Kokkos::MDRangePolicy can only be used for rank>1
81  template<typename executionSpace>
82  struct RangeKokkosIterationPolicy<1, executionSpace>
83  {
84  template<
85  class RangeType,
86  typename Func
87  >
88  static void forEach(const RangeType& range, const Func &func)
89  {
90  typedef typename RangeType::value_type T;
91  typedef Kokkos::RangePolicy<
92  Kokkos::IndexType<T>,
93  executionSpace
94  > ExecutionPolicy;
95 
96  ExecutionPolicy rangePolicy(range.getLo()[0], range.getHi()[0] + 1);
97 
98  internal::RangeKokkosIterationPolicyFunctor<Func, RangeType> functor{func};
99 
100  Kokkos::parallel_for("schnek:forEach", rangePolicy, functor);
101  }
102  };
103 
104  template<
105  size_t rank,
106  typename executionSpace
107  >
108  template<
109  class RangeType,
110  typename Func
111  >
112  inline void RangeKokkosIterationPolicy<rank, executionSpace>::forEach(const RangeType& range, const Func &func)
113  {
114  typedef typename RangeType::value_type T;
115  T lo[rank];
116  T hi[rank];
117  const typename RangeType::LimitType& loR = range.getLo();
118  const typename RangeType::LimitType& hiR = range.getHi();
119 
120  for (size_t i=0; i<rank; ++i)
121  {
122  lo[i] = loR[i];
123  hi[i] = hiR[i] + 1;
124  }
125 
126  Kokkos::MDRangePolicy<
127  Kokkos::IndexType<T>,
128  Kokkos::Rank<rank>,
129  executionSpace
130  > rangePolicy(lo, hi);
131 
132  internal::RangeKokkosIterationPolicyFunctor<Func, RangeType> functor{func};
133 
134  Kokkos::parallel_for("schnek:forEach", rangePolicy, functor);
135  }
136 
137 } // namespace schnek
138 
139 #endif // SCHNEK_HAVE_KOKKOS
140 #endif // SCHNEK_GRID_ITERATION_RANGEITERATION_HPP_
Definition: algo.hpp:30