3 // Copyright (C) 2007, 2008 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
>
56 typename
std::iterator_traits
<RandomAccessIterator
>::difference_type
57 parallel_sort_qs_divide(RandomAccessIterator begin
,
58 RandomAccessIterator end
,
59 Comparator comp
, typename
std::iterator_traits
60 <RandomAccessIterator
>::difference_type pivot_rank
,
61 typename
std::iterator_traits
62 <RandomAccessIterator
>::difference_type
63 num_samples
, thread_index_t num_threads
)
65 typedef std::iterator_traits
<RandomAccessIterator
> traits_type
;
66 typedef typename
traits_type::value_type value_type
;
67 typedef typename
traits_type::difference_type difference_type
;
69 difference_type n
= end
- begin
;
70 num_samples
= std::min(num_samples
, n
);
72 // Allocate uninitialized, to avoid default constructor.
74 static_cast<value_type
*>(::operator new(num_samples
75 * sizeof(value_type
)));
77 for (difference_type s
= 0; s
< num_samples
; ++s
)
79 const unsigned long long index
= static_cast<unsigned long long>(s
)
81 ::new(&(samples
[s
])) value_type(begin
[index
]);
84 __gnu_sequential::sort(samples
, samples
+ num_samples
, comp
);
86 value_type
& pivot
= samples
[pivot_rank
* num_samples
/ n
];
88 __gnu_parallel::binder2nd
<Comparator
, value_type
, value_type
, bool>
90 difference_type split
=
91 parallel_partition(begin
, end
, pred
, num_threads
);
93 ::operator delete(samples
);
98 /** @brief Unbalanced quicksort conquer step.
99 * @param begin Begin iterator of subsequence.
100 * @param end End iterator of subsequence.
101 * @param comp Comparator.
102 * @param num_threads Number of threads that are allowed to work on
105 template<typename RandomAccessIterator
, typename Comparator
>
107 parallel_sort_qs_conquer(RandomAccessIterator begin
,
108 RandomAccessIterator end
,
110 thread_index_t num_threads
)
112 typedef std::iterator_traits
<RandomAccessIterator
> traits_type
;
113 typedef typename
traits_type::value_type value_type
;
114 typedef typename
traits_type::difference_type difference_type
;
116 if (num_threads
<= 1)
118 __gnu_sequential::sort(begin
, end
, comp
);
122 difference_type n
= end
- begin
, pivot_rank
;
127 thread_index_t num_threads_left
;
129 if ((num_threads
% 2) == 1)
130 num_threads_left
= num_threads
/ 2 + 1;
132 num_threads_left
= num_threads
/ 2;
134 pivot_rank
= n
* num_threads_left
/ num_threads
;
136 difference_type split
=
137 parallel_sort_qs_divide(begin
, end
, comp
, pivot_rank
,
138 _Settings::get().sort_qs_num_samples_preset
,
141 #pragma omp parallel sections num_threads(2)
144 parallel_sort_qs_conquer(begin
, begin
+ split
,
145 comp
, num_threads_left
);
147 parallel_sort_qs_conquer(begin
+ split
, end
,
148 comp
, num_threads
- num_threads_left
);
154 /** @brief Unbalanced quicksort main call.
155 * @param begin Begin iterator of input sequence.
156 * @param end End iterator input sequence, ignored.
157 * @param comp Comparator.
158 * @param num_threads Number of threads that are allowed to work on
161 template<typename RandomAccessIterator
, typename Comparator
>
163 parallel_sort_qs(RandomAccessIterator begin
,
164 RandomAccessIterator end
,
166 thread_index_t num_threads
)
170 typedef std::iterator_traits
<RandomAccessIterator
> traits_type
;
171 typedef typename
traits_type::value_type value_type
;
172 typedef typename
traits_type::difference_type difference_type
;
174 difference_type n
= end
- begin
;
176 // At least one element per processor.
178 num_threads
= static_cast<thread_index_t
>(n
);
180 parallel_sort_qs_conquer(begin
, begin
+ n
, comp
, num_threads
);
183 } //namespace __gnu_parallel
185 #endif /* _GLIBCXX_PARALLEL_QUICKSORT_H */