RAJA
RAJA provides a collection of platform portability abstractions for C++ HPC applications.
forall.hpp
Go to the documentation of this file.
1 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
2 // Copyright (c) Lawrence Livermore National Security, LLC and other
3 // RAJA Project Developers. See top-level LICENSE and COPYRIGHT
4 // files for dates and other details. No copyright assignment is required
5 // to contribute to RAJA.
6 //
7 // SPDX-License-Identifier: (BSD-3-Clause)
8 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
9 
10 #ifndef RAJA_forall_param_openmp_HPP
11 #define RAJA_forall_param_openmp_HPP
12 
13 namespace RAJA
14 {
15 
16 namespace policy
17 {
18 namespace omp
19 {
20 namespace expt
21 {
22 
23 namespace internal
24 {
25 //
26 // omp for (Auto)
27 //
28 template<typename ExecPol,
29  typename Iterable,
30  typename Func,
31  typename ForallParam>
32 RAJA_INLINE concepts::enable_if<std::is_same<ExecPol, RAJA::policy::omp::Auto>>
33 forall_impl(const ExecPol& p,
34  Iterable&& iter,
35  Func&& loop_body,
36  ForallParam&& f_params)
37 {
38  using EXEC_POL = camp::decay<decltype(p)>;
39 
41  RAJA_OMP_DECLARE_REDUCTION_COMBINE;
42 
44 #pragma omp parallel
45  {
46 
48  auto body = thread_privatize(loop_body);
49 
50 #pragma omp for reduction(combine : f_params)
51  for (decltype(distance_it) i = 0; i < distance_it; ++i)
52  {
53  RAJA::expt::invoke_body(f_params, body.get_priv(), begin_it[i]);
54  }
55  }
56 
58 }
59 
60 //
61 // omp for schedule(static)
62 //
63 template<template<int> class ExecPol,
64  typename Iterable,
65  typename Func,
66  int ChunkSize,
67  typename ForallParam>
68 RAJA_INLINE concepts::enable_if<
69  std::is_same<ExecPol<ChunkSize>, RAJA::policy::omp::Static<ChunkSize>>>
70 forall_impl(const ExecPol<ChunkSize>& p,
71  Iterable&& iter,
72  Func&& loop_body,
73  ForallParam&& f_params)
74 {
75  using EXEC_POL = camp::decay<decltype(p)>;
76 
78  RAJA_OMP_DECLARE_REDUCTION_COMBINE;
79 
81 #pragma omp parallel
82  {
83 
85  auto body = thread_privatize(loop_body);
86 
87  if constexpr (ChunkSize > 0)
88  {
89 #pragma omp for schedule(static, ChunkSize) reduction(combine : f_params)
90  for (decltype(distance_it) i = 0; i < distance_it; ++i)
91  {
92  RAJA::expt::invoke_body(f_params, body.get_priv(), begin_it[i]);
93  }
94  }
95  else
96  {
97 #pragma omp for schedule(static) reduction(combine : f_params)
98  for (decltype(distance_it) i = 0; i < distance_it; ++i)
99  {
100  RAJA::expt::invoke_body(f_params, body.get_priv(), begin_it[i]);
101  }
102  }
103  }
104 
106 }
107 
108 //
109 // omp for schedule(runtime)
110 //
111 template<typename Iterable, typename Func, typename ForallParam>
112 RAJA_INLINE void forall_impl(const ::RAJA::policy::omp::Runtime& p,
113  Iterable&& iter,
114  Func&& loop_body,
115  ForallParam&& f_params)
116 {
117  using EXEC_POL = camp::decay<decltype(p)>;
118 
120  RAJA_OMP_DECLARE_REDUCTION_COMBINE;
121 
123 #pragma omp parallel
124  {
125 
127  auto body = thread_privatize(loop_body);
128 
129 #pragma omp for schedule(runtime) reduction(combine : f_params)
130  for (decltype(distance_it) i = 0; i < distance_it; ++i)
131  {
132  RAJA::expt::invoke_body(f_params, body.get_priv(), begin_it[i]);
133  }
134  }
135 
137 }
138 
139 //
140 // omp for nowait (Auto)
141 //
142 template<typename Iterable, typename Func, typename ForallParam>
143 RAJA_INLINE void forall_impl_nowait(const ::RAJA::policy::omp::Auto& p,
144  Iterable&& iter,
145  Func&& loop_body,
146  ForallParam&& f_params)
147 {
148  using EXEC_POL = camp::decay<decltype(p)>;
149 
151  RAJA_OMP_DECLARE_REDUCTION_COMBINE;
152 
154 #pragma omp parallel
155  {
156 
158  auto body = thread_privatize(loop_body);
159 
160 #pragma omp for nowait reduction(combine : f_params)
161  for (decltype(distance_it) i = 0; i < distance_it; ++i)
162  {
163  RAJA::expt::invoke_body(f_params, body.get_priv(), begin_it[i]);
164  }
165  }
166 
168 }
169 
170 //
171 // omp for schedule(dynamic, ChunkSize)
172 //
173 template<typename Iterable, typename Func, int ChunkSize, typename ForallParam>
174 RAJA_INLINE void forall_impl(const ::RAJA::policy::omp::Dynamic<ChunkSize>& p,
175  Iterable&& iter,
176  Func&& loop_body,
177  ForallParam&& f_params)
178 {
179  using EXEC_POL = camp::decay<decltype(p)>;
180 
182  RAJA_OMP_DECLARE_REDUCTION_COMBINE;
183 
185 #pragma omp parallel
186  {
187 
189  auto body = thread_privatize(loop_body);
190  if constexpr (ChunkSize > 0)
191  {
192 #pragma omp for schedule(dynamic, ChunkSize) reduction(combine : f_params)
193  for (decltype(distance_it) i = 0; i < distance_it; ++i)
194  {
195  RAJA::expt::invoke_body(f_params, body.get_priv(), begin_it[i]);
196  }
197  }
198  else
199  {
200 #pragma omp for schedule(dynamic) reduction(combine : f_params)
201  for (decltype(distance_it) i = 0; i < distance_it; ++i)
202  {
203  RAJA::expt::invoke_body(f_params, body.get_priv(), begin_it[i]);
204  }
205  }
206  }
207 
208 
210 }
211 
212 //
213 // omp for schedule(guided)
214 //
215 template<typename Iterable, typename Func, int ChunkSize, typename ForallParam>
216 RAJA_INLINE void forall_impl(const ::RAJA::policy::omp::Guided<ChunkSize>& p,
217  Iterable&& iter,
218  Func&& loop_body,
219  ForallParam&& f_params)
220 {
221  using EXEC_POL = camp::decay<decltype(p)>;
222 
224  RAJA_OMP_DECLARE_REDUCTION_COMBINE;
225 
227 #pragma omp parallel
228  {
229 
231  auto body = thread_privatize(loop_body);
232  if constexpr (ChunkSize <= 0)
233  {
234 #pragma omp for schedule(guided) reduction(combine : f_params)
235  for (decltype(distance_it) i = 0; i < distance_it; ++i)
236  {
237  RAJA::expt::invoke_body(f_params, body.get_priv(), begin_it[i]);
238  }
239  }
240  else
241  {
242 #pragma omp for schedule(guided, ChunkSize) reduction(combine : f_params)
243  for (decltype(distance_it) i = 0; i < distance_it; ++i)
244  {
245  RAJA::expt::invoke_body(f_params, body.get_priv(), begin_it[i]);
246  }
247  }
248  }
249 
251 }
252 
253 //
254 // omp for schedule(static) nowait
255 //
256 template<typename Iterable, typename Func, int ChunkSize, typename ForallParam>
257 RAJA_INLINE void forall_impl_nowait(
258  const ::RAJA::policy::omp::Static<ChunkSize>& p,
259  Iterable&& iter,
260  Func&& loop_body,
261  ForallParam&& f_params)
262 {
263  using EXEC_POL = camp::decay<decltype(p)>;
264 
266  RAJA_OMP_DECLARE_REDUCTION_COMBINE;
267 
269 #pragma omp parallel
270  {
271 
273  auto body = thread_privatize(loop_body);
274  if constexpr (ChunkSize <= 0)
275  {
276 #pragma omp for schedule(static) nowait reduction(combine : f_params)
277  for (decltype(distance_it) i = 0; i < distance_it; ++i)
278  {
279  RAJA::expt::invoke_body(f_params, body.get_priv(), begin_it[i]);
280  }
281  }
282  else
283  {
284 #pragma omp for schedule(static, ChunkSize) nowait reduction(combine : f_params)
285  for (decltype(distance_it) i = 0; i < distance_it; ++i)
286  {
287  RAJA::expt::invoke_body(f_params, body.get_priv(), begin_it[i]);
288  }
289  }
290  }
292 }
293 } // namespace internal
294 
295 template<typename Schedule,
296  typename Iterable,
297  typename Func,
298  typename ForallParam>
299 RAJA_INLINE resources::EventProxy<resources::Host> forall_impl(
300  resources::Host host_res,
302  Iterable&& iter,
303  Func&& loop_body,
304  ForallParam f_params)
305 {
306  expt::internal::forall_impl(Schedule {}, std::forward<Iterable>(iter),
307  std::forward<Func>(loop_body),
308  std::forward<ForallParam>(f_params));
309  return resources::EventProxy<resources::Host>(host_res);
310 }
311 } // namespace expt
312 
316 template<typename Iterable,
317  typename Func,
318  typename InnerPolicy,
319  typename ForallParam>
320 RAJA_INLINE concepts::enable_if_t<
321  resources::EventProxy<resources::Host>,
323  concepts::negate<
325 forall_impl(resources::Host host_res,
327  Iterable&& iter,
328  Func&& loop_body,
329  ForallParam f_params)
330 {
331  expt::forall_impl(host_res, InnerPolicy {}, iter, loop_body, f_params);
332  return resources::EventProxy<resources::Host>(host_res);
333 }
334 
335 } // namespace omp
336 
337 } // namespace policy
338 
339 } // namespace RAJA
340 
341 #endif // closing endif for header file include guard
value_type::device_call &[i_loop] iter
Definition: WorkRunner.hpp:216
constexpr RAJA_HOST_DEVICE auto invoke_body(Params &&params, Fn &&f, Ts &&... extra)
Definition: forall.hpp:598
RAJA_HOST_DEVICE auto thread_privatize(const T &item) -> Privatizer< T >
Create a private copy of the argument to be stored on the current thread's stack in a class of the Pr...
Definition: privatizer.hpp:88
RAJA_INLINE concepts::enable_if< std::is_same< ExecPol, RAJA::policy::omp::Auto > > forall_impl(const ExecPol &p, Iterable &&iter, Func &&loop_body, ForallParam &&f_params)
Definition: forall.hpp:33
RAJA_INLINE void forall_impl_nowait(const ::RAJA::policy::omp::Auto &p, Iterable &&iter, Func &&loop_body, ForallParam &&f_params)
Definition: forall.hpp:143
RAJA_INLINE resources::EventProxy< resources::Host > forall_impl(resources::Host host_res, const omp_for_schedule_exec< Schedule > &, Iterable &&iter, Func &&loop_body, ForallParam f_params)
Definition: forall.hpp:299
RAJA_INLINE concepts::enable_if_t< resources::EventProxy< resources::Host >, RAJA::expt::type_traits::is_ForallParamPack< ForallParam >, concepts::negate< RAJA::expt::type_traits::is_ForallParamPack_empty< ForallParam > > > forall_impl(resources::Host host_res, const omp_parallel_exec< InnerPolicy > &, Iterable &&iter, Func &&loop_body, ForallParam f_params)
Definition: forall.hpp:325
Definition: AlignedRangeIndexSetBuilders.cpp:35
auto & body
Definition: launch.hpp:177
#define RAJA_EXTRACT_BED_IT(CONTAINER)
Definition: forall.hpp:32
Definition: PolicyBase.hpp:75
static constexpr void parampack_resolve(EXEC_POL const &pol, ForallParamPack< Params... > &f_params, Args &&... args)
Definition: forall.hpp:304
static constexpr void parampack_init(EXEC_POL const &pol, ForallParamPack< Params... > &f_params, Args &&... args)
Definition: forall.hpp:269
Definition: TypeTraits.hpp:59
Definition: policy.hpp:109