Skip to content

Integrate Reduce::ByKey to prefetching#9698

Closed
gonidelis wants to merge 27 commits into
NVIDIA:mainfrom
gonidelis:prefetch_reducebykey
Closed

Integrate Reduce::ByKey to prefetching#9698
gonidelis wants to merge 27 commits into
NVIDIA:mainfrom
gonidelis:prefetch_reducebykey

Conversation

@gonidelis

Copy link
Copy Markdown
Member

Builds on top of #9496 and integrated cub reduce_by_key to prefetching

Full scan b200 tuning currently running

fixes #9566

gonidelis added 25 commits July 1, 2026 10:38
- Add `enum class BlockLoadPrefetch { none, l2, l1 }` to `BlockLoad`
- Add `Prefetch` template param to `BlockLoad` (last position, default `none`, backward-compatible)
- Implement prefetch via `cuda::apply_access_property` for `BLOCK_LOAD_DIRECT`; static_assert on all other algorithms
- Remove `BLOCK_LOAD_DIRECT_PREFETCH` algorithm enum value
- Wire `load_prefetch` through `ReduceByKeyPolicy` -> dispatch -> `agent_reduce_by_key_policy`
- Fire early prefetch in `ConsumeRange` before look-back stall
- All new paths compile to no-op by default - no SASS change
@gonidelis gonidelis requested review from a team as code owners July 2, 2026 23:50
@github-project-automation github-project-automation Bot moved this to Todo in CCCL Jul 2, 2026
@cccl-authenticator-app cccl-authenticator-app Bot moved this from Todo to In Review in CCCL Jul 2, 2026
@gonidelis gonidelis marked this pull request as draft July 2, 2026 23:52
@copy-pr-bot

copy-pr-bot Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

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.

@cccl-authenticator-app cccl-authenticator-app Bot moved this from In Review to In Progress in CCCL Jul 2, 2026
@gonidelis gonidelis force-pushed the prefetch_reducebykey branch from a6c7371 to 8e46ea3 Compare July 3, 2026 00:00
@coderabbitai

coderabbitai Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Summary by CodeRabbit

  • New Features

    • Added configurable load-prefetch options for block-level data loading and reduce-by-key processing.
    • Expanded tuning so performance settings can now vary by workload and hardware profile.
  • Bug Fixes

    • Improved reduce-by-key handling to include prefetch settings consistently in tuned policy selection.
    • Added broader test coverage for block load behavior across load algorithms and prefetch combinations.

Walkthrough

Adds a BlockLoadPrefetch mechanism to cub::BlockLoad, enabling optional PTX-level prefetch of keys/values. Threads a LoadPrefetch policy parameter through AgentReduceByKey and the ReduceByKey tuning policies, updates SM100 tuning tables and policy selection, and adds a corresponding benchmark tuning knob and tests.

Changes

BlockLoad prefetch feature

Layer / File(s) Summary
BlockLoad prefetch primitive
cub/cub/block/block_load.cuh
Adds detail::BlockLoadPrefetch enum, prefetch_block_load_tile helper emitting inline PTX/TMA bulk prefetch, and a new Prefetch template parameter threaded through all BlockLoad::Load overloads and docs.
AgentReduceByKey policy and tile prefetch wiring
cub/cub/agent/agent_reduce_by_key.cuh
agent_reduce_by_key_policy gains a LoadPrefetch parameter/LOAD_PREFETCH constant; ConsumeRange conditionally prefetches keys and values per tile.
Tuning policy load_prefetch field and dispatch formatting
cub/cub/device/dispatch/tuning/tuning_reduce_by_key.cuh, cub/cub/device/dispatch/dispatch_reduce_by_key.cuh
ReduceByKeyPolicy gains a load_prefetch field used in equality/stream output; SM100 tuning specializations and policy_selector return values set explicit BlockLoadPrefetch values; select_agent_policy forwards it; dispatch file reformats delay-constructor template args.
Benchmark tuning knob and test coverage
cub/benchmarks/bench/reduce/by_key.cu, cub/test/catch2_test_block_load.cu, cub/test/catch2_test_device_reduce_env.cu
Adds TUNE_LOAD_PREFETCH benchmark parameter, extends BlockLoad tests with a Prefetch parameter and new prefetch/cache-modifier test cases, and updates a ReduceByKeyPolicy test initializer.

Assessment against linked issues

Objective Addressed Explanation
Integrate cub::DeviceReduce::ReduceByKey with BlockLoad prefetch (#9566)
Measure impact (#9566) Benchmark tuning knob is added but measurement results are not part of the code diff.

Suggested reviewers: srinivasyadav18, bernhardmgruber


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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
cub/test/catch2_test_block_load.cu (1)

236-261: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

prefetch_kind omits BlockLoadPrefetch::l1.

The "works across all algorithms and cache levels" test only covers {l2, bulk_l2}, leaving the l1 code path in prefetch_block_load_tile (which emits prefetch.global.L1) completely untested.

✅ Add l1 coverage
 using prefetch_kind =
   c2h::enum_type_list<cub::detail::BlockLoadPrefetch,
                       cub::detail::BlockLoadPrefetch::l2,
+                      cub::detail::BlockLoadPrefetch::l1,
                       cub::detail::BlockLoadPrefetch::bulk_l2>;

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 98daa5dc-f11d-4b23-b857-1b72a6a3d7cb

📥 Commits

Reviewing files that changed from the base of the PR and between a1fb0b8 and a6c7371.

📒 Files selected for processing (7)
  • cub/benchmarks/bench/reduce/by_key.cu
  • cub/cub/agent/agent_reduce_by_key.cuh
  • cub/cub/block/block_load.cuh
  • cub/cub/device/dispatch/dispatch_reduce_by_key.cuh
  • cub/cub/device/dispatch/tuning/tuning_reduce_by_key.cuh
  • cub/test/catch2_test_block_load.cu
  • cub/test/catch2_test_device_reduce_env.cu

Comment on lines +745 to +753
// Fire prefetch before look-back stall so data arrives in L2 in time
if constexpr (AgentReduceByKeyPolicyT::LOAD_PREFETCH != detail::BlockLoadPrefetch::none)
{
const int items_to_prefetch = static_cast<int>(::cuda::std::min(num_remaining, static_cast<OffsetT>(TILE_ITEMS)));
detail::prefetch_block_load_tile<BLOCK_THREADS, 128, AgentReduceByKeyPolicyT::LOAD_PREFETCH>(
threadIdx.x, d_keys_in + tile_offset, items_to_prefetch);
detail::prefetch_block_load_tile<BLOCK_THREADS, 128, AgentReduceByKeyPolicyT::LOAD_PREFETCH>(
threadIdx.x, d_values_in + tile_offset, items_to_prefetch);
}

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.

🚀 Performance & Scalability | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Confirm CacheModifiedInputIterator's iterator_concept / to_address support
rg -n "class CacheModifiedInputIterator" -A 80 cub/cub/iterator/cache_modified_input_iterator.cuh | rg -n "iterator_concept|iterator_category|to_address|contiguous"

Repository: NVIDIA/cccl

Length of output: 260


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Map the relevant files and read the smallest useful slices.
for f in \
  cub/cub/iterator/cache_modified_input_iterator.cuh \
  cub/cub/block/block_load.cuh \
  cub/cub/agent/agent_reduce_by_key.cuh \
  cub/cub/agent/agent_reduce_by_key.tuning.cuh \
  cub/cub/iterator/util_type.cuh
do
  echo "=== $f ==="
  wc -l "$f"
done

echo
echo "=== cache_modified_input_iterator.cuh excerpt ==="
sed -n '1,220p' cub/cub/iterator/cache_modified_input_iterator.cuh

echo
echo "=== block_load.cuh prefetch-related excerpt ==="
rg -n "prefetch|can_prefetch|contiguous_iterator|CacheModifiedInputIterator" -A 8 -B 8 cub/cub/block/block_load.cuh

echo
echo "=== agent_reduce_by_key.cuh wrapping/prefetch excerpt ==="
sed -n '180,220p' cub/cub/agent/agent_reduce_by_key.cuh
echo
sed -n '730,760p' cub/cub/agent/agent_reduce_by_key.cuh

echo
echo "=== tuning references ==="
rg -n "load_prefetch|BlockLoadPrefetch::l2|LOAD_MODIFIER|LOAD_DEFAULT" -A 4 -B 4 cub/cub/agent cub/cub | head -n 200

Repository: NVIDIA/cccl

Length of output: 551


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "=== locate tuning file ==="
fd -a 'agent_reduce_by_key' cub/cub/agent

echo
echo "=== cache_modified_input_iterator.cuh relevant lines ==="
rg -n "class CacheModifiedInputIterator|iterator_category|iterator_concept|to_address|contiguous_iterator|operator\\+|operator\\*" -A 6 -B 6 cub/cub/iterator/cache_modified_input_iterator.cuh

echo
echo "=== block_load.cuh prefetch and iterator constraints ==="
rg -n "prefetch|can_prefetch_block_load|contiguous_iterator|CacheModifiedInputIterator|load_modifier" -A 8 -B 8 cub/cub/block/block_load.cuh

echo
echo "=== agent_reduce_by_key.cuh wrapping and prefetch sites ==="
sed -n '185,215p' cub/cub/agent/agent_reduce_by_key.cuh
echo
sed -n '740,758p' cub/cub/agent/agent_reduce_by_key.cuh

Repository: NVIDIA/cccl

Length of output: 21012


important: prefetch needs the unwrapped input iterator prefetch_block_load_tile is suppressed for CacheModifiedInputIterator, so calling it on d_keys_in + tile_offset / d_values_in + tile_offset is a no-op for raw-pointer inputs. That leaves LOAD_PREFETCH ineffective on the common pointer-based DeviceReduce::ReduceByKey path; prefetch the raw pointer before wrapping, or use a separate unwrapped prefetch path.

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

😬 CI Workflow Results

🟥 Finished in 2h 42m: Pass: 96%/287 | Total: 12d 22h | Max: 2h 41m | Hits: 19%/1087839

See results here.

@gonidelis

Copy link
Copy Markdown
Member Author

I am closing this in favor of the new design #9723

@gonidelis gonidelis closed this Jul 8, 2026
@github-project-automation github-project-automation Bot moved this from In Progress to Done in CCCL Jul 8, 2026
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.

Integrate cub::DeviceReduce::ReduceByKey to BlockLoad prefetch and measure impact.

1 participant