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
) { }
72 operator()(const std::pair
<T1
, T2
>& p1
,
73 const std::pair
<T1
, T2
>& p2
) const
75 if (comp(p1
.first
, p2
.first
))
78 if (comp(p2
.first
, p1
.first
))
82 return p1
.second
< p2
.second
;
86 /** @brief Compare a pair of types lexicographically, descending. */
87 template<typename T1
, typename T2
, typename Comparator
>
88 class lexicographic_reverse
: public std::binary_function
<T1
, T2
, bool>
94 lexicographic_reverse(Comparator
& _comp
) : comp(_comp
) { }
97 operator()(const std::pair
<T1
, T2
>& p1
,
98 const std::pair
<T1
, T2
>& p2
) const
100 if (comp(p2
.first
, p1
.first
))
103 if (comp(p1
.first
, p2
.first
))
107 return p2
.second
< p1
.second
;
112 * @brief Splits several sorted sequences at a certain global rank,
113 * resulting in a splitting point for each sequence.
114 * The sequences are passed via a sequence of random-access
115 * iterator pairs, none of the sequences may be empty. If there
116 * are several equal elements across the split, the ones on the
117 * left side will be chosen from sequences with smaller number.
118 * @param begin_seqs Begin of the sequence of iterator pairs.
119 * @param end_seqs End of the sequence of iterator pairs.
120 * @param rank The global rank to partition at.
121 * @param begin_offsets A random-access sequence begin where the
122 * result will be stored in. Each element of the sequence is an
123 * iterator that points to the first element on the greater part of
124 * the respective sequence.
125 * @param comp The ordering functor, defaults to std::less<T>.
127 template<typename RanSeqs
, typename RankType
, typename RankIterator
,
130 multiseq_partition(RanSeqs begin_seqs
, RanSeqs end_seqs
,
132 RankIterator begin_offsets
,
133 Comparator comp
= std::less
<
134 typename
std::iterator_traits
<typename
135 std::iterator_traits
<RanSeqs
>::value_type::
136 first_type
>::value_type
>()) // std::less<T>
138 _GLIBCXX_CALL(end_seqs
- begin_seqs
)
140 typedef typename
std::iterator_traits
<RanSeqs
>::value_type::first_type
142 typedef typename
std::iterator_traits
<It
>::difference_type
144 typedef typename
std::iterator_traits
<It
>::value_type value_type
;
146 lexicographic
<value_type
, int, Comparator
> lcomp(comp
);
147 lexicographic_reverse
<value_type
, int, Comparator
> lrcomp(comp
);
149 // Number of sequences, number of elements in total (possibly
150 // including padding).
151 difference_type m
= std::distance(begin_seqs
, end_seqs
), N
= 0,
154 for (int i
= 0; i
< m
; i
++)
155 N
+= std::distance(begin_seqs
[i
].first
, begin_seqs
[i
].second
);
159 for (int i
= 0; i
< m
; i
++)
160 begin_offsets
[i
] = begin_seqs
[i
].second
; // Very end.
164 _GLIBCXX_PARALLEL_ASSERT(m
!= 0 && N
!= 0 && rank
>= 0 && rank
< N
);
166 difference_type
* ns
= new difference_type
[m
];
167 difference_type
* a
= new difference_type
[m
];
168 difference_type
* b
= new difference_type
[m
];
171 ns
[0] = std::distance(begin_seqs
[0].first
, begin_seqs
[0].second
);
173 for (int i
= 0; i
< m
; i
++)
175 ns
[i
] = std::distance(begin_seqs
[i
].first
, begin_seqs
[i
].second
);
176 nmax
= std::max(nmax
, ns
[i
]);
181 // Pad all lists to this length, at least as long as any ns[i],
182 // equality iff nmax = 2^k - 1.
185 // From now on, including padding.
188 for (int i
= 0; i
< m
; i
++)
196 // 0 <= a[i] <= ns[i], 0 <= b[i] <= l
198 #define S(i) (begin_seqs[i].first)
200 // Initial partition.
201 std::vector
<std::pair
<value_type
, int> > sample
;
203 for (int i
= 0; i
< m
; i
++)
204 if (n
< ns
[i
]) //sequence long enough
205 sample
.push_back(std::make_pair(S(i
)[n
], i
));
206 __gnu_sequential::sort(sample
.begin(), sample
.end(), lcomp
);
208 for (int i
= 0; i
< m
; i
++) //conceptual infinity
209 if (n
>= ns
[i
]) //sequence too short, conceptual infinity
210 sample
.push_back(std::make_pair(S(i
)[0] /*dummy element*/, i
));
212 difference_type localrank
= rank
* m
/ N
;
215 for (j
= 0; j
< localrank
&& ((n
+ 1) <= ns
[sample
[j
].second
]); ++j
)
216 a
[sample
[j
].second
] += n
+ 1;
218 b
[sample
[j
].second
] -= n
+ 1;
220 // Further refinement.
225 int lmax_seq
= -1; // to avoid warning
226 const value_type
* lmax
= NULL
; // impossible to avoid the warning?
227 for (int i
= 0; i
< m
; i
++)
233 lmax
= &(S(i
)[a
[i
] - 1]);
238 // Max, favor rear sequences.
239 if (!comp(S(i
)[a
[i
] - 1], *lmax
))
241 lmax
= &(S(i
)[a
[i
] - 1]);
249 for (i
= 0; i
< m
; i
++)
251 difference_type middle
= (b
[i
] + a
[i
]) / 2;
252 if (lmax
&& middle
< ns
[i
] &&
253 lcomp(std::make_pair(S(i
)[middle
], i
),
254 std::make_pair(*lmax
, lmax_seq
)))
255 a
[i
] = std::min(a
[i
] + n
+ 1, ns
[i
]);
260 difference_type leftsize
= 0, total
= 0;
261 for (int i
= 0; i
< m
; i
++)
263 leftsize
+= a
[i
] / (n
+ 1);
264 total
+= l
/ (n
+ 1);
267 difference_type skew
= static_cast<difference_type
>
268 (static_cast<uint64
>(total
) * rank
/ N
- leftsize
);
272 // Move to the left, find smallest.
273 std::priority_queue
<std::pair
<value_type
, int>,
274 std::vector
<std::pair
<value_type
, int> >,
275 lexicographic_reverse
<value_type
, int, Comparator
> >
278 for (int i
= 0; i
< m
; i
++)
280 pq
.push(std::make_pair(S(i
)[b
[i
]], i
));
282 for (; skew
!= 0 && !pq
.empty(); --skew
)
284 int source
= pq
.top().second
;
287 a
[source
] = std::min(a
[source
] + n
+ 1, ns
[source
]);
290 if (b
[source
] < ns
[source
])
291 pq
.push(std::make_pair(S(source
)[b
[source
]], source
));
296 // Move to the right, find greatest.
297 std::priority_queue
<std::pair
<value_type
, int>,
298 std::vector
<std::pair
<value_type
, int> >,
299 lexicographic
<value_type
, int, Comparator
> > pq(lcomp
);
301 for (int i
= 0; i
< m
; i
++)
303 pq
.push(std::make_pair(S(i
)[a
[i
] - 1], i
));
305 for (; skew
!= 0; ++skew
)
307 int source
= pq
.top().second
;
314 pq
.push(std::make_pair(S(source
)[a
[source
] - 1], source
));
320 // a[i] == b[i] in most cases, except when a[i] has been clamped
321 // because of having reached the boundary
323 // Now return the result, calculate the offset.
325 // Compare the keys on both edges of the border.
327 // Maximum of left edge, minimum of right edge.
328 value_type
* maxleft
= NULL
;
329 value_type
* minright
= NULL
;
330 for (int i
= 0; i
< m
; i
++)
335 maxleft
= &(S(i
)[a
[i
] - 1]);
338 // Max, favor rear sequences.
339 if (!comp(S(i
)[a
[i
] - 1], *maxleft
))
340 maxleft
= &(S(i
)[a
[i
] - 1]);
346 minright
= &(S(i
)[b
[i
]]);
349 // Min, favor fore sequences.
350 if (comp(S(i
)[b
[i
]], *minright
))
351 minright
= &(S(i
)[b
[i
]]);
357 for (int i
= 0; i
< m
; i
++)
358 begin_offsets
[i
] = S(i
) + a
[i
];
367 * @brief Selects the element at a certain global rank from several
370 * The sequences are passed via a sequence of random-access
371 * iterator pairs, none of the sequences may be empty.
372 * @param begin_seqs Begin of the sequence of iterator pairs.
373 * @param end_seqs End of the sequence of iterator pairs.
374 * @param rank The global rank to partition at.
375 * @param offset The rank of the selected element in the global
376 * subsequence of elements equal to the selected element. If the
377 * selected element is unique, this number is 0.
378 * @param comp The ordering functor, defaults to std::less.
380 template<typename T
, typename RanSeqs
, typename RankType
,
383 multiseq_selection(RanSeqs begin_seqs
, RanSeqs end_seqs
, RankType rank
,
384 RankType
& offset
, Comparator comp
= std::less
<T
>())
386 _GLIBCXX_CALL(end_seqs
- begin_seqs
)
388 typedef typename
std::iterator_traits
<RanSeqs
>::value_type::first_type
390 typedef typename
std::iterator_traits
<It
>::difference_type
393 lexicographic
<T
, int, Comparator
> lcomp(comp
);
394 lexicographic_reverse
<T
, int, Comparator
> lrcomp(comp
);
396 // Number of sequences, number of elements in total (possibly
397 // including padding).
398 difference_type m
= std::distance(begin_seqs
, end_seqs
);
399 difference_type N
= 0;
400 difference_type nmax
, n
, r
;
402 for (int i
= 0; i
< m
; i
++)
403 N
+= std::distance(begin_seqs
[i
].first
, begin_seqs
[i
].second
);
405 if (m
== 0 || N
== 0 || rank
< 0 || rank
>= N
)
407 // Result undefined when there is no data or rank is outside bounds.
408 throw std::exception();
412 difference_type
* ns
= new difference_type
[m
];
413 difference_type
* a
= new difference_type
[m
];
414 difference_type
* b
= new difference_type
[m
];
417 ns
[0] = std::distance(begin_seqs
[0].first
, begin_seqs
[0].second
);
419 for (int i
= 0; i
< m
; ++i
)
421 ns
[i
] = std::distance(begin_seqs
[i
].first
, begin_seqs
[i
].second
);
422 nmax
= std::max(nmax
, ns
[i
]);
427 // Pad all lists to this length, at least as long as any ns[i],
428 // equality iff nmax = 2^k - 1
431 // From now on, including padding.
434 for (int i
= 0; i
< m
; ++i
)
442 // 0 <= a[i] <= ns[i], 0 <= b[i] <= l
444 #define S(i) (begin_seqs[i].first)
446 // Initial partition.
447 std::vector
<std::pair
<T
, int> > sample
;
449 for (int i
= 0; i
< m
; i
++)
451 sample
.push_back(std::make_pair(S(i
)[n
], i
));
452 __gnu_sequential::sort(sample
.begin(), sample
.end(),
453 lcomp
, sequential_tag());
455 // Conceptual infinity.
456 for (int i
= 0; i
< m
; i
++)
458 sample
.push_back(std::make_pair(S(i
)[0] /*dummy element*/, i
));
460 difference_type localrank
= rank
* m
/ N
;
463 for (j
= 0; j
< localrank
&& ((n
+ 1) <= ns
[sample
[j
].second
]); ++j
)
464 a
[sample
[j
].second
] += n
+ 1;
466 b
[sample
[j
].second
] -= n
+ 1;
468 // Further refinement.
473 const T
* lmax
= NULL
;
474 for (int i
= 0; i
< m
; ++i
)
479 lmax
= &(S(i
)[a
[i
] - 1]);
482 if (comp(*lmax
, S(i
)[a
[i
] - 1])) //max
483 lmax
= &(S(i
)[a
[i
] - 1]);
489 for (i
= 0; i
< m
; i
++)
491 difference_type middle
= (b
[i
] + a
[i
]) / 2;
492 if (lmax
&& middle
< ns
[i
] && comp(S(i
)[middle
], *lmax
))
493 a
[i
] = std::min(a
[i
] + n
+ 1, ns
[i
]);
498 difference_type leftsize
= 0, total
= 0;
499 for (int i
= 0; i
< m
; ++i
)
501 leftsize
+= a
[i
] / (n
+ 1);
502 total
+= l
/ (n
+ 1);
505 difference_type skew
= ((unsigned long long)total
* rank
/ N
510 // Move to the left, find smallest.
511 std::priority_queue
<std::pair
<T
, int>,
512 std::vector
<std::pair
<T
, int> >,
513 lexicographic_reverse
<T
, int, Comparator
> > pq(lrcomp
);
515 for (int i
= 0; i
< m
; ++i
)
517 pq
.push(std::make_pair(S(i
)[b
[i
]], i
));
519 for (; skew
!= 0 && !pq
.empty(); --skew
)
521 int source
= pq
.top().second
;
524 a
[source
] = std::min(a
[source
] + n
+ 1, ns
[source
]);
527 if (b
[source
] < ns
[source
])
528 pq
.push(std::make_pair(S(source
)[b
[source
]], source
));
533 // Move to the right, find greatest.
534 std::priority_queue
<std::pair
<T
, int>,
535 std::vector
<std::pair
<T
, int> >,
536 lexicographic
<T
, int, Comparator
> > pq(lcomp
);
538 for (int i
= 0; i
< m
; ++i
)
540 pq
.push(std::make_pair(S(i
)[a
[i
] - 1], i
));
542 for (; skew
!= 0; ++skew
)
544 int source
= pq
.top().second
;
551 pq
.push(std::make_pair(S(source
)[a
[source
] - 1], source
));
557 // a[i] == b[i] in most cases, except when a[i] has been clamped
558 // because of having reached the boundary
560 // Now return the result, calculate the offset.
562 // Compare the keys on both edges of the border.
564 // Maximum of left edge, minimum of right edge.
565 bool maxleftset
= false, minrightset
= false;
567 // Impossible to avoid the warning?
569 for (int i
= 0; i
< m
; ++i
)
575 maxleft
= S(i
)[a
[i
] - 1];
581 if (comp(maxleft
, S(i
)[a
[i
] - 1]))
582 maxleft
= S(i
)[a
[i
] - 1];
589 minright
= S(i
)[b
[i
]];
595 if (comp(S(i
)[b
[i
]], minright
))
596 minright
= S(i
)[b
[i
]];
601 // Minright is the splitter, in any case.
603 if (!maxleftset
|| comp(minright
, maxleft
))
605 // Good luck, everything is split unambigiously.
610 // We have to calculate an offset.
613 for (int i
= 0; i
< m
; ++i
)
615 difference_type lb
= std::lower_bound(S(i
), S(i
) + ns
[i
],