2007-09-11 22:32:51 +00:00
|
|
|
// -*- C++ -*-
|
|
|
|
|
2009-04-09 17:00:19 +02:00
|
|
|
// Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
|
2007-09-11 22:32:51 +00:00
|
|
|
//
|
|
|
|
// This file is part of the GNU ISO C++ 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
|
2009-04-09 17:00:19 +02:00
|
|
|
// Foundation; either version 3, or (at your option) any later
|
2007-09-11 22:32:51 +00:00
|
|
|
// 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.
|
|
|
|
|
2009-04-09 17:00:19 +02:00
|
|
|
// 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.
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-04-09 17:00:19 +02:00
|
|
|
// 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/>.
|
2007-09-11 22:32:51 +00:00
|
|
|
|
|
|
|
/** @file parallel/random_shuffle.h
|
|
|
|
* @brief Parallel implementation of std::random_shuffle().
|
|
|
|
* This file is a GNU parallel extension to the Standard C++ Library.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Written by Johannes Singler.
|
|
|
|
|
|
|
|
#ifndef _GLIBCXX_PARALLEL_RANDOM_SHUFFLE_H
|
|
|
|
#define _GLIBCXX_PARALLEL_RANDOM_SHUFFLE_H 1
|
|
|
|
|
|
|
|
#include <limits>
|
re PR libstdc++/33487 (parallel v3: more functions not in right namespace)
2007-10-06 Benjamin Kosnik <bkoz@redhat.com>
PR libstdc++/33487
* include/parallel/algorithmfwd.h (for_each, generate, generate_n,
transform, replace, replace_if, max_element, min_element, count,
count_if): Consistently construct overloads.
* include/parallel/numericfwd.h (accumulate, adjacent_difference,
inner_product): Same.
* include/parallel/algobase.h: Same.
* include/parallel/algo.h: Same.
* include/parallel/numeric: Same.
* include/bits/algorithmfwd.h: Correct find_end placement.
* docs/html/parallel_mode.html: Document some of the interface
conventions.
* include/parallel/search.h (calc_borders): Only use operator ==.
* include/parallel/algorithmfwd.h: Move __gnu_sequential bits to...
* include/parallel/tags.h: ...here, and use a using directive.
* include/parallel/random_shuffle.h: Include stl_numeric. Qualify
uses of partial_num with __gnu_sequential.
* include/parallel/tree.h: Formatting.
From-SVN: r129054
2007-10-06 15:08:58 +00:00
|
|
|
#include <bits/stl_numeric.h>
|
2007-09-11 22:32:51 +00:00
|
|
|
#include <parallel/parallel.h>
|
|
|
|
#include <parallel/random_number.h>
|
|
|
|
|
|
|
|
namespace __gnu_parallel
|
|
|
|
{
|
2007-11-22 10:13:08 +00:00
|
|
|
/** @brief Type to hold the index of a bin.
|
|
|
|
*
|
2009-09-16 11:02:15 +00:00
|
|
|
* Since many variables of this type are allocated, it should be
|
2007-11-22 10:13:08 +00:00
|
|
|
* chosen as small as possible.
|
|
|
|
*/
|
2009-09-16 09:47:25 +00:00
|
|
|
typedef unsigned short _BinIndex;
|
2007-11-22 10:13:08 +00:00
|
|
|
|
|
|
|
/** @brief Data known to every thread participating in
|
2009-09-16 09:47:25 +00:00
|
|
|
__gnu_parallel::__parallel_random_shuffle(). */
|
|
|
|
template<typename _RAIter>
|
|
|
|
struct _DRandomShufflingGlobalData
|
2007-09-11 22:32:51 +00:00
|
|
|
{
|
2009-09-16 09:47:25 +00:00
|
|
|
typedef std::iterator_traits<_RAIter> _TraitsType;
|
|
|
|
typedef typename _TraitsType::value_type _ValueType;
|
|
|
|
typedef typename _TraitsType::difference_type _DifferenceType;
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-23 08:09:50 +00:00
|
|
|
/** @brief Begin iterator of the __source. */
|
2009-09-16 09:47:25 +00:00
|
|
|
_RAIter& _M_source;
|
2007-09-11 22:32:51 +00:00
|
|
|
|
|
|
|
/** @brief Temporary arrays for each thread. */
|
2009-09-16 09:47:25 +00:00
|
|
|
_ValueType** _M_temporaries;
|
2007-09-11 22:32:51 +00:00
|
|
|
|
|
|
|
/** @brief Two-dimensional array to hold the thread-bin distribution.
|
|
|
|
*
|
2009-09-16 13:50:17 +00:00
|
|
|
* Dimensions (_M_num_threads + 1) __x (_M_num_bins + 1). */
|
2009-09-16 09:47:25 +00:00
|
|
|
_DifferenceType** _M_dist;
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
/** @brief Start indexes of the threads' __chunks. */
|
|
|
|
_DifferenceType* _M_starts;
|
2007-09-11 22:32:51 +00:00
|
|
|
|
|
|
|
/** @brief Number of the thread that will further process the
|
2009-09-17 08:42:39 +00:00
|
|
|
corresponding bin. */
|
2009-09-16 09:47:25 +00:00
|
|
|
_ThreadIndex* _M_bin_proc;
|
2007-09-11 22:32:51 +00:00
|
|
|
|
|
|
|
/** @brief Number of bins to distribute to. */
|
2009-09-16 09:47:25 +00:00
|
|
|
int _M_num_bins;
|
2007-09-11 22:32:51 +00:00
|
|
|
|
|
|
|
/** @brief Number of bits needed to address the bins. */
|
2009-09-16 09:47:25 +00:00
|
|
|
int _M_num_bits;
|
2007-09-11 22:32:51 +00:00
|
|
|
|
|
|
|
/** @brief Constructor. */
|
2009-09-23 08:09:50 +00:00
|
|
|
_DRandomShufflingGlobalData(_RAIter& __source)
|
|
|
|
: _M_source(__source) { }
|
2007-09-11 22:32:51 +00:00
|
|
|
};
|
|
|
|
|
2007-11-22 10:13:08 +00:00
|
|
|
/** @brief Local data for a thread participating in
|
2009-09-16 09:47:25 +00:00
|
|
|
__gnu_parallel::__parallel_random_shuffle().
|
2007-11-22 10:13:08 +00:00
|
|
|
*/
|
2009-09-23 08:09:50 +00:00
|
|
|
template<typename _RAIter, typename _RandomNumberGenerator>
|
2009-09-16 09:47:25 +00:00
|
|
|
struct _DRSSorterPU
|
2007-09-11 22:32:51 +00:00
|
|
|
{
|
|
|
|
/** @brief Number of threads participating in total. */
|
2009-09-16 13:50:17 +00:00
|
|
|
int _M_num_threads;
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-16 14:53:58 +00:00
|
|
|
/** @brief Begin index for bins taken care of by this thread. */
|
2009-09-16 09:47:25 +00:00
|
|
|
_BinIndex _M_bins_begin;
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-16 14:53:58 +00:00
|
|
|
/** @brief End index for bins taken care of by this thread. */
|
2009-09-16 09:47:25 +00:00
|
|
|
_BinIndex __bins_end;
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
/** @brief Random _M_seed for this thread. */
|
|
|
|
uint32 _M_seed;
|
2007-09-11 22:32:51 +00:00
|
|
|
|
|
|
|
/** @brief Pointer to global data. */
|
2009-09-16 09:47:25 +00:00
|
|
|
_DRandomShufflingGlobalData<_RAIter>* _M_sd;
|
2007-09-11 22:32:51 +00:00
|
|
|
};
|
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
/** @brief Generate a random number in @__c [0,2^logp).
|
|
|
|
* @param logp Logarithm (basis 2) of the upper range __bound.
|
|
|
|
* @param __rng Random number generator to use.
|
2007-11-22 10:13:08 +00:00
|
|
|
*/
|
2009-09-23 08:09:50 +00:00
|
|
|
template<typename _RandomNumberGenerator>
|
re PR libstdc++/33487 (parallel v3: more functions not in right namespace)
2007-10-06 Benjamin Kosnik <bkoz@redhat.com>
PR libstdc++/33487
* include/parallel/algorithmfwd.h (for_each, generate, generate_n,
transform, replace, replace_if, max_element, min_element, count,
count_if): Consistently construct overloads.
* include/parallel/numericfwd.h (accumulate, adjacent_difference,
inner_product): Same.
* include/parallel/algobase.h: Same.
* include/parallel/algo.h: Same.
* include/parallel/numeric: Same.
* include/bits/algorithmfwd.h: Correct find_end placement.
* docs/html/parallel_mode.html: Document some of the interface
conventions.
* include/parallel/search.h (calc_borders): Only use operator ==.
* include/parallel/algorithmfwd.h: Move __gnu_sequential bits to...
* include/parallel/tags.h: ...here, and use a using directive.
* include/parallel/random_shuffle.h: Include stl_numeric. Qualify
uses of partial_num with __gnu_sequential.
* include/parallel/tree.h: Formatting.
From-SVN: r129054
2007-10-06 15:08:58 +00:00
|
|
|
inline int
|
2009-09-23 08:09:50 +00:00
|
|
|
__random_number_pow2(int logp, _RandomNumberGenerator& __rng)
|
2009-09-16 09:47:25 +00:00
|
|
|
{ return __rng.__genrand_bits(logp); }
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2007-11-22 10:13:08 +00:00
|
|
|
/** @brief Random shuffle code executed by each thread.
|
2009-09-16 09:47:25 +00:00
|
|
|
* @param __pus Array of thread-local data records. */
|
2009-09-23 08:09:50 +00:00
|
|
|
template<typename _RAIter, typename _RandomNumberGenerator>
|
2008-01-10 02:07:41 +00:00
|
|
|
void
|
2009-09-16 09:47:25 +00:00
|
|
|
__parallel_random_shuffle_drs_pu(_DRSSorterPU<_RAIter,
|
2009-09-23 08:09:50 +00:00
|
|
|
_RandomNumberGenerator>* __pus)
|
2007-09-11 22:32:51 +00:00
|
|
|
{
|
2009-09-16 09:47:25 +00:00
|
|
|
typedef std::iterator_traits<_RAIter> _TraitsType;
|
|
|
|
typedef typename _TraitsType::value_type _ValueType;
|
|
|
|
typedef typename _TraitsType::difference_type _DifferenceType;
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
_ThreadIndex __iam = omp_get_thread_num();
|
2009-09-23 08:09:50 +00:00
|
|
|
_DRSSorterPU<_RAIter, _RandomNumberGenerator>* d = &__pus[__iam];
|
2009-09-16 09:47:25 +00:00
|
|
|
_DRandomShufflingGlobalData<_RAIter>* _M_sd = d->_M_sd;
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
// Indexing: _M_dist[bin][processor]
|
2009-09-17 08:42:39 +00:00
|
|
|
_DifferenceType __length = _M_sd->_M_starts[__iam + 1] -
|
|
|
|
_M_sd->_M_starts[__iam];
|
2009-09-16 09:47:25 +00:00
|
|
|
_BinIndex* __oracles = new _BinIndex[__length];
|
|
|
|
_DifferenceType* _M_dist = new _DifferenceType[_M_sd->_M_num_bins + 1];
|
|
|
|
_BinIndex* _M_bin_proc = new _BinIndex[_M_sd->_M_num_bins];
|
2009-09-16 13:50:17 +00:00
|
|
|
_ValueType** _M_temporaries = new _ValueType*[d->_M_num_threads];
|
2007-09-11 22:32:51 +00:00
|
|
|
|
|
|
|
// Compute oracles and count appearances.
|
2009-09-16 09:47:25 +00:00
|
|
|
for (_BinIndex __b = 0; __b < _M_sd->_M_num_bins + 1; ++__b)
|
|
|
|
_M_dist[__b] = 0;
|
|
|
|
int _M_num_bits = _M_sd->_M_num_bits;
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
_RandomNumber __rng(d->_M_seed);
|
2007-09-11 22:32:51 +00:00
|
|
|
|
|
|
|
// First main loop.
|
2009-09-16 09:47:25 +00:00
|
|
|
for (_DifferenceType __i = 0; __i < __length; ++__i)
|
2007-09-11 22:32:51 +00:00
|
|
|
{
|
2009-09-16 09:47:25 +00:00
|
|
|
_BinIndex __oracle = __random_number_pow2(_M_num_bits, __rng);
|
|
|
|
__oracles[__i] = __oracle;
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2007-11-22 10:13:08 +00:00
|
|
|
// To allow prefix (partial) sum.
|
2009-09-16 09:47:25 +00:00
|
|
|
++(_M_dist[__oracle + 1]);
|
2007-09-11 22:32:51 +00:00
|
|
|
}
|
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
for (_BinIndex __b = 0; __b < _M_sd->_M_num_bins + 1; ++__b)
|
|
|
|
_M_sd->_M_dist[__b][__iam + 1] = _M_dist[__b];
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2007-11-22 10:13:08 +00:00
|
|
|
# pragma omp barrier
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2007-11-22 10:13:08 +00:00
|
|
|
# pragma omp single
|
2007-09-11 22:32:51 +00:00
|
|
|
{
|
2009-09-17 08:42:39 +00:00
|
|
|
// Sum up bins, _M_sd->_M_dist[__s + 1][d->_M_num_threads] now contains
|
|
|
|
// the total number of items in bin __s
|
2009-09-16 09:47:25 +00:00
|
|
|
for (_BinIndex __s = 0; __s < _M_sd->_M_num_bins; ++__s)
|
2009-09-17 08:42:39 +00:00
|
|
|
__gnu_sequential::partial_sum(
|
|
|
|
_M_sd->_M_dist[__s + 1],
|
|
|
|
_M_sd->_M_dist[__s + 1] + d->_M_num_threads + 1,
|
|
|
|
_M_sd->_M_dist[__s + 1]);
|
2007-09-11 22:32:51 +00:00
|
|
|
}
|
|
|
|
|
2007-11-22 10:13:08 +00:00
|
|
|
# pragma omp barrier
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
_SequenceIndex __offset = 0, __global_offset = 0;
|
|
|
|
for (_BinIndex __s = 0; __s < d->_M_bins_begin; ++__s)
|
2009-09-16 13:50:17 +00:00
|
|
|
__global_offset += _M_sd->_M_dist[__s + 1][d->_M_num_threads];
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2007-11-22 10:13:08 +00:00
|
|
|
# pragma omp barrier
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
for (_BinIndex __s = d->_M_bins_begin; __s < d->__bins_end; ++__s)
|
2007-09-11 22:32:51 +00:00
|
|
|
{
|
2009-09-17 08:42:39 +00:00
|
|
|
for (int __t = 0; __t < d->_M_num_threads + 1; ++__t)
|
|
|
|
_M_sd->_M_dist[__s + 1][__t] += __offset;
|
|
|
|
__offset = _M_sd->_M_dist[__s + 1][d->_M_num_threads];
|
2007-09-11 22:32:51 +00:00
|
|
|
}
|
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
_M_sd->_M_temporaries[__iam] = static_cast<_ValueType*>(
|
|
|
|
::operator new(sizeof(_ValueType) * __offset));
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2007-11-22 10:13:08 +00:00
|
|
|
# pragma omp barrier
|
2007-09-11 22:32:51 +00:00
|
|
|
|
|
|
|
// Draw local copies to avoid false sharing.
|
2009-09-16 09:47:25 +00:00
|
|
|
for (_BinIndex __b = 0; __b < _M_sd->_M_num_bins + 1; ++__b)
|
|
|
|
_M_dist[__b] = _M_sd->_M_dist[__b][__iam];
|
|
|
|
for (_BinIndex __b = 0; __b < _M_sd->_M_num_bins; ++__b)
|
|
|
|
_M_bin_proc[__b] = _M_sd->_M_bin_proc[__b];
|
2009-09-16 13:50:17 +00:00
|
|
|
for (_ThreadIndex __t = 0; __t < d->_M_num_threads; ++__t)
|
2009-09-16 09:47:25 +00:00
|
|
|
_M_temporaries[__t] = _M_sd->_M_temporaries[__t];
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
_RAIter _M_source = _M_sd->_M_source;
|
|
|
|
_DifferenceType __start = _M_sd->_M_starts[__iam];
|
2007-09-11 22:32:51 +00:00
|
|
|
|
|
|
|
// Distribute according to oracles, second main loop.
|
2009-09-16 09:47:25 +00:00
|
|
|
for (_DifferenceType __i = 0; __i < __length; ++__i)
|
2007-09-11 22:32:51 +00:00
|
|
|
{
|
2009-09-16 09:47:25 +00:00
|
|
|
_BinIndex target_bin = __oracles[__i];
|
|
|
|
_ThreadIndex target_p = _M_bin_proc[target_bin];
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-16 13:50:17 +00:00
|
|
|
// Last column [d->_M_num_threads] stays unchanged.
|
2009-09-16 09:47:25 +00:00
|
|
|
::new(&(_M_temporaries[target_p][_M_dist[target_bin + 1]++]))
|
2009-09-17 08:42:39 +00:00
|
|
|
_ValueType(*(_M_source + __i + __start));
|
2007-09-11 22:32:51 +00:00
|
|
|
}
|
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
delete[] __oracles;
|
|
|
|
delete[] _M_dist;
|
|
|
|
delete[] _M_bin_proc;
|
|
|
|
delete[] _M_temporaries;
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2007-11-22 10:13:08 +00:00
|
|
|
# pragma omp barrier
|
2007-09-11 22:32:51 +00:00
|
|
|
|
|
|
|
// Shuffle bins internally.
|
2009-09-16 09:47:25 +00:00
|
|
|
for (_BinIndex __b = d->_M_bins_begin; __b < d->__bins_end; ++__b)
|
2007-09-11 22:32:51 +00:00
|
|
|
{
|
2009-09-16 09:47:25 +00:00
|
|
|
_ValueType* __begin =
|
|
|
|
_M_sd->_M_temporaries[__iam] +
|
2009-09-17 08:42:39 +00:00
|
|
|
((__b == d->_M_bins_begin)
|
|
|
|
? 0 : _M_sd->_M_dist[__b][d->_M_num_threads]),
|
2009-09-16 09:47:25 +00:00
|
|
|
* __end =
|
2009-09-17 08:42:39 +00:00
|
|
|
_M_sd->_M_temporaries[__iam] +
|
|
|
|
_M_sd->_M_dist[__b + 1][d->_M_num_threads];
|
2009-09-16 09:47:25 +00:00
|
|
|
__sequential_random_shuffle(__begin, __end, __rng);
|
|
|
|
std::copy(__begin, __end, _M_sd->_M_source + __global_offset +
|
2009-09-17 08:42:39 +00:00
|
|
|
((__b == d->_M_bins_begin)
|
|
|
|
? 0 : _M_sd->_M_dist[__b][d->_M_num_threads]));
|
2007-09-11 22:32:51 +00:00
|
|
|
}
|
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
::operator delete(_M_sd->_M_temporaries[__iam]);
|
2007-09-11 22:32:51 +00:00
|
|
|
}
|
|
|
|
|
2007-11-22 10:13:08 +00:00
|
|
|
/** @brief Round up to the next greater power of 2.
|
2009-09-16 09:47:25 +00:00
|
|
|
* @param __x _Integer to round up */
|
|
|
|
template<typename _Tp>
|
|
|
|
_Tp
|
|
|
|
__round_up_to_pow2(_Tp __x)
|
2007-09-11 22:32:51 +00:00
|
|
|
{
|
2009-09-16 09:47:25 +00:00
|
|
|
if (__x <= 1)
|
2007-09-11 22:32:51 +00:00
|
|
|
return 1;
|
|
|
|
else
|
2009-09-23 08:09:50 +00:00
|
|
|
return (_Tp)1 << (__rd_log2(__x - 1) + 1);
|
2007-09-11 22:32:51 +00:00
|
|
|
}
|
|
|
|
|
2007-11-22 10:13:08 +00:00
|
|
|
/** @brief Main parallel random shuffle step.
|
2009-09-16 09:47:25 +00:00
|
|
|
* @param __begin Begin iterator of sequence.
|
|
|
|
* @param __end End iterator of sequence.
|
|
|
|
* @param __n Length of sequence.
|
|
|
|
* @param __num_threads Number of threads to use.
|
|
|
|
* @param __rng Random number generator to use.
|
2007-11-22 10:13:08 +00:00
|
|
|
*/
|
2009-09-23 08:09:50 +00:00
|
|
|
template<typename _RAIter, typename _RandomNumberGenerator>
|
2008-01-10 02:07:41 +00:00
|
|
|
void
|
2009-09-16 09:47:25 +00:00
|
|
|
__parallel_random_shuffle_drs(_RAIter __begin,
|
2009-09-17 08:42:39 +00:00
|
|
|
_RAIter __end,
|
|
|
|
typename std::iterator_traits
|
|
|
|
<_RAIter>::difference_type __n,
|
|
|
|
_ThreadIndex __num_threads,
|
2009-09-23 08:09:50 +00:00
|
|
|
_RandomNumberGenerator& __rng)
|
2007-09-11 22:32:51 +00:00
|
|
|
{
|
2009-09-16 09:47:25 +00:00
|
|
|
typedef std::iterator_traits<_RAIter> _TraitsType;
|
|
|
|
typedef typename _TraitsType::value_type _ValueType;
|
|
|
|
typedef typename _TraitsType::difference_type _DifferenceType;
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
_GLIBCXX_CALL(__n)
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2008-02-18 00:00:00 +00:00
|
|
|
const _Settings& __s = _Settings::get();
|
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
if (__num_threads > __n)
|
|
|
|
__num_threads = static_cast<_ThreadIndex>(__n);
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
_BinIndex _M_num_bins, __num_bins_cache;
|
2007-09-11 22:32:51 +00:00
|
|
|
|
|
|
|
#if _GLIBCXX_RANDOM_SHUFFLE_CONSIDER_L1
|
|
|
|
// Try the L1 cache first.
|
|
|
|
|
|
|
|
// Must fit into L1.
|
2009-09-16 09:47:25 +00:00
|
|
|
__num_bins_cache = std::max<_DifferenceType>(
|
|
|
|
1, __n / (__s.L1_cache_size_lb / sizeof(_ValueType)));
|
|
|
|
__num_bins_cache = __round_up_to_pow2(__num_bins_cache);
|
2007-09-11 22:32:51 +00:00
|
|
|
|
|
|
|
// No more buckets than TLB entries, power of 2
|
|
|
|
// Power of 2 and at least one element per bin, at most the TLB size.
|
2009-09-16 09:47:25 +00:00
|
|
|
_M_num_bins = std::min<_DifferenceType>(__n, __num_bins_cache);
|
2007-09-11 22:32:51 +00:00
|
|
|
|
|
|
|
#if _GLIBCXX_RANDOM_SHUFFLE_CONSIDER_TLB
|
|
|
|
// 2 TLB entries needed per bin.
|
2009-09-16 09:47:25 +00:00
|
|
|
_M_num_bins = std::min<_DifferenceType>(__s.TLB_size / 2, _M_num_bins);
|
2007-09-11 22:32:51 +00:00
|
|
|
#endif
|
2009-09-16 09:47:25 +00:00
|
|
|
_M_num_bins = __round_up_to_pow2(_M_num_bins);
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
if (_M_num_bins < __num_bins_cache)
|
2007-09-11 22:32:51 +00:00
|
|
|
{
|
|
|
|
#endif
|
2007-11-22 10:13:08 +00:00
|
|
|
// Now try the L2 cache
|
|
|
|
// Must fit into L2
|
2009-09-16 09:47:25 +00:00
|
|
|
__num_bins_cache = static_cast<_BinIndex>(std::max<_DifferenceType>(
|
|
|
|
1, __n / (__s.L2_cache_size / sizeof(_ValueType))));
|
|
|
|
__num_bins_cache = __round_up_to_pow2(__num_bins_cache);
|
2007-11-22 10:13:08 +00:00
|
|
|
|
|
|
|
// No more buckets than TLB entries, power of 2.
|
2009-09-16 09:47:25 +00:00
|
|
|
_M_num_bins = static_cast<_BinIndex>(
|
|
|
|
std::min(__n, static_cast<_DifferenceType>(__num_bins_cache)));
|
2007-11-22 10:13:08 +00:00
|
|
|
// Power of 2 and at least one element per bin, at most the TLB size.
|
2007-09-11 22:32:51 +00:00
|
|
|
#if _GLIBCXX_RANDOM_SHUFFLE_CONSIDER_TLB
|
2007-11-22 10:13:08 +00:00
|
|
|
// 2 TLB entries needed per bin.
|
2009-09-16 09:47:25 +00:00
|
|
|
_M_num_bins = std::min(
|
|
|
|
static_cast<_DifferenceType>(__s.TLB_size / 2), _M_num_bins);
|
2007-09-11 22:32:51 +00:00
|
|
|
#endif
|
2009-09-16 09:47:25 +00:00
|
|
|
_M_num_bins = __round_up_to_pow2(_M_num_bins);
|
2007-09-11 22:32:51 +00:00
|
|
|
#if _GLIBCXX_RANDOM_SHUFFLE_CONSIDER_L1
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
__num_threads = std::min<_BinIndex>(__num_threads, _M_num_bins);
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
if (__num_threads <= 1)
|
|
|
|
return __sequential_random_shuffle(__begin, __end, __rng);
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
_DRandomShufflingGlobalData<_RAIter> _M_sd(__begin);
|
|
|
|
_DRSSorterPU<_RAIter, _RandomNumber >* __pus;
|
|
|
|
_DifferenceType* _M_starts;
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
# pragma omp parallel num_threads(__num_threads)
|
2007-09-11 22:32:51 +00:00
|
|
|
{
|
2009-09-16 09:47:25 +00:00
|
|
|
_ThreadIndex __num_threads = omp_get_num_threads();
|
2007-11-22 10:13:08 +00:00
|
|
|
# pragma omp single
|
|
|
|
{
|
2009-09-16 09:47:25 +00:00
|
|
|
__pus = new _DRSSorterPU<_RAIter, _RandomNumber>
|
|
|
|
[__num_threads];
|
|
|
|
|
|
|
|
_M_sd._M_temporaries = new _ValueType*[__num_threads];
|
|
|
|
_M_sd._M_dist = new _DifferenceType*[_M_num_bins + 1];
|
|
|
|
_M_sd._M_bin_proc = new _ThreadIndex[_M_num_bins];
|
|
|
|
for (_BinIndex __b = 0; __b < _M_num_bins + 1; ++__b)
|
|
|
|
_M_sd._M_dist[__b] = new _DifferenceType[__num_threads + 1];
|
|
|
|
for (_BinIndex __b = 0; __b < (_M_num_bins + 1); ++__b)
|
2007-11-22 10:13:08 +00:00
|
|
|
{
|
2009-09-16 09:47:25 +00:00
|
|
|
_M_sd._M_dist[0][0] = 0;
|
|
|
|
_M_sd._M_dist[__b][0] = 0;
|
2007-11-22 10:13:08 +00:00
|
|
|
}
|
2009-09-17 08:42:39 +00:00
|
|
|
_M_starts = _M_sd._M_starts
|
|
|
|
= new _DifferenceType[__num_threads + 1];
|
2007-11-22 10:13:08 +00:00
|
|
|
int bin_cursor = 0;
|
2009-09-16 09:47:25 +00:00
|
|
|
_M_sd._M_num_bins = _M_num_bins;
|
2009-09-23 08:09:50 +00:00
|
|
|
_M_sd._M_num_bits = __rd_log2(_M_num_bins);
|
2009-09-16 09:47:25 +00:00
|
|
|
|
|
|
|
_DifferenceType __chunk_length = __n / __num_threads,
|
|
|
|
__split = __n % __num_threads, __start = 0;
|
|
|
|
_DifferenceType bin_chunk_length = _M_num_bins / __num_threads,
|
|
|
|
bin_split = _M_num_bins % __num_threads;
|
|
|
|
for (_ThreadIndex __i = 0; __i < __num_threads; ++__i)
|
2007-11-22 10:13:08 +00:00
|
|
|
{
|
2009-09-16 09:47:25 +00:00
|
|
|
_M_starts[__i] = __start;
|
2009-09-17 08:42:39 +00:00
|
|
|
__start += (__i < __split)
|
|
|
|
? (__chunk_length + 1) : __chunk_length;
|
2009-09-16 09:47:25 +00:00
|
|
|
int __j = __pus[__i]._M_bins_begin = bin_cursor;
|
2007-11-22 10:13:08 +00:00
|
|
|
|
|
|
|
// Range of bins for this processor.
|
2009-09-16 09:47:25 +00:00
|
|
|
bin_cursor += (__i < bin_split) ?
|
2007-11-22 10:13:08 +00:00
|
|
|
(bin_chunk_length + 1) : bin_chunk_length;
|
2009-09-16 09:47:25 +00:00
|
|
|
__pus[__i].__bins_end = bin_cursor;
|
|
|
|
for (; __j < bin_cursor; ++__j)
|
|
|
|
_M_sd._M_bin_proc[__j] = __i;
|
2009-09-16 13:50:17 +00:00
|
|
|
__pus[__i]._M_num_threads = __num_threads;
|
2009-09-16 09:47:25 +00:00
|
|
|
__pus[__i]._M_seed = __rng(std::numeric_limits<uint32>::max());
|
|
|
|
__pus[__i]._M_sd = &_M_sd;
|
2007-11-22 10:13:08 +00:00
|
|
|
}
|
2009-09-16 09:47:25 +00:00
|
|
|
_M_starts[__num_threads] = __start;
|
2007-11-22 10:13:08 +00:00
|
|
|
} //single
|
2008-06-05 15:47:17 +00:00
|
|
|
// Now shuffle in parallel.
|
2009-09-16 09:47:25 +00:00
|
|
|
__parallel_random_shuffle_drs_pu(__pus);
|
2008-06-05 15:47:17 +00:00
|
|
|
} // parallel
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
delete[] _M_starts;
|
|
|
|
delete[] _M_sd._M_bin_proc;
|
|
|
|
for (int __s = 0; __s < (_M_num_bins + 1); ++__s)
|
|
|
|
delete[] _M_sd._M_dist[__s];
|
|
|
|
delete[] _M_sd._M_dist;
|
|
|
|
delete[] _M_sd._M_temporaries;
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
delete[] __pus;
|
2007-09-11 22:32:51 +00:00
|
|
|
}
|
|
|
|
|
2009-09-16 14:53:58 +00:00
|
|
|
/** @brief Sequential cache-efficient random shuffle.
|
2009-09-16 09:47:25 +00:00
|
|
|
* @param __begin Begin iterator of sequence.
|
|
|
|
* @param __end End iterator of sequence.
|
|
|
|
* @param __rng Random number generator to use.
|
2007-11-22 10:13:08 +00:00
|
|
|
*/
|
2009-09-23 08:09:50 +00:00
|
|
|
template<typename _RAIter, typename _RandomNumberGenerator>
|
2008-01-10 02:07:41 +00:00
|
|
|
void
|
2009-09-16 09:47:25 +00:00
|
|
|
__sequential_random_shuffle(_RAIter __begin,
|
|
|
|
_RAIter __end,
|
2009-09-23 08:09:50 +00:00
|
|
|
_RandomNumberGenerator& __rng)
|
2007-09-11 22:32:51 +00:00
|
|
|
{
|
2009-09-16 09:47:25 +00:00
|
|
|
typedef std::iterator_traits<_RAIter> _TraitsType;
|
|
|
|
typedef typename _TraitsType::value_type _ValueType;
|
|
|
|
typedef typename _TraitsType::difference_type _DifferenceType;
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
_DifferenceType __n = __end - __begin;
|
2008-02-18 00:00:00 +00:00
|
|
|
const _Settings& __s = _Settings::get();
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
_BinIndex _M_num_bins, __num_bins_cache;
|
2007-09-11 22:32:51 +00:00
|
|
|
|
|
|
|
#if _GLIBCXX_RANDOM_SHUFFLE_CONSIDER_L1
|
|
|
|
// Try the L1 cache first, must fit into L1.
|
2009-09-16 09:47:25 +00:00
|
|
|
__num_bins_cache =
|
|
|
|
std::max<_DifferenceType>
|
|
|
|
(1, __n / (__s.L1_cache_size_lb / sizeof(_ValueType)));
|
|
|
|
__num_bins_cache = __round_up_to_pow2(__num_bins_cache);
|
2007-09-11 22:32:51 +00:00
|
|
|
|
|
|
|
// No more buckets than TLB entries, power of 2
|
|
|
|
// Power of 2 and at least one element per bin, at most the TLB size
|
2009-09-16 09:47:25 +00:00
|
|
|
_M_num_bins = std::min(__n, (_DifferenceType)__num_bins_cache);
|
2007-09-11 22:32:51 +00:00
|
|
|
#if _GLIBCXX_RANDOM_SHUFFLE_CONSIDER_TLB
|
|
|
|
// 2 TLB entries needed per bin
|
2009-09-16 09:47:25 +00:00
|
|
|
_M_num_bins = std::min((_DifferenceType)__s.TLB_size / 2, _M_num_bins);
|
2007-09-11 22:32:51 +00:00
|
|
|
#endif
|
2009-09-16 09:47:25 +00:00
|
|
|
_M_num_bins = __round_up_to_pow2(_M_num_bins);
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
if (_M_num_bins < __num_bins_cache)
|
2007-09-11 22:32:51 +00:00
|
|
|
{
|
|
|
|
#endif
|
2007-11-22 10:13:08 +00:00
|
|
|
// Now try the L2 cache, must fit into L2.
|
2009-09-16 09:47:25 +00:00
|
|
|
__num_bins_cache =
|
|
|
|
static_cast<_BinIndex>(std::max<_DifferenceType>(
|
|
|
|
1, __n / (__s.L2_cache_size / sizeof(_ValueType))));
|
|
|
|
__num_bins_cache = __round_up_to_pow2(__num_bins_cache);
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2007-11-22 10:13:08 +00:00
|
|
|
// No more buckets than TLB entries, power of 2
|
|
|
|
// Power of 2 and at least one element per bin, at most the TLB size.
|
2009-09-16 09:47:25 +00:00
|
|
|
_M_num_bins = static_cast<_BinIndex>
|
|
|
|
(std::min(__n, static_cast<_DifferenceType>(__num_bins_cache)));
|
2007-09-11 22:32:51 +00:00
|
|
|
|
|
|
|
#if _GLIBCXX_RANDOM_SHUFFLE_CONSIDER_TLB
|
2007-11-22 10:13:08 +00:00
|
|
|
// 2 TLB entries needed per bin
|
2009-09-16 09:47:25 +00:00
|
|
|
_M_num_bins =
|
|
|
|
std::min<_DifferenceType>(__s.TLB_size / 2, _M_num_bins);
|
2007-09-11 22:32:51 +00:00
|
|
|
#endif
|
2009-09-16 09:47:25 +00:00
|
|
|
_M_num_bins = __round_up_to_pow2(_M_num_bins);
|
2007-09-11 22:32:51 +00:00
|
|
|
#if _GLIBCXX_RANDOM_SHUFFLE_CONSIDER_L1
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-09-23 08:09:50 +00:00
|
|
|
int _M_num_bits = __rd_log2(_M_num_bins);
|
2007-09-11 22:32:51 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
if (_M_num_bins > 1)
|
2007-09-11 22:32:51 +00:00
|
|
|
{
|
2009-09-16 09:47:25 +00:00
|
|
|
_ValueType* __target = static_cast<_ValueType*>(
|
|
|
|
::operator new(sizeof(_ValueType) * __n));
|
|
|
|
_BinIndex* __oracles = new _BinIndex[__n];
|
|
|
|
_DifferenceType* __dist0 = new _DifferenceType[_M_num_bins + 1],
|
|
|
|
* __dist1 = new _DifferenceType[_M_num_bins + 1];
|
2007-11-22 10:13:08 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
for (int __b = 0; __b < _M_num_bins + 1; ++__b)
|
|
|
|
__dist0[__b] = 0;
|
2007-11-22 10:13:08 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
_RandomNumber bitrng(__rng(0xFFFFFFFF));
|
2007-11-22 10:13:08 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
for (_DifferenceType __i = 0; __i < __n; ++__i)
|
2007-11-22 10:13:08 +00:00
|
|
|
{
|
2009-09-16 09:47:25 +00:00
|
|
|
_BinIndex __oracle = __random_number_pow2(_M_num_bits, bitrng);
|
|
|
|
__oracles[__i] = __oracle;
|
2007-11-22 10:13:08 +00:00
|
|
|
|
|
|
|
// To allow prefix (partial) sum.
|
2009-09-16 09:47:25 +00:00
|
|
|
++(__dist0[__oracle + 1]);
|
2007-11-22 10:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sum up bins.
|
2009-09-17 08:42:39 +00:00
|
|
|
__gnu_sequential::
|
|
|
|
partial_sum(__dist0, __dist0 + _M_num_bins + 1, __dist0);
|
2007-11-22 10:13:08 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
for (int __b = 0; __b < _M_num_bins + 1; ++__b)
|
|
|
|
__dist1[__b] = __dist0[__b];
|
2007-11-22 10:13:08 +00:00
|
|
|
|
|
|
|
// Distribute according to oracles.
|
2009-09-16 09:47:25 +00:00
|
|
|
for (_DifferenceType __i = 0; __i < __n; ++__i)
|
2009-09-17 08:42:39 +00:00
|
|
|
::new(&(__target[(__dist0[__oracles[__i]])++]))
|
|
|
|
_ValueType(*(__begin + __i));
|
2007-11-22 10:13:08 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
for (int __b = 0; __b < _M_num_bins; ++__b)
|
2007-11-22 10:13:08 +00:00
|
|
|
{
|
2009-09-16 09:47:25 +00:00
|
|
|
__sequential_random_shuffle(__target + __dist1[__b],
|
|
|
|
__target + __dist1[__b + 1],
|
|
|
|
__rng);
|
2007-11-22 10:13:08 +00:00
|
|
|
}
|
|
|
|
|
2008-07-09 15:26:56 +00:00
|
|
|
// Copy elements back.
|
2009-09-16 09:47:25 +00:00
|
|
|
std::copy(__target, __target + __n, __begin);
|
2008-07-09 15:26:56 +00:00
|
|
|
|
2009-09-16 09:47:25 +00:00
|
|
|
delete[] __dist0;
|
|
|
|
delete[] __dist1;
|
|
|
|
delete[] __oracles;
|
|
|
|
::operator delete(__target);
|
2007-09-11 22:32:51 +00:00
|
|
|
}
|
|
|
|
else
|
2009-09-16 09:47:25 +00:00
|
|
|
__gnu_sequential::random_shuffle(__begin, __end, __rng);
|
2007-09-11 22:32:51 +00:00
|
|
|
}
|
|
|
|
|
2007-11-22 10:13:08 +00:00
|
|
|
/** @brief Parallel random public call.
|
2009-09-16 09:47:25 +00:00
|
|
|
* @param __begin Begin iterator of sequence.
|
|
|
|
* @param __end End iterator of sequence.
|
|
|
|
* @param __rng Random number generator to use.
|
2007-11-22 10:13:08 +00:00
|
|
|
*/
|
2009-09-23 08:09:50 +00:00
|
|
|
template<typename _RAIter, typename _RandomNumberGenerator>
|
2007-09-11 22:32:51 +00:00
|
|
|
inline void
|
2009-09-16 09:47:25 +00:00
|
|
|
__parallel_random_shuffle(_RAIter __begin,
|
|
|
|
_RAIter __end,
|
2009-09-23 08:09:50 +00:00
|
|
|
_RandomNumberGenerator __rng = _RandomNumber())
|
2007-09-11 22:32:51 +00:00
|
|
|
{
|
2009-09-16 09:47:25 +00:00
|
|
|
typedef std::iterator_traits<_RAIter> _TraitsType;
|
|
|
|
typedef typename _TraitsType::difference_type _DifferenceType;
|
|
|
|
_DifferenceType __n = __end - __begin;
|
2009-09-17 08:42:39 +00:00
|
|
|
__parallel_random_shuffle_drs(
|
|
|
|
__begin, __end, __n, __get_max_threads(), __rng) ;
|
2007-09-11 22:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-07-15 10:06:58 +00:00
|
|
|
#endif /* _GLIBCXX_PARALLEL_RANDOM_SHUFFLE_H */
|