| 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 |
#ifndef _GLIBCXX_PARALLEL_PAR_LOOP_H |
| 34 |
#define _GLIBCXX_PARALLEL_PAR_LOOP_H 1 |
| 35 |
|
| 36 |
#include <omp.h> |
| 37 |
#include <parallel/settings.h> |
| 38 |
#include <parallel/base.h> |
| 39 |
#include <parallel/equally_split.h> |
| 40 |
|
| 41 |
namespace __gnu_parallel |
| 42 |
{ |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
template<typename _RAIter, |
| 62 |
typename _Op, |
| 63 |
typename _Fu, |
| 64 |
typename _Red, |
| 65 |
typename _Result> |
| 66 |
_Op |
| 67 |
__for_each_template_random_access_ed(_RAIter __begin, _RAIter __end, |
| 68 |
_Op __o, _Fu& __f, _Red __r, |
| 69 |
_Result __base, _Result& __output, |
| 70 |
typename std::iterator_traits<_RAIter>::difference_type __bound) |
| 71 |
{ |
| 72 |
typedef std::iterator_traits<_RAIter> _TraitsType; |
| 73 |
typedef typename _TraitsType::difference_type _DifferenceType; |
| 74 |
const _DifferenceType __length = __end - __begin; |
| 75 |
_Result *__thread_results; |
| 76 |
bool* __constructed; |
| 77 |
|
| 78 |
_ThreadIndex __num_threads = __gnu_parallel::min<_DifferenceType> |
| 79 |
(__get_max_threads(), __length); |
| 80 |
|
| 81 |
# pragma omp parallel num_threads(__num_threads) |
| 82 |
{ |
| 83 |
# pragma omp single |
| 84 |
{ |
| 85 |
__num_threads = omp_get_num_threads(); |
| 86 |
__thread_results = static_cast<_Result*> |
| 87 |
(::operator new(__num_threads * sizeof(_Result))); |
| 88 |
__constructed = new bool[__num_threads]; |
| 89 |
} |
| 90 |
|
| 91 |
_ThreadIndex __iam = omp_get_thread_num(); |
| 92 |
|
| 93 |
|
| 94 |
_Result* __reduct; |
| 95 |
|
| 96 |
_DifferenceType |
| 97 |
__start = __equally_split_point(__length, __num_threads, __iam), |
| 98 |
__stop = __equally_split_point(__length, __num_threads, __iam + 1); |
| 99 |
|
| 100 |
if (__start < __stop) |
| 101 |
{ |
| 102 |
__reduct = new _Result(__f(__o, __begin + __start)); |
| 103 |
++__start; |
| 104 |
__constructed[__iam] = true; |
| 105 |
} |
| 106 |
else |
| 107 |
__constructed[__iam] = false; |
| 108 |
|
| 109 |
for (; __start < __stop; ++__start) |
| 110 |
*__reduct = __r(*__reduct, __f(__o, __begin + __start)); |
| 111 |
|
| 112 |
if (__constructed[__iam]) |
| 113 |
{ |
| 114 |
::new(&__thread_results[__iam]) _Result(*__reduct); |
| 115 |
delete __reduct; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
for (_ThreadIndex __i = 0; __i < __num_threads; ++__i) |
| 120 |
if (__constructed[__i]) |
| 121 |
{ |
| 122 |
__output = __r(__output, __thread_results[__i]); |
| 123 |
__thread_results[__i].~_Result(); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
__f._M_finish_iterator = __begin + __length; |
| 129 |
|
| 130 |
::operator delete(__thread_results); |
| 131 |
|
| 132 |
delete[] __constructed; |
| 133 |
|
| 134 |
return __o; |
| 135 |
} |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
#endif |