Skip to content

[CUB] Refactor DeviceSelect::Unique to always take an environment - #9386

Closed
miscco wants to merge 2 commits into
NVIDIA:device_select_flagged_if_envfrom
miscco:device_select_unique_env
Closed

miscco wants to merge 2 commits into
NVIDIA:device_select_flagged_if_envfrom
miscco:device_select_unique_env

Conversation

@miscco

@miscco miscco commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

This refactors cub::DeviceSelect::Unique to always take an environment instead of just a stream.

With that we can take advantage of tunings even for those overloads.

We also refactor the PSTL algorithms that use this API to always pass the policy so a user can tune them

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

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

suggestion:

Walkthrough

This PR changes env dispatch helpers to accept environments by const-ref and add caller-owned temp-storage overloads; converts DeviceFind/DeviceSelect APIs to env-based signatures; adapts PSTL CUDA backends (find_if, unique) and adds a new CUDA unique_copy backend; and extends tests for user-provided temporary storage across environment variants.

Changes

Environment-based API migration

Layer / File(s) Summary
Dispatch infrastructure: const-ref env and user storage
cub/cub/detail/env_dispatch.cuh
Dispatch helpers now accept const EnvT& and add single-phase overloads that accept caller-provided d_temp_storage/temp_storage_bytes, querying stream/tuning only.
DeviceFind: stream-based to environment-based
cub/cub/device/device_find.cuh, cub/test/catch2_test_device_find_env.cu, libcudacxx/include/cuda/std/__pstl/cuda/find_if.h
FindIf/LowerBound/UpperBound converted to EnvT-based templates (defaulting to cuda::std::execution::env<>), implementations call detail::dispatch_with_env / _and_tuning; PSTL find_if updated to pass policy and use raw storage access; tests expanded to exercise null-sizing and caller-provided storage across env variants.
DeviceSelect::Unique: stream-based to environment-based
cub/cub/device/device_select.cuh, cub/test/catch2_test_device_select_unique.cu
Device-storage Unique overloads converted from stream-based to env-based (adds EnvT template constraints); env parameters are const EnvT&; implementations route through dispatch_with_env_and_tuning and call detail::select::dispatch with the tuned stream/policy; tests added for user-provided storage across environments and comparators.
PSTL unique: refactor to use DeviceSelect::Unique
libcudacxx/include/cuda/std/__pstl/cuda/unique.h
CUDA __par_impl simplified to call DeviceSelect::Unique directly, compute __num_selected using device temp-storage, and return __first + __num_selected; removed the prior unique_copy overload from unique dispatch.
PSTL unique_copy: new CUDA backend dispatch
libcudacxx/include/cuda/std/__pstl/dispatch.h, libcudacxx/include/cuda/std/__pstl/cuda/unique_copy.h, libcudacxx/include/cuda/std/__pstl/unique_copy.h
Adds __unique_copy tag and a CUDA PSTL backend that uses DeviceSelect::Unique with temp-storage management, async selected-count copy, synchronization, iterator advancement, and allocation-to-exception mapping.

Possibly related PRs

  • NVIDIA/cccl#8880: touches select/DeviceSelect dispatch and tuning API interactions relevant to env/tuning forwarding.

Suggested reviewers

  • NaderAlAwar
  • gevtushenko
  • 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: 3

🧹 Nitpick comments (4)
cub/test/catch2_test_device_select_unique.cu (1)

210-212: ⚡ Quick win

suggestion: Remove unused variables boundary and num_selected_std. The test validates output by comparing vectors (lines 241, 265) but never asserts the count matches the std::unique result. Either remove these variables or add REQUIRE(num_selected_out[0] == num_selected_std) assertions like other tests in this file do (see lines 328, 335).

libcudacxx/include/cuda/std/__pstl/cuda/unique.h (2)

93-102: 💤 Low value

suggestion: Error message on line 95 still references cub::DispatchSelectIf::Dispatch but the code now calls cub::DeviceSelect::Unique. Update for clarity:

       _CCCL_TRY_CUDA_API(
         CUB_NS_QUALIFIER::DeviceSelect::Unique,
-        "__pstl_cuda_unique: kernel launch of cub::DispatchSelectIf::Dispatch failed",
+        "__pstl_cuda_unique: kernel launch of cub::DeviceSelect::Unique failed",
         __storage.__get_temp_storage(),

90-91: 💤 Low value

suggestion: Comment says "as well as a copy of the input sequence as Unique is not inplace" but the code uses the in-place variant of DeviceSelect::Unique. The temporary storage is only for __num_selected and CUB internals. Consider updating the comment:

-    { // Create temporary storage for the return value as well as a copy of the input sequence as Unique is not inplace
+    { // Create temporary storage for the return value (num_selected) and CUB internal scratch
libcudacxx/include/cuda/std/__pstl/cuda/unique_copy.h (1)

83-93: 💤 Low value

suggestion: Error messages reference __pstl_cuda_unique but this is unique_copy. Update for debugging clarity:

  • Line 85: "__pstl_cuda_unique_copy: determination of device storage..."
  • Line 100: "__pstl_cuda_unique_copy: kernel launch..."
  • Line 112: "__pstl_cuda_unique_copy: copy of num_selected..."

Also applies to: 98-108, 110-117


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 2a9c4a23-bd8a-4562-b58e-775fd280c548

📥 Commits

Reviewing files that changed from the base of the PR and between 53d1cc1 and d97187e.

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

@miscco
miscco force-pushed the device_select_unique_env branch 4 times, most recently from dcfa290 to 1a3118a Compare June 11, 2026 07:18
@miscco miscco changed the title Refactor DeviceSelect::Unique to always take an environment [CUB] Refactor DeviceSelect::Unique to always take an environment Jun 11, 2026
@miscco
miscco force-pushed the device_select_unique_env branch from 1a3118a to ad308bb Compare June 11, 2026 09:20
@github-actions

This comment has been minimized.

Comment thread cub/cub/detail/env_dispatch.cuh Resolved
@miscco
miscco force-pushed the device_select_unique_env branch from ad308bb to d91bd3c Compare June 12, 2026 07:00
@miscco
miscco force-pushed the device_select_unique_env branch 2 times, most recently from 01bb694 to 9e66e24 Compare June 12, 2026 11:12
@github-actions

This comment has been minimized.

@miscco
miscco changed the base branch from main to device_select_flagged_if_env June 15, 2026 07:32
@miscco
miscco force-pushed the device_select_unique_env branch from 9e66e24 to e57d428 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
@github-actions

Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 2h 40m: Pass: 100%/343 | Total: 13d 13h | Max: 2h 27m | Hits: 35%/1341210

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.

3 participants