Repository navigation
[CUB] Refactor cub::DeviceMergeSort to always take an environment - #11587
Conversation
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. Note Reviews pausedIt 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 Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository: NVIDIA/cccl/.coderabbit.yaml Review profile: CHILL Plan: Enterprise Run ID: 📒 Files selected for processing (1)
Included review availability: Your plan provides up to 12 included reviews per hour; 11 remain after this review. 📝 SummarySummary by CodeRabbit
Walkthrough
ChangesMerge sort execution environments
Suggested reviewers: Merge Risk: ⚪ Minimal · up to No merge-blocking issue was identified; the change is ready for normal merge checks. Comment |
23a6c2e to
308715b
Compare
|
I have added a checklist for what kind of tests to add for a new single-phase API: #11488. Please apply those guidelines. |
|
I added the For item 6, the comparator is required, so every environment test uses one. The tuning tests mix a custom comparator with an environment. I didn’t add a separate comparator test; let me know if you want me to include one. |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository: NVIDIA/cccl/.coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 4c5d0558-b8a2-4071-9001-3027fa3974f6
📒 Files selected for processing (1)
cub/test/catch2_test_device_merge_sort_env.cu
Included review availability: Your plan provides up to 12 included reviews per hour; 11 remain after this review.
bernhardmgruber
left a comment
There was a problem hiding this comment.
Thanks for the corrections. I further updated our guidance (see #11609) and flagged some more nits below. Mostly looks good already.
| auto d_keys_out = c2h::device_vector<int>(7); | ||
| auto d_values_out = c2h::device_vector<int>(7); |
There was a problem hiding this comment.
Suggestion: Any output vector can be left uninitialized.
| auto d_keys_out = c2h::device_vector<int>(7); | |
| auto d_values_out = c2h::device_vector<int>(7); | |
| auto d_keys_out = c2h::device_vector<int>(7, thrust::no_init); | |
| auto d_values_out = c2h::device_vector<int>(7, thrust::no_init); |
There was a problem hiding this comment.
@CodeRabbit why didn't you suggest this? We have the rule perf.vector-init-before-overwrite in the review guidelines.
There was a problem hiding this comment.
@bernhardmgruber suggestion: I missed perf.vector-init-before-overwrite here. SortPairsCopy writes every element of both output vectors, so their initial values are unnecessary. Your thrust::no_init change avoids that initialization.
✏️ Learnings added
Learnt from: bernhardmgruber
Repo: NVIDIA/cccl PR: 11587
File: cub/test/catch2_test_device_merge_sort_env.cu:448-449
Timestamp: 2026-09-23T08:23:05.022Z
Learning: In CCCL CUB tests, apply the `perf.vector-init-before-overwrite` review rule to output `c2h::device_vector` objects. If an operation fully writes an output vector before any read, construct it with `thrust::no_init` to avoid unnecessary initialization.
Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.
You are interacting with an AI system.
| SECTION("cudaStream_t") | ||
| { | ||
| run(stream.get(), cuda::stream_ref{stream}); | ||
| } | ||
|
|
||
| SECTION("cuda::stream") | ||
| { | ||
| run(stream, cuda::stream_ref{stream}); | ||
| } | ||
|
|
||
| SECTION("cuda::stream_ref") | ||
| { | ||
| run(cuda::stream_ref{stream}, cuda::stream_ref{stream}); | ||
| } | ||
|
|
||
| SECTION("environment with stream") | ||
| { | ||
| run(stdexec::env{cuda::stream_ref{stream}}, cuda::stream_ref{stream}); | ||
| } | ||
|
|
||
| SECTION("cuda::execution::gpu") | ||
| { | ||
| run(cuda::execution::gpu, default_stream); | ||
| } | ||
|
|
||
| SECTION("cuda::execution::gpu with stream") | ||
| { | ||
| run(cuda::execution::gpu.with(cuda::get_stream, cuda::stream_ref{stream}), cuda::stream_ref{stream}); | ||
| } |
There was a problem hiding this comment.
Remark: I find this overkill, since it essentially just checks whether the cuda::get_stream query works for an environment. One test with a custom stream type should be enough IMO.
@miscco for vis, since I think you introduces some of these checks.
No change needed, we may just want to refactor this in the future across all of CUB's tests.
There was a problem hiding this comment.
I dont believe those are superfluous. We had multiple issues with stream_ref + rmm::stream_ref + eenvironment, so I really want to make sure that we do test:
- cudaStream_t
- cuda::stream_ref
- env with stream
(4. cuda::execution::gpu)
There was a problem hiding this comment.
That's fine, but do we have to do this for each CUB algorithm? They all boil down to cuda::get_stream handling them.
But as I said, no blocker.
| size_t expected_bytes_allocated{}; | ||
| REQUIRE( | ||
| cudaSuccess | ||
| == cub::DeviceMergeSort::SortKeys( | ||
| nullptr, expected_bytes_allocated, d_keys.data().get(), static_cast<int>(d_keys.size()), cuda::std::less<int>{})); | ||
|
|
||
| const cuda::stream_ref stream_ref{stream}; | ||
| auto env = stdexec::env{stream_ref, expected_allocation_size(expected_bytes_allocated)}; |
There was a problem hiding this comment.
Important: Please only use the check for expected_bytes_allocated in the "uses environment" test. That should be enough.
| REQUIRE(cudaSuccess == cudaStreamDestroy(custom_stream)); | ||
| } | ||
|
|
||
| CUB_TEST_CASE("DeviceMergeSort::SortKeys uses custom stream", "[merge_sort][device]", CUB_SMALL) |
There was a problem hiding this comment.
Important: Please guard any "uses custom stream" test under #if TEST_LAUNCH != 1
|
I adjusted the tests per your comments and #11609, hope I'm not causing any more work. |
|
/ok to test 7c230cc |
This comment has been minimized.
This comment has been minimized.
|
Remaining failures will be fixed in #11617, so we need to rebase once that lands. |
Guards the "uses custom stream" tests with TEST_LAUNCH != 1 and removes their expected_allocation_size check, which the "uses environment" tests already cover (NVIDIA#11609). Output vectors are constructed with thrust::no_init.
Part of NVIDIA#9875. Replaces the trailing cudaStream_t parameter of the two-phase SortKeys, SortKeysCopy, SortPairs, SortPairsCopy, StableSortKeys, StableSortKeysCopy and StableSortPairs overloads, and of the private NoNVTX helpers behind them, with an execution environment. The two-phase overloads now dispatch through dispatch_with_env_and_tuning, so cuda::get_stream and cuda::execution::tune are honored like in the single-phase overloads. Passing a cudaStream_t, nullptr or 0 keeps working. Adds env tests for the two-phase overloads: 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
Adds the missing "uses custom stream" tests for SortKeys, StableSortPairs and SortPairsCopy and switches the SortPairs and StableSortKeys ones to a stream_ref from c2h::make_current_device_stream(), so all seven variants follow the checklist from NVIDIA#11488. The SortPairs tuning test now uses the launch wrapper under the same guard as the other six. The legacy stream test keeps only the nullptr form. Developed in combination with Claude Fable 5.1
…am test Passing 0 for the former cudaStream_t parameter deduces EnvT as int and must keep running on the default stream, so the test covers it next to nullptr. Developed in combination with Claude Fable 5.1
7c230cc to
7607bc8
Compare
|
Rebased onto main now that #11617 is in. Reran the merge sort tests locally and they all pass. |
|
/ok to test bccd7d5 |
This comment has been minimized.
This comment has been minimized.
|
/ok to test 8c17012 |
🥳 CI Workflow Results🟩 Finished in 2h 49m: Pass: 100%/202 | Total: 9d 11h | Max: 2h 45m | Hits: 39%/741882See results here. AI failure analysis1. RAFT CUDA compilation: sccache could not archive missing NVCC stub output · 1 jobExplanation: NVCC was invoked through sccache v0.17.0-rapids.4, and no compiler diagnostic preceded sccache aborting while packaging outputs because an NVCC-generated temporary stub was absent. The PR changes only CUB DeviceMergeSort implementation and tests, not the failing external RAFT test or cache configuration, so the evidence indicates a cache/tooling failure rather than a demonstrated source compilation error. Evidence: Copy this prompt into a coding agentJobs: |
Description
Part of #9875 (the
DeviceMergeSortrow).Replaces the trailing
cudaStream_tparameter of the two-phaseSortKeys,SortKeysCopy,SortPairs,SortPairsCopy,StableSortKeys,StableSortKeysCopyandStableSortPairsoverloads, and of the private*NoNVTXhelpers behind them, with an execution environment (const EnvT& env = {}). The two-phase overloads now go throughdetail::dispatch_with_env_and_tuningwith user-provided storage, so they honorcuda::get_streamandcuda::execution::tunethe same way the single-phase overloads do. This follows the pattern of the merged sibling refactors (#9416, #9455 to #9459, #9463, #9464, #9552 to #9555, #11013).Existing callers keep working: a
cudaStream_tornullptrpassed in the last position is accepted throughcuda::get_stream, and a literal0still compiles and runs on the default stream. Callers checked:detail::merge_sort::dispatchdirectly and is unaffected.cuda::std::__pstlsort and the cudax multi-GPU HSS sort use the single-phase overloads and are unaffected.c/parallel.v2/src/merge_sort.cuJIT-compiles a call with a barecudaStream_t, which the environment query accepts (same situation asfor.cuafter [CUB] Refactorcub::DeviceForto always take an environment #11013).cub/test/catch2_test_device_merge_sort_custom_policy_hub.cupassesnullptrfor the stream and still compiles and passes.The doxygen blocks of the two-phase overloads now document
EnvTandenvinstead ofstream.Tests
Added to
cub/test/catch2_test_device_merge_sort_env.cu, restricted toTEST_LAUNCH == 0since the two-phase overloads do not go through the launch wrappers:<overload> works with user provided memory and environmentfor all seven overloads. Each runs once per environment kind (default env,cudaStream_t,cuda::stream,cuda::stream_ref, env withstream_ref,cuda::execution::gpu,gpu.with(get_stream, ...)), checks that the storage query matches the legacy overload, and usesstream_registry_factory_tto fail if a kernel is launched on any stream other than the one carried by the environment (or the default stream when it carries none).<overload> can be tuned with user provided memoryfor all seven overloads, with two block sizes verified viablock_size_extracting_op.DeviceMergeSort::SortKeys two-phase overload accepts null stream argumentsfornullptrand0, the forms callers of the formercudaStream_t stream = nullptrparameter pass.uses custom streamtest from the Document env-overload testing conventions in CUB test guide #11488 checklist with astream_reffromc2h::make_current_device_stream(); three were missing and two used a hand-created stream. TheSortPairs can be tunedtest now sits under the same launch guard and wrapper as the other six.Run locally on an RTX 5070 Ti (SM 120), CUDA 13.2, gcc 13.3, preset
cub-cpp20withCMAKE_CUDA_ARCHITECTURES=120. All of the following passed:cub.test.device.merge_sort_env.lid_{0,1,2}cub.test.device.merge_sort.lid_{0,1,2}cub.test.device.merge_sort_iterators.lid_{0,1,2}cub.test.device.merge_sort_vsmem.lid_{0,1,2}cub.test.device.merge_sort_api.lid_0,cub.test.device.merge_sort_env_api.lid_0,cub.test.device.merge_sort_custom_policy_hub.lid_0In addition, the same tree builds with clang 21 as the host compiler under C++17 (preset
cub-cpp17,-Werror -Wall -Wextra) with zero warnings, andcub.test.device.merge_sort_env.lid_{0,1,2},cub.test.device.merge_sort_api.lid_0,cub.test.device.merge_sort_env_api.lid_0andcub.test.device.merge_sort_custom_policy_hub.lid_0pass in that configuration too.For the C library v2 caller, I reproduced HostJIT's device-side
clang -cc1invocation (itscuda_minimalstubs, freestanding mode, NVTX and exceptions disabled, sm_120) on a translation unit that calls the two-phaseSortPairsCopyandSortKeysCopywith a barecudaStream_tthe wayc/parallel.v2/src/merge_sort.cuemits them; it compiles and emits the block-sort, partition and merge kernels. This used clang 21 rather than the LLVM 22 that libnvcc pins, so I passed an SDK version clang 21 recognizes.pre-commitpasses on the changed files. I could not build the MSVC or NVHPC configurations locally, nor the C library itself (libnvcc builds LLVM from source).Checklist
Developed with Claude Fable 5.1
Fixes: #11699