3 // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
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
8 // Foundation; either version 3, or (at your option) any later
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.
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.
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/>.
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.
30 // Written by Johannes Singler.
32 #ifndef _GLIBCXX_PARALLEL_MULTIWAY_MERGESORT_H
33 #define _GLIBCXX_PARALLEL_MULTIWAY_MERGESORT_H 1
37 #include <parallel/basic_iterator.h>
38 #include <bits/stl_algo.h>
39 #include <parallel/parallel.h>
40 #include <parallel/multiway_merge.h>
42 namespace __gnu_parallel
45 /** @brief Subsequence description. */
46 template<typename _DifferenceTp>
49 typedef _DifferenceTp difference_type;
51 /** @brief Begin of subsequence. */
52 difference_type begin;
54 /** @brief End of subsequence. */
58 /** @brief Data accessed by all threads.
60 * PMWMS = parallel multiway mergesort */
61 template<typename RandomAccessIterator>
62 struct PMWMSSortingData
64 typedef std::iterator_traits<RandomAccessIterator> traits_type;
65 typedef typename traits_type::value_type value_type;
66 typedef typename traits_type::difference_type difference_type;
68 /** @brief Number of threads involved. */
69 thread_index_t num_threads;
71 /** @brief Input begin. */
72 RandomAccessIterator source;
74 /** @brief Start indices, per thread. */
75 difference_type* starts;
77 /** @brief Storage in which to sort. */
78 value_type** temporary;
80 /** @brief Samples. */
83 /** @brief Offsets to add to the found positions. */
84 difference_type* offsets;
86 /** @brief Pieces of data to merge @c [thread][sequence] */
87 std::vector<Piece<difference_type> >* pieces;
91 * @brief Select samples from a sequence.
92 * @param sd Pointer to algorithm data. Result will be placed in
94 * @param num_samples Number of samples to select.
96 template<typename RandomAccessIterator, typename _DifferenceTp>
98 determine_samples(PMWMSSortingData<RandomAccessIterator>* sd,
99 _DifferenceTp num_samples)
101 typedef std::iterator_traits<RandomAccessIterator> traits_type;
102 typedef typename traits_type::value_type value_type;
103 typedef _DifferenceTp difference_type;
105 thread_index_t iam = omp_get_thread_num();
107 difference_type* es = new difference_type[num_samples + 2];
109 equally_split(sd->starts[iam + 1] - sd->starts[iam],
110 num_samples + 1, es);
112 for (difference_type i = 0; i < num_samples; ++i)
113 ::new(&(sd->samples[iam * num_samples + i]))
114 value_type(sd->source[sd->starts[iam] + es[i + 1]]);
119 /** @brief Split consistently. */
120 template<bool exact, typename RandomAccessIterator,
121 typename Comparator, typename SortingPlacesIterator>
122 struct split_consistently
126 /** @brief Split by exact splitting. */
127 template<typename RandomAccessIterator, typename Comparator,
128 typename SortingPlacesIterator>
129 struct split_consistently
130 <true, RandomAccessIterator, Comparator, SortingPlacesIterator>
133 const thread_index_t iam,
134 PMWMSSortingData<RandomAccessIterator>* sd,
137 std::iterator_traits<RandomAccessIterator>::difference_type
143 std::vector<std::pair<SortingPlacesIterator, SortingPlacesIterator> >
144 seqs(sd->num_threads);
145 for (thread_index_t s = 0; s < sd->num_threads; s++)
146 seqs[s] = std::make_pair(sd->temporary[s],
148 + (sd->starts[s + 1] - sd->starts[s]));
150 std::vector<SortingPlacesIterator> offsets(sd->num_threads);
152 // if not last thread
153 if (iam < sd->num_threads - 1)
154 multiseq_partition(seqs.begin(), seqs.end(),
155 sd->starts[iam + 1], offsets.begin(), comp);
157 for (int seq = 0; seq < sd->num_threads; seq++)
160 if (iam < (sd->num_threads - 1))
161 sd->pieces[iam][seq].end = offsets[seq] - seqs[seq].first;
163 // very end of this sequence
164 sd->pieces[iam][seq].end =
165 sd->starts[seq + 1] - sd->starts[seq];
170 for (thread_index_t seq = 0; seq < sd->num_threads; seq++)
172 // For each sequence.
174 sd->pieces[iam][seq].begin = sd->pieces[iam - 1][seq].end;
176 // Absolute beginning.
177 sd->pieces[iam][seq].begin = 0;
182 /** @brief Split by sampling. */
183 template<typename RandomAccessIterator, typename Comparator,
184 typename SortingPlacesIterator>
185 struct split_consistently<false, RandomAccessIterator, Comparator,
186 SortingPlacesIterator>
189 const thread_index_t iam,
190 PMWMSSortingData<RandomAccessIterator>* sd,
193 std::iterator_traits<RandomAccessIterator>::difference_type
197 typedef std::iterator_traits<RandomAccessIterator> traits_type;
198 typedef typename traits_type::value_type value_type;
199 typedef typename traits_type::difference_type difference_type;
201 determine_samples(sd, num_samples);
206 __gnu_sequential::sort(sd->samples,
207 sd->samples + (num_samples * sd->num_threads),
212 for (thread_index_t s = 0; s < sd->num_threads; ++s)
214 // For each sequence.
215 if (num_samples * iam > 0)
216 sd->pieces[iam][s].begin =
217 std::lower_bound(sd->temporary[s],
219 + (sd->starts[s + 1] - sd->starts[s]),
220 sd->samples[num_samples * iam],
224 // Absolute beginning.
225 sd->pieces[iam][s].begin = 0;
227 if ((num_samples * (iam + 1)) < (num_samples * sd->num_threads))
228 sd->pieces[iam][s].end =
229 std::lower_bound(sd->temporary[s],
231 + (sd->starts[s + 1] - sd->starts[s]),
232 sd->samples[num_samples * (iam + 1)],
237 sd->pieces[iam][s].end = sd->starts[s + 1] - sd->starts[s];
242 template<bool stable, typename RandomAccessIterator, typename Comparator>
243 struct possibly_stable_sort
247 template<typename RandomAccessIterator, typename Comparator>
248 struct possibly_stable_sort<true, RandomAccessIterator, Comparator>
250 void operator()(const RandomAccessIterator& begin,
251 const RandomAccessIterator& end, Comparator& comp) const
253 __gnu_sequential::stable_sort(begin, end, comp);
257 template<typename RandomAccessIterator, typename Comparator>
258 struct possibly_stable_sort<false, RandomAccessIterator, Comparator>
260 void operator()(const RandomAccessIterator begin,
261 const RandomAccessIterator end, Comparator& comp) const
263 __gnu_sequential::sort(begin, end, comp);
267 template<bool stable, typename SeqRandomAccessIterator,
268 typename RandomAccessIterator, typename Comparator,
270 struct possibly_stable_multiway_merge
274 template<typename SeqRandomAccessIterator, typename RandomAccessIterator,
275 typename Comparator, typename DiffType>
276 struct possibly_stable_multiway_merge
277 <true, SeqRandomAccessIterator, RandomAccessIterator, Comparator,
280 void operator()(const SeqRandomAccessIterator& seqs_begin,
281 const SeqRandomAccessIterator& seqs_end,
282 const RandomAccessIterator& target,
284 DiffType length_am) const
286 stable_multiway_merge(seqs_begin, seqs_end, target, length_am, comp,
291 template<typename SeqRandomAccessIterator, typename RandomAccessIterator,
292 typename Comparator, typename DiffType>
293 struct possibly_stable_multiway_merge
294 <false, SeqRandomAccessIterator, RandomAccessIterator, Comparator,
297 void operator()(const SeqRandomAccessIterator& seqs_begin,
298 const SeqRandomAccessIterator& seqs_end,
299 const RandomAccessIterator& target,
301 DiffType length_am) const
303 multiway_merge(seqs_begin, seqs_end, target, length_am, comp,
308 /** @brief PMWMS code executed by each thread.
309 * @param sd Pointer to algorithm data.
310 * @param comp Comparator.
312 template<bool stable, bool exact, typename RandomAccessIterator,
315 parallel_sort_mwms_pu(PMWMSSortingData<RandomAccessIterator>* sd,
318 typedef std::iterator_traits<RandomAccessIterator> traits_type;
319 typedef typename traits_type::value_type value_type;
320 typedef typename traits_type::difference_type difference_type;
322 thread_index_t iam = omp_get_thread_num();
324 // Length of this thread's chunk, before merging.
325 difference_type length_local = sd->starts[iam + 1] - sd->starts[iam];
327 // Sort in temporary storage, leave space for sentinel.
329 typedef value_type* SortingPlacesIterator;
332 static_cast<value_type*>(
333 ::operator new(sizeof(value_type) * (length_local + 1)));
336 std::uninitialized_copy(sd->source + sd->starts[iam],
337 sd->source + sd->starts[iam] + length_local,
340 possibly_stable_sort<stable, SortingPlacesIterator, Comparator>()
341 (sd->temporary[iam], sd->temporary[iam] + length_local, comp);
343 // Invariant: locally sorted subsequence in sd->temporary[iam],
344 // sd->temporary[iam] + length_local.
346 // No barrier here: Synchronization is done by the splitting routine.
348 difference_type num_samples =
349 _Settings::get().sort_mwms_oversampling * sd->num_threads - 1;
351 <exact, RandomAccessIterator, Comparator, SortingPlacesIterator>()
352 (iam, sd, comp, num_samples);
354 // Offset from target begin, length after merging.
355 difference_type offset = 0, length_am = 0;
356 for (thread_index_t s = 0; s < sd->num_threads; s++)
358 length_am += sd->pieces[iam][s].end - sd->pieces[iam][s].begin;
359 offset += sd->pieces[iam][s].begin;
363 std::pair<SortingPlacesIterator, SortingPlacesIterator> >
365 seq_vector_type seqs(sd->num_threads);
367 for (int s = 0; s < sd->num_threads; ++s)
370 std::make_pair(sd->temporary[s] + sd->pieces[iam][s].begin,
371 sd->temporary[s] + sd->pieces[iam][s].end);
374 possibly_stable_multiway_merge<
376 typename seq_vector_type::iterator,
377 RandomAccessIterator,
378 Comparator, difference_type>()
379 (seqs.begin(), seqs.end(),
380 sd->source + offset, comp,
385 ::operator delete(sd->temporary[iam]);
388 /** @brief PMWMS main call.
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.
395 template<bool stable, bool exact, typename RandomAccessIterator,
398 parallel_sort_mwms(RandomAccessIterator begin, RandomAccessIterator end,
400 thread_index_t num_threads)
402 _GLIBCXX_CALL(end - begin)
404 typedef std::iterator_traits<RandomAccessIterator> traits_type;
405 typedef typename traits_type::value_type value_type;
406 typedef typename traits_type::difference_type difference_type;
408 difference_type n = end - begin;
413 // at least one element per thread
415 num_threads = static_cast<thread_index_t>(n);
418 PMWMSSortingData<RandomAccessIterator> sd;
419 difference_type* starts;
421 # pragma omp parallel num_threads(num_threads)
423 num_threads = omp_get_num_threads(); //no more threads than requested
427 sd.num_threads = num_threads;
430 sd.temporary = new value_type*[num_threads];
434 difference_type size =
435 (_Settings::get().sort_mwms_oversampling * num_threads - 1)
437 sd.samples = static_cast<value_type*>(
438 ::operator new(size * sizeof(value_type)));
443 sd.offsets = new difference_type[num_threads - 1];
444 sd.pieces = new std::vector<Piece<difference_type> >[num_threads];
445 for (int s = 0; s < num_threads; ++s)
446 sd.pieces[s].resize(num_threads);
447 starts = sd.starts = new difference_type[num_threads + 1];
449 difference_type chunk_length = n / num_threads;
450 difference_type split = n % num_threads;
451 difference_type pos = 0;
452 for (int i = 0; i < num_threads; ++i)
455 pos += (i < split) ? (chunk_length + 1) : chunk_length;
457 starts[num_threads] = pos;
460 // Now sort in parallel.
461 parallel_sort_mwms_pu<stable, exact>(&sd, comp);
465 delete[] sd.temporary;
468 ::operator delete(sd.samples);
473 } //namespace __gnu_parallel
475 #endif /* _GLIBCXX_PARALLEL_MULTIWAY_MERGESORT_H */