Skip to content

Commit 8cc0b98

Browse files
f3schclaude
authored andcommitted
GPUCommonAlgorithm: partitionPoint
Device code has no std::partition_point, and kernels write the binary search by hand (e.g. the last ROF at or before a cluster in ITS/tracking/GPU/cuda/TrackingKernels.cu). Add GPUCommonAlgorithm::partitionPoint(first, count, pred), std::partition_point over the indices [first, first + count), callable from host and device code; lower and upper bounds are its predicates key(i) < value and !(value < key(i)). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Felix Schlepper <felix.schlepper@cern.ch>
1 parent 670d1f7 commit 8cc0b98

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

‎GPU/Common/GPUCommonAlgorithm.h‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class GPUCommonAlgorithm
4949
#endif
5050
template <class T>
5151
GPUd() static void swap(T& a, T& b);
52+
template <class I, class P>
53+
GPUhd() static I partitionPoint(I first, I count, const P& pred);
5254

5355
private:
5456
template <typename I>
@@ -330,6 +332,24 @@ GPUdi() void GPUCommonAlgorithm::swap(T& a, T& b)
330332
}
331333
#endif
332334

335+
// std::partition_point over the indices [first, first + count): the first index for which pred is false, pred being true on a prefix.
336+
// A lower bound of value in a sorted key(i) is pred(i) = key(i) < value, an upper bound pred(i) = !(value < key(i)).
337+
template <class I, class P>
338+
GPUhdi() I GPUCommonAlgorithm::partitionPoint(I first, I count, const P& pred)
339+
{
340+
while (count > 0) {
341+
const I step = count / 2;
342+
const I middle = first + step;
343+
if (pred(middle)) {
344+
first = middle + 1;
345+
count -= step + 1;
346+
} else {
347+
count = step;
348+
}
349+
}
350+
return first;
351+
}
352+
333353
} // namespace o2::gpu
334354

335355
// ----------------------------- WORK GROUP FUNCTIONS -----------------------------

0 commit comments

Comments
 (0)