Repository navigation
Conversation
This comment has been minimized.
This comment has been minimized.
f62f547 to
3c05a4a
Compare
OverviewThis PR refactors Core ChangesCUB Device Select Refactoring (
|
| Layer / File(s) | Summary |
|---|---|
DeviceSelect const EnvT& parameter signatures cub/cub/device/device_select.cuh |
Non-temp-storage overloads for Flagged, If, Unique, UniqueByKey, and FlaggedIf update the env parameter from EnvT by value to const EnvT& with default env = {}. |
DeviceSelect d_temp_storage environment dispatch cub/cub/device/device_select.cuh |
All d_temp_storage overloads replace stream-parameterized execution with const EnvT& env and route kernels through dispatch_with_env_and_tuning before calling detail::select::dispatch or detail::unique_by_key::dispatch. |
CUB test coverage for environment-based APIs cub/test/catch2_test_device_select_flagged.cu, cub/test/catch2_test_device_select_flagged_if.cu, cub/test/catch2_test_device_select_if.cu, cub/test/catch2_test_device_select_unique.cu, cub/test/catch2_test_device_select_unique_by_key_env.cu |
New test cases validate temp-storage sizing (nullptr query then allocation) and correctness across multiple execution environments: cudaStream_t, cuda::stream, cuda::stream_ref, cuda::std::execution::env, cuda::execution::gpu, and cuda::execution::gpu with stream binding. |
PSTL backend updates for existing algorithms libcudacxx/include/cuda/std/__pstl/cuda/copy_if.h, libcudacxx/include/cuda/std/__pstl/cuda/remove_if.h, libcudacxx/include/cuda/std/__pstl/cuda/shift_left.h, libcudacxx/include/cuda/std/__pstl/cuda/unique.h, libcudacxx/include/cuda/std/__pstl/dispatch.h |
copy_if, remove_if, and shift_left backends pass execution policy instead of CUDA stream to CUB device APIs and use raw-pointer storage accessors. PSTL unique is refactored to use DeviceSelect::Unique directly and remove DispatchSelectIf templating. Dispatch enum adds __unique_copy. |
PSTL unique_copy backend implementation libcudacxx/include/cuda/std/__pstl/cuda/unique_copy.h, libcudacxx/include/cuda/std/__pstl/unique_copy.h |
New CUDA backend dispatch for PSTL unique_copy: __par_impl queries DeviceSelect::Unique temp-storage, allocates via local helper, launches kernel into output iterator, copies selected count to host, and returns output iterator advanced by count. operator() routes random-access paths through CUDA backend with error mapping and fallback to ::cuda::std::unique_copy. |
important: Possibly related PRs
- NVIDIA/cccl#8880: Both PRs modify
cub/cub/device/device_select.cuhto routeDeviceSelectthroughdetail::dispatch_with_env_and_tuningfor environment-based/tuning dispatch.
important: Suggested labels
backport branch/3.3.x, backport branch/3.4.x
important: Suggested reviewers
- gonidelis
- shwina
- gevtushenko
Comment @coderabbitai help to get the list of available commands and usage tips.
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
libcudacxx/include/cuda/std/__pstl/cuda/find_if.h (1)
33-33:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winsuggestion: add a direct
#include <cub/detail/choose_offset.cuh>. This change now namesCUB_NS_QUALIFIER::detail::choose_offset_texplicitly, but the file only gets that declaration transitively through<cub/device/device_find.cuh>, which makes the header brittle ifdevice_find.cuhstops pulling it in. As per coding guidelines, files must include all headers for the symbols they use, no transitive header inclusions are allowed, and the most precise include should be used.Also applies to: 75-75
Source: Coding guidelines
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 95310559-3026-4b80-a2fb-9e2139d95ed9
📒 Files selected for processing (6)
cub/cub/detail/env_dispatch.cuhcub/cub/device/device_find.cuhcub/cub/device/device_select.cuhcub/test/catch2_test_device_find_env.cucub/test/catch2_test_device_select_unique_by_key.culibcudacxx/include/cuda/std/__pstl/cuda/find_if.h
|
I have no idea why some unrelated code is suddenly broken 😿 |
This comment has been minimized.
This comment has been minimized.
| auto d_temp = c2h::device_vector<uint8_t>(expected_allocation_size, thrust::no_init); | ||
| void* temp_storage = thrust::raw_pointer_cast(d_temp.data()); | ||
|
|
||
| auto test_unique_by_key = [&](const auto& env) { |
There was a problem hiding this comment.
The test only calls the no-predicate one, can we add a call passing cuda::std::equal_to<>{} + env to cover the modified sfinae?
|
insane, I've been chasing that CI bug for 2 hours. It's super not minimally reproducable. I guess the only solution would be to move the env tests to a separate file. it's some sort of c2h, gcc12, libcu++/thrust messiness with the new env test in the same TU, a shared iterator |
3c05a4a to
d080ad9
Compare
This comment has been minimized.
This comment has been minimized.
d080ad9 to
a57c6f1
Compare
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (2)
libcudacxx/include/cuda/std/__pstl/cuda/unique_copy.h (1)
39-40: 💤 Low valuesuggestion:
<cuda/std/__algorithm/unique.h>appears unused; onlyunique_copyis called in the fallback path (line 163). Consider removing line 39 to avoid transitive-include bloat.Source: Coding guidelines
cub/test/catch2_test_device_select_if.cu (1)
204-222: ⚡ Quick winsuggestion: This env-path test is missing an exact result check. It only proves that the prefix satisfies
leand the suffix stays default-initialized; it never checks the exact selected count or stable output against a reference. That leaves room for env-dispatch regressions that drop matching elements or reorder them while still passing this test.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 20cbf943-5a0a-47ec-96b0-764b3f2ca5a2
📒 Files selected for processing (13)
cub/cub/device/device_select.cuhcub/test/catch2_test_device_select_flagged.cucub/test/catch2_test_device_select_flagged_if.cucub/test/catch2_test_device_select_if.cucub/test/catch2_test_device_select_unique.cucub/test/catch2_test_device_select_unique_by_key.culibcudacxx/include/cuda/std/__pstl/cuda/copy_if.hlibcudacxx/include/cuda/std/__pstl/cuda/remove_if.hlibcudacxx/include/cuda/std/__pstl/cuda/shift_left.hlibcudacxx/include/cuda/std/__pstl/cuda/unique.hlibcudacxx/include/cuda/std/__pstl/cuda/unique_copy.hlibcudacxx/include/cuda/std/__pstl/dispatch.hlibcudacxx/include/cuda/std/__pstl/unique_copy.h
✅ Files skipped from review due to trivial changes (1)
- libcudacxx/include/cuda/std/__pstl/dispatch.h
🚧 Files skipped from review as they are similar to previous changes (1)
- cub/test/catch2_test_device_select_unique_by_key.cu
| C2H_TEST("DeviceSelect::FlaggedIf works with user provided memory and environment", | ||
| "[device][select_if]", | ||
| all_types, | ||
| flag_types) |
There was a problem hiding this comment.
important: The new env test is tagged for select_if, not select_flagged_if. Filtered select_flagged_if runs will skip this coverage, which makes the new dispatch path easy to miss in targeted testing.
| C2H_TEST("DeviceSelect::Flagged works with user provided memory and environment", "[device][select_flagged]", all_types) | ||
| { | ||
| using type = typename c2h::get<0, TestType>; | ||
|
|
||
| const int num_items = GENERATE_COPY(take(2, random(1, 1000000))); | ||
| c2h::device_vector<type> in(num_items); | ||
| c2h::device_vector<type> out(num_items); | ||
| c2h::gen(C2H_SEED(2), in); | ||
|
|
||
| c2h::device_vector<int> flags(num_items); | ||
| c2h::gen(C2H_SEED(1), flags, 0, 1); | ||
| const int num_selected = static_cast<int>(thrust::count(c2h::device_policy, flags.begin(), flags.end(), 1)); | ||
| const c2h::host_vector<type> reference = get_reference(in, flags); | ||
|
|
||
| // Needs to be device accessible | ||
| c2h::device_vector<int> num_selected_out(1, 0); | ||
| int* d_num_selected_out = thrust::raw_pointer_cast(num_selected_out.data()); | ||
|
|
||
| size_t expected_allocation_size = 0; | ||
| auto error = cub::DeviceSelect::Flagged( | ||
| static_cast<void*>(nullptr), | ||
| expected_allocation_size, | ||
| in.data(), | ||
| flags.begin(), | ||
| out.data(), | ||
| d_num_selected_out, | ||
| num_items); | ||
| REQUIRE(error == cudaSuccess); | ||
| REQUIRE(cudaSuccess == cudaPeekAtLastError()); | ||
| REQUIRE(cudaSuccess == cudaDeviceSynchronize()); | ||
|
|
||
| auto d_temp = c2h::device_vector<uint8_t>(expected_allocation_size, thrust::no_init); | ||
| void* temp_storage = thrust::raw_pointer_cast(d_temp.data()); | ||
|
|
||
| auto test_flagged = [&](const auto& env) { | ||
| size_t num_bytes = 0; | ||
| error = cub::DeviceSelect::Flagged( | ||
| static_cast<void*>(nullptr), num_bytes, in.data(), flags.begin(), out.data(), d_num_selected_out, num_items, env); | ||
| REQUIRE(error == cudaSuccess); | ||
| REQUIRE(cudaSuccess == cudaPeekAtLastError()); | ||
| REQUIRE(cudaSuccess == cudaDeviceSynchronize()); | ||
| REQUIRE(expected_allocation_size == num_bytes); | ||
|
|
||
| error = cub::DeviceSelect::Flagged( | ||
| temp_storage, num_bytes, in.data(), flags.begin(), out.data(), d_num_selected_out, num_items, env); | ||
| REQUIRE(error == cudaSuccess); | ||
| REQUIRE(cudaSuccess == cudaPeekAtLastError()); | ||
| REQUIRE(cudaSuccess == cudaDeviceSynchronize()); | ||
|
|
||
| out.resize(num_selected_out[0]); | ||
| REQUIRE(num_selected == num_selected_out[0]); | ||
| REQUIRE(reference == out); | ||
| }; | ||
|
|
||
| int current_device; | ||
| error = cudaGetDevice(¤t_device); | ||
| REQUIRE(error == cudaSuccess); | ||
|
|
||
| SECTION("DeviceSelect::Flagged works with cudaStream_t") | ||
| { | ||
| cuda::stream stream{cuda::devices[current_device]}; | ||
| test_flagged(stream.get()); | ||
| } | ||
|
|
||
| SECTION("DeviceSelect::Flagged works with cuda::stream") | ||
| { | ||
| cuda::stream stream{cuda::devices[current_device]}; | ||
| test_flagged(stream); | ||
| } | ||
|
|
||
| SECTION("DeviceSelect::Flagged works with cuda::stream_ref") | ||
| { | ||
| cuda::stream stream{cuda::devices[current_device]}; | ||
| cuda::stream_ref stream_ref{stream}; | ||
| test_flagged(stream_ref); | ||
| } | ||
|
|
||
| SECTION("DeviceSelect::Flagged works with cuda::std::execution::env") | ||
| { | ||
| cuda::std::execution::env env{}; | ||
| test_flagged(env); | ||
| } | ||
|
|
||
| SECTION("DeviceSelect::Flagged works with cuda::execution::gpu") | ||
| { | ||
| const auto policy = cuda::execution::gpu; | ||
| test_flagged(policy); | ||
| } | ||
|
|
||
| SECTION("DeviceSelect::Flagged works with cuda::execution::gpu with stream") | ||
| { | ||
| cuda::stream stream{cuda::devices[current_device]}; | ||
| const auto policy = cuda::execution::gpu.with(cuda::get_stream, stream); | ||
| test_flagged(policy); | ||
| } | ||
| } |
There was a problem hiding this comment.
important: The new env-path tests should be isolated into separate translation units. The PR discussion already ties same-TU env tests to unrelated gcc12/c2h/libcu++ failures, and these additions keep the same pattern in cub/test/catch2_test_device_select_flagged.cu, cub/test/catch2_test_device_select_flagged_if.cu, cub/test/catch2_test_device_select_if.cu, and cub/test/catch2_test_device_select_unique.cu. Move the env-only coverage into dedicated .cu files so the new API paths stop perturbing the legacy test instantiations. Based on PR discussion about the translation-unit interaction.
There was a problem hiding this comment.
didn't know you were reading our discussions @coderabbitai :)
There was a problem hiding this comment.
@gonidelis — yes, PR comments and discussion are part of the context I use to calibrate review feedback. The translation-unit interaction you described is exactly why I flagged the placement concern.
(´• ω •`) 🐇
🧠 Learnings used
Learnt from: sleeepyjack
Repo: NVIDIA/cccl PR: 8857
File: cudax/include/cuda/experimental/__cuco/__hyperloglog/hyperloglog_impl.cuh:426-428
Timestamp: 2026-05-06T23:47:17.597Z
Learning: In the CCCL/CUDA C++ Core Libraries codebase (NVIDIA/cccl), treat `_CCCL_API` as host-and-device callable. `_CCCL_API` expands to `_CCCL_HOST_DEVICE` via `libcudacxx/include/cuda/std/__cccl/visibility.h`, so functions marked with `_CCCL_API` may be invoked from both host and device code. When reviewing device-code contexts, do not flag `_CCCL_API`-annotated functions as if they were host-only.
Learnt from: NaderAlAwar
Repo: NVIDIA/cccl PR: 9171
File: libcudacxx/include/cuda/std/__cccl/diagnostic.h:126-126
Timestamp: 2026-06-03T13:24:36.356Z
Learning: When reviewing code that uses NVIDIA NVCC/NVRTC diagnostic suppression pragmas, treat `nv_diag_suppress` as supporting comma-separated warning numbers in a single directive. For example, `_Pragma("nv_diag_suppress 1444,20199")` (or an equivalent `#pragma nv_diag_suppress 1444,20199`) is valid; do not flag comma-separated warning IDs inside `nv_diag_suppress` pragmas as unsupported.
There was a problem hiding this comment.
Since you are at it, if you can find why the unrelated failure goes away once in separate TUs that owuld be great :)
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Find vsmem-related tests in CUB
rg -rn "vsmem" cub/test/ --type=cu -C3 | head -80
# Find what "shared iterator" is used in device_select tests
rg -rn "shared" cub/test/catch2_test_device_select --type=cu -C3 | head -60
# Look for value_type or iterator_traits specializations in c2h or catch2_test_device_select_common
rg -rn "value_type" cub/test/catch2_test_device_select_common.cuh -C3 | head -60
# Look for the env test in unique_by_key (mentioned in PR discussion as TU trigger)
rg -rn "value_type\|unsigned int\|uint32_t\|shared" cub/test/catch2_test_device_select_unique_by_key.cu -C3 | head -60
# Look for template specializations that might cause ODR issues
rg -rn "template.*specializ\|iterator_traits\|thrust::iterator_traits" cub/test/ --type=cu -C3 | head -60Length of output: 1114
🏁 Script executed:
#!/bin/bash
# Search for vsmem in the broader cub test suite
rg -rn "vsmem" cub/ -C3 | head -100
# Look at the catch2_test_device_select_unique_by_key.cu new test content
cat cub/test/catch2_test_device_select_unique_by_key.cu | head -200
# Check what iterator types are being used with thrust::copy in select tests
rg -rn "thrust::copy\|vsmem\|shared_iter\|SharedIter" cub/ -C5 | head -100Length of output: 12544
🏁 Script executed:
#!/bin/bash
# Look at the vsmem test for device select if
cat cub/test/catch2_test_device_select_if_vsmem.cu
# Check what shared iterators are used in unique_by_key test - specifically thrust::copy usage
rg -n "thrust::copy\|counting_iterator\|value_type\|shared" cub/test/catch2_test_device_select_unique_by_key.cu -C3 | head -100
# Check the c2h shared iterator infrastructure
rg -rn "shared\|value_type" cub/cub/iterator/ -C3 | head -80
rg -rn "SharedIter\|shared_iter" cub/ -C3 | head -80
# Look for value_type in catch2_test_device_select_if_vsmem.cu
rg -n "value_type\|thrust::copy\|counting_iterator\|uint\|unsigned" cub/test/catch2_test_device_select_if_vsmem.cu -C3 | head -80Length of output: 2546
🏁 Script executed:
#!/bin/bash
# Get the full unique_by_key test to find thrust::copy and shared iterator usage
cat cub/test/catch2_test_device_select_unique_by_key.cu | tail -300
# Search for thrust::copy in all select test files
rg -n "thrust::copy" cub/test/ -C5 | head -100
# Check cuda::counting_iterator default template argument
rg -rn "counting_iterator" cub/cub/ -C3 | head -40
rg -rn "class counting_iterator\|struct counting_iterator\|counting_iterator =" libcudacxx/include/cuda/ -C5 | head -60
# Check if there's a default template parameter for counting_iterator that maps to unsigned int
rg -rn "counting_iterator" libcudacxx/include/cuda/iterator -C5 | head -60
fd "counting_iterator" libcudacxx/include/Length of output: 26945
@gonidelis — found the failing test and the likely mechanism:
The affected test is "DeviceSelect::UniqueByKey works and uses vsmem for large types" in catch2_test_device_select_unique_by_key.cu:
auto vals_it = cuda::transform_iterator(cuda::counting_iterator(0U), index_to_huge_type_op_t<val_type>{});
// ...
thrust::copy(vals_it, vals_it + num_items, reference_vals.begin()); // reference_vals is host_vector<HugeType>cuda::counting_iterator(0U) — CTAD deduces value_type = unsigned int. The transform iterator's value_type should then be resolved by Thrust's result_of<index_to_huge_type_op_t<val_type>(unsigned int)> → the huge custom type. That's what makes thrust::copy work.
Why adding the env test breaks it in the same TU:
The new test adds #include <cuda/devices> and #include <cuda/std/execution>. Those headers introduce additional template specializations and overloads that become visible to Thrust's iterator trait machinery — specifically thrust::result_of or thrust::invoke_result — when resolving value_type for the transform iterator. With gcc12, this resolution path gets perturbed and falls back to the base iterator's value_type (unsigned int) instead of the functor's return type (HugeType). thrust::copy then sees a source with value_type = unsigned int copying into host_vector<HugeType> → type mismatch failure.
Why separate TUs fix it: the new env-related headers are absent from the TU that contains the vsmem test, so result_of resolves correctly without interference.
The underlying bug is a gcc12-specific sensitivity in Thrust's transform_iterator value-type deduction when additional cuda::std/execution-policy specializations become visible in the same TU. Worth filing against libcudacxx/Thrust with that minimal reproducer pattern (thrust::result_of + cuda::transform_iterator + gcc12 + <cuda/std/execution> in scope).
🧠 Learnings used
Learnt from: sleeepyjack
Repo: NVIDIA/cccl PR: 8857
File: cudax/include/cuda/experimental/__cuco/__hyperloglog/hyperloglog_impl.cuh:426-428
Timestamp: 2026-05-06T23:47:17.597Z
Learning: In the CCCL/CUDA C++ Core Libraries codebase (NVIDIA/cccl), treat `_CCCL_API` as host-and-device callable. `_CCCL_API` expands to `_CCCL_HOST_DEVICE` via `libcudacxx/include/cuda/std/__cccl/visibility.h`, so functions marked with `_CCCL_API` may be invoked from both host and device code. When reviewing device-code contexts, do not flag `_CCCL_API`-annotated functions as if they were host-only.
Learnt from: NaderAlAwar
Repo: NVIDIA/cccl PR: 9171
File: libcudacxx/include/cuda/std/__cccl/diagnostic.h:126-126
Timestamp: 2026-06-03T13:24:36.356Z
Learning: When reviewing code that uses NVIDIA NVCC/NVRTC diagnostic suppression pragmas, treat `nv_diag_suppress` as supporting comma-separated warning numbers in a single directive. For example, `_Pragma("nv_diag_suppress 1444,20199")` (or an equivalent `#pragma nv_diag_suppress 1444,20199`) is valid; do not flag comma-separated warning IDs inside `nv_diag_suppress` pragmas as unsupported.
This comment has been minimized.
This comment has been minimized.
|
@miscco i pushed a split into two different TUs it should fix the cuda12 gcc12 container ci bug. feel free to undo it if you don't want it |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
cub/test/catch2_test_device_select_unique_by_key_env.cu (1)
146-146: 📐 Maintainability & Code Quality | ⚡ Quick winsuggestion: Line 146 asserts identical temp-storage size across env variants, but env-based dispatch can legally select different policies/tunings and therefore different
num_bytes. Keep the per-env query/launch flow, but relax this toREQUIRE(num_bytes > 0 || num_items == 0)(or just drop the equality assertion) so the test validates correctness instead of an implementation detail.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 58a3d517-e478-4922-973c-ac6cf6dcb9bd
📒 Files selected for processing (2)
cub/test/catch2_test_device_select_unique_by_key.cucub/test/catch2_test_device_select_unique_by_key_env.cu
💤 Files with no reviewable changes (1)
- cub/test/catch2_test_device_select_unique_by_key.cu
This comment has been minimized.
This comment has been minimized.
We want to be able to pass tunings even to the APIs that currently only take a stream. Refactor so that we can pass an arbitrary environment to those APIs that take user provided memory
3b85cc5 to
cf96faa
Compare
😬 CI Workflow Results🟥 Finished in 2h 31m: Pass: 17%/287 | Total: 9d 18h | Max: 2h 31m | Hits: 34%/202241See results here. |
We want to be able to pass tunings even to the APIs that currently only take a stream.
Refactor so that we can pass an arbitrary environment to those APIs that take user provided memory
DeviceFindIf#9318