32 #ifndef _GLIBCXX_PARALLEL_RANDOM_SHUFFLE_H
33 #define _GLIBCXX_PARALLEL_RANDOM_SHUFFLE_H 1
51 template<
typename _RAIter>
54 typedef std::iterator_traits<_RAIter> _TraitsType;
55 typedef typename _TraitsType::value_type _ValueType;
56 typedef typename _TraitsType::difference_type _DifferenceType;
84 : _M_source(__source) { }
90 template<
typename _RAIter,
typename _RandomNumberGenerator>
113 template<
typename _RandomNumberGenerator>
116 {
return __rng.__genrand_bits(__logp); }
120 template<
typename _RAIter,
typename _RandomNumberGenerator>
123 _RandomNumberGenerator>* __pus)
125 typedef std::iterator_traits<_RAIter> _TraitsType;
126 typedef typename _TraitsType::value_type _ValueType;
127 typedef typename _TraitsType::difference_type _DifferenceType;
134 _DifferenceType __length = (__sd->
_M_starts[__iam + 1]
136 _BinIndex* __oracles =
new _BinIndex[__length];
137 _DifferenceType* __dist =
new _DifferenceType[__sd->
_M_num_bins + 1];
138 _BinIndex* __bin_proc =
new _BinIndex[__sd->
_M_num_bins];
139 _ValueType** __temporaries =
new _ValueType*[__d->
_M_num_threads];
142 for (_BinIndex __b = 0; __b < __sd->
_M_num_bins + 1; ++__b)
149 for (_DifferenceType __i = 0; __i < __length; ++__i)
152 __oracles[__i] = __oracle;
155 ++(__dist[__oracle + 1]);
158 for (_BinIndex __b = 0; __b < __sd->
_M_num_bins + 1; ++__b)
159 __sd->
_M_dist[__b][__iam + 1] = __dist[__b];
167 for (_BinIndex __s = 0; __s < __sd->
_M_num_bins; ++__s)
177 for (_BinIndex __s = 0; __s < __d->_M_bins_begin; ++__s)
178 __global_offset += __sd->_M_dist[__s + 1][__d->_M_num_threads];
182 for (_BinIndex __s = __d->_M_bins_begin; __s < __d->__bins_end; ++__s)
184 for (
int __t = 0; __t < __d->_M_num_threads + 1; ++__t)
185 __sd->_M_dist[__s + 1][__t] += __offset;
186 __offset = __sd->_M_dist[__s + 1][__d->_M_num_threads];
189 __sd->_M_temporaries[__iam] =
static_cast<_ValueType*
>
190 (::operator
new(
sizeof(_ValueType) * __offset));
195 for (_BinIndex __b = 0; __b < __sd->_M_num_bins + 1; ++__b)
196 __dist[__b] = __sd->_M_dist[__b][__iam];
197 for (_BinIndex __b = 0; __b < __sd->_M_num_bins; ++__b)
198 __bin_proc[__b] = __sd->_M_bin_proc[__b];
199 for (
_ThreadIndex __t = 0; __t < __d->_M_num_threads; ++__t)
200 __temporaries[__t] = __sd->_M_temporaries[__t];
202 _RAIter __source = __sd->_M_source;
203 _DifferenceType __start = __sd->_M_starts[__iam];
206 for (_DifferenceType __i = 0; __i < __length; ++__i)
208 _BinIndex __target_bin = __oracles[__i];
212 ::new(&(__temporaries[__target_p][__dist[__target_bin + 1]++]))
213 _ValueType(*(__source + __i + __start));
219 delete[] __temporaries;
224 for (_BinIndex __b = __d->_M_bins_begin; __b < __d->__bins_end; ++__b)
226 _ValueType* __begin =
227 (__sd->_M_temporaries[__iam]
228 + (__b == __d->_M_bins_begin
229 ? 0 : __sd->_M_dist[__b][__d->_M_num_threads])),
230 *__end = (__sd->_M_temporaries[__iam]
231 + __sd->_M_dist[__b + 1][__d->_M_num_threads]);
234 std::copy(__begin, __end, __sd->_M_source + __global_offset
235 + (__b == __d->_M_bins_begin
236 ? 0 : __sd->_M_dist[__b][__d->_M_num_threads]));
240 __sd->_M_temporaries[__iam][__i].~_ValueType();
241 ::operator
delete(__sd->_M_temporaries[__iam]);
246 template<
typename _Tp>
253 return (_Tp)1 << (
__rd_log2(__x - 1) + 1);
263 template<
typename _RAIter,
typename _RandomNumberGenerator>
266 typename std::iterator_traits
267 <_RAIter>::difference_type __n,
269 _RandomNumberGenerator& __rng)
271 typedef std::iterator_traits<_RAIter> _TraitsType;
272 typedef typename _TraitsType::value_type _ValueType;
273 typedef typename _TraitsType::difference_type _DifferenceType;
279 if (__num_threads > __n)
282 _BinIndex __num_bins, __num_bins_cache;
284 #if _GLIBCXX_RANDOM_SHUFFLE_CONSIDER_L1
289 std::max<_DifferenceType>(1, __n / (__s.L1_cache_size_lb
290 /
sizeof(_ValueType)));
295 __num_bins = std::min<_DifferenceType>(__n, __num_bins_cache);
297 #if _GLIBCXX_RANDOM_SHUFFLE_CONSIDER_TLB
299 __num_bins = std::min<_DifferenceType>(__s.
TLB_size / 2, __num_bins);
303 if (__num_bins < __num_bins_cache)
308 __num_bins_cache =
static_cast<_BinIndex
>
310 /
sizeof(_ValueType))));
314 __num_bins =
static_cast<_BinIndex
>
315 (
std::min(__n, static_cast<_DifferenceType>(__num_bins_cache)));
317 #if _GLIBCXX_RANDOM_SHUFFLE_CONSIDER_TLB
323 #if _GLIBCXX_RANDOM_SHUFFLE_CONSIDER_L1
328 std::max<_BinIndex>(__num_threads, __num_bins));
330 if (__num_threads <= 1)
340 _DifferenceType* __starts;
342 # pragma omp parallel num_threads(__num_threads)
350 __sd.
_M_dist =
new _DifferenceType*[__num_bins + 1];
352 for (_BinIndex __b = 0; __b < __num_bins + 1; ++__b)
353 __sd.
_M_dist[__b] =
new _DifferenceType[__num_threads + 1];
354 for (_BinIndex __b = 0; __b < (__num_bins + 1); ++__b)
359 __starts = __sd.
_M_starts =
new _DifferenceType[__num_threads + 1];
360 int __bin_cursor = 0;
364 _DifferenceType __chunk_length = __n / __num_threads,
365 __split = __n % __num_threads,
367 _DifferenceType __bin_chunk_length = __num_bins / __num_threads,
368 __bin_split = __num_bins % __num_threads;
371 __starts[__i] = __start;
372 __start += (__i < __split
373 ? (__chunk_length + 1) : __chunk_length);
377 __bin_cursor += (__i < __bin_split
378 ? (__bin_chunk_length + 1)
379 : __bin_chunk_length);
381 for (; __j < __bin_cursor; ++__j)
385 __pus[__i].
_M_sd = &__sd;
387 __starts[__num_threads] = __start;
395 for (
int __s = 0; __s < (__num_bins + 1); ++__s)
408 template<
typename _RAIter,
typename _RandomNumberGenerator>
411 _RandomNumberGenerator& __rng)
413 typedef std::iterator_traits<_RAIter> _TraitsType;
414 typedef typename _TraitsType::value_type _ValueType;
415 typedef typename _TraitsType::difference_type _DifferenceType;
417 _DifferenceType __n = __end - __begin;
420 _BinIndex __num_bins, __num_bins_cache;
422 #if _GLIBCXX_RANDOM_SHUFFLE_CONSIDER_L1
424 __num_bins_cache = std::max<_DifferenceType>
425 (1, __n / (__s.L1_cache_size_lb /
sizeof(_ValueType)));
430 __num_bins =
std::min(__n, (_DifferenceType)__num_bins_cache);
431 #if _GLIBCXX_RANDOM_SHUFFLE_CONSIDER_TLB
437 if (__num_bins < __num_bins_cache)
441 __num_bins_cache =
static_cast<_BinIndex
>
443 /
sizeof(_ValueType))));
448 __num_bins =
static_cast<_BinIndex
>
449 (
std::min(__n, static_cast<_DifferenceType>(__num_bins_cache)));
451 #if _GLIBCXX_RANDOM_SHUFFLE_CONSIDER_TLB
453 __num_bins = std::min<_DifferenceType>(__s.
TLB_size / 2, __num_bins);
456 #if _GLIBCXX_RANDOM_SHUFFLE_CONSIDER_L1
464 _ValueType* __target =
465 static_cast<_ValueType*
>(::operator
new(
sizeof(_ValueType) * __n));
466 _BinIndex* __oracles =
new _BinIndex[__n];
467 _DifferenceType* __dist0 =
new _DifferenceType[__num_bins + 1],
468 * __dist1 =
new _DifferenceType[__num_bins + 1];
470 for (
int __b = 0; __b < __num_bins + 1; ++__b)
475 for (_DifferenceType __i = 0; __i < __n; ++__i)
478 __oracles[__i] = __oracle;
481 ++(__dist0[__oracle + 1]);
488 for (
int __b = 0; __b < __num_bins + 1; ++__b)
489 __dist1[__b] = __dist0[__b];
492 for (_DifferenceType __i = 0; __i < __n; ++__i)
493 ::
new(&(__target[(__dist0[__oracles[__i]])++]))
494 _ValueType(*(__begin + __i));
496 for (
int __b = 0; __b < __num_bins; ++__b)
498 __target + __dist1[__b + 1], __rng);
501 std::copy(__target, __target + __n, __begin);
507 for (_DifferenceType __i = 0; __i < __n; ++__i)
508 __target[__i].~_ValueType();
509 ::operator
delete(__target);
512 __gnu_sequential::random_shuffle(__begin, __end, __rng);
520 template<
typename _RAIter,
typename _RandomNumberGenerator>
525 typedef std::iterator_traits<_RAIter> _TraitsType;
526 typedef typename _TraitsType::difference_type _DifferenceType;
527 _DifferenceType __n = __end - __begin;
529 __get_max_threads(), __rng);
int _M_num_threads
Number of threads participating in total.
End-user include file. Provides advanced settings and tuning options. This file is a GNU parallel ext...
int _M_num_bits
Number of bits needed to address the bins.
_Tp __round_up_to_pow2(_Tp __x)
Round up to the next greater power of 2.
class _Settings Run-time settings for the parallel mode including all tunable parameters.
unsigned short _BinIndex
Type to hold the index of a bin.
unsigned long long L2_cache_size
size of the L2 cache in bytes (underestimation).
void __parallel_random_shuffle_drs_pu(_DRSSorterPU< _RAIter, _RandomNumberGenerator > *__pus)
Random shuffle code executed by each thread.
_BinIndex _M_bins_begin
Begin index for bins taken care of by this thread.
uint16_t _ThreadIndex
Unsigned integer to index a thread number. The maximum thread number (for each processor) must fit in...
uint64_t _SequenceIndex
Unsigned integer to index __elements. The total number of elements for each algorithm must fit into t...
_BinIndex __bins_end
End index for bins taken care of by this thread.
_Size __rd_log2(_Size __n)
Calculates the rounded-down logarithm of __n for base 2.
Local data for a thread participating in __gnu_parallel::__parallel_random_shuffle().
const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
int __random_number_pow2(int __logp, _RandomNumberGenerator &__rng)
Generate a random number in [0,2^__logp).
_DifferenceType ** _M_dist
Two-dimensional array to hold the thread-bin distribution.
_DRandomShufflingGlobalData(_RAIter &__source)
Constructor.
const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
uint32_t _M_seed
Random _M_seed for this thread.
Data known to every thread participating in __gnu_parallel::__parallel_random_shuffle().
_RAIter & _M_source
Begin iterator of the __source.
_DifferenceType * _M_starts
Start indexes of the threads' __chunks.
_DRandomShufflingGlobalData< _RAIter > * _M_sd
Pointer to global data.
GNU parallel code for public use.
Random number generator, based on the Mersenne twister.
static const _Settings & get()
Get the global settings.
_ValueType ** _M_temporaries
Temporary arrays for each thread.
unsigned int TLB_size
size of the Translation Lookaside Buffer (underestimation).
#define _GLIBCXX_CALL(__n)
Macro to produce log message when entering a function.
void __parallel_random_shuffle(_RAIter __begin, _RAIter __end, _RandomNumberGenerator __rng=_RandomNumber())
Parallel random public call.
void __parallel_random_shuffle_drs(_RAIter __begin, _RAIter __end, typename std::iterator_traits< _RAIter >::difference_type __n, _ThreadIndex __num_threads, _RandomNumberGenerator &__rng)
Main parallel random shuffle step.
void __sequential_random_shuffle(_RAIter __begin, _RAIter __end, _RandomNumberGenerator &__rng)
Sequential cache-efficient random shuffle.
_OutputIterator partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
Return list of partial sums.
_ThreadIndex * _M_bin_proc
Number of the thread that will further process the corresponding bin.
Random number generator based on the Mersenne twister. This file is a GNU parallel extension to the S...
int _M_num_bins
Number of bins to distribute to.