Repository navigation
Unify atomic and two-phase reduction - #9349
Conversation
|
Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually. Contributors can view more details about this message here. |
7ae35ed to
d6cad28
Compare
|
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:
📝 WalkthroughWalkthroughThe PR unifies reduce’s deterministic and nondeterministic paths under a shared ChangesUnify nondeterministic reduce under StableReductionOrder
Suggested reviewers
Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 33959b11-5fd6-48e6-b8ca-66c2bf741cb7
📒 Files selected for processing (9)
c/parallel/include/cccl/c/reduce.hc/parallel/src/reduce.cucub/cub/device/device_reduce.cuhcub/cub/device/dispatch/dispatch_reduce.cuhcub/cub/device/dispatch/dispatch_reduce_nondeterministic.cuhcub/cub/device/dispatch/kernels/kernel_reduce.cuhcub/cub/device/dispatch/tuning/tuning_reduce.cuhcub/cub/device/dispatch/tuning/tuning_reduce_nondeterministic.cuhcub/test/catch2_test_device_reduce_nondeterministic.cu
💤 Files with no reviewable changes (2)
- cub/cub/device/dispatch/dispatch_reduce_nondeterministic.cuh
- cub/cub/device/dispatch/tuning/tuning_reduce_nondeterministic.cuh
| CUB_RUNTIME_FUNCTION static constexpr size_t InitSize() | ||
| { | ||
| return sizeof(InitValueT); | ||
| } |
There was a problem hiding this comment.
important: InitSize() is now used to zero the atomic output buffer, but it returns sizeof(InitValueT). Valid not_guaranteed calls can still have an init type narrower than AccumT/the output element type, and then only the low bytes of d_out[0] get cleared before the fetch_add path runs. That makes the reduction accumulate onto garbage. This needs to use the accumulator/output element size instead.
| if constexpr (UseAtomics) | ||
| { | ||
| reduce_grid_size = ::cuda::std::max(1, reduce_grid_size); |
There was a problem hiding this comment.
suggestion: ::cuda::std::max was added here without a direct <cuda/std/algorithm> include. That currently relies on transitive headers and makes this header fragile.
As per coding guidelines, files must include all headers related to the symbols that they are using; no transitive header inclusions are allowed.
Source: Coding guidelines
This comment has been minimized.
This comment has been minimized.
fa08256 to
494ffff
Compare
I think this is fine and no action is required. The user asks for no guarantees and gets them by accident. This is fine.
Yes. I added the static assertions. |
There was a problem hiding this comment.
♻️ Duplicate comments (2)
cub/cub/device/dispatch/dispatch_reduce.cuh (2)
101-105:⚠️ Potential issue | 🟠 Major | ⚡ Quick winimportant:
InitSize()returnssizeof(InitValueT), but line 655 uses it to memsetd_out[0]which holdsAccumT. Whensizeof(InitValueT) < sizeof(AccumT)(e.g.,intinit withdoubleaccumulator), only partial bytes are cleared, leaving garbage in the high bytes before atomic adds accumulate onto it.Return
sizeof(AccumT)or use a dedicated output-element size.
686-692: 🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick winsuggestion:
::cuda::std::maxis used but<cuda/std/algorithm>is not directly included. This relies on transitive inclusion which the coding guidelines prohibit.Source: Coding guidelines
🧹 Nitpick comments (3)
cub/cub/device/dispatch/kernels/kernel_reduce.cuh (1)
188-196: 💤 Low valuesuggestion: The
AgentReducePolicy<0, 0, ...>instantiation passes zeros for threads and items. This works becauseNoScalingreceives the actual values andAgentReducereads them from the scaling policy. However, passing explicit zeros is confusing sinceAgentReducePolicy::BLOCK_THREADSandITEMS_PER_THREADwill be 0. Consider adding a brief comment explaining that the actual values come fromNoScaling.cub/cub/device/dispatch/dispatch_reduce.cuh (2)
629-629: 💤 Low valuesuggestion:
sm_countandsm_occupancyare initialized to 0 then immediately assigned. Per coding guidelines, preferconstwith direct initialization or restructure to avoid mutable temporaries.- int sm_count = 0; - if (const auto error = CubDebug(launcher_factory.MultiProcessorCount(sm_count))) + int sm_count_tmp = 0; + if (const auto error = CubDebug(launcher_factory.MultiProcessorCount(sm_count_tmp))) { return error; } + const int sm_count = sm_count_tmp;Also applies to: 637-637
Source: Coding guidelines
594-602: 💤 Low valuesuggestion:
get_device_ptrcasts and dereferences avoid*asvoid**. This works for the intended use case but is fragile. Consider a template or documenting the strict aliasing implications.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 280d0787-db37-4886-b24b-def7cfdc318d
📒 Files selected for processing (9)
c/parallel/include/cccl/c/reduce.hc/parallel/src/reduce.cucub/cub/device/device_reduce.cuhcub/cub/device/dispatch/dispatch_reduce.cuhcub/cub/device/dispatch/dispatch_reduce_nondeterministic.cuhcub/cub/device/dispatch/kernels/kernel_reduce.cuhcub/cub/device/dispatch/tuning/tuning_reduce.cuhcub/cub/device/dispatch/tuning/tuning_reduce_nondeterministic.cuhcub/test/catch2_test_device_reduce_nondeterministic.cu
💤 Files with no reviewable changes (2)
- cub/cub/device/dispatch/tuning/tuning_reduce_nondeterministic.cuh
- cub/cub/device/dispatch/dispatch_reduce_nondeterministic.cuh
✅ Files skipped from review due to trivial changes (1)
- c/parallel/include/cccl/c/reduce.h
🚧 Files skipped from review as they are similar to previous changes (3)
- cub/test/catch2_test_device_reduce_nondeterministic.cu
- cub/cub/device/device_reduce.cuh
- c/parallel/src/reduce.cu
494ffff to
51bfd8e
Compare
This comment has been minimized.
This comment has been minimized.
NaderAlAwar
left a comment
There was a problem hiding this comment.
Important: cub/benchmarks/bench/reduce/nondeterministic.cu:23 still refers to the deleted cub::detail::reduce_nondeterministic namespace
51bfd8e to
1793c8f
Compare
There was a problem hiding this comment.
Actionable comments posted: 3
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
c/parallel/src/reduce.cu (1)
153-168: 🩺 Stability & Availability | 🟠 Major | ⚡ Quick winimportant: validate
buildbefore any compile work.Line 153 accepts a public C API output pointer, but the function later writes through
buildwithout checking for null. Matchload,cleanup, andlink_ltoirby returningCUDA_ERROR_INVALID_VALUE.try { + if (build == nullptr) + { + return CUDA_ERROR_INVALID_VALUE; + } + if (determinism == CCCL_NOT_GUARANTEED && (op.type != CCCL_PLUS || output_it.type != CCCL_POINTER))As per path instructions,
c/**/*reviews focus on C API/ABI stability and error/status handling.Source: Path instructions
🧹 Nitpick comments (1)
cub/cub/device/dispatch/kernels/kernel_reduce.cuh (1)
166-166: 📐 Maintainability & Code Quality | 🔵 Trivialsuggestion: track or resolve this TODO with the exact nvc++ failure mode. As written, the workaround is hard to validate or remove later. I can help draft a focused regression check or tracking issue.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 814d75af-10cc-4066-93b9-6a32fd7c9c94
📒 Files selected for processing (11)
c/parallel/include/cccl/c/reduce.hc/parallel/src/reduce.cuc/parallel/test/test_reduce.cppcub/cub/device/device_reduce.cuhcub/cub/device/dispatch/dispatch_reduce.cuhcub/cub/device/dispatch/dispatch_reduce_nondeterministic.cuhcub/cub/device/dispatch/kernels/kernel_reduce.cuhcub/cub/device/dispatch/tuning/tuning_reduce.cuhcub/cub/device/dispatch/tuning/tuning_reduce_nondeterministic.cuhcub/test/catch2_test_device_reduce_env.cucub/test/catch2_test_device_reduce_nondeterministic.cu
💤 Files with no reviewable changes (2)
- cub/cub/device/dispatch/tuning/tuning_reduce_nondeterministic.cuh
- cub/cub/device/dispatch/dispatch_reduce_nondeterministic.cuh
🚧 Files skipped from review as they are similar to previous changes (5)
- cub/test/catch2_test_device_reduce_nondeterministic.cu
- cub/cub/device/device_reduce.cuh
- cub/cub/device/dispatch/tuning/tuning_reduce.cuh
- cub/cub/device/dispatch/dispatch_reduce.cuh
- cub/test/catch2_test_device_reduce_env.cu
This comment has been minimized.
This comment has been minimized.
83f810f to
96676d2
Compare
96676d2 to
f53f5b2
Compare
| // Return if the caller is simply requesting the size of the storage allocation | ||
| if (d_temp_storage == nullptr) | ||
| { | ||
| temp_storage_bytes = 1; | ||
| return cudaSuccess; | ||
| } |
There was a problem hiding this comment.
I believe we can factor that out of the if
There was a problem hiding this comment.
I had this initially and then MSVC complained because you can't put a constexpr bool inside an expression of a runtime if, since in some cases that is always true or false, and MSVC will recommend to use if constexpr instead ;)
This comment has been minimized.
This comment has been minimized.
|
|
||
| std::unique_ptr<char[]> payload(static_cast<char*>(build_ptr->payload)); | ||
| std::free(build_ptr->runtime_policy); | ||
| delete static_cast<cub::detail::reduce::policy_selector*>(build_ptr->runtime_policy); |
There was a problem hiding this comment.
Suggestion: since this was allocated with std::malloc, isn't it preferable to use std::free?
There was a problem hiding this comment.
It's allocated with:
auto policy_ptr = std::make_unique<cub::detail::reduce::policy_selector>(policy_sel);
which uses new. Should be fine.
🥳 CI Workflow Results🟩 Finished in 1h 13m: Pass: 100%/291 | Total: 3d 03h | Max: 1h 05m | Hits: 99%/199557See results here. |
This PR tries to unify the atomic/non-deterministic and two-phase reduction dispatches and kernels. The goal is simplification and elimination of redundancy.
But I am not sure the code is actually more readable, not sure.@NaderAlAwar also agrees the direction is good.