3 // Copyright (C) 2007 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 2, 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 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
31 /** @file parallel/quicksort.h
32 * @brief Implementation of a unbalanced parallel quicksort (in-place).
33 * This file is a GNU parallel extension to the Standard C++ Library.
36 // Written by Johannes Singler.
38 #ifndef _GLIBCXX_PARALLEL_QUICKSORT_H
39 #define _GLIBCXX_PARALLEL_QUICKSORT_H 1
41 #include <parallel/parallel.h>
42 #include <parallel/partition.h>
44 namespace __gnu_parallel
46 /** @brief Unbalanced quicksort divide step.
47 * @param begin Begin iterator of subsequence.
48 * @param end End iterator of subsequence.
49 * @param comp Comparator.
50 * @param pivot_rank Desired rank of the pivot.
51 * @param num_samples Choose pivot from that many samples.
52 * @param num_threads Number of threads that are allowed to work on
55 template<typename RandomAccessIterator
, typename Comparator
>
57 typename
std::iterator_traits
<RandomAccessIterator
>::difference_type
58 parallel_sort_qs_divide(
59 RandomAccessIterator begin
,
60 RandomAccessIterator end
,
62 typename
std::iterator_traits
<RandomAccessIterator
>::difference_type
64 typename
std::iterator_traits
<RandomAccessIterator
>::difference_type
66 thread_index_t num_threads
)
68 typedef std::iterator_traits
<RandomAccessIterator
> traits_type
;
69 typedef typename
traits_type::value_type value_type
;
70 typedef typename
traits_type::difference_type difference_type
;
72 difference_type n
= end
- begin
;
73 num_samples
= std::min(num_samples
, n
);
75 // Allocate uninitialized, to avoid default constructor.
76 value_type
* samples
= static_cast<value_type
*>(
77 operator new(num_samples
* sizeof(value_type
)));
79 for (difference_type s
= 0; s
< num_samples
; s
++)
81 const unsigned long long index
= static_cast<unsigned long long>(s
)
83 new(samples
+ s
) value_type(begin
[index
]);
86 __gnu_sequential::sort(samples
, samples
+ num_samples
, comp
);
88 value_type
& pivot
= samples
[pivot_rank
* num_samples
/ n
];
90 __gnu_parallel::binder2nd
<Comparator
, value_type
, value_type
, bool>
92 difference_type split
= parallel_partition(begin
, end
, pred
, num_threads
);
97 /** @brief Unbalanced quicksort conquer step.
98 * @param begin Begin iterator of subsequence.
99 * @param end End iterator of subsequence.
100 * @param comp Comparator.
101 * @param num_threads Number of threads that are allowed to work on
104 template<typename RandomAccessIterator
, typename Comparator
>
106 parallel_sort_qs_conquer(RandomAccessIterator begin
,
107 RandomAccessIterator end
,
109 thread_index_t num_threads
)
111 typedef std::iterator_traits
<RandomAccessIterator
> traits_type
;
112 typedef typename
traits_type::value_type value_type
;
113 typedef typename
traits_type::difference_type difference_type
;
115 if (num_threads
<= 1)
117 __gnu_sequential::sort(begin
, end
, comp
);
121 difference_type n
= end
- begin
, pivot_rank
;
126 thread_index_t num_threads_left
;
128 if ((num_threads
% 2) == 1)
129 num_threads_left
= num_threads
/ 2 + 1;
131 num_threads_left
= num_threads
/ 2;
133 pivot_rank
= n
* num_threads_left
/ num_threads
;
135 difference_type split
= parallel_sort_qs_divide(
136 begin
, end
, comp
, pivot_rank
,
137 Settings::sort_qs_num_samples_preset
, num_threads
);
139 #pragma omp parallel sections
142 parallel_sort_qs_conquer(begin
, begin
+ split
,
143 comp
, num_threads_left
);
145 parallel_sort_qs_conquer(begin
+ split
, end
,
146 comp
, num_threads
- num_threads_left
);
152 /** @brief Unbalanced quicksort main call.
153 * @param begin Begin iterator of input sequence.
154 * @param end End iterator input sequence, ignored.
155 * @param comp Comparator.
156 * @param n Length of input sequence.
157 * @param num_threads Number of threads that are allowed to work on
160 template<typename RandomAccessIterator
, typename Comparator
>
163 RandomAccessIterator begin
,
164 RandomAccessIterator end
,
166 typename
std::iterator_traits
<RandomAccessIterator
>::difference_type n
,
171 typedef std::iterator_traits
<RandomAccessIterator
> traits_type
;
172 typedef typename
traits_type::value_type value_type
;
173 typedef typename
traits_type::difference_type difference_type
;
178 // At least one element per processor.
180 num_threads
= static_cast<thread_index_t
>(n
);
182 Settings::sort_qs_num_samples_preset
= 100;
185 omp_set_num_threads(num_threads
);
187 parallel_sort_qs_conquer(begin
, begin
+ n
, comp
, num_threads
);
190 } //namespace __gnu_parallel