Skip to content

Add CUB Prefetch as an option to all cub::BlockLoad algorithms - #9496

Closed
gonidelis wants to merge 25 commits into
NVIDIA:mainfrom
gonidelis:prefetch_block_load
Closed

gonidelis wants to merge 25 commits into
NVIDIA:mainfrom
gonidelis:prefetch_block_load

Conversation

@gonidelis

@gonidelis gonidelis commented Jun 16, 2026 •

Copy link
Copy Markdown
Member

fixes #9588 and #9589 with one shot

Introduce prefetching as an enum option to all BlockLoad algorithms and add tests

The value options for prefetching are {l2, l1, bulk_l2, none}. None is no-op and only l2 and bulk_l2 (TMA) are currently supported. If user tries L1 it will just dispatch to L2 silently.

Native ptx assembly is being used, until instructions is exposed as a library option later on.

  • Make prefetching default tuning for if !is_primitive<input_t>

Current PR is expected to emit no SASS diffs with main

@copy-pr-bot

copy-pr-bot Bot commented Jun 16, 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 Todo to In Progress in CCCL Jun 16, 2026
@gonidelis
gonidelis force-pushed the prefetch_block_load branch from 7dde881 to dccf644 Compare June 24, 2026 23:18
@gonidelis
gonidelis marked this pull request as ready for review June 25, 2026 00:00
@gonidelis
gonidelis requested review from a team as code owners June 25, 2026 00:00
@cccl-authenticator-app cccl-authenticator-app Bot moved this from In Progress to In Review in CCCL Jun 25, 2026
@gonidelis
gonidelis requested a review from fbusato June 25, 2026 00:01
@coderabbitai

coderabbitai Bot commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It 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 reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds BlockLoad direct-load prefetch support, extends block-load tests, and threads a separate load-prefetch setting through reduce-by-key tuning, dispatch, and benchmarking.

Changes

BlockLoad prefetch support

Layer / File(s) Summary
Prefetch contract
cub/cub/block/block_load.cuh
Adds BlockLoadPrefetch, helper utilities for tile prefetch hints, the BlockLoad template parameter, and stream output/documentation.
Direct load execution
cub/cub/block/block_load.cuh
Updates BLOCK_LOAD_DIRECT load paths to issue prefetch hints when enabled and keeps prefetch disabled for the other algorithms with static_assert.
BlockLoad prefetch tests
cub/test/catch2_test_block_load.cu
Extends the test harness with the prefetch template argument, cache-modifier coverage, and a direct-load L2 prefetch case.

Reduce-by-key prefetch wiring

Layer / File(s) Summary
Policy and tuning
cub/cub/device/dispatch/tuning/tuning_reduce_by_key.cuh
Adds a load_prefetch field to ReduceByKeyPolicy and includes it in equality, formatting, and default construction.
Agent and dispatch wiring
cub/cub/agent/agent_reduce_by_key.cuh, cub/cub/device/device_run_length_encode.cuh, cub/cub/device/dispatch/dispatch_reduce_by_key.cuh
Threads load_prefetch through reduce-by-key execution and dispatch call sites.
Benchmark selector
cub/benchmarks/bench/reduce/by_key.cu
Adds a tuning axis for load prefetch and maps it into the selected reduce-by-key policy.

Assessment against linked issues

Objective Addressed Explanation
Add prefetching as an enum option to BlockLoad::BLOCK_LOAD_DIRECT with {none, l1, l2} and L1→L2 dispatch [#9588] ❓ BlockLoadPrefetch and the direct-load prefetch path are present, but the summaries do not show explicit L1-to-L2 dispatch behavior.

Out-of-scope changes

Code Change Explanation
Reduce-by-key load_prefetch tuning field and policy formatting/defaults (cub/cub/device/dispatch/tuning/tuning_reduce_by_key.cuh) This adds a separate reduce-by-key tuning axis, not part of the BlockLoad direct-prefetch objective in #9588.
Reduce-by-key agent/device/benchmark plumbing (cub/cub/agent/agent_reduce_by_key.cuh, cub/cub/device/device_run_length_encode.cuh, cub/cub/device/dispatch/dispatch_reduce_by_key.cuh, cub/benchmarks/bench/reduce/by_key.cu) These changes wire prefetch through reduce-by-key execution and benchmarking rather than the BlockLoad API requested by #9588.

Suggested reviewers

  • fbusato
  • shwina

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: 4

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

216-232: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

suggestion: Add a brief comment explaining the #if IPT == 1 guard. The test hardcodes items_per_thread = 4, so the guard exists only to register the case once across the IPT-parametrized builds, not because of a platform constraint. Without a note the intent is opaque.

As per path instructions: "If a test is unsupported, expected to fail, disabled, or skipped on a platform, motivate it with a comment".

Source: Path instructions

cub/cub/device/dispatch/tuning/tuning_reduce_by_key.cuh (1)

1644-1652: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

suggestion: policy_selector_from_hub rebuilds ReduceByKeyPolicy without forwarding ReduceByKeyPolicyT::LOAD_PREFETCH, so load_prefetch silently falls back to none. Harmless today since every hub tuning defaults to none, but the moment a compile-time tuning sets a prefetch level this path will drop it. Forward it for symmetry with the runtime selectors.

       ReduceByKeyPolicyT::SCAN_ALGORITHM,
       lookback_delay_policy_from_type<typename ReduceByKeyPolicyT::detail::delay_constructor_t>,
+      ReduceByKeyPolicyT::LOAD_PREFETCH,
     };
cub/cub/agent/agent_reduce_by_key.cuh (1)

745-751: 🎯 Functional Correctness | 🔵 Trivial

suggestion: LOAD_PREFETCH is independent of LOAD_ALGORITHM here, so this can emit prefetch hints even on non-BLOCK_LOAD_DIRECT paths. If that exception is intentional, document it; otherwise add an explicit guard before issuing the hints.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 52afeb81-727b-458c-9d07-510a403a77db

📥 Commits

Reviewing files that changed from the base of the PR and between 2b4b3db and 38b607f.

📒 Files selected for processing (6)
  • 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

Comment thread cub/cub/block/block_load.cuh Outdated
#include <cub/util_ptx.cuh>
#include <cub/util_type.cuh>

#include <cuda/__annotated_ptr/apply_access_property.h>

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.

📐 Maintainability & Code Quality | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Locate the header that defines cuda::access_property
fd -t f . --full-path -p '*__annotated_ptr*' | sort
rg -nP 'struct\s+access_property|class\s+access_property|namespace\s+__ap' -g '*annotated_ptr*' -C2

Repository: NVIDIA/cccl

Length of output: 254


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect the target file and locate the declaration/definition of cuda::access_property.
git ls-files 'cub/cub/block/block_load.cuh' 'cuda/**' | sed -n '1,120p'

echo '--- block_load.cuh ---'
cat -n cub/cub/block/block_load.cuh | sed -n '1,120p'

echo '--- search access_property ---'
rg -n --hidden --glob '!**/build/**' --glob '!**/.git/**' 'access_property' cuda cub libcudacxx thrust c 2>/dev/null | sed -n '1,200p'

Repository: NVIDIA/cccl

Length of output: 34556


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Find headers related to apply_access_property and access_property.
fd -a -t f 'apply_access_property.h' .
fd -a -t f 'access_property' .

Repository: NVIDIA/cccl

Length of output: 1404


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo '--- file size ---'
wc -l cub/cub/block/block_load.cuh

echo '--- block_load.cuh (relevant lines) ---'
sed -n '1,120p' cub/cub/block/block_load.cuh | cat -n

echo '--- all occurrences of access_property ---'
rg -n --hidden --glob '!**/build/**' --glob '!**/.git/**' 'access_property' . | sed -n '1,240p'

echo '--- candidate headers containing access_property definition ---'
rg -n --hidden --glob '!**/build/**' --glob '!**/.git/**' 'struct\s+access_property|enum\s+class\s+access_property|namespace\s+access_property' cuda libcudacxx cub thrust c | sed -n '1,240p'

Repository: NVIDIA/cccl

Length of output: 40925


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Narrow search to CUDA headers that could define cuda::access_property.
fd -a -t f 'apply_access_property.h' cuda
fd -a -t f 'access_property.h' cuda
fd -a -t f '__access_property' cuda

Repository: NVIDIA/cccl

Length of output: 241


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo '--- block_load.cuh ---'
sed -n '1,120p' cub/cub/block/block_load.cuh | cat -n

echo '--- access_property references ---'
rg -n --hidden --glob '!**/build/**' --glob '!**/.git/**' 'access_property' cuda cub libcudacxx thrust c | sed -n '1,240p'

echo '--- candidate defining headers ---'
fd -a -t f 'access_property.h' cuda libcudacxx cub thrust c
fd -a -t f 'apply_access_property.h' cuda libcudacxx cub thrust c

Repository: NVIDIA/cccl

Length of output: 40264


important: include <cuda/__annotated_ptr/access_property.h> explicitly here; ::cuda::access_property::normal is used directly, so this header should not rely on the transitive include from <cuda/__annotated_ptr/apply_access_property.h>.

Source: Coding guidelines

Comment thread cub/cub/block/block_load.cuh Resolved
Comment thread cub/cub/block/block_load.cuh Resolved
Comment thread cub/cub/block/block_load.cuh Resolved
@github-actions

This comment has been minimized.

Comment thread cub/cub/block/block_load.cuh Resolved
Comment thread cub/cub/block/block_load.cuh Resolved
Comment thread cub/cub/block/block_load.cuh Resolved
Comment thread cub/cub/block/block_load.cuh Resolved
Comment thread cub/cub/block/block_load.cuh Resolved
Comment thread cub/cub/block/block_load.cuh Resolved
Comment thread cub/test/catch2_test_block_load.cu Resolved

@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.

♻️ Duplicate comments (1)
cub/cub/device/dispatch/tuning/tuning_reduce_by_key.cuh (1)

41-44: 🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win

critical: inserting load_prefetch before scan_algorithm breaks the many positional ReduceByKeyPolicy{...} initializers that were not updated below. For example, Line 1040 now feeds BLOCK_SCAN_WARP_SCANS into load_prefetch, so the SM80/SM90/SM100 selector tables stop compiling. Keep the new field at the end of the aggregate or update every positional initializer to pass BlockLoadPrefetch::none explicitly.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: fd11b43c-09ed-4480-84c7-eff772d0c110

📥 Commits

Reviewing files that changed from the base of the PR and between 5a91918 and 7bebc38.

📒 Files selected for processing (5)
  • cub/cub/block/block_load.cuh
  • cub/cub/device/device_run_length_encode.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
🚧 Files skipped from review as they are similar to previous changes (2)
  • cub/test/catch2_test_block_load.cu
  • cub/cub/block/block_load.cuh

@github-actions

This comment has been minimized.

@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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
cub/cub/device/dispatch/tuning/tuning_reduce_by_key.cuh (1)

43-52: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

important: Adding load_prefetch here makes the existing 6-field aggregate initializers silently keep compiling, but policy_selector_from_hub still constructs ReduceByKeyPolicy without ReduceByKeyPolicyT::LOAD_PREFETCH on Lines 1645-1652. Any hub-based selector with a non-default prefetch will therefore be downgraded back to detail::BlockLoadPrefetch::none, so the dispatch layer never sees the tuned value. Please thread LOAD_PREFETCH through that conversion too. As per path instructions, "Focus on algorithm correctness, temporary-storage protocol, dispatch/policy selection, stream behavior, CUDA error handling, synchronization, memory access safety, performance regressions, and test coverage."

Source: Path instructions


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 716bb11e-fd08-4c27-855e-e2626b5cdab4

📥 Commits

Reviewing files that changed from the base of the PR and between 28b7dca and 5ad52a2.

📒 Files selected for processing (1)
  • cub/cub/device/dispatch/tuning/tuning_reduce_by_key.cuh

@github-actions

This comment has been minimized.

Comment thread cub/cub/agent/agent_reduce_by_key.cuh Outdated
// 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)));

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.

Suggestion: prefetching is not a memory access wrt. to access checking, so we can prefetch any amount of data, even out of bounds (we just later must not actually touch out of bounds data, as usual). Since the min has a small cost and creates a branch and two code paths, maybe we are more efficient with just always prefetching the entire tile (thus loading more data then necessary in the last tile):

Suggested change
const int items_to_prefetch = static_cast<int>(::cuda::std::min(num_remaining, static_cast<OffsetT>(TILE_ITEMS)));
const int items_to_prefetch = TILE_ITEMS;

Can you try to measure the impact?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that will take some time as TMA work preempts. I 'll leave your comment open as a reminder before we wrap

Comment thread cub/cub/block/block_load.cuh Resolved
Comment thread cub/cub/block/block_load.cuh Resolved
Comment thread cub/cub/block/block_load.cuh Outdated
if constexpr (can_prefetch_block_load<RandomAccessIterator>)
{
using input_t = cub::detail::it_value_t<RandomAccessIterator>;
const ::cuda::std::size_t total_bytes = static_cast<::cuda::std::size_t>(items_to_prefetch) * sizeof(input_t);

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.

Suggestion: we probably never prefetch more than 4GiB of data, so the total_bytes and offset below could just be a 32-bit unsigned int.

@gevtushenko

Copy link
Copy Markdown
Collaborator

I am a bit torn ... maybe the user really wanted the cache modifier.

@bernhardmgruber we discussed this offline, we should not unwrap cache modified iterators. User might've expressed intent to skip caching to preserve other data in cache. If we unwrap iterator on a minor update, we'll evict some cache all of a suden and likely cause unpleasent performance investigation.

The two options are: consider prefetch as a hint and skip it for iterators, or static assert and fail to compile.

@gonidelis

Copy link
Copy Markdown
Member Author

beautiful results - encodings incoming. worth noting that many tunings have .prf_1 at the end which means prefetching helped.

ggonidelis@D4K4HYVQXG umbriel-b200-072 % ~/dev/cccl/benchmarks/scripts/analyze.py -R='.*' --top=2  *.db
cub.bench.reduce.by_key[KeyT{ct}=I32, ValueT{ct}=I8, OffsetT{ct}=I32]:
                                                          variant     score  mins     means      maxs
382  ipt_15.tpb_224.trp_0.ld_0.ns_220.dcid_5.l2w_650.prf_1 (7.db)  1.060842   1.0  1.055629  1.153846
445  ipt_15.tpb_224.trp_0.ld_0.ns_596.dcid_7.l2w_150.prf_0 (7.db)  1.055916   1.0  1.051331  1.153846
cub.bench.reduce.by_key[KeyT{ct}=I32, ValueT{ct}=I16, OffsetT{ct}=I32]:
                                                          variant     score      mins     means      maxs
547  ipt_15.tpb_224.trp_1.ld_0.ns_112.dcid_7.l2w_790.prf_1 (6.db)  1.159591  1.000000  1.142592  1.279262
505  ipt_15.tpb_224.trp_0.ld_0.ns_580.dcid_5.l2w_640.prf_1 (2.db)  1.134074  0.939665  1.118701  1.261795
cub.bench.reduce.by_key[KeyT{ct}=I32, ValueT{ct}=I32, OffsetT{ct}=I32]:
                                                          variant     score      mins     means      maxs
721  ipt_22.tpb_128.trp_1.ld_1.ns_712.dcid_7.l2w_325.prf_1 (6.db)  1.147089  0.997917  1.129629  1.310492
729  ipt_22.tpb_160.trp_1.ld_1.ns_260.dcid_0.l2w_750.prf_1 (0.db)  1.135921  0.929688  1.119375  1.296594
cub.bench.reduce.by_key[KeyT{ct}=I32, ValueT{ct}=I64, OffsetT{ct}=I32]:
                                                           variant     score      mins     means      maxs
553  ipt_18.tpb_128.trp_1.ld_1.ns_352.dcid_1.l2w_1000.prf_0 (1.db)  1.113159  0.886029  1.099018  1.268657
576   ipt_18.tpb_160.trp_1.ld_0.ns_368.dcid_7.l2w_960.prf_1 (7.db)  1.107240  0.994350  1.094440  1.231307
cub.bench.reduce.by_key[KeyT{ct}=I32, ValueT{ct}=I128, OffsetT{ct}=I32]:
                                                          variant     score      mins     means      maxs
76  ipt_11.tpb_128.trp_1.ld_0.ns_172.dcid_2.l2w_1110.prf_0 (0.db)  1.023318  0.998840  1.030449  1.153846
77   ipt_11.tpb_128.trp_1.ld_0.ns_172.dcid_2.l2w_890.prf_0 (3.db)  1.022080  0.992456  1.029595  1.153846
cub.bench.reduce.by_key[KeyT{ct}=I32, ValueT{ct}=F64, OffsetT{ct}=I32]:
                                                          variant     score     mins     means      maxs
338  ipt_14.tpb_128.trp_1.ld_1.ns_116.dcid_1.l2w_675.prf_1 (7.db)  1.088496  1.00000  1.078048  1.178835
388  ipt_15.tpb_128.trp_1.ld_0.ns_588.dcid_0.l2w_830.prf_0 (0.db)  1.077604  0.95514  1.067971  1.166664
cub.bench.reduce.by_key[KeyT{ct}=I64, ValueT{ct}=I16, OffsetT{ct}=I32]:
                                                          variant     score      mins     means      maxs
218  ipt_10.tpb_256.trp_0.ld_1.ns_204.dcid_2.l2w_840.prf_0 (3.db)  1.060259  1.000000  1.053234  1.133961
816   ipt_9.tpb_224.trp_0.ld_1.ns_348.dcid_2.l2w_680.prf_0 (6.db)  1.047353  0.998162  1.041739  1.111886
cub.bench.reduce.by_key[KeyT{ct}=I64, ValueT{ct}=I32, OffsetT{ct}=I32]:
                                                          variant     score      mins     means      maxs
410  ipt_15.tpb_160.trp_1.ld_0.ns_144.dcid_2.l2w_460.prf_0 (2.db)  1.056840  1.000000  1.050106  1.131681
412  ipt_15.tpb_160.trp_1.ld_0.ns_152.dcid_2.l2w_455.prf_0 (3.db)  1.056437  0.997243  1.049734  1.131617
cub.bench.reduce.by_key[KeyT{ct}=I64, ValueT{ct}=I128, OffsetT{ct}=I32]:
                                                         variant     score  mins     means      maxs
761  ipt_9.tpb_160.trp_1.ld_0.ns_292.dcid_2.l2w_855.prf_0 (5.db)  1.039613   1.0  1.040452  1.153846
779  ipt_9.tpb_160.trp_1.ld_1.ns_452.dcid_2.l2w_695.prf_0 (4.db)  1.031713   1.0  1.032396  1.133333
cub.bench.reduce.by_key[KeyT{ct}=I64, ValueT{ct}=F32, OffsetT{ct}=I32]:
                                                         variant     score      mins     means      maxs
804  ipt_9.tpb_192.trp_1.ld_1.ns_136.dcid_2.l2w_535.prf_0 (2.db)  1.066794  0.998162  1.058861  1.136407
794  ipt_9.tpb_192.trp_1.ld_0.ns_136.dcid_2.l2w_540.prf_0 (2.db)  1.066762  0.998162  1.058833  1.136407
cub.bench.reduce.by_key[KeyT{ct}=I64, ValueT{ct}=F64, OffsetT{ct}=I32]:
                                                           variant     score      mins     means      maxs
601   ipt_19.tpb_160.trp_1.ld_0.ns_764.dcid_7.l2w_155.prf_0 (1.db)  1.141686  1.000000  1.126421  1.275834
553  ipt_19.tpb_160.trp_1.ld_0.ns_1448.dcid_7.l2w_105.prf_0 (0.db)  1.128135  0.848193  1.109163  1.250848
cub.bench.reduce.by_key[KeyT{ct}=I32, ValueT{ct}=F32, OffsetT{ct}=I32]:
                                                          variant     score      mins     means      maxs
162  ipt_12.tpb_256.trp_1.ld_0.ns_328.dcid_7.l2w_630.prf_1 (1.db)  1.110952  1.000000  1.098896  1.204693
164  ipt_12.tpb_256.trp_1.ld_0.ns_416.dcid_2.l2w_265.prf_0 (2.db)  1.103624  0.933333  1.090912  1.252996
cub.bench.reduce.by_key[KeyT{ct}=I64, ValueT{ct}=I8, OffsetT{ct}=I32]:
                                                          variant     score      mins     means      maxs
412  ipt_13.tpb_160.trp_1.ld_0.ns_164.dcid_1.l2w_685.prf_1 (6.db)  1.075069  1.000000  1.066168  1.165174
429  ipt_13.tpb_160.trp_1.ld_1.ns_164.dcid_1.l2w_685.prf_1 (0.db)  1.074178  0.997159  1.065482  1.160753
cub.bench.reduce.by_key[KeyT{ct}=I64, ValueT{ct}=I64, OffsetT{ct}=I32]:
                                                          variant     score      mins     means      maxs
369  ipt_11.tpb_256.trp_1.ld_0.ns_540.dcid_7.l2w_145.prf_0 (2.db)  1.015146  0.998162  1.013808  1.066528
390  ipt_11.tpb_256.trp_1.ld_1.ns_540.dcid_7.l2w_145.prf_0 (0.db)  1.015141  0.989071  1.013704  1.064449

@gonidelis
gonidelis force-pushed the prefetch_block_load branch from 3dc95c1 to 6826bd2 Compare June 30, 2026 19:02
@github-actions

This comment has been minimized.

@gonidelis
gonidelis force-pushed the prefetch_block_load branch from 1223650 to 6dfff87 Compare July 1, 2026 01:36
@github-actions

This comment has been minimized.

@gonidelis
gonidelis force-pushed the prefetch_block_load branch from 6dfff87 to f853fbe Compare July 1, 2026 17:45
@gonidelis
gonidelis force-pushed the prefetch_block_load branch from f853fbe to d99482b Compare July 1, 2026 17:58
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

static constexpr BlockLoadAlgorithm load_algorithm = BLOCK_LOAD_DIRECT;
using delay_constructor = exponential_backon_jitter_window_constructor_t<2044, 240>;
static constexpr CacheLoadModifier load_modifier = LOAD_CA;
static constexpr BlockLoadPrefetch load_prefetch = BlockLoadPrefetch::none;

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.

Suggestion: Please avoid making changes to the obsolete tunings and policy hubs.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in separate pr

@gonidelis gonidelis changed the title Add CUB BLOCK_LOAD_DIRECT_PREFETCH Add CUB Prefetch as an option to all cub::BlockLoad algorithms Jul 2, 2026
@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

😬 CI Workflow Results

🟥 Finished in 2h 46m: Pass: 97%/287 | Total: 12d 08h | Max: 2h 45m | Hits: 19%/1060811

See results here.

@bernhardmgruber bernhardmgruber 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.

In principle, LGTM. Just a lot of nits, so I'll wait another round before approving.

Comment on lines +241 to +242
//! Enumerates the cache levels that :cpp:class:`cub::BlockLoad` can prefetch into when using
//! :cpp:enumerator:`cub::BLOCK_LOAD_DIRECT`.

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.

Maybe:

Suggested change
//! Enumerates the cache levels that :cpp:class:`cub::BlockLoad` can prefetch into when using
//! :cpp:enumerator:`cub::BLOCK_LOAD_DIRECT`.
//! Enumerates the cache levels that :cpp:class:`cub::BlockLoad` can prefetch into.

Comment on lines +244 to +253
//! Pass as the ``Prefetch`` template argument of :cpp:class:`cub::BlockLoad`. The default is
//! ``detail::BlockLoadPrefetch::none`` (no prefetch).
//!
//! .. note::
//! Prefetch hints are silently suppressed for :cpp:class:`cub::CacheModifiedInputIterator`.
//! Those iterators apply a load modifier (e.g. ``LOAD_NC``) that routes loads through a
//! specific cache path. Emitting a prefetch hint for the same addresses would touch caches
//! the user explicitly chose to bypass, defeating the purpose of the modifier.
//! Prefetch hints are also suppressed for any iterator type that is not a contiguous
//! memory iterator (i.e., one for which ``std::to_address`` is not applicable).

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.

Strong suggestion: This documents behavior of BlockLoad, not the enum. So this comment should be moved to BlockLoad.

//! @endrst
enum class BlockLoadPrefetch : int
{
//! No prefetch hint emitted. Default behavior, identical to not specifying a prefetch level.

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.

Suggested change
//! No prefetch hint emitted. Default behavior, identical to not specifying a prefetch level.
//! No prefetch hint emitted.

{
//! No prefetch hint emitted. Default behavior, identical to not specifying a prefetch level.
none,
//! Emit an L2 prefetch hint (``prefetch.global.L2``) before loading each cache line.

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.

Suggested change
//! Emit an L2 prefetch hint (``prefetch.global.L2``) before loading each cache line.
//! Emit an L2 prefetch hint (``prefetch.global.L2``) to all affected cache lines.

//! No prefetch hint emitted. Default behavior, identical to not specifying a prefetch level.
none,
//! Emit an L2 prefetch hint (``prefetch.global.L2``) before loading each cache line.
//! All threads collectively cover the tile, each issuing one instruction per cache line.

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.

Suggestion: Again, I would document this at the level of BlockLoad

Applies to the other enumerators as well.

{
using input_t = cub::detail::it_value_t<RandomAccessIterator>;
const unsigned int total_bytes = static_cast<unsigned int>(items_to_prefetch) * sizeof(input_t);
const auto* const base = reinterpret_cast<const char*>(::cuda::std::to_address(block_src_it));

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.

Suggestion: base is a bit generic as a name, I'd use something like:

Suggested change
const auto* const base = reinterpret_cast<const char*>(::cuda::std::to_address(block_src_it));
const auto* const src_ptr = reinterpret_cast<const char*>(::cuda::std::to_address(block_src_it));

To say it's the source memory and it really is a pointer.

Comment on lines +314 to +329
if (::cuda::device::__block_elect_one())
{
// srcMem must be 16-byte aligned per PTX ISA; align base down and extend size to compensate
const auto* const aligned_base = ::cuda::align_down(base, 16);
const unsigned int prefix = static_cast<unsigned int>(base - aligned_base);
const unsigned int aligned_size = ::cuda::round_up(total_bytes + prefix, 16u);
if (aligned_size > 0)
{
#if _CCCL_CUDA_COMPILER(NVHPC) || __CUDA_ARCH__ >= 900
asm volatile("cp.async.bulk.prefetch.L2.global [%0], %1;"
:
: "l"(::cuda::ptx::__as_ptr_gmem(aligned_base)), "r"(aligned_size)
: "memory");
#endif
}
}

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.

Strong suggestion: I think I would guard the entire block by NV_IF_TARGET(NV_PROVIDES_SM90. There is no point in running the elect_one or the address computation if we are not going to run the asm statement later. We could even consider, to static_assert when we are not compiling for SM90+.

Comment on lines 906 to +910
//! - BlockLoad can be optionally specialized by different data movement strategies:
//!
//! #. :cpp:enumerator:`cub::BLOCK_LOAD_DIRECT`:
//! A :ref:`blocked arrangement <flexible-data-arrangement>` of data is read directly from memory.
//! Combine with ``Prefetch = cub::detail::BlockLoadPrefetch::l2`` to emit L2 prefetch hints before each load.

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.

Suggestion: I would add another paragraph after the numerators that lists the prefetch enumerators. We can then document that they only work for BLOCK_LOAD_DIRECT today. But the design is open to use prefetching for the other modifiers as well.

Comment on lines +1119 to +1123
if constexpr (Prefetch != detail::BlockLoadPrefetch::none)
{
detail::prefetch_block_load_tile<ThreadsPerBlock, 128, Prefetch>(
linear_tid, block_src_it, ThreadsPerBlock * ItemsPerThread);
}

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.

Suggestion: if we put the handling of detail::BlockLoadPrefetch::none into prefetch_block_load_tile, we don't need to repeat the if constexpr all over the place here and below ;)

Comment on lines 21 to +26
template <int ItemsPerThread,
int ThreadsInBlock,
cub::BlockLoadAlgorithm LoadAlgorithm,
typename InputIteratorT,
typename OutputIteratorT>
typename OutputIteratorT,
cub::detail::BlockLoadPrefetch Prefetch = cub::detail::BlockLoadPrefetch::none>

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.

Suggestion: In general, try to place non-deducible template parameters first, because you have to list them at the call site. But then you don't have to list the ones that can be deduced by the compiler. So here, I would put the Prefetch parameter after LoadAlgorithm, so InputIteratorT and OutputIteratorT can be deduced.

@gonidelis
gonidelis marked this pull request as draft July 6, 2026 22:04
@cccl-authenticator-app cccl-authenticator-app Bot moved this from In Review to In Progress in CCCL Jul 6, 2026
gonidelis added a commit to gonidelis/cccl that referenced this pull request Jul 8, 2026
* Use 32-bit multiplication for the tile byte count (truncated anyway)
* Guard the entire bulk_l2 branch with NV_IF_TARGET(NV_PROVIDES_SM_90) instead of only the asm statement
* Handle LoadPrefetch::none inside prefetch_tile so call sites need no enablement check
* Move the Prefetch template parameter before the defaulted PrefetchStride so common call sites don't restate the stride
@gonidelis

Copy link
Copy Markdown
Member Author

I am closing this in favor of #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
@gonidelis gonidelis mentioned this pull request Jul 14, 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.

Add cub::detail::BlockPrefetch

5 participants