Skip to content

[CUB] Refactor DeviceSelect::Flagged to always take an environment - #9388

Closed
miscco wants to merge 1 commit into
NVIDIA:mainfrom
miscco:device_select_flagged
Closed

miscco wants to merge 1 commit into
NVIDIA:mainfrom
miscco:device_select_flagged

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.

Also use the new overload for PSTL shift_left

@miscco
miscco requested review from a team as code owners June 11, 2026 07:45
@miscco
miscco requested a review from gevtushenko June 11, 2026 07:45
@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

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: b5189efd-43f2-4410-830b-afdcb5d41ef8

📥 Commits

Reviewing files that changed from the base of the PR and between 21856ae and fa5d66c.

📒 Files selected for processing (3)
  • cub/cub/device/device_select.cuh
  • cub/test/catch2_test_device_select_flagged.cu
  • libcudacxx/include/cuda/std/__pstl/cuda/shift_left.h

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::Flagged to consistently take an execution environment parameter, allowing callers to pass tuning/execution context (including user-provided memory scenarios) through the unified environment mechanism. It also updates CUDA PSTL cuda::std::shift_left to call DeviceSelect::Flagged using the environment/policy-based API.

Changes by File

cub/cub/device/device_select.cuh

  • Updated cub::DeviceSelect::Flagged overloads to take env as a const EnvT& (instead of by value).
  • For overloads that previously accepted an explicit cudaStream_t stream = nullptr (notably the out-of-place overloads with d_temp_storage), the stream parameter was removed and replaced with an environment-based parameter:
    • added template <typename EnvT = ::cuda::std::execution::env<>>
    • signature form now ends with const EnvT& env = {}
  • Updated implementations to dispatch through detail::dispatch_with_env_and_tuning, forwarding the stream derived from the provided environment into the underlying detail::select::dispatch.
  • Updated Doxygen to document EnvT and the updated environment parameter.

cub/test/catch2_test_device_select_flagged.cu

  • Added C2H_TEST("DeviceSelect::Flagged works with user provided memory and environment", ...).
  • The test:
    • probes DeviceSelect::Flagged with d_temp_storage == nullptr to determine expected_allocation_size
    • allocates d_temp_storage accordingly
    • validates both the “null temp storage” query and the actual run across multiple environment variants:
      • cudaStream_t
      • cuda::stream, cuda::stream_ref
      • cuda::std::execution::env
      • cuda::execution::gpu
      • cuda::execution::gpu configured with a stream
    • checks CUDA error handling and verifies output against a stable host reference.

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

  • Updated the CUDA PSTL backend for cuda::std::shift_left to use the new environment-based DeviceSelect::Flagged calls:
    • passes __policy to DeviceSelect::Flagged instead of passing the raw CUDA stream (__stream.get())
    • adjusts the temp-storage pointer usage to use __get_raw_ptr<0>().

Public API / Behavior Notes

  • cub::DeviceSelect::Flagged overloads now uniformly accept an environment parameter (const EnvT& env), removing the need for a separate explicit stream parameter in the updated overloads.
  • Existing call patterns that provide a cudaStream_t can continue to work via the environment parameter (as exercised by the new test).

Walkthrough

The PR refactors cub::DeviceSelect::Flagged overloads to take environment arguments by const-reference with a defaulted template parameter, removes explicit cudaStream_t parameters, and routes implementations through detail::dispatch_with_env_and_tuning. The PSTL shift_left backend is updated to pass execution policy instead of raw streams. Comprehensive tests validate the API across multiple environment types.

Changes

DeviceSelect::Flagged environment dispatch refactor

Layer / File(s) Summary
Out-of-place Flagged migration
cub/cub/device/device_select.cuh
Out-of-place overloads now accept env as const EnvT& (defaulted to ::cuda::std::execution::env<>), remove explicit cudaStream_t parameter, and dispatch via dispatch_with_env_and_tuning before calling underlying select dispatch. Doxygen documentation updated to document the EnvT template parameter.
In-place Flagged migration
cub/cub/device/device_select.cuh
In-place overloads now accept env as const EnvT& (defaulted to ::cuda::std::execution::env<>), remove explicit cudaStream_t parameter, and dispatch via dispatch_with_env_and_tuning before calling underlying select dispatch. Doxygen documentation updated to document the EnvT template parameter.
Flagged tests across environment types
cub/test/catch2_test_device_select_flagged.cu
Test includes CUDA device and execution headers. New test probes required temporary storage size with null pointer, allocates device memory, and executes Flagged across multiple environment variants (cudaStream_t, cuda::stream, cuda::stream_ref, cuda::std::execution::env, cuda::execution::gpu); validates CUDA status, returned storage size, and output correctness for each variant.
PSTL shift_left backend migration
libcudacxx/include/cuda/std/__pstl/cuda/shift_left.h
Both cub::DeviceSelect::Flagged calls updated to pass __policy (execution policy) instead of __stream.get() to align with new environment-based dispatch API. Storage pointer access changed from __get_ptr<0>() to __get_raw_ptr<0>().

Possibly related PRs

  • NVIDIA/cccl#9316: Both PRs adjust the cub::DeviceSelect env/tuning integration—this PR changes DeviceSelect::Flagged overloads to route via dispatch_with_env_and_tuning and updates downstream PSTL callsites, aligning with broader work to productize the public DeviceSelect tuning API.

Suggested reviewers

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

🧹 Nitpick comments (4)
cub/cub/device/device_select.cuh (1)

456-456: 💤 Low value

suggestion: If overloads (lines 456, 544) and FlaggedIf overloads (lines 1240, 1340) still take EnvT env by value while Flagged was updated to const EnvT& env. Consider updating these in a follow-up PR for consistency.

Also applies to: 544-544

cub/test/catch2_test_device_find_env.cu (1)

198-198: ⚡ Quick win

suggestion: Use thrust::raw_pointer_cast for consistency with lines 83 and 119 in the same file.

cub/test/catch2_test_device_select_flagged.cu (2)

307-307: ⚡ Quick win

suggestion: Use thrust::raw_pointer_cast for consistency with line 230 in the same file.


434-434: ⚡ Quick win

suggestion: Use thrust::raw_pointer_cast for consistency with line 230 in the same file.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 1307a2f6-fee1-4c1d-87dd-3a8c080f6457

📥 Commits

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

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

@github-actions

This comment has been minimized.

@miscco
miscco force-pushed the device_select_flagged branch from 934e84b to d4b1ac4 Compare June 12, 2026 07:26
@miscco
miscco enabled auto-merge (squash) June 12, 2026 07:29

@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/cub/device/device_select.cuh (1)

169-185: ⚡ Quick win

suggestion: Add one user-memory Flagged test that passes a tuning-bearing environment, not just stream/env wrappers. These overloads now exist to preserve env-selected policy through dispatch_with_env_and_tuning(...), but the supplied coverage only exercises cudaStream_t, cuda::stream, cuda::stream_ref, plain env, and cuda::execution::gpu. A custom tuning case would pin the specific behavior this refactor is introducing.

Also applies to: 668-689


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 2f222faf-3a1e-40a5-b270-8039877996ef

📥 Commits

Reviewing files that changed from the base of the PR and between 934e84b and d4b1ac4.

📒 Files selected for processing (7)
  • 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.cu
  • libcudacxx/include/cuda/std/__pstl/cuda/find_if.h
  • libcudacxx/include/cuda/std/__pstl/cuda/shift_left.h
🚧 Files skipped from review as they are similar to previous changes (6)
  • libcudacxx/include/cuda/std/__pstl/cuda/shift_left.h
  • libcudacxx/include/cuda/std/__pstl/cuda/find_if.h
  • cub/cub/detail/env_dispatch.cuh
  • cub/test/catch2_test_device_select_flagged.cu
  • cub/cub/device/device_find.cuh
  • cub/test/catch2_test_device_find_env.cu

@miscco
miscco force-pushed the device_select_flagged branch from d4b1ac4 to 21856ae Compare June 12, 2026 11:12
@github-actions

This comment has been minimized.

Comment on lines +683 to +684
NullType{},
NullType{},

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.

Suggestion: consider adding a comment that those values are the select and equality op. Applies to other places as well.

Comment on lines +203 to +204
c2h::device_vector<type> in(num_items);
c2h::device_vector<type> out(num_items);

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.

Suggested change
c2h::device_vector<type> in(num_items);
c2h::device_vector<type> out(num_items);
c2h::device_vector<type> in(num_items, thrust::no_init);
c2h::device_vector<type> out(num_items, thrust::no_init);

c2h::device_vector<type> out(num_items);
c2h::gen(C2H_SEED(2), in);

c2h::device_vector<int> flags(num_items);

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.

Suggested change
c2h::device_vector<int> flags(num_items);
c2h::device_vector<int> flags(num_items, thrust::no_init);

int* d_num_selected_out = thrust::raw_pointer_cast(num_selected_out.data());

size_t expected_allocation_size = 0;
auto error = cub::DeviceSelect::Flagged(

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.

Important: The current unit test file is compiled three times as requested by the // %PARAM% TEST_LAUNCH lid 0:1:2 comment. The intention is to use the launch wrapper select_flagged instead of the public API, so the unit test will cover host-side launch, device-side launch, and graph capture launch. If the test here can support the three launch types, which I think it does, please use the launch wrapper.

If a test cannot work with launch wrappers, you can use the public API, but then please guard the test if #if TEST_LAUNCH == 0 so the test is only compiled once for launch wrapper zero.

We want to be able to pass tunings  to the APIs that take user provided memory.
@miscco
miscco force-pushed the device_select_flagged branch from 21856ae to fa5d66c Compare June 15, 2026 07:35
@miscco miscco closed this Jun 15, 2026
auto-merge was automatically disabled June 15, 2026 07:36

Pull request was closed

@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 branch June 15, 2026 07:37
@github-actions

Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 2h 39m: Pass: 100%/343 | Total: 12d 22h | Max: 2h 25m | Hits: 37%/1296808

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