Skip to content

[CUB] Refactor DeviceSelect::If to always take an environment - #9391

Closed
miscco wants to merge 1 commit into
NVIDIA:device_select_flaggedfrom
miscco:device_select_env
Closed

miscco wants to merge 1 commit into
NVIDIA:device_select_flaggedfrom
miscco:device_select_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:25
@miscco
miscco requested a review from wmaxey June 11, 2026 08:25
@miscco
miscco requested a review from srinivasyadav18 June 11, 2026 08:25
@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
@miscco
miscco force-pushed the device_select_env branch from 23818f2 to 7d4e7d9 Compare June 11, 2026 08:26
@coderabbitai

coderabbitai Bot commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

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::DeviceSelect::If and related selection APIs to always accept an environment parameter, enabling caller-provided tunings to be applied to APIs that take user-provided memory. The changes modify environment-based overloads across the CUB device selection API and corresponding CUDA PSTL implementations.

Key Changes

CUB Device Selection API (cub/cub/device/device_select.cuh)

  • Signature update: Environment-based DeviceSelect::Flagged and DeviceSelect::If overloads now pass env by const reference (const EnvT& env = {}) instead of by value (EnvT env = {})
  • Temp-storage overloads refactored: Stream-based temp-storage overloads are replaced with environment-driven versions that accept an EnvT template parameter and const EnvT& env argument
  • Dispatch mechanism: Temp-storage overloads now route through detail::dispatch_with_env_and_tuning(...) to derive a policy selector, which is forwarded into detail::select::dispatch(...)
  • Documentation updates: Doxygen comments updated from stream-based to environment-based terminology

Tests

New test in cub/test/catch2_test_device_select_if.cu:

  • Validates DeviceSelect::If with user-provided temporary storage
  • Queries required temp-storage size via nullptr call, allocates buffer, then runs selection using provided storage
  • Verifies CUDA status and output correctness for each execution

New test in cub/test/catch2_test_device_select_flagged.cu:

  • Validates DeviceSelect::Flagged with user-provided temporary storage
  • Tests across multiple environment types: raw cudaStream_t, cuda::stream, cuda::stream_ref, cuda::std::execution::env, and cuda::execution::gpu
  • Verifies allocation size consistency, output counts, and output correctness

CUDA PSTL Updates

libcudacxx/include/cuda/std/__pstl/cuda/copy_if.h:

  • Updated DeviceSelect::If calls to pass the execution policy instead of stream handle
  • Changed temp-storage pointer retrieval from __get_ptr<0>() to __get_raw_ptr<0>()

libcudacxx/include/cuda/std/__pstl/cuda/remove_if.h:

  • Updated DeviceSelect::If calls to pass the execution policy instead of stream handle
  • Changed temp-storage pointer accessor from __get_ptr<0>() to __get_raw_ptr<0>()

libcudacxx/include/cuda/std/__pstl/cuda/shift_left.h:

  • Updated both initial sizing and kernel launch calls to DeviceSelect::Flagged to pass execution policy instead of stream handle
  • Updated temp-storage pointer accessor to __get_raw_ptr<0>()

Impact

  • Enables tuning through environment objects for all DeviceSelect APIs that accept user-provided memory
  • Maintains API consistency with const-reference semantics for environment parameters
  • Provides comprehensive test coverage for environment-based API usage with caller-allocated storage

Walkthrough

This PR refactors CUB's DeviceSelect device algorithms to use execution environments and environment-based dispatch instead of raw CUDA streams. Environment parameters change from pass-by-value to const-reference; temp-storage overloads migrate from stream-based to environment-based, dispatching through dispatch_with_env_and_tuning to derive and apply policy selectors. PSTL backends (copy_if, remove_if, shift_left) are updated to pass execution policies instead of stream handles. Tests validate user-provided temporary storage paths across multiple environment and policy types.

Changes

DeviceSelect Environment-Based Dispatch

Layer / File(s) Summary
DeviceSelect const-reference environment parameters
cub/cub/device/device_select.cuh
Flagged and If environment-based non-temp-storage overloads change env parameter to const EnvT& env, and EnvT template parameter documentation is added.
DeviceSelect temp-storage environment dispatch
cub/cub/device/device_select.cuh
Stream-based temp-storage Flagged and If overloads are replaced with environment-based overloads that add EnvT template parameter, accept const EnvT& env, and dispatch via detail::dispatch_with_env_and_tuning to derive policy_selector for select::dispatch. FlaggedIf is similarly updated.
PSTL backend policy-based wiring
libcudacxx/include/cuda/std/__pstl/cuda/copy_if.h, remove_if.h, shift_left.h
Backends pass execution __policy instead of cudaStream_t to cub::DeviceSelect, use nullptr for temp-storage sizing, and access temporary buffers via __get_raw_ptr<0>().
User-provided temporary storage tests
cub/test/catch2_test_device_select_flagged.cu, catch2_test_device_select_if.cu
New tests query required temp-storage, allocate device buffers, and exercise user-provided storage paths across cudaStream_t, cuda::stream, cuda::stream_ref, cuda::std::execution::env, and cuda::execution::gpu variants with CUDA success and correctness checks.

Suggested reviewers

  • gevtushenko

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: 3

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

131-131: 💤 Low value

suggestion: Orphan //! @endcond with no matching `//! `@cond. The prior dispatch_with_env_and_tuning (line 78) is not Doxygen-hidden, so this one presumably shouldn't be either. Remove this line or add //! @cond`` before line 116 if hiding is intended.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 6fbe98e9-2f6c-4544-b42a-5f00b350b05e

📥 Commits

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

📒 Files selected for processing (8)
  • cub/cub/detail/env_dispatch.cuh
  • cub/cub/device/device_find.cuh
  • cub/cub/device/device_select.cuh
  • cub/test/catch2_test_device_find_env.cu
  • cub/test/catch2_test_device_select_if.cu
  • libcudacxx/include/cuda/std/__pstl/cuda/copy_if.h
  • libcudacxx/include/cuda/std/__pstl/cuda/find_if.h
  • libcudacxx/include/cuda/std/__pstl/cuda/remove_if.h

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

This comment has been minimized.

@miscco
miscco force-pushed the device_select_env branch from 7d4e7d9 to 39bd5a9 Compare June 12, 2026 07:03

@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.

🧹 Nitpick comments (1)
cub/test/catch2_test_device_select_if.cu (1)

188-215: ⚡ Quick win

suggestion: avoid asserting that temp-storage size is identical between the no-env query and every env query. DeviceSelect::If is being moved to environment-driven dispatch/tuning, so valid env-specific policies can require different num_bytes. Query and allocate per-env (or allocate the max across envs) and assert call success, instead of expected_allocation_size == num_bytes.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 9c6dc9d7-ba80-470b-9559-dd13c55d8521

📥 Commits

Reviewing files that changed from the base of the PR and between 7d4e7d9 and 39bd5a9.

📒 Files selected for processing (8)
  • cub/cub/detail/env_dispatch.cuh
  • cub/cub/device/device_find.cuh
  • cub/cub/device/device_select.cuh
  • cub/test/catch2_test_device_find_env.cu
  • cub/test/catch2_test_device_select_if.cu
  • libcudacxx/include/cuda/std/__pstl/cuda/copy_if.h
  • libcudacxx/include/cuda/std/__pstl/cuda/find_if.h
  • libcudacxx/include/cuda/std/__pstl/cuda/remove_if.h
🚧 Files skipped from review as they are similar to previous changes (6)
  • libcudacxx/include/cuda/std/__pstl/cuda/remove_if.h
  • libcudacxx/include/cuda/std/__pstl/cuda/find_if.h
  • cub/cub/detail/env_dispatch.cuh
  • cub/cub/device/device_find.cuh
  • cub/test/catch2_test_device_find_env.cu
  • cub/cub/device/device_select.cuh

@miscco
miscco force-pushed the device_select_env branch from 39bd5a9 to 025d5ca Compare June 12, 2026 11:12

@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.

🧹 Nitpick comments (1)
cub/test/catch2_test_device_select_flagged.cu (1)

232-292: ⚡ Quick win

suggestion: Add one section that carries a non-default tuning in env. Every case here varies the stream/env wrapper, but they all still exercise the default policy_selector. A regression that stops forwarding the selector returned by dispatch_with_env_and_tuning would still pass this test, even though that forwarding is the new behavior this PR is introducing for user-provided temp storage. Based on PR objectives, this refactor is meant to let caller-provided tunings affect the user-supplied-memory path.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 7cf318e8-f002-4ff7-829f-3b8f0c6a4126

📥 Commits

Reviewing files that changed from the base of the PR and between 39bd5a9 and 025d5ca.

📒 Files selected for processing (6)
  • cub/cub/device/device_select.cuh
  • cub/test/catch2_test_device_select_flagged.cu
  • cub/test/catch2_test_device_select_if.cu
  • libcudacxx/include/cuda/std/__pstl/cuda/copy_if.h
  • libcudacxx/include/cuda/std/__pstl/cuda/remove_if.h
  • libcudacxx/include/cuda/std/__pstl/cuda/shift_left.h
🚧 Files skipped from review as they are similar to previous changes (3)
  • libcudacxx/include/cuda/std/__pstl/cuda/remove_if.h
  • cub/test/catch2_test_device_select_if.cu
  • libcudacxx/include/cuda/std/__pstl/cuda/copy_if.h

@github-actions

This comment has been minimized.

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 changed the base branch from main to device_select_flagged June 15, 2026 07:32
@miscco
miscco force-pushed the device_select_env branch from 025d5ca to a0bb633 Compare June 15, 2026 07:35
@miscco miscco closed this Jun 15, 2026
@github-project-automation github-project-automation Bot moved this from In Review to Done in CCCL Jun 15, 2026
@miscco
miscco deleted the device_select_env branch June 15, 2026 07:37
@github-actions

Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 2h 33m: Pass: 100%/343 | Total: 13d 10h | Max: 2h 30m | Hits: 37%/1295876

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.

1 participant