aboutsummaryrefslogtreecommitdiff
path: root/libcilkrts/include/cilk/reducer_list.h
blob: d021577f816879496c3648f421fb8e7066fd10a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
 * Copyright (C) 2009-2011 
 * Intel Corporation
 * 
 * This file is part of the Intel Cilk Plus Library.  This library is free
 * software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * Under Section 7 of GPL version 3, you are granted additional
 * permissions described in the GCC Runtime Library Exception, version
 * 3.1, as published by the Free Software Foundation.
 * 
 * You should have received a copy of the GNU General Public License and
 * a copy of the GCC Runtime Library Exception along with this program;
 * see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 */

/*
 * reducer_list.h
 *
 * Purpose: Reducer hyperobject to accumulate a list of elements.
 *
 * Classes: reducer_list_append<Type, Allocator>
 *          reducer_list_prepend<Type, Allocator>
 *
 * Description:
 * ============
 * This component provides reducer-type hyperobject representations that allow
 * either prepending or appending values to an STL list.  By replacing the
 * variable with the hyperobject defined in this component, the data race is
 * eliminated.
 *
 * Usage Example:
 * ==============
 * Assume we wish to traverse an array of objects, performing an operation on
 * each object and accumulating the result of the operation into an STL list
 * variable.
 *..
 *  int compute(const X& v);
 *
 *  int test()
 *  {
 *      const std::size_t ARRAY_SIZE = 1000000;
 *      extern X myArray[ARRAY_SIZE];
 *      // ...
 *
 *      std::list<int> result;
 *      for (std::size_t i = 0; i < ARRAY_SIZE; ++i)
 *      {
 *          result.push_back(compute(myArray[i]));
 *      }
 *
 *      std::cout << "The result is: ";
 *      for (std::list<int>::iterator i = result.begin(); i != result.end();
 *           ++i)
 *      {
 *          std::cout << *i << " " << std::endl;
 *      }
 *
 *      return 0;
 *  }
 *..
 * Changing the 'for' to a 'cilk_for' will cause the loop to run in parallel,
 * but doing so will create a data race on the 'result' list.
 * The race is solved by changing 'result' to a 'reducer_list_append'
 * hyperobject:
 *..
 *  int compute(const X& v);
 *
 *  int test()
 *  {
 *      const std::size_t ARRAY_SIZE = 1000000;
 *      extern X myArray[ARRAY_SIZE];
 *      // ...
 *
 *      cilk::reducer_list_append<int> result;
 *      cilk_for (std::size_t i = 0; i < ARRAY_SIZE; ++i)
 *      {
 *          result->push_back(compute(myArray[i]));
 *      }
 *
 *      std::cout << "The result is: ";
 *      const std::list &r = result->get_value();
 *      for (std::list<int>::const_iterator i = r.begin(); i != r.end(); ++i)
 *      {
 *          std::cout << *i << " " << std::endl;
 *      }
 *
 *      return 0;
 *  }
 *..
 *
 * Operations provided:
 * ====================
 *
 * 'reducer_list_prepend' and 'reducer_list_append' support accumulation of an
 * ordered list of items.  Lists accumulated in Cilk++ strands will be merged
 * to maintain the order of the lists - the order will be the same as if the
 * application was run on a single core.
 *
 * The the current value of the reducer can be gotten and set using the
 * 'get_value', 'get_reference', and 'set_value' methods.  As with most
 * reducers, these methods produce deterministic results only if called before
 * the first spawn after creating a 'hyperobject' or when all strands spawned
 * since creating the 'hyperobject' have been synced.
 */

#ifndef REDUCER_LIST_H_INCLUDED
#define REDUCER_LIST_H_INCLUDED

#include <cilk/reducer.h>
#include <list>

namespace cilk
{

/**
 * @brief Reducer hyperobject to accumulate a list of elements where elements
 * are added to the end of the list.
 */
template<class _Ty,
         class _Ax = std::allocator<_Ty> >
class reducer_list_append
{
public:
    /// std::list reducer_list_prepend is based on
    typedef std::list<_Ty, _Ax> list_type;
    /// Type of elements in a reducer_list_prepend
    typedef _Ty list_value_type;
    /// Type of elements in a reducer_list_prepend
    typedef _Ty basic_value_type;

public:
    /// Definition of data view, operation, and identity for reducer_list_append
    struct Monoid: monoid_base<std::list<_Ty, _Ax> >
    {
        static void reduce (std::list<_Ty, _Ax> *left,
                            std::list<_Ty, _Ax> *right);
    };

private:
    reducer<Monoid> imp_;

public:

    // Default Constructor - Construct a reducer with an empty list
    reducer_list_append();

    // Construct a reducer with an initial list
    reducer_list_append(const std::list<_Ty, _Ax> &initial_value);

    // Return a const reference to the current list
    const std::list<_Ty, _Ax> &get_value() const;

    // Return a reference to the current list
    std::list<_Ty, _Ax> &get_reference();
    std::list<_Ty, _Ax> const &get_reference() const;

    // Replace the list's contents with the given list
    void set_value(const list_type &value);

    // Add an element to the end of the list
    void push_back(const _Ty element);

    reducer_list_append&       operator*()       { return *this; }
    reducer_list_append const& operator*() const { return *this; }

    reducer_list_append*       operator->()       { return this; }
    reducer_list_append const* operator->() const { return this; }

private:
    // Not copyable
    reducer_list_append(const reducer_list_append&);
    reducer_list_append& operator=(const reducer_list_append&);

};  // class reducer_list_append

/////////////////////////////////////////////////////////////////////////////
// Implementation of inline and template functions
/////////////////////////////////////////////////////////////////////////////

// ------------------------------------------
// template class reducer_list_append::Monoid
// ------------------------------------------

/**
 * Appends list from "right" reducer_list onto the end of the "left".
 * When done, the "right" reducer_list is empty.
 *
 * @tparam _Ty - Type of the list elements
 * @tparam _Ax - Allocator object used to define the storage allocation
 * model.  If not specified, the allocator class template for _Ty is used.
 * @param left reducer_list to be reduced into
 * @param right reducer_list to be reduced from
 */
template<class _Ty, class _Ax>
void
reducer_list_append<_Ty, _Ax>::Monoid::reduce(std::list<_Ty, _Ax> *left,
                                              std::list<_Ty, _Ax> *right)
{
    left->splice(left->end(), *right);
}

/**
 * Default constructor - creates an empty list
 *
 * @tparam _Ty - Type of the list elements
 * @tparam _Ax - Allocator object used to define the storage allocation
 * model.  If not specified, the allocator class template for _Ty is used.
 */
template<class _Ty, class _Ax>
reducer_list_append<_Ty, _Ax>::reducer_list_append() :
    imp_()
{
}

/**
 * Construct a reducer_list_append based on a list
 *
 * @tparam _Ty - Type of the list elements
 * @tparam _Ax - Allocator object used to define the storage allocation
 * model.  If not specified, the allocator class template for _Ty is used.
 * @param initial_value - [in] Inital list
 */
template<class _Ty, class _Ax>
reducer_list_append<_Ty, _Ax>::reducer_list_append(const std::list<_Ty, _Ax> &initial_value) :
    imp_(std::list<_Ty, _Ax>(initial_value))
{
}

/**
 * Allows read-only access to the list - same as get_reference()
 *
 * @warning If this method is called before the parallel calculation is
 * complete, the list returned by this method will be a partial result.
 *
 * @tparam _Ty - Type of the list elements
 * @tparam _Ax - Allocator object used to define the storage allocation
 * model.  If not specified, the allocator class template for _Ty is used.
 * @returns A const reference to the list that is the current contents of this view.
 */
template<class _Ty, class _Ax>
const std::list<_Ty, _Ax> &reducer_list_append<_Ty, _Ax>::get_value() const
{
    return imp_.view();
}

/**
 * Allows mutable access to list
 *
 * @warning If this method is called before the parallel calculation is
 * complete, the list returned by this method will be a partial result.
 *
 * @tparam _Ty - Type of the list elements
 * @tparam _Ax - Allocator object used to define the storage allocation
 * model.  If not specified, the allocator class template for _Ty is used.
 * @returns A reference to the list that is the current contents of this view.
 */
template<class _Ty, class _Ax>
std::list<_Ty, _Ax> &reducer_list_append<_Ty, _Ax>::get_reference()
{
    return imp_.view();
}

/**
 * Allows read-only access to list
 *
 * @warning If this method is called before the parallel calculation is
 * complete, the list returned by this method will be a partial result.
 *
 * @tparam _Ty - Type of the list elements
 * @tparam _Ax - Allocator object used to define the storage allocation
 * model.  If not specified, the allocator class template for _Ty is used.
 * @returns A const reference to the list that is the current contents of this view
 */
template<class _Ty, class _Ax>
const std::list<_Ty, _Ax> &reducer_list_append<_Ty, _Ax>::get_reference() const
{
    return imp_.view();
}

/**
 * Replace the list's contents
 *
 * @tparam _Ty - Type of the list elements
 * @tparam _Ax - Allocator object used to define the storage allocation
 * model.  If not specified, the allocator class template for _Ty is used.
 * @param value - The list to replace the current contents of this view
 */
template<class _Ty, class _Ax>
void reducer_list_append<_Ty, _Ax>::set_value(const list_type &value)
{
    // Clean out any value in our list
    imp_.view().clear();

    // If the new list is empty, we're done
    if (value.empty())
        return;

    // Copy each element into our list
    imp_.view() = value;
}

/**
 * Adds an element to the end of the list
 *
 * @tparam _Ty - Type of the list elements
 * @tparam _Ax - Allocator object used to define the storage allocation
 * model.  If not specified, the allocator class template for _Ty is used.
 * @param element - The element to be added to the end of the list
 */
template<class _Ty, class _Ax>
void reducer_list_append<_Ty, _Ax>::push_back(const _Ty element)
{
    imp_.view().push_back(element);
}

/**
 * @brief Reducer hyperobject to accumulate a list of elements where elements are
 * added to the beginning of the list.
 */
template<class _Ty,
         class _Ax = std::allocator<_Ty> >
class reducer_list_prepend
{
public:
    /// std::list reducer_list_prepend is based on
    typedef std::list<_Ty, _Ax> list_type;
    /// Type of elements in a reducer_list_prepend
    typedef _Ty list_value_type;
    /// Type of elements in a reducer_list_prepend
    typedef _Ty basic_value_type;

public:
    /// @brief Definition of data view, operation, and identity for reducer_list_prepend
    struct Monoid: monoid_base<std::list<_Ty, _Ax> >
    {
        static void reduce (std::list<_Ty, _Ax> *left,
                            std::list<_Ty, _Ax> *right);
    };

private:
    reducer<Monoid> imp_;

public:

    // Default Constructor - Construct a reducer with an empty list
    reducer_list_prepend();

    // Construct a reducer with an initial list
    reducer_list_prepend(const std::list<_Ty, _Ax> &initial_value);

    // Return a const reference to the current list
    const std::list<_Ty, _Ax> &get_value() const;

    // Return a reference to the current list
    std::list<_Ty, _Ax> &get_reference();
    std::list<_Ty, _Ax> const &get_reference() const;

    // Replace the list's contents with the given list
    void set_value(const list_type &value);

    // Add an element to the beginning of the list
    void push_front(const _Ty element);

    reducer_list_prepend&       operator*()       { return *this; }
    reducer_list_prepend const& operator*() const { return *this; }

    reducer_list_prepend*       operator->()       { return this; }
    reducer_list_prepend const* operator->() const { return this; }

private:
    // Not copyable
    reducer_list_prepend(const reducer_list_prepend&);
    reducer_list_prepend& operator=(const reducer_list_prepend&);

};  // class reducer_list_prepend

/////////////////////////////////////////////////////////////////////////////
// Implementation of inline and template functions
/////////////////////////////////////////////////////////////////////////////

// ------------------------------------
// template class reducer_list_prepend::Monoid
// ------------------------------------

/**
 * Appends list from "right" reducer_list onto the end of the "left".
 * When done, the "right" reducer_list is empty.
 */
template<class _Ty, class _Ax>
void
reducer_list_prepend<_Ty, _Ax>::Monoid::reduce(std::list<_Ty, _Ax> *left,
                                               std::list<_Ty, _Ax> *right)
{
    left->splice(left->begin(), *right);
}

/**
 * Default constructor - creates an empty list
 */
template<class _Ty, class _Ax>
reducer_list_prepend<_Ty, _Ax>::reducer_list_prepend() :
    imp_(std::list<_Ty, _Ax>())
{
}

/**
 * Construct a reducer_list_prepend based on a list.
 *
 * @param initial_value List used to initialize the reducer_list_prepend
 */
template<class _Ty, class _Ax>
reducer_list_prepend<_Ty, _Ax>::reducer_list_prepend(const std::list<_Ty, _Ax> &initial_value) :
    imp_(std::list<_Ty, _Ax>(initial_value))
{
}

/**
 * Allows read-only access to the list - same as get_reference()
 *
 * @warning If this method is called before the parallel calculation is
 * complete, the list returned by this method will be a partial result.
 *
 * @tparam _Ty - Type of the list elements
 * @tparam _Ax - Allocator object used to define the storage allocation
 * model.  If not specified, the allocator class template for _Ty is used.
 * @returns A const reference to the list that is the current contents of this view.
 */
template<class _Ty, class _Ax>
const std::list<_Ty, _Ax> &reducer_list_prepend<_Ty, _Ax>::get_value() const
{
    return imp_.view();
}

/**
 * Allows mutable access to the list
 *
 * @warning If this method is called before the parallel calculation is
 * complete, the list returned by this method will be a partial result.
 *
 * @tparam _Ty - Type of the list elements
 * @tparam _Ax - Allocator object used to define the storage allocation
 * model.  If not specified, the allocator class template for _Ty is used.
 * @returns A mutable reference to the list that is the current contents of this view.
 */
template<class _Ty, class _Ax>
std::list<_Ty, _Ax> &reducer_list_prepend<_Ty, _Ax>::get_reference()
{
    return imp_.view();
}

/**
 * Allows read-only access to the list
 *
 * @warning If this method is called before the parallel calculation is
 * complete, the list returned by this method will be a partial result.
 *
 * @tparam _Ty - Type of the list elements
 * @tparam _Ax - Allocator object used to define the storage allocation
 * model.  If not specified, the allocator class template for _Ty is used.
 * @returns A read-only reference to the list that is the current contents of this view.
 */
template<class _Ty, class _Ax>
const std::list<_Ty, _Ax> &reducer_list_prepend<_Ty, _Ax>::get_reference() const
{
    return imp_.view();
}

/**
 * Replace the list's contents
 *
 * @tparam _Ty - Type of the list elements
 * @tparam _Ax - Allocator object used to define the storage allocation
 * model.  If not specified, the allocator class template for _Ty is used.
 * @param value - The list to replace the current contents of this view
 */
template<class _Ty, class _Ax>
void reducer_list_prepend<_Ty, _Ax>::set_value(const list_type &value)
{
    // Clean out any value in our list
    imp_.view().clear();

    // If the new list is empty, we're done
    if (value.empty())
        return;

    // Copy each element into our list
    imp_.view() = value;
}

/**
 * Add an element to the beginning of the list
 *
 * @tparam _Ty - Type of the list elements
 * @tparam _Ax - Allocator object used to define the storage allocation
 * model.  If not specified, the allocator class template for _Ty is used.
 * @param element Element to be added to the beginning of the list
 */
template<class _Ty, class _Ax>
void reducer_list_prepend<_Ty, _Ax>::push_front(const _Ty element)
{
    imp_.view().push_front(element);
}

}	// namespace cilk

#endif //  REDUCER_LIST_H_INCLUDED