DASH  0.3.0
Operation.h
1 #ifndef DASH__ALGORITHM__OPERATION_H__
2 #define DASH__ALGORITHM__OPERATION_H__
3 
4 #include <dash/Meta.h>
5 #include <dash/Types.h>
6 
8 
9 #include <functional>
10 
11 
19 namespace dash {
20 
21 namespace internal {
22 
23 
24 enum class OpKind {
25  ARITHMETIC,
26  BITWISE,
27  NOOP
28 };
29 
36 template <
37  typename ValueType,
38  dart_operation_t OP,
39  OpKind KIND,
40  bool enabled = true >
41 class ReduceOperation {
42  static constexpr const dart_operation_t _op = OP;
43  static constexpr const OpKind _kind = KIND;
44 public:
45  typedef ValueType value_type;
46 
47 public:
48  static constexpr dart_operation_t
49  dart_operation() {
50  return _op;
51  }
52 
53  static constexpr OpKind
54  op_kind() {
55  return _kind;
56  }
57 };
58 
65 template<typename BinaryOperation, typename = void>
66 struct dart_reduce_operation
67  : public std::integral_constant<dart_operation_t, DART_OP_UNDEFINED>
68 { };
69 
74 template<typename BinaryOperation>
75 struct dart_reduce_operation<BinaryOperation,
76  typename std::enable_if<
77  // no-ops cannot be used in DART collectives
78  BinaryOperation::op_kind() != dash::internal::OpKind::NOOP &&
79  // only pre-defined operations can be used in DART collectives,
80  // they are derived from dash::internal::ReduceOperation
81  std::is_base_of<
82  dash::internal::ReduceOperation<
83  typename BinaryOperation::value_type,
84  BinaryOperation::dart_operation(),
85  BinaryOperation::op_kind(), true>,
86  BinaryOperation>::value>::type>
87  : public std::integral_constant<dart_operation_t,
88  BinaryOperation::dart_operation()>
89 { };
90 
91 } // namespace internal
92 
93 #ifdef DOXYGEN
94 
110 template<typename BinaryOperation>
112 {
113  dart_operation_t value;
114 }
115 #endif // DOXYGEN
116 
124 template< typename ValueType >
125 struct min
126 : public internal::ReduceOperation< ValueType, DART_OP_MIN,
127  dash::internal::OpKind::ARITHMETIC,
128  dash::is_arithmetic<ValueType>::value > {
129  ValueType operator()(
130  const ValueType & lhs,
131  const ValueType & rhs) const {
132  return (lhs < rhs) ? lhs : rhs;
133  }
134 };
135 
143 template< typename ValueType >
144 struct max
145 : public internal::ReduceOperation< ValueType, DART_OP_MAX,
146  dash::internal::OpKind::ARITHMETIC,
147  dash::is_arithmetic<ValueType>::value > {
148  ValueType operator()(
149  const ValueType & lhs,
150  const ValueType & rhs) const {
151  return (lhs > rhs) ? lhs : rhs;
152  }
153 };
154 
162 template< typename ValueType >
163 struct plus
164 : public internal::ReduceOperation< ValueType, DART_OP_SUM,
165  dash::internal::OpKind::ARITHMETIC,
166  dash::is_arithmetic<ValueType>::value > {
167  ValueType operator()(
168  const ValueType & lhs,
169  const ValueType & rhs) const {
170  return lhs + rhs;
171  }
172 };
173 
181 template< typename ValueType >
182 struct multiply
183 : public internal::ReduceOperation< ValueType, DART_OP_PROD,
184  dash::internal::OpKind::ARITHMETIC,
185  dash::is_arithmetic<ValueType>::value > {
186  ValueType operator()(
187  const ValueType & lhs,
188  const ValueType & rhs) const {
189  return lhs * rhs;
190  }
191 };
192 
200 template< typename ValueType >
201 struct first
202 : public internal::ReduceOperation< ValueType, DART_OP_NO_OP,
203  dash::internal::OpKind::NOOP, true > {
204  ValueType operator()(const ValueType& lhs, const ValueType& /*rhs*/) const
205  {
206  return lhs;
207  }
208 };
209 
217 template< typename ValueType >
218 struct second
219 : public internal::ReduceOperation< ValueType, DART_OP_REPLACE,
220  dash::internal::OpKind::NOOP, true > {
221  ValueType operator()(const ValueType& /*lhs*/, const ValueType& rhs) const
222  {
223  return rhs;
224  }
225 };
226 
234 template< typename ValueType >
235 struct bit_and
236 : public internal::ReduceOperation< ValueType, DART_OP_BAND,
237  dash::internal::OpKind::BITWISE, true > {
238  ValueType operator()(
239  const ValueType & lhs,
240  const ValueType & rhs) const {
241  return lhs & rhs;
242  }
243 };
244 
252 template< typename ValueType >
253 struct bit_or
254 : public internal::ReduceOperation< ValueType, DART_OP_BOR,
255  dash::internal::OpKind::BITWISE, true > {
256  ValueType operator()(
257  const ValueType & lhs,
258  const ValueType & rhs) const {
259  return lhs | rhs;
260  }
261 };
262 
270 template< typename ValueType >
271 struct bit_xor
272 : public internal::ReduceOperation< ValueType, DART_OP_BXOR,
273  dash::internal::OpKind::BITWISE, true > {
274  ValueType operator()(
275  const ValueType & lhs,
276  const ValueType & rhs) const {
277  return lhs ^ rhs;
278  }
279 };
280 
281 } // namespace dash
282 
283 #endif // DASH__ALGORITHM__OPERATION_H__
Returns second operand.
Definition: Operation.h:218
Reduce operands to their sum.
Definition: Operation.h:163
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8
Reduce operands with binary OR.
Definition: Operation.h:253
Returns second operand.
Definition: Operation.h:201
Reduce operands to their maximum value.
Definition: Operation.h:144
Reduce operands with binary AND.
Definition: Operation.h:235
Reduce operands to their product.
Definition: Operation.h:182
Reduce operands with binary XOR.
Definition: Operation.h:271
struct dash::dart_operation ValueType
Reduce operands to their minimum value.
see https://en.cppreference.com/w/cpp/feature_test for recommended feature tests
Definition: cstddef.h:8
Query the underlying dart_operation_t for arbitrary binary operations.
Definition: Operation.h:111