OSDN Git Service

2007-11-28 Johannes Singler <singler@ira.uka.de>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / parallel / quicksort.h
1 // -*- C++ -*-
2
3 // Copyright (C) 2007 Free Software Foundation, Inc.
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
8 // Foundation; either version 2, or (at your option) any later
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
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.
20
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
29 // Public License.
30
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.
34  */
35
36 // Written by Johannes Singler.
37
38 #ifndef _GLIBCXX_PARALLEL_QUICKSORT_H
39 #define _GLIBCXX_PARALLEL_QUICKSORT_H 1
40
41 #include <parallel/parallel.h>
42 #include <parallel/partition.h>
43
44 namespace __gnu_parallel
45 {
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
53    *  this part.
54    */
55   template<typename RandomAccessIterator, typename Comparator>
56   inline
57   typename std::iterator_traits<RandomAccessIterator>::difference_type
58   parallel_sort_qs_divide(
59       RandomAccessIterator begin,
60       RandomAccessIterator end,
61       Comparator comp,
62       typename std::iterator_traits<RandomAccessIterator>::difference_type
63           pivot_rank,
64       typename std::iterator_traits<RandomAccessIterator>::difference_type
65           num_samples,
66       thread_index_t num_threads)
67   {
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;
71
72     difference_type n = end - begin;
73     num_samples = std::min(num_samples, n);
74
75     // Allocate uninitialized, to avoid default constructor.
76     value_type* samples = static_cast<value_type*>(
77       ::operator new(num_samples * sizeof(value_type)));
78
79     for (difference_type s = 0; s < num_samples; ++s)
80       {
81         const unsigned long long index = static_cast<unsigned long long>(s)
82                         * n / num_samples;
83         new(&(samples[s])) value_type(begin[index]);
84       }
85
86     __gnu_sequential::sort(samples, samples + num_samples, comp);
87
88     value_type& pivot = samples[pivot_rank * num_samples / n];
89
90     __gnu_parallel::binder2nd<Comparator, value_type, value_type, bool>
91         pred(comp, pivot);
92     difference_type split = parallel_partition(begin, end, pred, num_threads);
93
94     delete[] samples;
95
96     return split;
97   }
98
99   /** @brief Unbalanced quicksort conquer step.
100    *  @param begin Begin iterator of subsequence.
101    *  @param end End iterator of subsequence.
102    *  @param comp Comparator.
103    *  @param num_threads Number of threads that are allowed to work on
104    *  this part.
105    */
106   template<typename RandomAccessIterator, typename Comparator>
107   inline void
108   parallel_sort_qs_conquer(RandomAccessIterator begin,
109                            RandomAccessIterator end,
110                            Comparator comp,
111                            thread_index_t num_threads)
112   {
113     typedef std::iterator_traits<RandomAccessIterator> traits_type;
114     typedef typename traits_type::value_type value_type;
115     typedef typename traits_type::difference_type difference_type;
116
117     if (num_threads <= 1)
118       {
119         __gnu_sequential::sort(begin, end, comp);
120         return;
121       }
122
123     difference_type n = end - begin, pivot_rank;
124
125     if (n <= 1)
126       return;
127
128     thread_index_t num_threads_left;
129
130     if ((num_threads % 2) == 1)
131       num_threads_left = num_threads / 2 + 1;
132     else
133       num_threads_left = num_threads / 2;
134
135     pivot_rank = n * num_threads_left / num_threads;
136
137     difference_type split = parallel_sort_qs_divide(
138         begin, end, comp, pivot_rank,
139         Settings::sort_qs_num_samples_preset, num_threads);
140
141 #pragma omp parallel sections
142     {
143 #pragma omp section
144       parallel_sort_qs_conquer(begin, begin + split,
145                                comp, num_threads_left);
146 #pragma omp section
147       parallel_sort_qs_conquer(begin + split, end,
148                                comp, num_threads - num_threads_left);
149     }
150   }
151
152
153
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 n Length of input sequence.
159    *  @param num_threads Number of threads that are allowed to work on
160    *  this part.
161    */
162   template<typename RandomAccessIterator, typename Comparator>
163   inline void
164   parallel_sort_qs(
165       RandomAccessIterator begin,
166       RandomAccessIterator end,
167       Comparator comp,
168       typename std::iterator_traits<RandomAccessIterator>::difference_type n,
169       int num_threads)
170   {
171     _GLIBCXX_CALL(n)
172
173     typedef std::iterator_traits<RandomAccessIterator> traits_type;
174     typedef typename traits_type::value_type value_type;
175     typedef typename traits_type::difference_type difference_type;
176
177     if (n == 0)
178       return;
179
180     // At least one element per processor.
181     if (num_threads > n)
182       num_threads = static_cast<thread_index_t>(n);
183
184     Settings::sort_qs_num_samples_preset = 100;
185
186     // Hard to avoid.
187     omp_set_num_threads(num_threads);
188
189     parallel_sort_qs_conquer(begin, begin + n, comp, num_threads);
190   }
191
192 } //namespace __gnu_parallel
193
194 #endif