Skip to content

[CUB] Refactor cub::DeviceFor to always take an environment - #11013

Merged
pauleonix merged 18 commits into
NVIDIA:mainfrom
macdonaldezra:refactor-device-for-env
Sep 9, 2026
Merged

pauleonix merged 18 commits into
NVIDIA:mainfrom
macdonaldezra:refactor-device-for-env

Conversation

@macdonaldezra

@macdonaldezra macdonaldezra commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Description

Part of #9875 (the DeviceFor row).
Fixes: #11713
Fixes: #11714

Replaces the trailing cudaStream_t parameter of the two-phase Bulk, ForEachN, ForEach, ForEachCopyN, ForEachCopy, ForEachInExtents, and ForEachInLayout overloads with an execution environment, and removes the bare-stream single-phase overloads and their SFINAE, which are no longer needed now that environments are taken by const& (#10105).

Notes:

  • A new static_assert rejects env types whose conversion to cudaStream_t is not const-qualified — previously served by the removed overloads, these would otherwise silently launch on the default stream. Flagged for discussion: happy to generalize it into cuda::get_stream/env_dispatch.cuh as a follow-up, or drop it for exact parity with the sibling migrations.
  • Also fixes a fast_div_mod assertion failure for empty extents in ForEachInExtents, the mislabeled ForEachInLayout NVTX range, and its param docs.

Checklist

  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

@macdonaldezra
macdonaldezra requested a review from a team as a code owner August 26, 2026 02:57
@macdonaldezra
macdonaldezra requested review from pauleonix and a lite review from Copilot August 26, 2026 02:57
@github-project-automation github-project-automation Bot moved this to Todo in CCCL Aug 26, 2026
@copy-pr-bot

copy-pr-bot Bot commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

Copilot AI 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.

Pull request overview

This PR continues the CUB device-scope API migration away from a trailing cudaStream_t parameter by refactoring cub::DeviceFor to consistently accept an execution environment (const Env&) for both single-phase and two-phase overloads. It also tightens environment/stream querying behavior to avoid silent fallback to the default stream for env types that are only non-const convertible to cudaStream_t, and fixes ForEachInExtents behavior for empty extents plus a mislabeled NVTX range.

Changes:

  • Replaced cudaStream_t parameters in DeviceFor two-phase overloads with const Env&, and removed the bare-stream single-phase overloads/SFINAE.
  • Added a compile-time guard to reject env types whose stream conversion cannot be queried through a const& (preventing silent default-stream launches).
  • Fixed ForEachInExtents empty-extent handling (avoids fast_div_mod assertion) and corrected the ForEachInLayout NVTX range name; expanded/updated Catch2 coverage accordingly.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
cub/cub/device/device_for.cuh Refactors DeviceFor APIs to use environments everywhere, adds const-query static_assert guard, fixes empty-extents path, and corrects NVTX labeling/docs.
cub/test/catch2_test_device_for_env.cu Adds broad environment-kind acceptance tests, stream-routing verification, and two-phase tuning propagation coverage for the updated env-based APIs.
cub/test/catch2_test_device_for_api.cu Updates unambiguity guard tests and adds size-query guards for extents/layout plus a runtime test for empty-extents handling.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@coderabbitai

coderabbitai Bot commented Aug 26, 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
📝 Summary

Summary by CodeRabbit

  • New Features

    • Device iteration APIs now support execution environments for stream selection across bulk, foreach, copy, extent, and layout operations.
    • Added support for stream, wrapper, property, GPU, and tuning environments.
    • Improved CUDA graph-capture behavior with environment-selected streams.
  • Bug Fixes

    • Empty extents are handled safely without unnecessary setup.
    • Improved overload resolution for temporary-storage and size-query operations.
  • Tests

    • Added comprehensive coverage for environments, tuning, extents, layouts, and graph capture.

Walkthrough

Changes

DeviceFor operations now use execution environments for stream selection. Legacy cudaStream_t overloads and related constraints were removed. Extent and layout dispatch now handle empty shapes and add NVTX instrumentation. Tests cover environment variants, stream routing, tuning, and overload resolution.

Changes

DeviceFor execution environments

Layer / File(s) Summary
Environment-based API contracts
cub/cub/device/device_for.cuh
Temporary-storage and execution overloads now accept execution environments. Legacy stream overloads and related constraints were removed. Internal temporary-storage dispatch forwards the environment.
Extent and layout dispatch
cub/cub/device/device_for.cuh
Extent and layout APIs forward environments through internal dispatch. Public calls add NVTX ranges, const-qualify internal data, and handle empty shapes before division-helper construction.
Environment and API validation
cub/test/catch2_test_device_for_api.cu, cub/test/catch2_test_device_for_env.cu
Tests cover environment variants, stream routing, stream capture, two-phase tuning, overload resolution, and empty dynamic extents.

Suggested reviewers: miscco, pauleonix, bernhardmgruber

Merge Risk: 🟡 Moderate · up to ff330

The PR changes DeviceFor stream selection to use execution environments, but mutable-only wrappers may now run on the default CUDA stream instead of their represented stream, potentially breaking synchronization or capture assumptions. A multi-GPU test can also target the wrong device, and a test const-correctness issue remains open; these should be fixed or explicitly accepted before merging.


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

@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_for.cuh (1)

1278-1289: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

suggestion: extents, shape, sub_sizes_div_array, extents_div_array, and op_wrapper are never modified after initialization. Declare them const.

-    auto extents                   = layout_mapping.extents();
+    const auto extents             = layout_mapping.extents();
     using ShapeT                   = implicit_prom_t<extent_index_type>;
-    auto shape                     = static_cast<ShapeT>(cub::detail::size(extents));
+    const auto shape               = static_cast<ShapeT>(cub::detail::size(extents));
     // must precede the fast_div_mod arrays below, whose constructor asserts a positive divisor
     if (shape == 0)
     {
       return cudaSuccess;
     }
-    fast_mod_array_t sub_sizes_div_array = cub::detail::sub_sizes_fast_div_mod<is_layout_right>(extents, seq);
-    fast_mod_array_t extents_div_array   = cub::detail::extents_fast_div_mod(extents, seq);
-    for_each::op_wrapper_extents_t<OpType, extents_type, is_layout_right, fast_mod_array_t> op_wrapper{
+    const fast_mod_array_t sub_sizes_div_array = cub::detail::sub_sizes_fast_div_mod<is_layout_right>(extents, seq);
+    const fast_mod_array_t extents_div_array   = cub::detail::extents_fast_div_mod(extents, seq);
+    const for_each::op_wrapper_extents_t<OpType, extents_type, is_layout_right, fast_mod_array_t> op_wrapper{
       op, extents, sub_sizes_div_array, extents_div_array};

As per coding guidelines: "All variables that are not modified must be declared const, including cast results, function return values, and loop-invariant computations."

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 38b95cf6-a477-4750-b9e7-9fe3ee2c9994

📥 Commits

Reviewing files that changed from the base of the PR and between ffddb67 and 626229e.

📒 Files selected for processing (3)
  • cub/cub/device/device_for.cuh
  • cub/test/catch2_test_device_for_api.cu
  • cub/test/catch2_test_device_for_env.cu

Included review availability: Your plan provides up to 12 included reviews per hour; 11 remain after this review.

@coderabbitai

coderabbitai Bot commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

Comment thread cub/cub/device/device_for.cuh Resolved
Comment thread cub/cub/device/device_for.cuh Resolved
Comment thread cub/cub/device/device_for.cuh Resolved
@github-project-automation github-project-automation Bot moved this from In Review to In Progress in CCCL Aug 26, 2026

@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: cbd4e601-3346-4d85-9f7c-1677215a9eb4

📥 Commits

Reviewing files that changed from the base of the PR and between 577e221 and 3201778.

📒 Files selected for processing (2)
  • cub/cub/device/device_for.cuh
  • cub/test/catch2_test_device_for_env.cu

Included review availability: Your plan provides up to 12 included reviews per hour; 11 remain after this review.

Comment thread cub/test/catch2_test_device_for_env.cu Resolved
@macdonaldezra
macdonaldezra requested a review from miscco August 26, 2026 17:16
@macdonaldezra
macdonaldezra force-pushed the refactor-device-for-env branch from 7a96f60 to ff3305c Compare September 1, 2026 16:33
@coderabbitai

coderabbitai Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@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: ae10d251-70a3-4255-abc6-116817670d95

📥 Commits

Reviewing files that changed from the base of the PR and between 4a3cb15 and ff3305c.

📒 Files selected for processing (3)
  • cub/cub/device/device_for.cuh
  • cub/test/catch2_test_device_for_api.cu
  • cub/test/catch2_test_device_for_env.cu
🚧 Files skipped from review as they are similar to previous changes (2)
  • cub/test/catch2_test_device_for_api.cu
  • cub/cub/device/device_for.cuh

Included review availability: Your plan provides up to 12 included reviews per hour; 11 remain after this review.

Comment thread cub/test/catch2_test_device_for_env.cu Resolved
@Jacobfaib

This comment was marked as resolved.

@github-actions

This comment has been minimized.

@pauleonix

This comment was marked as resolved.

@github-actions

This comment has been minimized.

@pauleonix

This comment was marked as resolved.

@github-actions

This comment has been minimized.

@github-project-automation github-project-automation Bot moved this from In Progress to In Review in CCCL Sep 7, 2026
Comment thread cub/test/catch2_test_device_for_env.cu Resolved
Comment thread cub/test/catch2_test_device_for_env.cu Resolved
Comment thread cub/test/catch2_test_device_for_env.cu Resolved
@bernhardmgruber

Copy link
Copy Markdown
Contributor

/ok to test c4ee8bd

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@bernhardmgruber

Copy link
Copy Markdown
Contributor

/ok to test b2006d0

@github-actions

This comment has been minimized.

@bernhardmgruber

Copy link
Copy Markdown
Contributor

/ok to test aa20309

@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 2h 55m: Pass: 100%/176 | Total: 8d 14h | Max: 2h 55m | Hits: 33%/569347

See results here.

@pauleonix
pauleonix merged commit 6dfa726 into NVIDIA:main Sep 9, 2026
204 checks passed
@github-project-automation github-project-automation Bot moved this from In Review to Done in CCCL Sep 9, 2026
@pauleonix

Copy link
Copy Markdown
Contributor

Thank you for your contribution! 🎉

@macdonaldezra

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback everyone!

kaatish pushed a commit to kaatish/cccl that referenced this pull request Sep 11, 2026
…#11013)

* [CUB] Refactor `cub::DeviceFor` to always take an environment

* Address PR comments

* Update __for_each_n, remove static assert, and add test case for __for_each_n

* Address code-rabbit comments re const and additional device-ref comment

* Suppress MSVC C4127 on the empty-extent check, which is constant for fully static extents

* Fix graph race condition in threads_multiple_graphs test case

* Revert "Fix graph race condition in threads_multiple_graphs test case"

This reverts commit 6a8e638.

* Use lambdas inside test body and move utility functions to top of file

* Use existing block size extracting function
charan-003 pushed a commit to charan-003/cccl that referenced this pull request Sep 15, 2026
…#11013)

* [CUB] Refactor `cub::DeviceFor` to always take an environment

* Address PR comments

* Update __for_each_n, remove static assert, and add test case for __for_each_n

* Address code-rabbit comments re const and additional device-ref comment

* Suppress MSVC C4127 on the empty-extent check, which is constant for fully static extents

* Fix graph race condition in threads_multiple_graphs test case

* Revert "Fix graph race condition in threads_multiple_graphs test case"

This reverts commit 6a8e638.

* Use lambdas inside test body and move utility functions to top of file

* Use existing block size extracting function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

DeviceFor: migrate two-phase overloads to Env (multi-dimensional) DeviceFor: migrate two-phase overloads to Env (core)

7 participants