]>
Commit | Line | Data |
---|---|---|
4710dd51 | 1 | /* reducer_opadd.h -*- C++ -*- |
2 | * | |
0657c20f | 3 | * Copyright (C) 2009-2016, Intel Corporation |
4710dd51 | 4 | * All rights reserved. |
5 | * | |
4710dd51 | 6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions | |
8 | * are met: | |
9 | * | |
10 | * * Redistributions of source code must retain the above copyright | |
11 | * notice, this list of conditions and the following disclaimer. | |
12 | * * Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in | |
14 | * the documentation and/or other materials provided with the | |
15 | * distribution. | |
16 | * * Neither the name of Intel Corporation nor the names of its | |
17 | * contributors may be used to endorse or promote products derived | |
18 | * from this software without specific prior written permission. | |
19 | * | |
4710dd51 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
24 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
26 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | |
27 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
28 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY | |
30 | * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
31 | * POSSIBILITY OF SUCH DAMAGE. | |
0657c20f | 32 | * |
33 | * ********************************************************************* | |
34 | * | |
35 | * PLEASE NOTE: This file is a downstream copy of a file mainitained in | |
36 | * a repository at cilkplus.org. Changes made to this file that are not | |
37 | * submitted through the contribution process detailed at | |
38 | * http://www.cilkplus.org/submit-cilk-contribution will be lost the next | |
39 | * time that a new version is released. Changes only submitted to the | |
40 | * GNU compiler collection or posted to the git repository at | |
41 | * https://bitbucket.org/intelcilkruntime/intel-cilk-runtime.git are | |
42 | * not tracked. | |
43 | * | |
44 | * We welcome your contributions to this open source project. Thank you | |
45 | * for your assistance in helping us improve Cilk Plus. | |
4710dd51 | 46 | */ |
47 | ||
48 | /** @file reducer_opadd.h | |
49 | * | |
50 | * @brief Defines classes for doing parallel addition reductions. | |
51 | * | |
52 | * @ingroup ReducersAdd | |
53 | * | |
54 | * @see ReducersAdd | |
55 | */ | |
56 | ||
57 | #ifndef REDUCER_OPADD_H_INCLUDED | |
58 | #define REDUCER_OPADD_H_INCLUDED | |
59 | ||
60 | #include <cilk/reducer.h> | |
61 | ||
62 | /** @defgroup ReducersAdd Addition Reducers | |
63 | * | |
64 | * Addition reducers allow the computation of the sum of a set of values in | |
65 | * parallel. | |
66 | * | |
67 | * @ingroup Reducers | |
68 | * | |
0657c20f | 69 | * You should be familiar with @ref pagereducers "Intel(R) Cilk(TM) Plus reducers", |
70 | * described in file `reducers.md`, and particularly with @ref reducers_using, | |
71 | * before trying to use the information in this file. | |
4710dd51 | 72 | * |
73 | * @section redopadd_usage Usage Example | |
74 | * | |
75 | * cilk::reducer< cilk::op_add<int> > r; | |
76 | * cilk_for (int i = 0; i != N; ++i) { | |
77 | * *r += a[i]; | |
78 | * } | |
79 | * return r.get_value(); | |
80 | * | |
81 | * @section redopadd_monoid The Monoid | |
82 | * | |
83 | * @subsection redopadd_monoid_values Value Set | |
84 | * | |
85 | * The value set of an addition reducer is the set of values of `Type`, which | |
86 | * is expected to be a builtin numeric type (or something like it, such as | |
87 | * `std::complex`). | |
88 | * | |
89 | * @subsection redopadd_monoid_operator Operator | |
90 | * | |
91 | * The operator of an addition reducer is the addition operator, defined by | |
0657c20f | 92 | * the "`+`" binary operator on `Type`. |
4710dd51 | 93 | * |
94 | * @subsection redopadd_monoid_identity Identity | |
95 | * | |
0657c20f | 96 | * The identity value of the reducer is the numeric value "`0`". This is |
4710dd51 | 97 | * expected to be the value of the default constructor `Type()`. |
98 | * | |
99 | * @section redopadd_operations Operations | |
100 | * | |
101 | * @subsection redopadd_constructors Constructors | |
102 | * | |
103 | * reducer() // identity | |
104 | * reducer(const Type& value) | |
105 | * reducer(move_in(Type& variable)) | |
106 | * | |
107 | * @subsection redopadd_get_set Set and Get | |
108 | * | |
109 | * r.set_value(const Type& value) | |
110 | * const Type& = r.get_value() const | |
111 | * r.move_in(Type& variable) | |
112 | * r.move_out(Type& variable) | |
113 | * | |
114 | * @subsection redopadd_initial Initial Values | |
115 | * | |
116 | * If an addition reducer is constructed without an explicit initial value, | |
117 | * then its initial value will be its identity value, as long as `Type` | |
118 | * satisfies the requirements of @ref redopadd_types. | |
119 | * | |
120 | * @subsection redopadd_view_ops View Operations | |
121 | * | |
122 | * *r += a | |
123 | * *r -= a | |
124 | * ++*r | |
125 | * --*r | |
126 | * (*r)++ | |
127 | * (*r)-- | |
128 | * *r = *r + a | |
129 | * *r = *r - a | |
130 | * *r = *r ± a1 ± a2 … ± an | |
131 | * | |
132 | * The post-increment and post-decrement operations do not return a value. (If | |
133 | * they did, they would expose the value contained in the view, which is | |
134 | * non-deterministic in the middle of a reduction.) | |
135 | * | |
136 | * Note that subtraction operations are allowed on an addition reducer because | |
137 | * subtraction is equivalent to addition with a negated operand. It is true | |
138 | * that `(x - y) - z` is not equivalent to `x - (y - z)`, but | |
139 | * `(x + (-y)) + (-z)` _is_ equivalent to `x + ((-y) + (-z))`. | |
140 | * | |
141 | * @section redopadd_floating_point Issues with Floating-Point Types | |
142 | * | |
143 | * Because of precision and round-off issues, floating-point addition is not | |
0657c20f | 144 | * really associative. For example, `(1e30 + -1e30) + 1 == 1`, but |
4710dd51 | 145 | * `1e30 + (-1e30 + 1) == 0`. |
146 | * | |
0657c20f | 147 | * In many cases, this won't matter, but computations which have been |
4710dd51 | 148 | * carefully ordered to control round-off errors may not deal well with |
149 | * being reassociated. In general, you should be sure to understand the | |
0657c20f | 150 | * floating-point behavior of your program before doing any transformation |
151 | * that will reassociate its computations. | |
4710dd51 | 152 | * |
153 | * @section redopadd_types Type and Operator Requirements | |
154 | * | |
155 | * `Type` must be `Copy Constructible`, `Default Constructible`, and | |
156 | * `Assignable`. | |
157 | * | |
0657c20f | 158 | * The operator "`+=`" must be defined on `Type`, with `x += a` having the |
159 | * same meaning as `x = x + a`. In addition, if the code uses the "`-=`", | |
4710dd51 | 160 | * pre-increment, post-increment, pre-decrement, or post-decrement operators, |
161 | * then the corresponding operators must be defined on `Type`. | |
162 | * | |
163 | * The expression `Type()` must be a valid expression which yields the | |
164 | * identity value (the value of `Type` whose numeric value is zero). | |
165 | * | |
166 | * @section redopadd_in_c Addition Reducers in C | |
167 | * | |
168 | * The @ref CILK_C_REDUCER_OPADD and @ref CILK_C_REDUCER_OPADD_TYPE macros can | |
169 | * be used to do addition reductions in C. For example: | |
170 | * | |
171 | * CILK_C_REDUCER_OPADD(r, double, 0); | |
172 | * CILK_C_REGISTER_REDUCER(r); | |
173 | * cilk_for(int i = 0; i != n; ++i) { | |
174 | * REDUCER_VIEW(r) += a[i]; | |
175 | * } | |
176 | * CILK_C_UNREGISTER_REDUCER(r); | |
177 | * printf("The sum of the elements of a is %f\n", REDUCER_VIEW(r)); | |
178 | * | |
179 | * See @ref reducers_c_predefined. | |
180 | */ | |
181 | ||
182 | #ifdef __cplusplus | |
183 | ||
184 | namespace cilk { | |
185 | ||
186 | /** The addition reducer view class. | |
187 | * | |
0657c20f | 188 | * This is the view class for reducers created with |
189 | * `cilk::reducer< cilk::op_add<Type> >`. It holds the accumulator variable | |
190 | * for the reduction, and allows only addition and subtraction operations to | |
4710dd51 | 191 | * be performed on it. |
192 | * | |
0657c20f | 193 | * @note The reducer "dereference" operation (`reducer::operator *()`) |
194 | * yields a reference to the view. Thus, for example, the view class's | |
4710dd51 | 195 | * `+=` operation would be used in an expression like `*r += a`, where |
196 | * `r` is an op_add reducer variable. | |
197 | * | |
0657c20f | 198 | * @tparam Type The type of the contained accumulator variable. This will |
199 | * be the value type of a monoid_with_view that is | |
4710dd51 | 200 | * instantiated with this view. |
201 | * | |
202 | * @see ReducersAdd | |
203 | * @see op_add | |
204 | * | |
205 | * @ingroup ReducersAdd | |
206 | */ | |
207 | template <typename Type> | |
208 | class op_add_view : public scalar_view<Type> | |
209 | { | |
210 | typedef scalar_view<Type> base; | |
0657c20f | 211 | |
4710dd51 | 212 | public: |
0657c20f | 213 | /** Class to represent the right-hand side of |
4710dd51 | 214 | * `*reducer = *reducer ± value`. |
215 | * | |
216 | * The only assignment operator for the op_add_view class takes an | |
217 | * rhs_proxy as its operand. This results in the syntactic restriction | |
218 | * that the only expressions that can be assigned to an op_add_view are | |
0657c20f | 219 | * ones which generate an rhs_proxy - that is, expressions of the form |
4710dd51 | 220 | * `op_add_view ± value ... ± value`. |
221 | * | |
222 | * @warning | |
0657c20f | 223 | * The lhs and rhs views in such an assignment must be the same; |
4710dd51 | 224 | * otherwise, the behavior will be undefined. (I.e., `v1 = v1 + x` is |
225 | * legal; `v1 = v2 + x` is illegal.) This condition will be checked with a | |
226 | * runtime assertion when compiled in debug mode. | |
227 | * | |
228 | * @see op_add_view | |
229 | */ | |
230 | class rhs_proxy { | |
231 | friend class op_add_view; | |
232 | ||
233 | const op_add_view* m_view; | |
234 | Type m_value; | |
235 | ||
0657c20f | 236 | // Constructor is invoked only from op_add_view::operator+() and |
4710dd51 | 237 | // op_add_view::operator-(). |
238 | // | |
239 | rhs_proxy(const op_add_view* view, const Type& value) : | |
240 | m_view(view), m_value(value) {} | |
241 | ||
242 | rhs_proxy& operator=(const rhs_proxy&); // Disable assignment operator | |
243 | rhs_proxy(); // Disable default constructor | |
244 | ||
245 | public: | |
0657c20f | 246 | ///@{ |
247 | /** Adds or subtracts an additional rhs value. If `v` is an op_add_view | |
248 | * and `a1` is a value, then the expression `v + a1` invokes the view's | |
249 | * `operator+()` to create an rhs_proxy for `(v, a1)`; then | |
250 | * `v + a1 + a2` invokes the rhs_proxy's `operator+()` to create a new | |
4710dd51 | 251 | * rhs_proxy for `(v, a1+a2)`. This allows the right-hand side of an |
0657c20f | 252 | * assignment to be not just `view ± value`, but |
4710dd51 | 253 | * `view ± value ± value ... ± value`. The effect is that |
254 | * | |
255 | * v = v ± a1 ± a2 ... ± an; | |
256 | * | |
257 | * is evaluated as | |
258 | * | |
259 | * v = v ± (±a1 ± a2 ... ± an); | |
260 | */ | |
261 | rhs_proxy& operator+(const Type& x) { m_value += x; return *this; } | |
262 | rhs_proxy& operator-(const Type& x) { m_value -= x; return *this; } | |
0657c20f | 263 | ///@} |
4710dd51 | 264 | }; |
265 | ||
0657c20f | 266 | |
267 | /** Default/identity constructor. This constructor initializes the | |
4710dd51 | 268 | * contained value to `Type()`, which is expected to be the identity value |
269 | * for addition on `Type`. | |
270 | */ | |
271 | op_add_view() : base() {} | |
272 | ||
273 | /** Construct with a specified initial value. | |
274 | */ | |
275 | explicit op_add_view(const Type& v) : base(v) {} | |
0657c20f | 276 | |
277 | /** Reduces the views of two strands. | |
4710dd51 | 278 | * |
279 | * This function is invoked by the @ref op_add monoid to combine the views | |
280 | * of two strands when the right strand merges with the left one. It adds | |
281 | * the value contained in the right-strand view to the value contained in | |
282 | * the left-strand view, and leaves the value in the right-strand view | |
283 | * undefined. | |
284 | * | |
285 | * @param right A pointer to the right-strand view. (`this` points to | |
286 | * the left-strand view.) | |
287 | * | |
288 | * @note Used only by the @ref op_add monoid to implement the monoid | |
289 | * reduce operation. | |
290 | */ | |
291 | void reduce(op_add_view* right) { this->m_value += right->m_value; } | |
292 | ||
293 | /** @name Accumulator variable updates. | |
294 | * | |
295 | * These functions support the various syntaxes for incrementing or | |
296 | * decrementing the accumulator variable contained in the view. | |
297 | */ | |
0657c20f | 298 | ///@{ |
4710dd51 | 299 | |
0657c20f | 300 | /** Increments the accumulator variable by @a x. |
4710dd51 | 301 | */ |
302 | op_add_view& operator+=(const Type& x) { this->m_value += x; return *this; } | |
303 | ||
0657c20f | 304 | /** Decrements the accumulator variable by @a x. |
4710dd51 | 305 | */ |
306 | op_add_view& operator-=(const Type& x) { this->m_value -= x; return *this; } | |
307 | ||
308 | /** Pre-increment. | |
309 | */ | |
310 | op_add_view& operator++() { ++this->m_value; return *this; } | |
311 | ||
0657c20f | 312 | /** Post-increments. |
4710dd51 | 313 | * |
314 | * @note Conventionally, post-increment operators return the old value | |
315 | * of the incremented variable. However, reducer views do not | |
316 | * expose their contained values, so `view++` does not have a | |
317 | * return value. | |
318 | */ | |
319 | void operator++(int) { this->m_value++; } | |
320 | ||
0657c20f | 321 | /** Pre-decrements. |
4710dd51 | 322 | */ |
323 | op_add_view& operator--() { --this->m_value; return *this; } | |
324 | ||
0657c20f | 325 | /** Post-decrements. |
4710dd51 | 326 | * |
327 | * @note Conventionally, post-decrement operators return the old value | |
328 | * of the decremented variable. However, reducer views do not | |
329 | * expose their contained values, so `view--` does not have a | |
330 | * return value. | |
331 | */ | |
332 | void operator--(int) { this->m_value--; } | |
333 | ||
0657c20f | 334 | /** Creates an object representing `*this + x`. |
4710dd51 | 335 | * |
336 | * @see rhs_proxy | |
337 | */ | |
338 | rhs_proxy operator+(const Type& x) const { return rhs_proxy(this, x); } | |
339 | ||
0657c20f | 340 | /** Creates an object representing `*this - x`. |
4710dd51 | 341 | * |
342 | * @see rhs_proxy | |
343 | */ | |
344 | rhs_proxy operator-(const Type& x) const { return rhs_proxy(this, -x); } | |
345 | ||
0657c20f | 346 | /** Assigns the result of a `view ± value` expression to the view. Note that |
4710dd51 | 347 | * this is the only assignment operator for this class. |
348 | * | |
349 | * @see rhs_proxy | |
350 | */ | |
351 | op_add_view& operator=(const rhs_proxy& rhs) { | |
352 | __CILKRTS_ASSERT(this == rhs.m_view); | |
353 | this->m_value += rhs.m_value; | |
354 | return *this; | |
355 | } | |
0657c20f | 356 | |
357 | ///@} | |
4710dd51 | 358 | }; |
359 | ||
360 | ||
0657c20f | 361 | /** Monoid class for addition reductions. Instantiate the cilk::reducer |
4710dd51 | 362 | * template class with an op_add monoid to create an addition reducer class. |
363 | * For example, to compute | |
364 | * the sum of a set of `int` values: | |
365 | * | |
366 | * cilk::reducer< cilk::op_add<int> > r; | |
367 | * | |
368 | * @tparam Type The reducer value type. | |
369 | * @tparam Align If `false` (the default), reducers instantiated on this | |
0657c20f | 370 | * monoid will be naturally aligned (the Intel Cilk Plus library 1.0 |
4710dd51 | 371 | * behavior). If `true`, reducers instantiated on this monoid |
0657c20f | 372 | * will be cache-aligned for binary compatibility with |
373 | * reducers in Intel Cilk Plus library version 0.9. | |
4710dd51 | 374 | * |
375 | * @see ReducersAdd | |
376 | * @see op_add_view | |
377 | * | |
378 | * @ingroup ReducersAdd | |
379 | */ | |
380 | template <typename Type, bool Align = false> | |
381 | struct op_add : public monoid_with_view<op_add_view<Type>, Align> {}; | |
382 | ||
383 | /** **Deprecated** addition reducer wrapper class. | |
384 | * | |
385 | * reducer_opadd is the same as @ref reducer<@ref op_add>, except that | |
386 | * reducer_opadd is a proxy for the contained view, so that accumulator | |
387 | * variable update operations can be applied directly to the reducer. For | |
388 | * example, a value is added to a `reducer<%op_add>` with `*r += a`, but a | |
389 | * value can be added to a `%reducer_opadd` with `r += a`. | |
390 | * | |
391 | * @deprecated Users are strongly encouraged to use `reducer<monoid>` | |
0657c20f | 392 | * reducers rather than the old wrappers like reducer_opadd. |
4710dd51 | 393 | * The `reducer<monoid>` reducers show the reducer/monoid/view |
394 | * architecture more clearly, are more consistent in their | |
395 | * implementation, and present a simpler model for new | |
396 | * user-implemented reducers. | |
397 | * | |
0657c20f | 398 | * @note Implicit conversions are provided between `%reducer_opadd` |
4710dd51 | 399 | * and `reducer<%op_add>`. This allows incremental code |
400 | * conversion: old code that used `%reducer_opadd` can pass a | |
401 | * `%reducer_opadd` to a converted function that now expects a | |
402 | * pointer or reference to a `reducer<%op_add>`, and vice | |
403 | * versa. | |
404 | * | |
405 | * @tparam Type The value type of the reducer. | |
406 | * | |
407 | * @see op_add | |
408 | * @see reducer | |
409 | * @see ReducersAdd | |
410 | * | |
411 | * @ingroup ReducersAdd | |
412 | */ | |
413 | template <typename Type> | |
414 | class reducer_opadd : public reducer< op_add<Type, true> > | |
415 | { | |
416 | typedef reducer< op_add<Type, true> > base; | |
417 | using base::view; | |
418 | ||
419 | public: | |
420 | /// The view type for the reducer. | |
421 | typedef typename base::view_type view_type; | |
0657c20f | 422 | |
423 | /// The view's rhs proxy type. | |
4710dd51 | 424 | typedef typename view_type::rhs_proxy rhs_proxy; |
425 | ||
426 | /// The view type for the reducer. | |
427 | typedef view_type View; | |
428 | ||
429 | /// The monoid type for the reducer. | |
430 | typedef typename base::monoid_type Monoid; | |
431 | ||
432 | /** @name Constructors | |
433 | */ | |
0657c20f | 434 | ///@{ |
435 | ||
4710dd51 | 436 | /** Default (identity) constructor. |
437 | * | |
438 | * Constructs the wrapper with the default initial value of `Type()`. | |
439 | */ | |
440 | reducer_opadd() {} | |
441 | ||
442 | /** Value constructor. | |
443 | * | |
444 | * Constructs the wrapper with a specified initial value. | |
445 | */ | |
446 | explicit reducer_opadd(const Type& initial_value) : base(initial_value) {} | |
0657c20f | 447 | |
448 | ///@} | |
4710dd51 | 449 | |
450 | /** @name Forwarded functions | |
451 | * @details Functions that update the contained accumulator variable are | |
452 | * simply forwarded to the contained @ref op_add_view. */ | |
0657c20f | 453 | ///@{ |
454 | ||
4710dd51 | 455 | /// @copydoc op_add_view::operator+=(const Type&) |
456 | reducer_opadd& operator+=(const Type& x) { view() += x; return *this; } | |
0657c20f | 457 | |
4710dd51 | 458 | /// @copydoc op_add_view::operator-=(const Type&) |
459 | reducer_opadd& operator-=(const Type& x) { view() -= x; return *this; } | |
0657c20f | 460 | |
4710dd51 | 461 | /// @copydoc op_add_view::operator++() |
462 | reducer_opadd& operator++() { ++view(); return *this; } | |
0657c20f | 463 | |
4710dd51 | 464 | /// @copydoc op_add_view::operator++(int) |
465 | void operator++(int) { view()++; } | |
0657c20f | 466 | |
4710dd51 | 467 | /// @copydoc op_add_view::operator-\-() |
468 | reducer_opadd& operator--() { --view(); return *this; } | |
0657c20f | 469 | |
4710dd51 | 470 | /// @copydoc op_add_view::operator-\-(int) |
471 | void operator--(int) { view()--; } | |
472 | ||
473 | // The legacy definitions of reducer_opadd::operator+() and | |
474 | // reducer_opadd::operator-() have different behavior and a different | |
475 | // return type than this definition. The legacy version is defined as a | |
476 | // member function, so this new version is defined as a free function to | |
0657c20f | 477 | // give it a different signature, so that they won't end up sharing a |
4710dd51 | 478 | // single object file entry. |
479 | ||
480 | /// @copydoc op_add_view::operator+(const Type&) const | |
481 | friend rhs_proxy operator+(const reducer_opadd& r, const Type& x) | |
0657c20f | 482 | { |
483 | return r.view() + x; | |
4710dd51 | 484 | } |
485 | /// @copydoc op_add_view::operator-(const Type&) const | |
486 | friend rhs_proxy operator-(const reducer_opadd& r, const Type& x) | |
0657c20f | 487 | { |
488 | return r.view() - x; | |
4710dd51 | 489 | } |
490 | /// @copydoc op_add_view::operator=(const rhs_proxy&) | |
0657c20f | 491 | reducer_opadd& operator=(const rhs_proxy& temp) |
4710dd51 | 492 | { |
493 | view() = temp; | |
0657c20f | 494 | return *this; |
4710dd51 | 495 | } |
0657c20f | 496 | ///@} |
4710dd51 | 497 | |
498 | /** @name Dereference | |
499 | * @details Dereferencing a wrapper is a no-op. It simply returns the | |
500 | * wrapper. Combined with the rule that the wrapper forwards view | |
501 | * operations to its contained view, this means that view operations can | |
502 | * be written the same way on reducers and wrappers, which is convenient | |
503 | * for incrementally converting old code using wrappers to use reducers | |
504 | * instead. That is: | |
505 | * | |
506 | * reducer< op_add<int> > r; | |
507 | * *r += a; // *r returns the view | |
508 | * // operator += is a view member function | |
509 | * | |
510 | * reducer_opadd<int> w; | |
511 | * *w += a; // *w returns the wrapper | |
512 | * // operator += is a wrapper member function that | |
513 | * // calls the corresponding view function | |
514 | */ | |
0657c20f | 515 | ///@{ |
4710dd51 | 516 | reducer_opadd& operator*() { return *this; } |
517 | reducer_opadd const& operator*() const { return *this; } | |
518 | ||
519 | reducer_opadd* operator->() { return this; } | |
520 | reducer_opadd const* operator->() const { return this; } | |
0657c20f | 521 | ///@} |
522 | ||
4710dd51 | 523 | /** @name Upcast |
0657c20f | 524 | * @details In Intel Cilk Plus library 0.9, reducers were always cache-aligned. |
525 | * In library 1.0, reducer cache alignment is optional. By default, | |
526 | * reducers are unaligned (i.e., just naturally aligned), but legacy | |
527 | * wrappers inherit from cache-aligned reducers for binary compatibility. | |
4710dd51 | 528 | * |
529 | * This means that a wrapper will automatically be upcast to its aligned | |
530 | * reducer base class. The following conversion operators provide | |
531 | * pseudo-upcasts to the corresponding unaligned reducer class. | |
532 | */ | |
0657c20f | 533 | ///@{ |
4710dd51 | 534 | operator reducer< op_add<Type, false> >& () |
535 | { | |
536 | return *reinterpret_cast< reducer< op_add<Type, false> >* >(this); | |
537 | } | |
538 | operator const reducer< op_add<Type, false> >& () const | |
539 | { | |
540 | return *reinterpret_cast< const reducer< op_add<Type, false> >* >(this); | |
541 | } | |
0657c20f | 542 | ///@} |
4710dd51 | 543 | }; |
544 | ||
545 | /// @cond internal | |
546 | /** Metafunction specialization for reducer conversion. | |
547 | * | |
0657c20f | 548 | * This specialization of the @ref legacy_reducer_downcast template class |
549 | * defined in reducer.h causes the `reducer< op_add<Type> >` class to have an | |
550 | * `operator reducer_opadd<Type>& ()` conversion operator that statically | |
4710dd51 | 551 | * downcasts the `reducer<op_add>` to the corresponding `reducer_opadd` type. |
552 | * (The reverse conversion, from `reducer_opadd` to `reducer<op_add>`, is just | |
553 | * an upcast, which is provided for free by the language.) | |
554 | * | |
555 | * @ingroup ReducersAdd | |
556 | */ | |
557 | template <typename Type, bool Align> | |
558 | struct legacy_reducer_downcast<reducer<op_add<Type, Align> > > | |
559 | { | |
560 | typedef reducer_opadd<Type> type; | |
561 | }; | |
562 | /// @endcond | |
563 | ||
564 | } // namespace cilk | |
565 | ||
566 | #endif // __cplusplus | |
567 | ||
568 | ||
569 | /** @ingroup ReducersAdd | |
570 | */ | |
0657c20f | 571 | ///@{ |
4710dd51 | 572 | |
573 | /** @name C Language Reducer Macros | |
574 | * | |
0657c20f | 575 | * These macros are used to declare and work with numeric op_add reducers in |
4710dd51 | 576 | * C code. |
577 | * | |
578 | * @see @ref page_reducers_in_c | |
579 | */ | |
0657c20f | 580 | ///@{ |
581 | ||
4710dd51 | 582 | __CILKRTS_BEGIN_EXTERN_C |
583 | ||
0657c20f | 584 | /** Declares opadd reducer type name. |
4710dd51 | 585 | * |
586 | * This macro expands into the identifier which is the name of the op_add | |
587 | * reducer type for a specified numeric type. | |
588 | * | |
589 | * @param tn The @ref reducers_c_type_names "numeric type name" specifying | |
590 | * the type of the reducer. | |
591 | * | |
592 | * @see @ref reducers_c_predefined | |
593 | * @see ReducersAdd | |
594 | */ | |
595 | #define CILK_C_REDUCER_OPADD_TYPE(tn) \ | |
596 | __CILKRTS_MKIDENT(cilk_c_reducer_opadd_,tn) | |
597 | ||
0657c20f | 598 | /** Declares an op_add reducer object. |
4710dd51 | 599 | * |
600 | * This macro expands into a declaration of an op_add reducer object for a | |
601 | * specified numeric type. For example: | |
602 | * | |
603 | * CILK_C_REDUCER_OPADD(my_reducer, double, 0.0); | |
604 | * | |
605 | * @param obj The variable name to be used for the declared reducer object. | |
606 | * @param tn The @ref reducers_c_type_names "numeric type name" specifying | |
607 | * the type of the reducer. | |
608 | * @param v The initial value for the reducer. (A value which can be | |
609 | * assigned to the numeric type represented by @a tn.) | |
610 | * | |
611 | * @see @ref reducers_c_predefined | |
612 | * @see ReducersAdd | |
613 | */ | |
614 | #define CILK_C_REDUCER_OPADD(obj,tn,v) \ | |
615 | CILK_C_REDUCER_OPADD_TYPE(tn) obj = \ | |
616 | CILK_C_INIT_REDUCER(_Typeof(obj.value), \ | |
617 | __CILKRTS_MKIDENT(cilk_c_reducer_opadd_reduce_,tn), \ | |
618 | __CILKRTS_MKIDENT(cilk_c_reducer_opadd_identity_,tn), \ | |
619 | __cilkrts_hyperobject_noop_destroy, v) | |
620 | ||
621 | /// @cond internal | |
622 | ||
0657c20f | 623 | /** Declares the op_add reducer functions for a numeric type. |
4710dd51 | 624 | * |
625 | * This macro expands into external function declarations for functions which | |
626 | * implement the reducer functionality for the op_add reducer type for a | |
627 | * specified numeric type. | |
628 | * | |
629 | * @param t The value type of the reducer. | |
0657c20f | 630 | * @param tn The value "type name" identifier, used to construct the reducer |
4710dd51 | 631 | * type name, function names, etc. |
632 | */ | |
633 | #define CILK_C_REDUCER_OPADD_DECLARATION(t,tn) \ | |
634 | typedef CILK_C_DECLARE_REDUCER(t) CILK_C_REDUCER_OPADD_TYPE(tn); \ | |
635 | __CILKRTS_DECLARE_REDUCER_REDUCE(cilk_c_reducer_opadd,tn,l,r); \ | |
636 | __CILKRTS_DECLARE_REDUCER_IDENTITY(cilk_c_reducer_opadd,tn); | |
0657c20f | 637 | |
638 | /** Defines the op_add reducer functions for a numeric type. | |
4710dd51 | 639 | * |
640 | * This macro expands into function definitions for functions which implement | |
641 | * the reducer functionality for the op_add reducer type for a specified | |
642 | * numeric type. | |
643 | * | |
644 | * @param t The value type of the reducer. | |
0657c20f | 645 | * @param tn The value "type name" identifier, used to construct the reducer |
4710dd51 | 646 | * type name, function names, etc. |
647 | */ | |
648 | #define CILK_C_REDUCER_OPADD_DEFINITION(t,tn) \ | |
649 | typedef CILK_C_DECLARE_REDUCER(t) CILK_C_REDUCER_OPADD_TYPE(tn); \ | |
650 | __CILKRTS_DECLARE_REDUCER_REDUCE(cilk_c_reducer_opadd,tn,l,r) \ | |
651 | { *(t*)l += *(t*)r; } \ | |
652 | __CILKRTS_DECLARE_REDUCER_IDENTITY(cilk_c_reducer_opadd,tn) \ | |
653 | { *(t*)v = 0; } | |
0657c20f | 654 | |
655 | ///@{ | |
656 | /** @def CILK_C_REDUCER_OPADD_INSTANCE | |
657 | * @brief Declares or defines implementation functions for a reducer type. | |
4710dd51 | 658 | * |
659 | * In the runtime source file c_reducers.c, the macro `CILK_C_DEFINE_REDUCERS` | |
0657c20f | 660 | * will be defined, and this macro will generate reducer implementation |
4710dd51 | 661 | * functions. Everywhere else, `CILK_C_DEFINE_REDUCERS` will be undefined, |
662 | * and this macro will expand into external declarations for the functions. | |
663 | */ | |
664 | #ifdef CILK_C_DEFINE_REDUCERS | |
665 | # define CILK_C_REDUCER_OPADD_INSTANCE(t,tn) \ | |
666 | CILK_C_REDUCER_OPADD_DEFINITION(t,tn) | |
667 | #else | |
668 | # define CILK_C_REDUCER_OPADD_INSTANCE(t,tn) \ | |
669 | CILK_C_REDUCER_OPADD_DECLARATION(t,tn) | |
670 | #endif | |
0657c20f | 671 | ///@} |
4710dd51 | 672 | |
0657c20f | 673 | /* Declares or defines an instance of the reducer type and its functions for each |
4710dd51 | 674 | * numeric type. |
675 | */ | |
676 | CILK_C_REDUCER_OPADD_INSTANCE(char, char) | |
677 | CILK_C_REDUCER_OPADD_INSTANCE(unsigned char, uchar) | |
678 | CILK_C_REDUCER_OPADD_INSTANCE(signed char, schar) | |
679 | CILK_C_REDUCER_OPADD_INSTANCE(wchar_t, wchar_t) | |
680 | CILK_C_REDUCER_OPADD_INSTANCE(short, short) | |
681 | CILK_C_REDUCER_OPADD_INSTANCE(unsigned short, ushort) | |
682 | CILK_C_REDUCER_OPADD_INSTANCE(int, int) | |
683 | CILK_C_REDUCER_OPADD_INSTANCE(unsigned int, uint) | |
684 | CILK_C_REDUCER_OPADD_INSTANCE(unsigned int, unsigned) /* alternate name */ | |
685 | CILK_C_REDUCER_OPADD_INSTANCE(long, long) | |
686 | CILK_C_REDUCER_OPADD_INSTANCE(unsigned long, ulong) | |
687 | CILK_C_REDUCER_OPADD_INSTANCE(long long, longlong) | |
688 | CILK_C_REDUCER_OPADD_INSTANCE(unsigned long long, ulonglong) | |
689 | CILK_C_REDUCER_OPADD_INSTANCE(float, float) | |
690 | CILK_C_REDUCER_OPADD_INSTANCE(double, double) | |
691 | CILK_C_REDUCER_OPADD_INSTANCE(long double, longdouble) | |
692 | ||
693 | //@endcond | |
694 | ||
695 | __CILKRTS_END_EXTERN_C | |
696 | ||
0657c20f | 697 | ///@} |
4710dd51 | 698 | |
0657c20f | 699 | ///@} |
4710dd51 | 700 | |
701 | #endif /* REDUCER_OPADD_H_INCLUDED */ |