summaryrefslogtreecommitdiff
path: root/libcxx/include/__pstl/internal/numeric_impl.h
blob: f26be1d985e425cc28c0c0e0b5bfd2dead9611d8 (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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef _PSTL_NUMERIC_IMPL_H
#define _PSTL_NUMERIC_IMPL_H

#include <__assert>
#include <__config>
#include <iterator>
#include <type_traits>
#include <numeric>

#include "parallel_backend.h"
#include "execution_impl.h"
#include "unseq_backend_simd.h"
#include "algorithm_fwd.h"

namespace __pstl {
namespace __internal {

//------------------------------------------------------------------------
// transform_reduce (version with two binary functions, according to draft N4659)
//------------------------------------------------------------------------

template <class _ForwardIterator1, class _ForwardIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
_Tp __brick_transform_reduce(
    _ForwardIterator1 __first1,
    _ForwardIterator1 __last1,
    _ForwardIterator2 __first2,
    _Tp __init,
    _BinaryOperation1 __binary_op1,
    _BinaryOperation2 __binary_op2,
    /*is_vector=*/std::false_type) noexcept {
  return std::inner_product(__first1, __last1, __first2, __init, __binary_op1, __binary_op2);
}

template <class _RandomAccessIterator1,
          class _RandomAccessIterator2,
          class _Tp,
          class _BinaryOperation1,
          class _BinaryOperation2>
_Tp __brick_transform_reduce(
    _RandomAccessIterator1 __first1,
    _RandomAccessIterator1 __last1,
    _RandomAccessIterator2 __first2,
    _Tp __init,
    _BinaryOperation1 __binary_op1,
    _BinaryOperation2 __binary_op2,
    /*is_vector=*/std::true_type) noexcept {
  typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _DifferenceType;
  return __unseq_backend::__simd_transform_reduce(
      __last1 - __first1, __init, __binary_op1, [=, &__binary_op2](_DifferenceType __i) {
        return __binary_op2(__first1[__i], __first2[__i]);
      });
}

template <class _Tag,
          class _ExecutionPolicy,
          class _ForwardIterator1,
          class _ForwardIterator2,
          class _Tp,
          class _BinaryOperation1,
          class _BinaryOperation2>
_Tp __pattern_transform_reduce(
    _Tag,
    _ExecutionPolicy&&,
    _ForwardIterator1 __first1,
    _ForwardIterator1 __last1,
    _ForwardIterator2 __first2,
    _Tp __init,
    _BinaryOperation1 __binary_op1,
    _BinaryOperation2 __binary_op2) noexcept {
  return __brick_transform_reduce(
      __first1, __last1, __first2, __init, __binary_op1, __binary_op2, typename _Tag::__is_vector{});
}

template <class _IsVector,
          class _ExecutionPolicy,
          class _RandomAccessIterator1,
          class _RandomAccessIterator2,
          class _Tp,
          class _BinaryOperation1,
          class _BinaryOperation2>
_Tp __pattern_transform_reduce(
    __parallel_tag<_IsVector> __tag,
    _ExecutionPolicy&& __exec,
    _RandomAccessIterator1 __first1,
    _RandomAccessIterator1 __last1,
    _RandomAccessIterator2 __first2,
    _Tp __init,
    _BinaryOperation1 __binary_op1,
    _BinaryOperation2 __binary_op2) {
  using __backend_tag = typename decltype(__tag)::__backend_tag;

  return __internal::__except_handler([&]() {
    return __par_backend::__parallel_transform_reduce(
        __backend_tag{},
        std::forward<_ExecutionPolicy>(__exec),
        __first1,
        __last1,
        [__first1, __first2, __binary_op2](_RandomAccessIterator1 __i) mutable {
          return __binary_op2(*__i, *(__first2 + (__i - __first1)));
        },
        __init,
        __binary_op1, // Combine
        [__first1, __first2, __binary_op1, __binary_op2](
            _RandomAccessIterator1 __i, _RandomAccessIterator1 __j, _Tp __init) -> _Tp {
          return __internal::__brick_transform_reduce(
              __i, __j, __first2 + (__i - __first1), __init, __binary_op1, __binary_op2, _IsVector{});
        });
  });
}

//------------------------------------------------------------------------
// transform_reduce (version with unary and binary functions)
//------------------------------------------------------------------------

template <class _ForwardIterator, class _Tp, class _BinaryOperation, class _UnaryOperation>
_Tp __brick_transform_reduce(
    _ForwardIterator __first,
    _ForwardIterator __last,
    _Tp __init,
    _BinaryOperation __binary_op,
    _UnaryOperation __unary_op,
    /*is_vector=*/std::false_type) noexcept {
  return std::transform_reduce(__first, __last, __init, __binary_op, __unary_op);
}

template <class _RandomAccessIterator, class _Tp, class _UnaryOperation, class _BinaryOperation>
_Tp __brick_transform_reduce(
    _RandomAccessIterator __first,
    _RandomAccessIterator __last,
    _Tp __init,
    _BinaryOperation __binary_op,
    _UnaryOperation __unary_op,
    /*is_vector=*/std::true_type) noexcept {
  typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type _DifferenceType;
  return __unseq_backend::__simd_transform_reduce(
      __last - __first, __init, __binary_op, [=, &__unary_op](_DifferenceType __i) {
        return __unary_op(__first[__i]);
      });
}

template <class _Tag,
          class _ExecutionPolicy,
          class _ForwardIterator,
          class _Tp,
          class _BinaryOperation,
          class _UnaryOperation>
_Tp __pattern_transform_reduce(
    _Tag,
    _ExecutionPolicy&&,
    _ForwardIterator __first,
    _ForwardIterator __last,
    _Tp __init,
    _BinaryOperation __binary_op,
    _UnaryOperation __unary_op) noexcept {
  return __internal::__brick_transform_reduce(
      __first, __last, __init, __binary_op, __unary_op, typename _Tag::__is_vector{});
}

template <class _IsVector,
          class _ExecutionPolicy,
          class _RandomAccessIterator,
          class _Tp,
          class _BinaryOperation,
          class _UnaryOperation>
_Tp __pattern_transform_reduce(
    __parallel_tag<_IsVector> __tag,
    _ExecutionPolicy&& __exec,
    _RandomAccessIterator __first,
    _RandomAccessIterator __last,
    _Tp __init,
    _BinaryOperation __binary_op,
    _UnaryOperation __unary_op) {
  using __backend_tag = typename decltype(__tag)::__backend_tag;

  return __internal::__except_handler([&]() {
    return __par_backend::__parallel_transform_reduce(
        __backend_tag{},
        std::forward<_ExecutionPolicy>(__exec),
        __first,
        __last,
        [__unary_op](_RandomAccessIterator __i) mutable { return __unary_op(*__i); },
        __init,
        __binary_op,
        [__unary_op, __binary_op](_RandomAccessIterator __i, _RandomAccessIterator __j, _Tp __init) {
          return __internal::__brick_transform_reduce(__i, __j, __init, __binary_op, __unary_op, _IsVector{});
        });
  });
}

//------------------------------------------------------------------------
// transform_exclusive_scan
//
// walk3 evaluates f(x,y,z) for (x,y,z) drawn from [first1,last1), [first2,...), [first3,...)
//------------------------------------------------------------------------

// Exclusive form
template <class _ForwardIterator, class _OutputIterator, class _UnaryOperation, class _Tp, class _BinaryOperation>
std::pair<_OutputIterator, _Tp> __brick_transform_scan(
    _ForwardIterator __first,
    _ForwardIterator __last,
    _OutputIterator __result,
    _UnaryOperation __unary_op,
    _Tp __init,
    _BinaryOperation __binary_op,
    /*Inclusive*/ std::false_type,
    /*is_vector=*/std::false_type) noexcept {
  for (; __first != __last; ++__first, ++__result) {
    *__result = __init;
    __init = __binary_op(__init, __unary_op(*__first));
  }
  return std::make_pair(__result, __init);
}

// Inclusive form
template <class _RandomAccessIterator, class _OutputIterator, class _UnaryOperation, class _Tp, class _BinaryOperation>
std::pair<_OutputIterator, _Tp> __brick_transform_scan(
    _RandomAccessIterator __first,
    _RandomAccessIterator __last,
    _OutputIterator __result,
    _UnaryOperation __unary_op,
    _Tp __init,
    _BinaryOperation __binary_op,
    /*Inclusive*/ std::true_type,
    /*is_vector=*/std::false_type) noexcept {
  for (; __first != __last; ++__first, ++__result) {
    __init    = __binary_op(__init, __unary_op(*__first));
    *__result = __init;
  }
  return std::make_pair(__result, __init);
}

// type is arithmetic and binary operation is a user defined operation.
template <typename _Tp, typename _BinaryOperation>
using is_arithmetic_udop =
    std::integral_constant<bool,
                           std::is_arithmetic<_Tp>::value && !std::is_same<_BinaryOperation, std::plus<_Tp>>::value>;

// [restriction] - T shall be DefaultConstructible.
// [violation] - default ctor of T shall set the identity value for binary_op.
template <class _RandomAccessIterator,
          class _OutputIterator,
          class _UnaryOperation,
          class _Tp,
          class _BinaryOperation,
          class _Inclusive>
typename std::enable_if<!is_arithmetic_udop<_Tp, _BinaryOperation>::value, std::pair<_OutputIterator, _Tp>>::type
__brick_transform_scan(
    _RandomAccessIterator __first,
    _RandomAccessIterator __last,
    _OutputIterator __result,
    _UnaryOperation __unary_op,
    _Tp __init,
    _BinaryOperation __binary_op,
    _Inclusive,
    /*is_vector=*/std::true_type) noexcept {
#if defined(_PSTL_UDS_PRESENT)
  return __unseq_backend::__simd_scan(
      __first, __last - __first, __result, __unary_op, __init, __binary_op, _Inclusive());
#else
  // We need to call serial brick here to call function for inclusive and exclusive scan that depends on _Inclusive()
  // value
  return __internal::__brick_transform_scan(
      __first,
      __last,
      __result,
      __unary_op,
      __init,
      __binary_op,
      _Inclusive(),
      /*is_vector=*/std::false_type());
#endif
}

template <class _RandomAccessIterator,
          class _OutputIterator,
          class _UnaryOperation,
          class _Tp,
          class _BinaryOperation,
          class _Inclusive>
typename std::enable_if<is_arithmetic_udop<_Tp, _BinaryOperation>::value, std::pair<_OutputIterator, _Tp>>::type
__brick_transform_scan(
    _RandomAccessIterator __first,
    _RandomAccessIterator __last,
    _OutputIterator __result,
    _UnaryOperation __unary_op,
    _Tp __init,
    _BinaryOperation __binary_op,
    _Inclusive,
    /*is_vector=*/std::true_type) noexcept {
  return __internal::__brick_transform_scan(
      __first,
      __last,
      __result,
      __unary_op,
      __init,
      __binary_op,
      _Inclusive(),
      /*is_vector=*/std::false_type());
}

template <class _Tag,
          class _ExecutionPolicy,
          class _ForwardIterator,
          class _OutputIterator,
          class _UnaryOperation,
          class _Tp,
          class _BinaryOperation,
          class _Inclusive>
_OutputIterator __pattern_transform_scan(
    _Tag,
    _ExecutionPolicy&&,
    _ForwardIterator __first,
    _ForwardIterator __last,
    _OutputIterator __result,
    _UnaryOperation __unary_op,
    _Tp __init,
    _BinaryOperation __binary_op,
    _Inclusive) noexcept {
  return __internal::__brick_transform_scan(
             __first, __last, __result, __unary_op, __init, __binary_op, _Inclusive(), typename _Tag::__is_vector{})
      .first;
}

template <class _IsVector,
          class _ExecutionPolicy,
          class _RandomAccessIterator,
          class _OutputIterator,
          class _UnaryOperation,
          class _Tp,
          class _BinaryOperation,
          class _Inclusive>
typename std::enable_if<!std::is_floating_point<_Tp>::value, _OutputIterator>::type __pattern_transform_scan(
    __parallel_tag<_IsVector> __tag,
    _ExecutionPolicy&& __exec,
    _RandomAccessIterator __first,
    _RandomAccessIterator __last,
    _OutputIterator __result,
    _UnaryOperation __unary_op,
    _Tp __init,
    _BinaryOperation __binary_op,
    _Inclusive) {
  using __backend_tag = typename decltype(__tag)::__backend_tag;

  typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type _DifferenceType;

  return __internal::__except_handler([&]() {
    __par_backend::__parallel_transform_scan(
        __backend_tag{},
        std::forward<_ExecutionPolicy>(__exec),
        __last - __first,
        [__first, __unary_op](_DifferenceType __i) mutable { return __unary_op(__first[__i]); },
        __init,
        __binary_op,
        [__first, __unary_op, __binary_op](_DifferenceType __i, _DifferenceType __j, _Tp __init) {
          // Execute serial __brick_transform_reduce, due to the explicit SIMD vectorization (reduction) requires a
          // commutative operation for the guarantee of correct scan.
          return __internal::__brick_transform_reduce(
              __first + __i,
              __first + __j,
              __init,
              __binary_op,
              __unary_op,
              /*__is_vector*/ std::false_type());
        },
        [__first, __unary_op, __binary_op, __result](_DifferenceType __i, _DifferenceType __j, _Tp __init) {
          return __internal::__brick_transform_scan(
                     __first + __i,
                     __first + __j,
                     __result + __i,
                     __unary_op,
                     __init,
                     __binary_op,
                     _Inclusive(),
                     _IsVector{})
              .second;
        });
    return __result + (__last - __first);
  });
}

template <class _IsVector,
          class _ExecutionPolicy,
          class _RandomAccessIterator,
          class _OutputIterator,
          class _UnaryOperation,
          class _Tp,
          class _BinaryOperation,
          class _Inclusive>
typename std::enable_if<std::is_floating_point<_Tp>::value, _OutputIterator>::type __pattern_transform_scan(
    __parallel_tag<_IsVector> __tag,
    _ExecutionPolicy&& __exec,
    _RandomAccessIterator __first,
    _RandomAccessIterator __last,
    _OutputIterator __result,
    _UnaryOperation __unary_op,
    _Tp __init,
    _BinaryOperation __binary_op,
    _Inclusive) {
  using __backend_tag = typename decltype(__tag)::__backend_tag;

  typedef typename std::iterator_traits<_RandomAccessIterator>::difference_type _DifferenceType;
  _DifferenceType __n = __last - __first;

  if (__n <= 0) {
    return __result;
  }
  return __internal::__except_handler([&]() {
    __par_backend::__parallel_strict_scan(
        __backend_tag{},
        std::forward<_ExecutionPolicy>(__exec),
        __n,
        __init,
        [__first, __unary_op, __binary_op, __result](_DifferenceType __i, _DifferenceType __len) {
          return __internal::__brick_transform_scan(
                     __first + __i,
                     __first + (__i + __len),
                     __result + __i,
                     __unary_op,
                     _Tp{},
                     __binary_op,
                     _Inclusive(),
                     _IsVector{})
              .second;
        },
        __binary_op,
        [__result, &__binary_op](_DifferenceType __i, _DifferenceType __len, _Tp __initial) {
          return *(std::transform(__result + __i,
                                  __result + __i + __len,
                                  __result + __i,
                                  [&__initial, &__binary_op](const _Tp& __x) {
                                    return __binary_op(__initial, __x);
                                  }) -
                   1);
        },
        [](_Tp) {});
    return __result + (__last - __first);
  });
}

//------------------------------------------------------------------------
// adjacent_difference
//------------------------------------------------------------------------

template <class _ForwardIterator, class _OutputIterator, class _BinaryOperation>
_OutputIterator __brick_adjacent_difference(
    _ForwardIterator __first,
    _ForwardIterator __last,
    _OutputIterator __d_first,
    _BinaryOperation __op,
    /*is_vector*/ std::false_type) noexcept {
  return std::adjacent_difference(__first, __last, __d_first, __op);
}

template <class _RandomAccessIterator1, class _RandomAccessIterator2, class BinaryOperation>
_RandomAccessIterator2 __brick_adjacent_difference(
    _RandomAccessIterator1 __first,
    _RandomAccessIterator1 __last,
    _RandomAccessIterator2 __d_first,
    BinaryOperation __op,
    /*is_vector=*/std::true_type) noexcept {
  _LIBCPP_ASSERT(__first != __last, "Range cannot be empty");

  typedef typename std::iterator_traits<_RandomAccessIterator1>::reference _ReferenceType1;
  typedef typename std::iterator_traits<_RandomAccessIterator2>::reference _ReferenceType2;

  auto __n   = __last - __first;
  *__d_first = *__first;
  return __unseq_backend::__simd_walk_3(
      __first + 1,
      __n - 1,
      __first,
      __d_first + 1,
      [&__op](_ReferenceType1 __x, _ReferenceType1 __y, _ReferenceType2 __z) { __z = __op(__x, __y); });
}

template <class _Tag, class _ExecutionPolicy, class _ForwardIterator, class _OutputIterator, class _BinaryOperation>
_OutputIterator __pattern_adjacent_difference(
    _Tag,
    _ExecutionPolicy&&,
    _ForwardIterator __first,
    _ForwardIterator __last,
    _OutputIterator __d_first,
    _BinaryOperation __op) noexcept {
  return __internal::__brick_adjacent_difference(__first, __last, __d_first, __op, typename _Tag::__is_vector{});
}

template <class _IsVector,
          class _ExecutionPolicy,
          class _RandomAccessIterator1,
          class _RandomAccessIterator2,
          class _BinaryOperation>
_RandomAccessIterator2 __pattern_adjacent_difference(
    __parallel_tag<_IsVector> __tag,
    _ExecutionPolicy&& __exec,
    _RandomAccessIterator1 __first,
    _RandomAccessIterator1 __last,
    _RandomAccessIterator2 __d_first,
    _BinaryOperation __op) {
  _LIBCPP_ASSERT(__first != __last, "range cannot be empty");
  typedef typename std::iterator_traits<_RandomAccessIterator1>::reference _ReferenceType1;
  typedef typename std::iterator_traits<_RandomAccessIterator2>::reference _ReferenceType2;

  using __backend_tag = typename decltype(__tag)::__backend_tag;

  *__d_first = *__first;
  __par_backend::__parallel_for(
      __backend_tag{},
      std::forward<_ExecutionPolicy>(__exec),
      __first,
      __last - 1,
      [&__op, __d_first, __first](_RandomAccessIterator1 __b, _RandomAccessIterator1 __e) {
        _RandomAccessIterator2 __d_b = __d_first + (__b - __first);
        __internal::__brick_walk3(
            __b,
            __e,
            __b + 1,
            __d_b + 1,
            [&__op](_ReferenceType1 __x, _ReferenceType1 __y, _ReferenceType2 __z) { __z = __op(__y, __x); },
            _IsVector{});
      });
  return __d_first + (__last - __first);
}

} // namespace __internal
} // namespace __pstl

#endif /* _PSTL_NUMERIC_IMPL_H */