Skip to content

[CUB] Refactor DevicePartition::If to always take an environment - #9393

Closed
miscco wants to merge 1 commit into
NVIDIA:test_device_partition_flaggedfrom
miscco:device_partition_flagged_env
Closed

miscco wants to merge 1 commit into
NVIDIA:test_device_partition_flaggedfrom
miscco:device_partition_flagged_env

Conversation

@miscco

@miscco miscco commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

We want to be able to pass tunings to the APIs that take user provided memory

Make sure we can pass any environment or stream type to them

@miscco
miscco requested review from a team as code owners June 11, 2026 08:52
@miscco
miscco requested a review from pciolkosz June 11, 2026 08:52
@miscco
miscco requested a review from elstehle June 11, 2026 08:52
@github-project-automation github-project-automation Bot moved this to Todo in CCCL Jun 11, 2026
@cccl-authenticator-app cccl-authenticator-app Bot moved this from Todo to In Review in CCCL Jun 11, 2026
@coderabbitai

coderabbitai Bot commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

Pull request was closed or merged during review

Note: CodeRabbit is enabled on this repository as a convenience for maintainers
and contributors. Use your best judgment when considering its review comments and
suggestions — a suggested change may be inadequate, unnecessary, or safe to ignore.
Contributors are not expected to address every comment. Human reviews are what
ultimately matter for merging.

Overview

This PR refactors cub::DevicePartition::If (and aligns DevicePartition::Flagged) to consistently take a const EnvT& env execution/environment parameter in all overloads. This enables callers (including STL/PSTL backends) to pass user-provided tuning associated with CUDA execution environments through the CUB dispatch chain.

Core Changes

cub::DevicePartition::If overload refactor (cub/cub/device/device_partition.cuh)

  • Device-storage overloads: replace explicit cudaStream_t parameters with const EnvT& env (adding EnvT = ::cuda::std::execution::env<> default), and route dispatch through detail::dispatch_with_env_and_tuning.
  • Environment-based overloads: change env from by-value to const EnvT&.

In addition, the device-storage overloads now:

  • construct a default policy_selector from iterator/value/offset (and for three-way, per-partition offset) types, and
  • forward that policy_selector into the underlying:
    • detail::select::dispatch for two-way partitioning, or
    • detail::three_way_partition::dispatch for three-way partitioning.

cub::DevicePartition::Flagged alignment (cub/cub/device/device_partition.cuh)

  • Applies the same pattern: replace explicit cudaStream_t with const EnvT& env, default template EnvT = ::cuda::std::execution::env<>, switch by-value env params to const EnvT&, and dispatch through detail::dispatch_with_env_and_tuning.

Test Coverage

New/updated device tests

  • cub/test/catch2_test_device_partition_if.cu

    • Adds: DevicePartition::If works with user provided memory and environment (for all_types, gated with #if TEST_LAUNCH == 0).
    • Validates:
      • temp-storage sizing via nullptr, then re-invocation with user-provided device temp storage
      • correct output vs std::stable_partition
      • multiple execution/environment variants, including stream forms and cuda::std::execution::env / cuda::execution::gpu (with/without stream), while also checking CUDA error handling (cudaPeekAtLastError/cudaDeviceSynchronize).
  • cub/test/catch2_test_device_partition_flagged.cu

    • Adds a similar gated test for cub::DevicePartition::Flagged, validating temp-storage sizing and output correctness across multiple environment variants.

PSTL Integration Updates

Updated CUDA PSTL backends to pass the execution policy (__policy) into the refactored CUB partition dispatch points:

  • libcudacxx/include/cuda/std/__pstl/cuda/partition.h: cub::DevicePartition::If now receives __policy for both the temp-storage sizing query and the kernel invocation.
  • libcudacxx/include/cuda/std/__pstl/cuda/partition_copy.h: cuda::std::partition_copy dispatch now passes __policy into DevicePartition::If in both sizing and launch paths.
  • libcudacxx/include/cuda/std/__pstl/cuda/stable_partition.h: updated calls to DevicePartition::If (sizing, and the actual partition) to pass __policy instead of __stream.get().
  • libcudacxx/include/cuda/std/__pstl/cuda/rotate.h and rotate_copy.h: updated DevicePartition::Flagged dispatch to pass __policy (including the sizing query and partition launch path).

Impact

  • Signature/behavior change: DevicePartition::If (and Flagged) overloads now consistently use an env/execution-policy model (const EnvT& env) instead of an explicit stream-only parameter.
  • Dispatch plumbing: device-storage paths now route through dispatch_with_env_and_tuning and forward a computed policy_selector into the underlying partition dispatch.
  • Total lines changed: +308 / -77 across modified files.
  • Dependency: this PR is intended to be merged after PR #9318 completes.

Walkthrough

The PR converts CUB's DevicePartition API from stream-based execution to environment-based execution by adding EnvT template parameters and routing dispatch through dispatch_with_env_and_tuning with computed policy selectors. PSTL backends are updated to pass execution policies instead of raw streams to DevicePartition and DeviceTransform calls. Tests validate user-provided temp storage across multiple environment forms.

Changes

DevicePartition environment-based execution and PSTL integration

Layer / File(s) Summary
DevicePartition::Flagged environment adaptation
cub/cub/device/device_partition.cuh
Devicestorage overload replaces cudaStream_t with const EnvT& env parameter; constructs default policy_selector from iterator/flag/output/offset types; dispatches through detail::dispatch_with_env_and_tuning, forwarding policy_selector into detail::select::dispatch; convenience overload takes env by const-ref.
DevicePartition::If two-way partition environment adaptation
cub/cub/device/device_partition.cuh
Devicestorage overload signature changes from cudaStream_t to const EnvT& env; constructs default policy_selector from iterator/value/offset types; routes through detail::dispatch_with_env_and_tuning, forwarding policy_selector into detail::select::dispatch; convenience overload takes env by const-ref.
DevicePartition::If three-way partition environment adaptation
cub/cub/device/device_partition.cuh
Devicestorage overload replaces cudaStream_t with const EnvT& env; constructs three-way policy_selector from input value type and per-partition offset types; dispatches via detail::dispatch_with_env_and_tuning into detail::three_way_partition::dispatch; convenience overload takes env by const-ref.
DevicePartition tests for user-provided temp storage and environment forms
cub/test/catch2_test_device_partition_flagged.cu, cub/test/catch2_test_device_partition_if.cu
CUDA headers added for execution environment and device selection; comprehensive tests validate both Flagged and If with nullptr sizing queries followed by user-provided temp allocations, exercising multiple environment forms (cudaStream_t, cuda::stream, cuda::stream_ref, cuda::std::execution::env, cuda::execution::gpu with/without stream); assert CUDA status, temp-size consistency, and output correctness against std::stable_partition reference.
PSTL partition and partition_copy: propagate execution policy to DevicePartition
libcudacxx/include/cuda/std/__pstl/cuda/partition.h, libcudacxx/include/cuda/std/__pstl/cuda/partition_copy.h
Pass execution __policy to cub::DevicePartition::If calls in both temporary-storage sizing queries (replacing nullptr) and kernel invocations (replacing __stream.get()).
PSTL rotate and rotate_copy: propagate execution policy to DevicePartition
libcudacxx/include/cuda/std/__pstl/cuda/rotate.h, libcudacxx/include/cuda/std/__pstl/cuda/rotate_copy.h
Pass execution __policy to cub::DevicePartition::Flagged calls in temporary-storage sizing and kernel invocations; rotate also adjusts raw buffer pointer source.
PSTL stable_partition: propagate execution policy to DevicePartition and DeviceTransform
libcudacxx/include/cuda/std/__pstl/cuda/stable_partition.h
Pass execution __policy to cub::DevicePartition::If and cub::DeviceTransform::TransformIf calls, replacing prior __stream.get() arguments in sizing, staging, and partition invocations.

Possibly related PRs

  • NVIDIA/cccl#9316: Introduces public PartitionPolicy tuning API; main PR's policy selector construction and dispatch_with_env_and_tuning routing directly build on that infrastructure.
  • NVIDIA/cccl#9324: Establishes three-way partition policy selection and dispatch; main PR's three-way DevicePartition::If refactoring depends on that tuning work.

Suggested reviewers

  • gevtushenko
  • gonidelis
  • davebayer

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
cub/cub/detail/env_dispatch.cuh (1)

117-129: suggestion: This new user-memory path now feeds policy selection through the env/tuning dispatcher, so it can change the kernel variant selected for DeviceFind and DevicePartition. Please run the SASS-diff and benchmark flow for the affected Device* benchmarks before merge. As per coding guidelines, **/*.{cpp,cu,cuh}: Do not commit SASS code changes without running benchmarks to check for performance regressions.

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 18345e1e-685e-4c8c-9fd7-da9a89f1a7dd

📥 Commits

Reviewing files that changed from the base of the PR and between d2a0a8a and ba1593e.

📒 Files selected for processing (9)
  • cub/cub/detail/env_dispatch.cuh
  • cub/cub/device/device_find.cuh
  • cub/cub/device/device_partition.cuh
  • cub/test/catch2_test_device_find_env.cu
  • cub/test/catch2_test_device_partition_if.cu
  • libcudacxx/include/cuda/std/__pstl/cuda/find_if.h
  • libcudacxx/include/cuda/std/__pstl/cuda/partition.h
  • libcudacxx/include/cuda/std/__pstl/cuda/partition_copy.h
  • libcudacxx/include/cuda/std/__pstl/cuda/stable_partition.h

Comment thread cub/test/catch2_test_device_find_env.cu Resolved
@github-actions

This comment has been minimized.

@miscco
miscco force-pushed the device_partition_flagged_env branch 2 times, most recently from 7efaa10 to b587246 Compare June 12, 2026 11:14
@github-actions

This comment has been minimized.

@bernhardmgruber bernhardmgruber left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the above, otherwise LGTM

We want to be able to pass tunings to the APIs that take user provided memory

Make sure we can pass any environment or stream type to them
@miscco
miscco force-pushed the device_partition_flagged_env branch from b587246 to cc62cf4 Compare June 15, 2026 09:25
@miscco
miscco changed the base branch from main to test_device_partition_flagged June 15, 2026 09:27
@miscco miscco closed this Jun 15, 2026
@miscco
miscco deleted the device_partition_flagged_env branch June 15, 2026 09:34
@github-actions

Copy link
Copy Markdown
Contributor

😬 CI Workflow Results

🟥 Finished in 1h 32m: Pass: 81%/343 | Total: 5d 20h | Max: 1h 28m | Hits: 71%/662747

See results here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants