]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/parallel/multiway_mergesort.h
algobase.h: Uglify internal identifiers.
[thirdparty/gcc.git] / libstdc++-v3 / include / parallel / multiway_mergesort.h
CommitLineData
c2ba9709
JS
1// -*- C++ -*-
2
748086b7 3// Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
c2ba9709
JS
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the terms
7// of the GNU General Public License as published by the Free Software
748086b7 8// Foundation; either version 3, or (at your option) any later
c2ba9709
JS
9// version.
10
11// This library is distributed in the hope that it will be useful, but
12// WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15
748086b7
JJ
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
c2ba9709 19
748086b7
JJ
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
c2ba9709
JS
24
25/** @file parallel/multiway_mergesort.h
26 * @brief Parallel multiway merge sort.
27 * This file is a GNU parallel extension to the Standard C++ Library.
28 */
29
30// Written by Johannes Singler.
31
cbcd1e45
JS
32#ifndef _GLIBCXX_PARALLEL_MULTIWAY_MERGESORT_H
33#define _GLIBCXX_PARALLEL_MULTIWAY_MERGESORT_H 1
c2ba9709
JS
34
35#include <vector>
36
37#include <parallel/basic_iterator.h>
38#include <bits/stl_algo.h>
39#include <parallel/parallel.h>
40#include <parallel/multiway_merge.h>
c2ba9709
JS
41
42namespace __gnu_parallel
43{
44
e683ee2a
JS
45/** @brief Subsequence description. */
46template<typename _DifferenceTp>
1acba85b 47 struct _Piece
c2ba9709 48 {
1acba85b 49 typedef _DifferenceTp _DifferenceType;
c2ba9709
JS
50
51 /** @brief Begin of subsequence. */
1acba85b 52 _DifferenceType __begin;
c2ba9709
JS
53
54 /** @brief End of subsequence. */
1acba85b 55 _DifferenceType __end;
c2ba9709
JS
56 };
57
e683ee2a
JS
58/** @brief Data accessed by all threads.
59 *
60 * PMWMS = parallel multiway mergesort */
1acba85b
JS
61template<typename _RAIter>
62 struct _PMWMSSortingData
c2ba9709 63 {
1acba85b
JS
64 typedef std::iterator_traits<_RAIter> _TraitsType;
65 typedef typename _TraitsType::value_type _ValueType;
66 typedef typename _TraitsType::difference_type _DifferenceType;
c2ba9709 67
e683ee2a 68 /** @brief Number of threads involved. */
1acba85b 69 _ThreadIndex __num_threads;
e683ee2a 70
1acba85b
JS
71 /** @brief Input __begin. */
72 _RAIter _M_source;
c2ba9709
JS
73
74 /** @brief Start indices, per thread. */
1acba85b 75 _DifferenceType* _M_starts;
c2ba9709 76
c2ba9709 77 /** @brief Storage in which to sort. */
1acba85b 78 _ValueType** _M_temporary;
c2ba9709 79
c2ba9709 80 /** @brief Samples. */
1acba85b 81 _ValueType* _M_samples;
c2ba9709
JS
82
83 /** @brief Offsets to add to the found positions. */
1acba85b 84 _DifferenceType* _M_offsets;
c2ba9709 85
1acba85b
JS
86 /** @brief Pieces of data to merge @__c [thread][__sequence] */
87 std::vector<_Piece<_DifferenceType> >* _M_pieces;
e683ee2a
JS
88};
89
90/**
1acba85b
JS
91 * @brief Select _M_samples from a sequence.
92 * @param __sd Pointer to algorithm data. _Result will be placed in
93 * @__c __sd->_M_samples.
94 * @param __num_samples Number of _M_samples to select.
e683ee2a 95 */
1acba85b 96template<typename _RAIter, typename _DifferenceTp>
5817ff8e 97 void
1acba85b
JS
98 __determine_samples(_PMWMSSortingData<_RAIter>* __sd,
99 _DifferenceTp __num_samples)
c2ba9709 100 {
1acba85b
JS
101 typedef std::iterator_traits<_RAIter> _TraitsType;
102 typedef typename _TraitsType::value_type _ValueType;
103 typedef _DifferenceTp _DifferenceType;
c2ba9709 104
1acba85b 105 _ThreadIndex __iam = omp_get_thread_num();
c2ba9709 106
1acba85b 107 _DifferenceType* __es = new _DifferenceType[__num_samples + 2];
c891154f 108
1acba85b
JS
109 equally_split(__sd->_M_starts[__iam + 1] - __sd->_M_starts[__iam],
110 __num_samples + 1, __es);
c2ba9709 111
1acba85b
JS
112 for (_DifferenceType __i = 0; __i < __num_samples; ++__i)
113 ::new(&(__sd->_M_samples[__iam * __num_samples + __i]))
114 _ValueType(__sd->_M_source[__sd->_M_starts[__iam] + __es[__i + 1]]);
e683ee2a 115
1acba85b 116 delete[] __es;
c2ba9709
JS
117 }
118
f9985df5 119/** @brief Split consistently. */
1acba85b
JS
120template<bool __exact, typename _RAIter,
121 typename _Compare, typename _SortingPlacesIterator>
122 struct _SplitConsistently
f9985df5
JS
123 {
124 };
125
126/** @brief Split by exact splitting. */
1acba85b
JS
127template<typename _RAIter, typename _Compare,
128 typename _SortingPlacesIterator>
129 struct _SplitConsistently
130 <true, _RAIter, _Compare, _SortingPlacesIterator>
f9985df5
JS
131 {
132 void operator()(
1acba85b
JS
133 const _ThreadIndex __iam,
134 _PMWMSSortingData<_RAIter>* __sd,
135 _Compare& __comp,
f9985df5 136 const typename
1acba85b
JS
137 std::iterator_traits<_RAIter>::difference_type
138 __num_samples)
f9985df5
JS
139 const
140 {
141# pragma omp barrier
142
1acba85b
JS
143 std::vector<std::pair<_SortingPlacesIterator, _SortingPlacesIterator> >
144 seqs(__sd->__num_threads);
145 for (_ThreadIndex __s = 0; __s < __sd->__num_threads; __s++)
146 seqs[__s] = std::make_pair(__sd->_M_temporary[__s],
147 __sd->_M_temporary[__s]
148 + (__sd->_M_starts[__s + 1] - __sd->_M_starts[__s]));
f9985df5 149
1acba85b 150 std::vector<_SortingPlacesIterator> _M_offsets(__sd->__num_threads);
f9985df5
JS
151
152 // if not last thread
1acba85b 153 if (__iam < __sd->__num_threads - 1)
f9985df5 154 multiseq_partition(seqs.begin(), seqs.end(),
1acba85b 155 __sd->_M_starts[__iam + 1], _M_offsets.begin(), __comp);
f9985df5 156
1acba85b 157 for (int __seq = 0; __seq < __sd->__num_threads; __seq++)
f9985df5
JS
158 {
159 // for each sequence
1acba85b
JS
160 if (__iam < (__sd->__num_threads - 1))
161 __sd->_M_pieces[__iam][__seq].__end = _M_offsets[__seq] - seqs[__seq].first;
f9985df5
JS
162 else
163 // very end of this sequence
1acba85b
JS
164 __sd->_M_pieces[__iam][__seq].__end =
165 __sd->_M_starts[__seq + 1] - __sd->_M_starts[__seq];
f9985df5
JS
166 }
167
168# pragma omp barrier
169
1acba85b 170 for (_ThreadIndex __seq = 0; __seq < __sd->__num_threads; __seq++)
f9985df5
JS
171 {
172 // For each sequence.
1acba85b
JS
173 if (__iam > 0)
174 __sd->_M_pieces[__iam][__seq].__begin = __sd->_M_pieces[__iam - 1][__seq].__end;
f9985df5
JS
175 else
176 // Absolute beginning.
1acba85b 177 __sd->_M_pieces[__iam][__seq].__begin = 0;
f9985df5
JS
178 }
179 }
180 };
181
182/** @brief Split by sampling. */
1acba85b
JS
183template<typename _RAIter, typename _Compare,
184 typename _SortingPlacesIterator>
185 struct _SplitConsistently<false, _RAIter, _Compare,
186 _SortingPlacesIterator>
f9985df5
JS
187 {
188 void operator()(
1acba85b
JS
189 const _ThreadIndex __iam,
190 _PMWMSSortingData<_RAIter>* __sd,
191 _Compare& __comp,
f9985df5 192 const typename
1acba85b
JS
193 std::iterator_traits<_RAIter>::difference_type
194 __num_samples)
f9985df5
JS
195 const
196 {
1acba85b
JS
197 typedef std::iterator_traits<_RAIter> _TraitsType;
198 typedef typename _TraitsType::value_type _ValueType;
199 typedef typename _TraitsType::difference_type _DifferenceType;
f9985df5 200
1acba85b 201 __determine_samples(__sd, __num_samples);
f9985df5
JS
202
203# pragma omp barrier
204
205# pragma omp single
1acba85b
JS
206 __gnu_sequential::sort(__sd->_M_samples,
207 __sd->_M_samples + (__num_samples * __sd->__num_threads),
208 __comp);
f9985df5
JS
209
210# pragma omp barrier
211
1acba85b 212 for (_ThreadIndex __s = 0; __s < __sd->__num_threads; ++__s)
f9985df5
JS
213 {
214 // For each sequence.
1acba85b
JS
215 if (__num_samples * __iam > 0)
216 __sd->_M_pieces[__iam][__s].__begin =
217 std::lower_bound(__sd->_M_temporary[__s],
218 __sd->_M_temporary[__s]
219 + (__sd->_M_starts[__s + 1] - __sd->_M_starts[__s]),
220 __sd->_M_samples[__num_samples * __iam],
221 __comp)
222 - __sd->_M_temporary[__s];
f9985df5
JS
223 else
224 // Absolute beginning.
1acba85b
JS
225 __sd->_M_pieces[__iam][__s].__begin = 0;
226
227 if ((__num_samples * (__iam + 1)) < (__num_samples * __sd->__num_threads))
228 __sd->_M_pieces[__iam][__s].__end =
229 std::lower_bound(__sd->_M_temporary[__s],
230 __sd->_M_temporary[__s]
231 + (__sd->_M_starts[__s + 1] - __sd->_M_starts[__s]),
232 __sd->_M_samples[__num_samples * (__iam + 1)],
233 __comp)
234 - __sd->_M_temporary[__s];
f9985df5 235 else
1acba85b
JS
236 // Absolute __end.
237 __sd->_M_pieces[__iam][__s].__end = __sd->_M_starts[__s + 1] - __sd->_M_starts[__s];
f9985df5
JS
238 }
239 }
240 };
241
1acba85b
JS
242template<bool __stable, typename _RAIter, typename _Compare>
243 struct __possibly_stable_sort
f9985df5
JS
244 {
245 };
246
1acba85b
JS
247template<typename _RAIter, typename _Compare>
248 struct __possibly_stable_sort<true, _RAIter, _Compare>
f9985df5 249 {
1acba85b
JS
250 void operator()(const _RAIter& __begin,
251 const _RAIter& __end, _Compare& __comp) const
f9985df5 252 {
1acba85b 253 __gnu_sequential::stable_sort(__begin, __end, __comp);
f9985df5
JS
254 }
255 };
256
1acba85b
JS
257template<typename _RAIter, typename _Compare>
258 struct __possibly_stable_sort<false, _RAIter, _Compare>
f9985df5 259 {
1acba85b
JS
260 void operator()(const _RAIter __begin,
261 const _RAIter __end, _Compare& __comp) const
f9985df5 262 {
1acba85b 263 __gnu_sequential::sort(__begin, __end, __comp);
f9985df5
JS
264 }
265 };
266
1acba85b
JS
267template<bool __stable, typename Seq_RAIter,
268 typename _RAIter, typename _Compare,
f9985df5 269 typename DiffType>
1acba85b 270 struct __possibly_stable_multiway_merge
f9985df5
JS
271 {
272 };
273
1acba85b
JS
274template<typename Seq_RAIter, typename _RAIter,
275 typename _Compare, typename DiffType>
276 struct __possibly_stable_multiway_merge
277 <true, Seq_RAIter, _RAIter, _Compare,
f9985df5
JS
278 DiffType>
279 {
1acba85b
JS
280 void operator()(const Seq_RAIter& __seqs_begin,
281 const Seq_RAIter& __seqs_end,
282 const _RAIter& __target,
283 _Compare& __comp,
284 DiffType __length_am) const
f9985df5 285 {
1acba85b 286 stable_multiway_merge(__seqs_begin, __seqs_end, __target, __length_am, __comp,
36fc5958 287 sequential_tag());
f9985df5
JS
288 }
289 };
290
1acba85b
JS
291template<typename Seq_RAIter, typename _RAIter,
292 typename _Compare, typename DiffType>
293 struct __possibly_stable_multiway_merge
294 <false, Seq_RAIter, _RAIter, _Compare,
f9985df5
JS
295 DiffType>
296 {
1acba85b
JS
297 void operator()(const Seq_RAIter& __seqs_begin,
298 const Seq_RAIter& __seqs_end,
299 const _RAIter& __target,
300 _Compare& __comp,
301 DiffType __length_am) const
f9985df5 302 {
1acba85b 303 multiway_merge(__seqs_begin, __seqs_end, __target, __length_am, __comp,
36fc5958 304 sequential_tag());
f9985df5
JS
305 }
306 };
307
e683ee2a 308/** @brief PMWMS code executed by each thread.
1acba85b
JS
309 * @param __sd Pointer to algorithm data.
310 * @param __comp Comparator.
e683ee2a 311 */
1acba85b
JS
312template<bool __stable, bool __exact, typename _RAIter,
313 typename _Compare>
5817ff8e 314 void
1acba85b
JS
315 parallel_sort_mwms_pu(_PMWMSSortingData<_RAIter>* __sd,
316 _Compare& __comp)
c2ba9709 317 {
1acba85b
JS
318 typedef std::iterator_traits<_RAIter> _TraitsType;
319 typedef typename _TraitsType::value_type _ValueType;
320 typedef typename _TraitsType::difference_type _DifferenceType;
c2ba9709 321
1acba85b 322 _ThreadIndex __iam = omp_get_thread_num();
c2ba9709
JS
323
324 // Length of this thread's chunk, before merging.
1acba85b 325 _DifferenceType __length_local = __sd->_M_starts[__iam + 1] - __sd->_M_starts[__iam];
c2ba9709 326
f9985df5 327 // Sort in temporary storage, leave space for sentinel.
c2ba9709 328
1acba85b 329 typedef _ValueType* _SortingPlacesIterator;
c2ba9709 330
1acba85b
JS
331 __sd->_M_temporary[__iam] =
332 static_cast<_ValueType*>(
333 ::operator new(sizeof(_ValueType) * (__length_local + 1)));
c2ba9709
JS
334
335 // Copy there.
1acba85b
JS
336 std::uninitialized_copy(__sd->_M_source + __sd->_M_starts[__iam],
337 __sd->_M_source + __sd->_M_starts[__iam] + __length_local,
338 __sd->_M_temporary[__iam]);
e683ee2a 339
1acba85b
JS
340 __possibly_stable_sort<__stable, _SortingPlacesIterator, _Compare>()
341 (__sd->_M_temporary[__iam], __sd->_M_temporary[__iam] + __length_local, __comp);
e683ee2a 342
1acba85b
JS
343 // Invariant: locally sorted subsequence in sd->_M_temporary[__iam],
344 // __sd->_M_temporary[__iam] + __length_local.
e683ee2a 345
f9985df5 346 // No barrier here: Synchronization is done by the splitting routine.
e683ee2a 347
1acba85b
JS
348 _DifferenceType __num_samples =
349 _Settings::get().sort_mwms_oversampling * __sd->__num_threads - 1;
350 _SplitConsistently
351 <__exact, _RAIter, _Compare, _SortingPlacesIterator>()
352 (__iam, __sd, __comp, __num_samples);
c2ba9709 353
1acba85b
JS
354 // Offset from __target __begin, __length after merging.
355 _DifferenceType __offset = 0, __length_am = 0;
356 for (_ThreadIndex __s = 0; __s < __sd->__num_threads; __s++)
c2ba9709 357 {
1acba85b
JS
358 __length_am += __sd->_M_pieces[__iam][__s].__end - __sd->_M_pieces[__iam][__s].__begin;
359 __offset += __sd->_M_pieces[__iam][__s].__begin;
c2ba9709
JS
360 }
361
f9985df5 362 typedef std::vector<
1acba85b 363 std::pair<_SortingPlacesIterator, _SortingPlacesIterator> >
f9985df5 364 seq_vector_type;
1acba85b 365 seq_vector_type seqs(__sd->__num_threads);
c2ba9709 366
1acba85b 367 for (int __s = 0; __s < __sd->__num_threads; ++__s)
c2ba9709 368 {
1acba85b
JS
369 seqs[__s] =
370 std::make_pair(__sd->_M_temporary[__s] + __sd->_M_pieces[__iam][__s].__begin,
371 __sd->_M_temporary[__s] + __sd->_M_pieces[__iam][__s].__end);
c2ba9709
JS
372 }
373
1acba85b
JS
374 __possibly_stable_multiway_merge<
375 __stable,
f9985df5 376 typename seq_vector_type::iterator,
1acba85b
JS
377 _RAIter,
378 _Compare, _DifferenceType>()
f9985df5 379 (seqs.begin(), seqs.end(),
1acba85b
JS
380 __sd->_M_source + __offset, __comp,
381 __length_am);
c2ba9709 382
e683ee2a 383# pragma omp barrier
c2ba9709 384
1acba85b 385 ::operator delete(__sd->_M_temporary[__iam]);
c2ba9709
JS
386 }
387
e683ee2a 388/** @brief PMWMS main call.
1acba85b
JS
389 * @param __begin Begin iterator of sequence.
390 * @param __end End iterator of sequence.
391 * @param __comp Comparator.
392 * @param __n Length of sequence.
393 * @param __num_threads Number of threads to use.
e683ee2a 394 */
1acba85b
JS
395template<bool __stable, bool __exact, typename _RAIter,
396 typename _Compare>
5817ff8e 397 void
1acba85b
JS
398 parallel_sort_mwms(_RAIter __begin, _RAIter __end,
399 _Compare __comp,
400 _ThreadIndex __num_threads)
c2ba9709 401 {
1acba85b 402 _GLIBCXX_CALL(__end - __begin)
e683ee2a 403
1acba85b
JS
404 typedef std::iterator_traits<_RAIter> _TraitsType;
405 typedef typename _TraitsType::value_type _ValueType;
406 typedef typename _TraitsType::difference_type _DifferenceType;
c2ba9709 407
1acba85b 408 _DifferenceType __n = __end - __begin;
f9985df5 409
1acba85b 410 if (__n <= 1)
c2ba9709
JS
411 return;
412
e683ee2a 413 // at least one element per thread
1acba85b
JS
414 if (__num_threads > __n)
415 __num_threads = static_cast<_ThreadIndex>(__n);
c2ba9709 416
e683ee2a 417 // shared variables
1acba85b
JS
418 _PMWMSSortingData<_RAIter> __sd;
419 _DifferenceType* _M_starts;
c2ba9709 420
1acba85b 421# pragma omp parallel num_threads(__num_threads)
e683ee2a 422 {
1acba85b 423 __num_threads = omp_get_num_threads(); //no more threads than requested
e683ee2a
JS
424
425# pragma omp single
426 {
1acba85b
JS
427 __sd.__num_threads = __num_threads;
428 __sd._M_source = __begin;
c2ba9709 429
1acba85b 430 __sd._M_temporary = new _ValueType*[__num_threads];
f9985df5 431
1acba85b 432 if (!__exact)
e683ee2a 433 {
1acba85b
JS
434 _DifferenceType size =
435 (_Settings::get().sort_mwms_oversampling * __num_threads - 1)
436 * __num_threads;
437 __sd._M_samples = static_cast<_ValueType*>(
438 ::operator new(size * sizeof(_ValueType)));
e683ee2a
JS
439 }
440 else
1acba85b
JS
441 __sd._M_samples = NULL;
442
443 __sd._M_offsets = new _DifferenceType[__num_threads - 1];
444 __sd._M_pieces = new std::vector<_Piece<_DifferenceType> >[__num_threads];
445 for (int __s = 0; __s < __num_threads; ++__s)
446 __sd._M_pieces[__s].resize(__num_threads);
447 _M_starts = __sd._M_starts = new _DifferenceType[__num_threads + 1];
448
449 _DifferenceType __chunk_length = __n / __num_threads;
450 _DifferenceType __split = __n % __num_threads;
451 _DifferenceType __pos = 0;
452 for (int __i = 0; __i < __num_threads; ++__i)
e683ee2a 453 {
1acba85b
JS
454 _M_starts[__i] = __pos;
455 __pos += (__i < __split) ? (__chunk_length + 1) : __chunk_length;
e683ee2a 456 }
1acba85b 457 _M_starts[__num_threads] = __pos;
f9985df5 458 } //single
e683ee2a
JS
459
460 // Now sort in parallel.
1acba85b 461 parallel_sort_mwms_pu<__stable, __exact>(&__sd, __comp);
e683ee2a 462 } //parallel
c2ba9709 463
1acba85b
JS
464 delete[] _M_starts;
465 delete[] __sd._M_temporary;
c2ba9709 466
1acba85b
JS
467 if (!__exact)
468 ::operator delete(__sd._M_samples);
c2ba9709 469
1acba85b
JS
470 delete[] __sd._M_offsets;
471 delete[] __sd._M_pieces;
c2ba9709 472 }
e683ee2a 473} //namespace __gnu_parallel
c2ba9709 474
cbcd1e45 475#endif /* _GLIBCXX_PARALLEL_MULTIWAY_MERGESORT_H */