| 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 |
#ifndef _GLIBCXX_PARALLEL_WORKSTEALING_H |
| 41 |
#define _GLIBCXX_PARALLEL_WORKSTEALING_H 1 |
| 42 |
|
| 43 |
#include <parallel/parallel.h> |
| 44 |
#include <parallel/random_number.h> |
| 45 |
#include <parallel/compatibility.h> |
| 46 |
|
| 47 |
namespace __gnu_parallel |
| 48 |
{ |
| 49 |
|
| 50 |
#define _GLIBCXX_JOB_VOLATILE volatile |
| 51 |
|
| 52 |
|
| 53 |
template<typename _DifferenceTp> |
| 54 |
struct _Job |
| 55 |
{ |
| 56 |
typedef _DifferenceTp _DifferenceType; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
_GLIBCXX_JOB_VOLATILE _DifferenceType _M_first; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
_GLIBCXX_JOB_VOLATILE _DifferenceType _M_last; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
_GLIBCXX_JOB_VOLATILE _DifferenceType _M_load; |
| 73 |
}; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
template<typename _RAIter, |
| 94 |
typename _Op, |
| 95 |
typename _Fu, |
| 96 |
typename _Red, |
| 97 |
typename _Result> |
| 98 |
_Op |
| 99 |
__for_each_template_random_access_workstealing(_RAIter __begin, |
| 100 |
_RAIter __end, _Op __op, |
| 101 |
_Fu& __f, _Red __r, |
| 102 |
_Result __base, |
| 103 |
_Result& __output, |
| 104 |
typename std::iterator_traits<_RAIter>::difference_type __bound) |
| 105 |
{ |
| 106 |
_GLIBCXX_CALL(__end - __begin) |
| 107 |
|
| 108 |
typedef std::iterator_traits<_RAIter> _TraitsType; |
| 109 |
typedef typename _TraitsType::difference_type _DifferenceType; |
| 110 |
|
| 111 |
const _Settings& __s = _Settings::get(); |
| 112 |
|
| 113 |
_DifferenceType __chunk_size = |
| 114 |
static_cast<_DifferenceType>(__s.workstealing_chunk_size); |
| 115 |
|
| 116 |
|
| 117 |
_DifferenceType __length = (__bound < 0) ? (__end - __begin) : __bound; |
| 118 |
|
| 119 |
|
| 120 |
const int __stride = (__s.cache_line_size * 10 |
| 121 |
/ sizeof(_Job<_DifferenceType>) + 1); |
| 122 |
|
| 123 |
|
| 124 |
_ThreadIndex __busy = 0; |
| 125 |
|
| 126 |
_Job<_DifferenceType> *__job; |
| 127 |
|
| 128 |
omp_lock_t __output_lock; |
| 129 |
omp_init_lock(&__output_lock); |
| 130 |
|
| 131 |
|
| 132 |
__output = __base; |
| 133 |
|
| 134 |
|
| 135 |
_ThreadIndex __num_threads = __gnu_parallel::max<_ThreadIndex> |
| 136 |
(1, __gnu_parallel::min<_DifferenceType>(__length, |
| 137 |
__get_max_threads())); |
| 138 |
|
| 139 |
# pragma omp parallel shared(__busy) num_threads(__num_threads) |
| 140 |
{ |
| 141 |
# pragma omp single |
| 142 |
{ |
| 143 |
__num_threads = omp_get_num_threads(); |
| 144 |
|
| 145 |
|
| 146 |
__job = new _Job<_DifferenceType>[__num_threads * __stride]; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
bool __iam_working = false; |
| 153 |
|
| 154 |
|
| 155 |
_ThreadIndex __iam = omp_get_thread_num(); |
| 156 |
|
| 157 |
|
| 158 |
_Job<_DifferenceType>& __my_job = __job[__iam * __stride]; |
| 159 |
|
| 160 |
|
| 161 |
_ThreadIndex __victim; |
| 162 |
|
| 163 |
|
| 164 |
_Result __result = _Result(); |
| 165 |
|
| 166 |
|
| 167 |
_DifferenceType __steal; |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
_RandomNumber __rand_gen(__iam, __num_threads); |
| 172 |
|
| 173 |
|
| 174 |
# pragma omp atomic |
| 175 |
++__busy; |
| 176 |
|
| 177 |
__iam_working = true; |
| 178 |
|
| 179 |
|
| 180 |
__my_job._M_first = static_cast<_DifferenceType> |
| 181 |
(__iam * (__length / __num_threads)); |
| 182 |
|
| 183 |
__my_job._M_last = (__iam == (__num_threads - 1) |
| 184 |
? (__length - 1) |
| 185 |
: ((__iam + 1) * (__length / __num_threads) - 1)); |
| 186 |
__my_job._M_load = __my_job._M_last - __my_job._M_first + 1; |
| 187 |
|
| 188 |
|
| 189 |
if (__my_job._M_first <= __my_job._M_last) |
| 190 |
{ |
| 191 |
|
| 192 |
_DifferenceType __my_first = __my_job._M_first; |
| 193 |
__result = __f(__op, __begin + __my_first); |
| 194 |
__my_job._M_first += 1; |
| 195 |
__my_job._M_load -= 1; |
| 196 |
} |
| 197 |
|
| 198 |
_RAIter __current; |
| 199 |
|
| 200 |
# pragma omp barrier |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
while (__busy > 0) |
| 205 |
{ |
| 206 |
|
| 207 |
# pragma omp flush(__busy) |
| 208 |
|
| 209 |
|
| 210 |
while (__my_job._M_first <= __my_job._M_last) |
| 211 |
{ |
| 212 |
|
| 213 |
|
| 214 |
_DifferenceType __current_job = |
| 215 |
__fetch_and_add<_DifferenceType>(&(__my_job._M_first), |
| 216 |
__chunk_size); |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
__my_job._M_load = __my_job._M_last - __my_job._M_first + 1; |
| 221 |
for (_DifferenceType __job_counter = 0; |
| 222 |
__job_counter < __chunk_size |
| 223 |
&& __current_job <= __my_job._M_last; |
| 224 |
++__job_counter) |
| 225 |
{ |
| 226 |
|
| 227 |
__current = __begin + __current_job; |
| 228 |
++__current_job; |
| 229 |
|
| 230 |
|
| 231 |
__result = __r(__result, __f(__op, __current)); |
| 232 |
} |
| 233 |
|
| 234 |
# pragma omp flush(__busy) |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
if (__iam_working) |
| 239 |
{ |
| 240 |
|
| 241 |
# pragma omp atomic |
| 242 |
--__busy; |
| 243 |
|
| 244 |
__iam_working = false; |
| 245 |
} |
| 246 |
|
| 247 |
_DifferenceType __supposed_first, __supposed_last, |
| 248 |
__supposed_load; |
| 249 |
do |
| 250 |
{ |
| 251 |
|
| 252 |
__yield(); |
| 253 |
# pragma omp flush(__busy) |
| 254 |
__victim = __rand_gen(); |
| 255 |
__supposed_first = __job[__victim * __stride]._M_first; |
| 256 |
__supposed_last = __job[__victim * __stride]._M_last; |
| 257 |
__supposed_load = __job[__victim * __stride]._M_load; |
| 258 |
} |
| 259 |
while (__busy > 0 |
| 260 |
&& ((__supposed_load <= 0) |
| 261 |
|| ((__supposed_first + __supposed_load - 1) |
| 262 |
!= __supposed_last))); |
| 263 |
|
| 264 |
if (__busy == 0) |
| 265 |
break; |
| 266 |
|
| 267 |
if (__supposed_load > 0) |
| 268 |
{ |
| 269 |
|
| 270 |
|
| 271 |
__steal = (__supposed_load < 2) ? 1 : __supposed_load / 2; |
| 272 |
|
| 273 |
|
| 274 |
_DifferenceType __stolen_first = |
| 275 |
__fetch_and_add<_DifferenceType> |
| 276 |
(&(__job[__victim * __stride]._M_first), __steal); |
| 277 |
_DifferenceType __stolen_try = (__stolen_first + __steal |
| 278 |
- _DifferenceType(1)); |
| 279 |
|
| 280 |
__my_job._M_first = __stolen_first; |
| 281 |
__my_job._M_last = __gnu_parallel::min(__stolen_try, |
| 282 |
__supposed_last); |
| 283 |
__my_job._M_load = __my_job._M_last - __my_job._M_first + 1; |
| 284 |
|
| 285 |
|
| 286 |
# pragma omp atomic |
| 287 |
++__busy; |
| 288 |
__iam_working = true; |
| 289 |
|
| 290 |
# pragma omp flush(__busy) |
| 291 |
} |
| 292 |
# pragma omp flush(__busy) |
| 293 |
} |
| 294 |
|
| 295 |
omp_set_lock(&__output_lock); |
| 296 |
__output = __r(__output, __result); |
| 297 |
omp_unset_lock(&__output_lock); |
| 298 |
} |
| 299 |
|
| 300 |
delete[] __job; |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
__f._M_finish_iterator = __begin + __length; |
| 305 |
|
| 306 |
omp_destroy_lock(&__output_lock); |
| 307 |
|
| 308 |
return __op; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
#endif |