Skip to content

[CUB] Refactor DeviceSelect::FlaggedIf to always take an environment - #9389

Closed
miscco wants to merge 1 commit into
NVIDIA:device_select_envfrom
miscco:device_select_flagged_if_env
Closed

miscco wants to merge 1 commit into
NVIDIA:device_select_envfrom
miscco:device_select_flagged_if_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 07:58
@miscco
miscco requested review from Jacobfaib and NaderAlAwar June 11, 2026 07:58
@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

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::FlaggedIf and related selection APIs to consistently accept an execution environment parameter. This enables passing user-provided tunings to APIs that operate on user-allocated memory and ensures consistent support for any environment or stream type across the API.

Key Changes

Core API Updates (cub/cub/device/device_select.cuh)

The following DeviceSelect methods are refactored to accept execution environments:

  • DeviceSelect::Flagged:

    • Temp-storage overloads now accept const EnvT& env = {} (with EnvT = ::cuda::std::execution::env<>) instead of cudaStream_t stream
    • Non-temp-storage overloads now take const EnvT& env by const-reference instead of by value
  • DeviceSelect::If:

    • Temp-storage overloads now accept const EnvT& env = {} instead of cudaStream_t stream
    • Non-temp-storage overloads now take const EnvT& env by const-reference instead of by value
  • DeviceSelect::FlaggedIf:

    • Temp-storage overloads now accept const EnvT& env = {} (with EnvT = ::cuda::std::execution::env<>) instead of cudaStream_t stream
    • Non-temp-storage overloads now take const EnvT& env by const-reference instead of by value
    • Both non-in-place and potentially in-place forms are updated

The implementations now route through detail::dispatch_with_env_and_tuning to obtain the selected policy and environment-derived stream/storage for detail::select::dispatch.

Test Coverage

New comprehensive test cases validate the environment-aware APIs:

  • catch2_test_device_select_flagged_if.cu: Validates DeviceSelect::FlaggedIf with user-provided memory and multiple execution environment types (raw stream handle, cuda::stream, cuda::stream_ref, cuda::std::execution::env, cuda::execution::gpu)

  • catch2_test_device_select_flagged.cu: Validates DeviceSelect::Flagged with user-supplied memory and supported execution/environment types

  • catch2_test_device_select_if.cu: Validates DeviceSelect::If with user-provided memory and various environment forms

All tests verify that temporary storage size requirements are consistent across environments and that selection results are correct.

PSTL Integration Updates

Updated CUDA backend implementations to use environment-based overloads:

  • libcudacxx/include/cuda/std/__pstl/cuda/copy_if.h: Updated __pstl_dispatch for __copy_if to use the environment-based DeviceSelect::If API
  • libcudacxx/include/cuda/std/__pstl/cuda/remove_if.h: Updated __pstl_dispatch for __remove_if to pass policy-backed temporary storage instead of explicit stream objects
  • libcudacxx/include/cuda/std/__pstl/cuda/shift_left.h: Updated __pstl_dispatch for shift_left to use environment-based DeviceSelect::Flagged calls

These changes ensure user-provided tunings are respected in PSTL implementations.

Technical Details

The refactoring preserves backward compatibility through default template parameters (EnvT = ::cuda::std::execution::env<>) and by-reference environment parameters. All temp-storage determination calls can be made with nullptr storage pointers, and the selected environment-derived stream/storage is obtained via the dispatch mechanism.

Note: This PR should be merged after PR #9318.

Walkthrough

CUB's DeviceSelect APIs (Flagged, If, FlaggedIf) transition temp-storage overloads from explicit cudaStream_t parameters to environment-based const EnvT& env parameters; implementations route through detail::dispatch_with_env_and_tuning. Non-temp-storage overloads updated to accept env by const-reference. PSTL adapters and tests updated accordingly.

Changes

DeviceSelect environment-based dispatch refactor

Layer / File(s) Summary
DeviceSelect::Flagged environment-based signatures and dispatch
cub/cub/device/device_select.cuh
Flagged temp-storage overloads add EnvT template parameter and accept const EnvT& env = {}; implementations route through dispatch_with_env_and_tuning forwarding environment-derived stream to detail::select::dispatch. Non-temp-storage overloads updated to take env as const EnvT& instead of by-value. Doxygen documentation adjusted for env parameters.
DeviceSelect::If environment-based signatures and dispatch
cub/cub/device/device_select.cuh
If temp-storage overload adds EnvT template and accepts const EnvT& env = {}; implementation uses dispatch_with_env_and_tuning pattern matching Flagged. Non-temp-storage overloads (non-in-place and potentially in-place) take env as const EnvT&.
DeviceSelect::FlaggedIf environment-based signatures and dispatch
cub/cub/device/device_select.cuh
FlaggedIf temp-storage overloads (both non-in-place and potentially in-place) add EnvT template and accept const EnvT& env = {}; implementations use dispatch_with_env_and_tuning forwarding policy/stream into select dispatch. Non-temp-storage overloads take env as const EnvT&.
DeviceSelect test coverage for user temp storage and environments
cub/test/catch2_test_device_select_flagged.cu, cub/test/catch2_test_device_select_if.cu, cub/test/catch2_test_device_select_flagged_if.cu
New test cases added exercising user-provided temporary storage sizing (nullptr query followed by allocation and reuse) and validation across multiple environment forms (raw cudaStream_t, cuda::stream, cuda::stream_ref, cuda::std::execution::env, cuda::execution::gpu). Tests verify CUDA success states, synchronization, and output correctness against reference results. Includes updated CUDA headers for device/iterator/execution support.
PSTL algorithm adapters for environment-based DeviceSelect
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
Updated PSTL adapters (copy_if, remove_if, shift_left) to pass nullptr for temp-storage queries instead of explicit stream arguments, and changed pointer retrieval from __get_ptr<0>() to __get_raw_ptr<0>() for both kernel invocation and host result copy operations.

Suggested labels

backport branch/3.3.x, backport branch/3.4.x

Suggested reviewers

  • Jacobfaib
  • 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: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: eda339c3-aef7-47cf-a100-9f148328fefa

📥 Commits

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

📒 Files selected for processing (6)
  • 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_flagged_if.cu
  • libcudacxx/include/cuda/std/__pstl/cuda/find_if.h

Comment on lines +254 to +273
return detail::dispatch_with_env(
d_temp_storage,
temp_storage_bytes,
env,
[&]([[maybe_unused]] auto tuning, void* storage, size_t& bytes, auto stream) {
if (storage == nullptr)
{
bytes = 1;
return cudaSuccess;
}

return DeviceTransform::__transform_internal(
::cuda::std::make_tuple(d_values),
d_output,
static_cast<ValuesOffsetT>(values_num_items),
::cuda::always_true{},
detail::find::make_binary_search_transform_op<detail::find::lower_bound>(
d_range, static_cast<RangeOffsetT>(range_num_items), comp),
::cuda::stream_ref{stream});
return DeviceTransform::__transform_internal(
::cuda::std::make_tuple(d_values),
d_output,
static_cast<ValuesOffsetT>(values_num_items),
::cuda::always_true{},
detail::find::make_binary_search_transform_op<detail::find::lower_bound>(
d_range, static_cast<RangeOffsetT>(range_num_items), comp),
::cuda::stream_ref{stream});
});

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.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

important: Forward env into DeviceTransform::__transform_internal here instead of collapsing it to ::cuda::stream_ref{stream}. These overloads now accept an execution environment, but the implementation only preserves the stream before entering DeviceTransform. cub/cub/device/device_transform.cuh:76-125 selects transform tuning from the Env it receives, so any tuning carried by env is silently ignored for LowerBound and UpperBound with external temp storage. That misses the stated goal of honoring environment-based tuning on user-provided-memory APIs.

Also applies to: 383-402

@miscco
miscco force-pushed the device_select_flagged_if_env branch from e328b00 to 8d2dafa Compare June 11, 2026 09:58
@github-actions

This comment has been minimized.

@miscco
miscco force-pushed the device_select_flagged_if_env branch 3 times, most recently from b1add85 to bed1844 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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
cub/cub/device/device_select.cuh (2)

125-150: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

suggestion: The new docs describe the last parameter as an execution environment only, but these overloads are also meant to preserve stream-style usage. Please call out raw/default stream arguments explicitly here; otherwise the public API docs read as if stream support was removed. Based on PR objectives, these overloads should accept any environment or stream type.

Also applies to: 630-652, 768-794, 902-924, 1013-1042, 1127-1152


155-165: ⚠️ Potential issue | 🔴 Critical

important: Restore/bridge cudaStream_t-style null sentinels (e.g., 0/nullptr) in DeviceSelect env-based overloads

  • cub/cub/detail/env_dispatch.cuh derives the stream via ::cuda::__call_or(::cuda::get_stream, ::cuda::stream_ref{cudaStream_t{}}, env). If a caller passes 0, that will deduce EnvT=int for the env parameter, and the stream query will only compile if get_stream is defined for integral types (or convertible), which isn’t evident from the dispatch layer alone.
  • The in-tree search for DeviceSelect::{Flagged,If,FlaggedIf}(..., 0/nullptr) produced no matches, so this is likely a compatibility break for external callers rather than existing CCCL call sites.
  • Keep a cudaStream_t forwarding overload (or add a constrained bridge that normalizes 0/nullptr into the expected env/stream representation before calling the env dispatch path) to preserve previous overload resolution for null-stream sentinels.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 6d45bece-7658-4272-85e7-e63cbff645bb

📥 Commits

Reviewing files that changed from the base of the PR and between b1add85 and bed1844.

📒 Files selected for processing (7)
  • cub/cub/device/device_select.cuh
  • cub/test/catch2_test_device_select_flagged.cu
  • cub/test/catch2_test_device_select_flagged_if.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 (1)
  • cub/test/catch2_test_device_select_flagged_if.cu

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

libcu++ changes LGTM

@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_env June 15, 2026 07:32
@miscco
miscco force-pushed the device_select_flagged_if_env branch from bed1844 to d19baae 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_flagged_if_env branch June 15, 2026 07:37
@github-actions

Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 2h 29m: Pass: 100%/287 | Total: 11d 20h | Max: 2h 27m | Hits: 20%/931331

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