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/multiseq_selection.h
32 * @brief Functions to find elements of a certain global rank in
33 * multiple sorted sequences. Also serves for splitting such
36 * The algorithm description can be found in
38 * P. J. Varman, S. D. Scheufler, B. R. Iyer, and G. R. Ricard.
39 * Merging Multiple Lists on Hierarchical-Memory Multiprocessors.
40 * Journal of Parallel and Distributed Computing, 12(2):171–177, 1991.
42 * This file is a GNU parallel extension to the Standard C++ Library.
45 // Written by Johannes Singler.
47 #ifndef _GLIBCXX_PARALLEL_MULTISEQ_SELECTION_H
48 #define _GLIBCXX_PARALLEL_MULTISEQ_SELECTION_H 1
53 #include <bits/stl_algo.h>
55 #include <parallel/sort.h>
57 namespace __gnu_parallel
59 /** @brief Compare a pair of types lexicographically, ascending. */
60 template<typename T1
, typename T2
, typename Comparator
>
62 : public std::binary_function
<std::pair
<T1
, T2
>, std::pair
<T1
, T2
>, bool>
68 lexicographic(Comparator
& _comp
) : comp(_comp
) { }
71 operator()(const std::pair
<T1
, T2
>& p1
,
72 const std::pair
<T1
, T2
>& p2
) const
74 if (comp(p1
.first
, p2
.first
))
77 if (comp(p2
.first
, p1
.first
))
81 return p1
.second
< p2
.second
;
85 /** @brief Compare a pair of types lexicographically, descending. */
86 template<typename T1
, typename T2
, typename Comparator
>
87 class lexicographic_reverse
: public std::binary_function
<T1
, T2
, bool>
93 lexicographic_reverse(Comparator
& _comp
) : comp(_comp
) { }
96 operator()(const std::pair
<T1
, T2
>& p1
,
97 const std::pair
<T1
, T2
>& p2
) const
99 if (comp(p2
.first
, p1
.first
))
102 if (comp(p1
.first
, p2
.first
))
106 return p2
.second
< p1
.second
;
111 * @brief Splits several sorted sequences at a certain global rank,
112 * resulting in a splitting point for each sequence.
113 * The sequences are passed via a sequence of random-access
114 * iterator pairs, none of the sequences may be empty. If there
115 * are several equal elements across the split, the ones on the
116 * left side will be chosen from sequences with smaller number.
117 * @param begin_seqs Begin of the sequence of iterator pairs.
118 * @param end_seqs End of the sequence of iterator pairs.
119 * @param rank The global rank to partition at.
120 * @param begin_offsets A random-access sequence begin where the
121 * result will be stored in. Each element of the sequence is an
122 * iterator that points to the first element on the greater part of
123 * the respective sequence.
124 * @param comp The ordering functor, defaults to std::less<T>.
126 template<typename RanSeqs
, typename RankType
, typename RankIterator
,
129 multiseq_partition(RanSeqs begin_seqs
, RanSeqs end_seqs
,
131 RankIterator begin_offsets
,
132 Comparator comp
= std::less
<
133 typename
std::iterator_traits
<typename
134 std::iterator_traits
<RanSeqs
>::value_type::
135 first_type
>::value_type
>()) // std::less<T>
137 _GLIBCXX_CALL(end_seqs
- begin_seqs
)
139 typedef typename
std::iterator_traits
<RanSeqs
>::value_type::first_type
141 typedef typename
std::iterator_traits
<It
>::difference_type
143 typedef typename
std::iterator_traits
<It
>::value_type value_type
;
145 lexicographic
<value_type
, int, Comparator
> lcomp(comp
);
146 lexicographic_reverse
<value_type
, int, Comparator
> lrcomp(comp
);
148 // Number of sequences, number of elements in total (possibly
149 // including padding).
150 difference_type m
= std::distance(begin_seqs
, end_seqs
), N
= 0,
153 for (int i
= 0; i
< m
; i
++)
154 N
+= std::distance(begin_seqs
[i
].first
, begin_seqs
[i
].second
);
158 for (int i
= 0; i
< m
; i
++)
159 begin_offsets
[i
] = begin_seqs
[i
].second
; // Very end.
163 _GLIBCXX_PARALLEL_ASSERT(m
!= 0 && N
!= 0 && rank
>= 0 && rank
< N
);
165 difference_type
* ns
= new difference_type
[m
];
166 difference_type
* a
= new difference_type
[m
];
167 difference_type
* b
= new difference_type
[m
];
170 ns
[0] = std::distance(begin_seqs
[0].first
, begin_seqs
[0].second
);
172 for (int i
= 0; i
< m
; i
++)
174 ns
[i
] = std::distance(begin_seqs
[i
].first
, begin_seqs
[i
].second
);
175 nmax
= std::max(nmax
, ns
[i
]);
180 // Pad all lists to this length, at least as long as any ns[i],
181 // equality iff nmax = 2^k - 1.
184 // From now on, including padding.
187 for (int i
= 0; i
< m
; i
++)
195 // 0 <= a[i] <= ns[i], 0 <= b[i] <= l
197 #define S(i) (begin_seqs[i].first)
199 // Initial partition.
200 std::vector
<std::pair
<value_type
, int> > sample
;
202 for (int i
= 0; i
< m
; i
++)
203 if (n
< ns
[i
]) //sequence long enough
204 sample
.push_back(std::make_pair(S(i
)[n
], i
));
205 __gnu_sequential::sort(sample
.begin(), sample
.end(), lcomp
);
207 for (int i
= 0; i
< m
; i
++) //conceptual infinity
208 if (n
>= ns
[i
]) //sequence too short, conceptual infinity
209 sample
.push_back(std::make_pair(S(i
)[0] /*dummy element*/, i
));
211 difference_type localrank
= rank
* m
/ N
;
214 for (j
= 0; j
< localrank
&& ((n
+ 1) <= ns
[sample
[j
].second
]); ++j
)
215 a
[sample
[j
].second
] += n
+ 1;
217 b
[sample
[j
].second
] -= n
+ 1;
219 // Further refinement.
224 int lmax_seq
= -1; // to avoid warning
225 const value_type
* lmax
= NULL
; // impossible to avoid the warning?
226 for (int i
= 0; i
< m
; i
++)
232 lmax
= &(S(i
)[a
[i
] - 1]);
237 // Max, favor rear sequences.
238 if (!comp(S(i
)[a
[i
] - 1], *lmax
))
240 lmax
= &(S(i
)[a
[i
] - 1]);
248 for (i
= 0; i
< m
; i
++)
250 difference_type middle
= (b
[i
] + a
[i
]) / 2;
251 if (lmax
&& middle
< ns
[i
] &&
252 lcomp(std::make_pair(S(i
)[middle
], i
),
253 std::make_pair(*lmax
, lmax_seq
)))
254 a
[i
] = std::min(a
[i
] + n
+ 1, ns
[i
]);
259 difference_type leftsize
= 0, total
= 0;
260 for (int i
= 0; i
< m
; i
++)
262 leftsize
+= a
[i
] / (n
+ 1);
263 total
+= l
/ (n
+ 1);
266 difference_type skew
= static_cast<difference_type
>
267 (static_cast<uint64_t>(total
) * rank
/ N
- leftsize
);
271 // Move to the left, find smallest.
272 std::priority_queue
<std::pair
<value_type
, int>,
273 std::vector
<std::pair
<value_type
, int> >,
274 lexicographic_reverse
<value_type
, int, Comparator
> >
277 for (int i
= 0; i
< m
; i
++)
279 pq
.push(std::make_pair(S(i
)[b
[i
]], i
));
281 for (; skew
!= 0 && !pq
.empty(); --skew
)
283 int source
= pq
.top().second
;
286 a
[source
] = std::min(a
[source
] + n
+ 1, ns
[source
]);
289 if (b
[source
] < ns
[source
])
290 pq
.push(std::make_pair(S(source
)[b
[source
]], source
));
295 // Move to the right, find greatest.
296 std::priority_queue
<std::pair
<value_type
, int>,
297 std::vector
<std::pair
<value_type
, int> >,
298 lexicographic
<value_type
, int, Comparator
> > pq(lcomp
);
300 for (int i
= 0; i
< m
; i
++)
302 pq
.push(std::make_pair(S(i
)[a
[i
] - 1], i
));
304 for (; skew
!= 0; ++skew
)
306 int source
= pq
.top().second
;
313 pq
.push(std::make_pair(S(source
)[a
[source
] - 1], source
));
319 // a[i] == b[i] in most cases, except when a[i] has been clamped
320 // because of having reached the boundary
322 // Now return the result, calculate the offset.
324 // Compare the keys on both edges of the border.
326 // Maximum of left edge, minimum of right edge.
327 value_type
* maxleft
= NULL
;
328 value_type
* minright
= NULL
;
329 for (int i
= 0; i
< m
; i
++)
334 maxleft
= &(S(i
)[a
[i
] - 1]);
337 // Max, favor rear sequences.
338 if (!comp(S(i
)[a
[i
] - 1], *maxleft
))
339 maxleft
= &(S(i
)[a
[i
] - 1]);
345 minright
= &(S(i
)[b
[i
]]);
348 // Min, favor fore sequences.
349 if (comp(S(i
)[b
[i
]], *minright
))
350 minright
= &(S(i
)[b
[i
]]);
356 for (int i
= 0; i
< m
; i
++)
357 begin_offsets
[i
] = S(i
) + a
[i
];
366 * @brief Selects the element at a certain global rank from several
369 * The sequences are passed via a sequence of random-access
370 * iterator pairs, none of the sequences may be empty.
371 * @param begin_seqs Begin of the sequence of iterator pairs.
372 * @param end_seqs End of the sequence of iterator pairs.
373 * @param rank The global rank to partition at.
374 * @param offset The rank of the selected element in the global
375 * subsequence of elements equal to the selected element. If the
376 * selected element is unique, this number is 0.
377 * @param comp The ordering functor, defaults to std::less.
379 template<typename T
, typename RanSeqs
, typename RankType
,
382 multiseq_selection(RanSeqs begin_seqs
, RanSeqs end_seqs
, RankType rank
,
383 RankType
& offset
, Comparator comp
= std::less
<T
>())
385 _GLIBCXX_CALL(end_seqs
- begin_seqs
)
387 typedef typename
std::iterator_traits
<RanSeqs
>::value_type::first_type
389 typedef typename
std::iterator_traits
<It
>::difference_type
392 lexicographic
<T
, int, Comparator
> lcomp(comp
);
393 lexicographic_reverse
<T
, int, Comparator
> lrcomp(comp
);
395 // Number of sequences, number of elements in total (possibly
396 // including padding).
397 difference_type m
= std::distance(begin_seqs
, end_seqs
);
398 difference_type N
= 0;
399 difference_type nmax
, n
, r
;
401 for (int i
= 0; i
< m
; i
++)
402 N
+= std::distance(begin_seqs
[i
].first
, begin_seqs
[i
].second
);
404 if (m
== 0 || N
== 0 || rank
< 0 || rank
>= N
)
406 // Result undefined when there is no data or rank is outside bounds.
407 throw std::exception();
411 difference_type
* ns
= new difference_type
[m
];
412 difference_type
* a
= new difference_type
[m
];
413 difference_type
* b
= new difference_type
[m
];
416 ns
[0] = std::distance(begin_seqs
[0].first
, begin_seqs
[0].second
);
418 for (int i
= 0; i
< m
; ++i
)
420 ns
[i
] = std::distance(begin_seqs
[i
].first
, begin_seqs
[i
].second
);
421 nmax
= std::max(nmax
, ns
[i
]);
426 // Pad all lists to this length, at least as long as any ns[i],
427 // equality iff nmax = 2^k - 1
430 // From now on, including padding.
433 for (int i
= 0; i
< m
; ++i
)
441 // 0 <= a[i] <= ns[i], 0 <= b[i] <= l
443 #define S(i) (begin_seqs[i].first)
445 // Initial partition.
446 std::vector
<std::pair
<T
, int> > sample
;
448 for (int i
= 0; i
< m
; i
++)
450 sample
.push_back(std::make_pair(S(i
)[n
], i
));
451 __gnu_sequential::sort(sample
.begin(), sample
.end(),
452 lcomp
, sequential_tag());
454 // Conceptual infinity.
455 for (int i
= 0; i
< m
; i
++)
457 sample
.push_back(std::make_pair(S(i
)[0] /*dummy element*/, i
));
459 difference_type localrank
= rank
* m
/ N
;
462 for (j
= 0; j
< localrank
&& ((n
+ 1) <= ns
[sample
[j
].second
]); ++j
)
463 a
[sample
[j
].second
] += n
+ 1;
465 b
[sample
[j
].second
] -= n
+ 1;
467 // Further refinement.
472 const T
* lmax
= NULL
;
473 for (int i
= 0; i
< m
; ++i
)
478 lmax
= &(S(i
)[a
[i
] - 1]);
481 if (comp(*lmax
, S(i
)[a
[i
] - 1])) //max
482 lmax
= &(S(i
)[a
[i
] - 1]);
488 for (i
= 0; i
< m
; i
++)
490 difference_type middle
= (b
[i
] + a
[i
]) / 2;
491 if (lmax
&& middle
< ns
[i
] && comp(S(i
)[middle
], *lmax
))
492 a
[i
] = std::min(a
[i
] + n
+ 1, ns
[i
]);
497 difference_type leftsize
= 0, total
= 0;
498 for (int i
= 0; i
< m
; ++i
)
500 leftsize
+= a
[i
] / (n
+ 1);
501 total
+= l
/ (n
+ 1);
504 difference_type skew
= ((unsigned long long)total
* rank
/ N
509 // Move to the left, find smallest.
510 std::priority_queue
<std::pair
<T
, int>,
511 std::vector
<std::pair
<T
, int> >,
512 lexicographic_reverse
<T
, int, Comparator
> > pq(lrcomp
);
514 for (int i
= 0; i
< m
; ++i
)
516 pq
.push(std::make_pair(S(i
)[b
[i
]], i
));
518 for (; skew
!= 0 && !pq
.empty(); --skew
)
520 int source
= pq
.top().second
;
523 a
[source
] = std::min(a
[source
] + n
+ 1, ns
[source
]);
526 if (b
[source
] < ns
[source
])
527 pq
.push(std::make_pair(S(source
)[b
[source
]], source
));
532 // Move to the right, find greatest.
533 std::priority_queue
<std::pair
<T
, int>,
534 std::vector
<std::pair
<T
, int> >,
535 lexicographic
<T
, int, Comparator
> > pq(lcomp
);
537 for (int i
= 0; i
< m
; ++i
)
539 pq
.push(std::make_pair(S(i
)[a
[i
] - 1], i
));
541 for (; skew
!= 0; ++skew
)
543 int source
= pq
.top().second
;
550 pq
.push(std::make_pair(S(source
)[a
[source
] - 1], source
));
556 // a[i] == b[i] in most cases, except when a[i] has been clamped
557 // because of having reached the boundary
559 // Now return the result, calculate the offset.
561 // Compare the keys on both edges of the border.
563 // Maximum of left edge, minimum of right edge.
564 bool maxleftset
= false, minrightset
= false;
566 // Impossible to avoid the warning?
568 for (int i
= 0; i
< m
; ++i
)
574 maxleft
= S(i
)[a
[i
] - 1];
580 if (comp(maxleft
, S(i
)[a
[i
] - 1]))
581 maxleft
= S(i
)[a
[i
] - 1];
588 minright
= S(i
)[b
[i
]];
594 if (comp(S(i
)[b
[i
]], minright
))
595 minright
= S(i
)[b
[i
]];
600 // Minright is the splitter, in any case.
602 if (!maxleftset
|| comp(minright
, maxleft
))
604 // Good luck, everything is split unambiguously.
609 // We have to calculate an offset.
612 for (int i
= 0; i
< m
; ++i
)
614 difference_type lb
= std::lower_bound(S(i
), S(i
) + ns
[i
],