[thrust] Single-pass is_partitioned via adjacent zip_iterator#8427
Open
edenfunf wants to merge 3 commits intoNVIDIA:mainfrom
Open
[thrust] Single-pass is_partitioned via adjacent zip_iterator#8427edenfunf wants to merge 3 commits intoNVIDIA:mainfrom
is_partitioned via adjacent zip_iterator#8427edenfunf wants to merge 3 commits intoNVIDIA:mainfrom
Conversation
The previous implementation required two kernel launches:
find_if_not to locate the partition boundary, then
find_if to check whether any true element follows it.
Replace this with a single find_if over adjacent element pairs
(a[i], a[i+1]) built with cuda::make_zip_iterator. The predicate
detects a "false → true" transition that violates the partitioning
invariant. Using zip_iterator::operator- (which returns the minimum
component distance) the synthetic range has n-1 elements, so only
one kernel launch is needed regardless of input.
Edge-case handling:
- Empty range: early return true (avoids computing first+1).
- n==1: zip distance is min(1,0)=0, find_if_n returns first_zip;
get<1>(first_zip) == last is true, which is correct.
Pattern mirrors cuda::std::is_partitioned in
libcudacxx/include/cuda/std/__pstl/is_partitioned.h.
Fixes NVIDIA#8085
Contributor
added 2 commits
April 15, 2026 19:42
When iterating over thrust::device_vector, dereferencing yields device_reference<T> rather than T. Applying a predicate to a device_reference<T> and then applying operator! to the result fails to compile: converting device_reference<T> to bool requires two user-defined conversions (device_reference → T → void*), which C++ forbids in a single implicit conversion sequence. Fix by wrapping each tuple element with thrust::raw_reference_cast before passing to the predicate, and assigning the result to an intermediate bool. This matches the pattern used elsewhere in Thrust (internal_functional.h, head_flags.h) for the same proxy- reference problem, and mirrors the intent of the PSTL reference implementation which stores pred results in const bool locals. Verified: all 14 tests pass on RTX 5070 / CUDA 12.9 (sm_89): thrust.test.is_partitioned 8/8 pass thrust.test.cuda.is_partitioned.cdp_0 3/3 pass thrust.test.cuda.is_partitioned.cdp_1 3/3 pass
Contributor
Author
|
@gevtushenko Hi, could you please take a look at this PR when you have time? This PR replaces the current two-pass CUDA implementation of Key points:
Benchmarks showed up to ~33% speedup compared to the previous implementation. I've verified:
Would really appreciate your feedback, thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Replaces the two-pass
thrust::is_partitionedCUDA implementation with a single-pass version that mirrorscuda::std::is_partitionedfrom the PSTL.Before:
ItemsIt boundary = cuda_cub::find_if_not(policy, first, last, predicate); ItemsIt end = cuda_cub::find_if(policy, boundary, last, predicate); return end == last;Two kernel launches in the fully-partitioned case (the common fast path).
After:
One kernel launch, scanning adjacent pairs for a "false → true" transition.
Why
thrust::is_partitionedwas benchmarked as significantly slower thancuda::std::is_partitioned(up to ~33 % slower forint8_tat large N), even though both check the same invariant. The root cause is the extra kernel launch:find_if_notscans the whole range to find the boundary.find_ifthen scans from the boundary to the end.The PSTL avoids this by zipping adjacent elements and scanning once for any
(!pred(a[i]) && pred(a[i+1]))pair. This PR ports that approach to the Thrust CUDA backend.Closes #8085.
How
__is_partitioned_fn<Predicate>functor (host+device callable) that takes acuda::zip_iteratorvalue and returnstrueon a partitioning violation.cuda::make_zip_iterator(first, first+1)/cuda::make_zip_iterator(last, last). Becausezip_iterator::operator-returns the minimum component distance, the synthetic range has exactlyn-1elements — the number of adjacent pairs.::cuda::std::get<1>(result.__iterators()) == last(notresult == last_zip) to handle then==1edge case correctly: whenn==1the zip distance is 0,find_if_nreturnsfirst_zip, andget<1>(first_zip) == lastevaluates totrueas expected.libcudacxx/include/cuda/std/__pstl/is_partitioned.h.thrust::raw_reference_castbefore evaluation, and results stored inconst boollocals, to handle proxy-reference iterators (e.g.device_vector<T>::iteratorwhose dereference yieldsdevice_reference<T>). Without this, implicit bool conversion requires two user-defined conversions, which C++ forbids. This matches the pattern used inthrust/detail/internal_functional.handthrust/detail/range/head_flags.hfor the same proxy-reference problem.Test
All existing tests in
thrust/testing/is_partitioned.cuandthrust/testing/cuda/is_partitioned.cuwere compiled and executed on an NVIDIA GeForce RTX 5070 (sm_89), CUDA 12.9, MSVC 19.50:The tests cover the following cases:
truetruetruetruetruefalsethrust::partitiontruedevice_vector<custom_numeric>)true/falsethrust::cuda::par.on(s))true/falsethrust::seq,thrust::device)true/false