Skip to content

[CUB] Refactor cub::DeviceMemcpy to always take an environment - #11590

Merged
bernhardmgruber merged 4 commits into
NVIDIA:mainfrom
SamMausberg:refactor/device-memcpy-env
Sep 30, 2026
Merged

bernhardmgruber merged 4 commits into
NVIDIA:mainfrom
SamMausberg:refactor/device-memcpy-env

Conversation

@SamMausberg

@SamMausberg SamMausberg commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Description

Part of #9875 (the DeviceMemcpy row).

Replaces the trailing cudaStream_t parameter of the two-phase DeviceMemcpy::Batched overload with an execution environment (const EnvT& env = {}). The overload now goes through detail::dispatch_with_env_and_tuning with user-provided storage, so it honors cuda::get_stream and cuda::execution::tune the same way the single-phase overload does. This mirrors the merged DeviceCopy::Batched refactor (#9416) line by line, and follows #11587 and #11589 for DeviceMergeSort and DeviceMerge.

Existing callers keep working: a cudaStream_t, nullptr or literal 0 passed in the last position is accepted (cuda::get_stream handles the first two, the third falls back to the default stream as before). No caller outside the CUB tests uses DeviceMemcpy::Batched in this repository.

The doxygen block of the two-phase overload now documents EnvT and env instead of stream.

Tests

Added to cub/test/catch2_test_device_memcpy_env.cu, restricted to TEST_LAUNCH == 0 since the two-phase overload does not go through the launch wrappers:

  • DeviceMemcpy::Batched works with user provided memory and environment. It runs through test_with_custom_streams for every stream and environment kind the helper covers, checks that the storage query matches the legacy overload, and verifies the copied buffers.
  • DeviceMemcpy::Batched can be tuned with user provided memory, with two block sizes verified via block_size_extracting_constant_iterator.
  • DeviceMemcpy::Batched two-phase overload accepts legacy null stream arguments for nullptr and 0.

Run locally on an RTX 5070 Ti (SM 120), CUDA 13.2, gcc 13.3, preset cub-cpp20 with CMAKE_CUDA_ARCHITECTURES=120. All of the following passed:

  • cub.test.device.memcpy_env.lid_{0,1,2}
  • cub.test.device.memcpy_batched.lid_{0,1,2}
  • cub.test.device.memcpy_env_api.lid_0

In addition, the same tree builds with clang 21 as the host compiler under C++17 (preset cub-cpp17, -Werror -Wall -Wextra) with zero warnings, and cub.test.device.memcpy_env.lid_{0,1,2} and cub.test.device.memcpy_env_api.lid_0 pass in that configuration too.

pre-commit passes on the changed files. I could not build the MSVC or NVHPC configurations locally.

Checklist

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

Developed with Claude Fable 5.1

Fixes: #11701

Part of NVIDIA#9875. Replaces the trailing cudaStream_t parameter of the
two-phase DeviceMemcpy::Batched overload with an execution environment.
The overload now dispatches through dispatch_with_env_and_tuning, so
cuda::get_stream and cuda::execution::tune are honored like in the
single-phase overload, mirroring DeviceCopy::Batched. Passing a
cudaStream_t, nullptr or 0 keeps working.

Adds env tests for the two-phase overload: per-environment-kind runs
with user provided storage that verify the launch stream, tuning
extraction with two block sizes, and the legacy null-stream arguments.

Developed in combination with Claude Fable 5.1
@copy-pr-bot

copy-pr-bot Bot commented Sep 22, 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.

@coderabbitai

coderabbitai Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

⚙️ Run configuration

Configuration used: Repository: NVIDIA/cccl/.coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: e2c0b562-4b7c-43ab-a8e8-14fe953d2d8d

📥 Commits

Reviewing files that changed from the base of the PR and between 3dacc19 and 73d2f10.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Summary

Summary by CodeRabbit

  • New Features

    • DeviceMemcpy::Batched now accepts execution environments to configure stream selection and performance tuning.
    • Temporary-storage operations support default or customized environments. Documentation describes policy-selector tuning for DeviceMemcpy algorithms.
  • Compatibility

    • Calls that pass nullptr or 0 for the stream continue to work.
  • Tests

    • Added coverage for default and custom-stream execution, caller-provided temporary storage, tuned block-size selection, and copy completion.

Walkthrough

DeviceMemcpy::Batched now accepts an execution environment for stream and tuning customization. Documentation describes the environment parameter. Tests cover temporary-storage queries, custom-stream execution, tuning, and nullptr and 0 stream arguments.

Changes

Batched memcpy environment support

Layer / File(s) Summary
Environment API and dispatch
cub/cub/device/device_memcpy.cuh
The temporary-storage overload accepts EnvT, defaults to cuda::std::execution::env<>, and dispatches through environment-provided stream and tuning customization.
Environment and compatibility tests
cub/test/catch2_test_device_memcpy_env.cu
Tests cover temporary-storage queries and execution with custom streams, default-stream arguments, and tuning. Existing tests also use uninitialized destination elements.

Assessment against linked issues

Objective Addressed Explanation
Migrate the two-phase Batched overload from cudaStream_t to const Env& [#11701] ✅
Add tests for the environment-aware overload [#11701] ✅

Suggested reviewers: bernhardmgruber

Priority: ⬇️ Low

Change: Refactor

Merge Risk: 🔵 Low · up to 3dacc

The environment API preserves the inspected dispatch contracts. This is mergeable with a bounded testing follow-up: strengthen the custom-stream test to detect incorrect stream selection.


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

The two-phase environment test now goes through the shared
test_with_custom_streams helper instead of a local sweep over stream
kinds, and result vectors are created with thrust::no_init.

@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_memcpy_env.cu (1)

218-229: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

suggestion: Instrument custom-stream dispatch with stream capture.

cuda::get_stream(env) is valid for all helper inputs and returns the default stream for environments without a custom stream. However, synchronization and vector equality do not prove stream placement. Default-stream ordering can make that check pass after a regression.

For non-default streams, capture the execution call and require a captured graph node. Keep the existing uncaptured call for the result check.

Suggested fix
     REQUIRE(num_bytes == expected_bytes);
 
+    auto stream = cuda::get_stream(env).get();
+    if (stream != nullptr) {
+      cudaGraph_t graph{};
+      size_t graph_nodes = 0;
+      REQUIRE(cudaSuccess == cudaStreamBeginCapture(stream, cudaStreamCaptureModeGlobal));
+      REQUIRE(
+        cudaSuccess == cub::DeviceMemcpy::Batched(temp_storage, num_bytes, input_it, output_it, sizes, num_buffers, env));
+      REQUIRE(cudaSuccess == cudaStreamEndCapture(stream, &graph));
+      REQUIRE(cudaSuccess == cudaGraphGetNodes(graph, nullptr, &graph_nodes));
+      REQUIRE(graph_nodes > 0);
+      REQUIRE(cudaSuccess == cudaGraphDestroy(graph));
+    }
+
     REQUIRE(
       cudaSuccess == cub::DeviceMemcpy::Batched(temp_storage, num_bytes, input_it, output_it, sizes, num_buffers, env));

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: NVIDIA/cccl/.coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: fdd48610-6cbb-4a1a-97e5-1eb3534abdbb

📥 Commits

Reviewing files that changed from the base of the PR and between 7f6d186 and 3dacc19.

📒 Files selected for processing (1)
  • cub/test/catch2_test_device_memcpy_env.cu

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 9 remain after this review.

@bernhardmgruber

Copy link
Copy Markdown
Contributor

/ok to test 3dacc19

@github-actions

This comment has been minimized.

@bernhardmgruber

Copy link
Copy Markdown
Contributor

/ok to test 73d2f10

@github-actions

Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 2h 59m: Pass: 100%/202 | Total: 7d 05h | Max: 2h 48m | Hits: 45%/642469

See results here.

@bernhardmgruber
bernhardmgruber merged commit 1a0e5b1 into NVIDIA:main Sep 30, 2026
224 of 225 checks passed
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.

DeviceMemcpy: migrate two-phase overloads to Env

2 participants