RAJA
RAJA provides a collection of platform portability abstractions for C++ HPC applications.
reduce.hpp
Go to the documentation of this file.
1 
14 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
15 // Copyright (c) Lawrence Livermore National Security, LLC and other
16 // RAJA Project Developers. See top-level LICENSE and COPYRIGHT
17 // files for dates and other details. No copyright assignment is required
18 // to contribute to RAJA.
19 //
20 // SPDX-License-Identifier: (BSD-3-Clause)
21 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
22 
23 #ifndef RAJA_omp_reduce_HPP
24 #define RAJA_omp_reduce_HPP
25 
26 #include "RAJA/config.hpp"
27 
28 #if defined(RAJA_ENABLE_OPENMP)
29 
30 #include <memory>
31 #include <vector>
32 
33 #include <omp.h>
34 
35 #include "RAJA/util/types.hpp"
36 
38 #include "RAJA/pattern/reduce.hpp"
39 
41 
42 namespace RAJA
43 {
44 
45 namespace detail
46 {
47 template<typename T, typename Reduce>
48 class ReduceOMP
49  : public reduce::detail::BaseCombinable<T, Reduce, ReduceOMP<T, Reduce>>
50 {
51  using Base = reduce::detail::BaseCombinable<T, Reduce, ReduceOMP>;
52 
53 public:
54  using Base::Base;
56  ReduceOMP() = delete;
57 
58  ~ReduceOMP()
59  {
60  if (Base::parent)
61  {
62 #pragma omp critical(ompReduceCritical)
63  Reduce()(Base::parent->local(), Base::my_data);
64  Base::my_data = Base::identity;
65  }
66  }
67 };
68 
69 } // namespace detail
70 
71 RAJA_DECLARE_ALL_REDUCERS(omp_reduce, detail::ReduceOMP)
72 
73 //
75 // Old ordered reductions are included below.
76 //
78 
79 namespace detail
80 {
81 template<typename T, typename Reduce>
82 class ReduceOMPOrdered
83  : public reduce::detail::
84  BaseCombinable<T, Reduce, ReduceOMPOrdered<T, Reduce>>
85 {
86  using Base = reduce::detail::BaseCombinable<T, Reduce, ReduceOMPOrdered>;
87  std::shared_ptr<std::vector<T>> data;
88 
89 public:
90  ReduceOMPOrdered() { reset(T(), T()); }
91 
93  explicit ReduceOMPOrdered(T init_val, T identity_)
94  {
95  reset(init_val, identity_);
96  }
97 
98  void reset(T init_val, T identity_)
99  {
100  Base::reset(init_val, identity_);
101  data = std::shared_ptr<std::vector<T>>(
102  std::make_shared<std::vector<T>>(omp_get_max_threads(), identity_));
103  }
104 
105  ~ReduceOMPOrdered()
106  {
107  Reduce {}((*data)[omp_get_thread_num()], Base::my_data);
108  Base::my_data = Base::identity;
109  }
110 
111  T get_combined() const
112  {
113  if (Base::my_data != Base::identity)
114  {
115  Reduce {}((*data)[omp_get_thread_num()], Base::my_data);
116  Base::my_data = Base::identity;
117  }
118 
119  T res = Base::identity;
120  for (size_t i = 0; i < data->size(); ++i)
121  {
122  Reduce {}(res, (*data)[i]);
123  }
124  return res;
125  }
126 };
127 
128 } // namespace detail
129 
130 RAJA_DECLARE_ALL_REDUCERS(omp_reduce_ordered, detail::ReduceOMPOrdered)
131 
132 } // namespace RAJA
133 
134 #endif // closing endif for RAJA_ENABLE_OPENMP guard
135 
136 #endif // closing endif for header file include guard
constexpr auto Reduce(T *target)
Definition: reducer.hpp:231
Definition: AlignedRangeIndexSetBuilders.cpp:35
Header file containing RAJA OpenMP policy definitions.
Base types used in common for RAJA reducer objects.
#define RAJA_DECLARE_ALL_REDUCERS(POL, COMBINER)
Definition: reduce.hpp:46
Header file providing RAJA reduction declarations.
Header file for RAJA type definitions.