You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[FEA]: Add a CUB BlockLoad using prefetching #3126
Given the success we had with the prefetching implementation in cub::DeviceTransform, we should try to refactor this into one or several new cub::BlockLoad and validate its impact in at least one CUB algorithm.
Decide which block load algorithms would benefit from prefetching. I assume BLOCK_LOAD_DIRECT and possibly BLOCK_LOAD_VECTORIZE.
Add the equivalent of cub::LoadDirectBlocked etc. (3 overloads) using prefetching. Look at cub::DeviceTransform for how to prefetch to L2
Add a new enumerator to cub::BlockLoadAlgorithm and implement it in cub::BlockLoad
Examine benefits of using L1 prefetching (available only in CTK higher than 13.4)
With all the above decided during the design process, this will now be used as a tracking issue for prefetching. See sub-issues below.
Redesigning prefetch interface independently of BlockLoad
We choose to expose the prefetch interface as a function (free or not)
prefetch_tile(int tid, RandomAccessIterator block_src_it, int items_to_prefetch)
which operates based on a tag-enum:
enumclassLoadPrefetch : int
{
none,
l2,
l1,
bulk_l2,
};
Two things to consider:
1. "When do we want for the prefetch hint to actually emit?"
Two options:
A) As part of the .Load() operation, possibly right before the actual load executes
B) As early as possible before .Load() executes for the prefetch to have enough leeway to bring the data over.
// (B) example // AgentReduceByKey::ConsumeTile — simplifiedvoidConsumeTile(OffsetT tile_offset, ScanTileStateT& tile_state)
{
// (B) agent placement — free function, fired at the top of the tile:
detail::prefetch_tile<BLOCK_THREADS>(threadIdx.x, d_keys_in + tile_offset, TILE_ITEMS); // ← hint issued HERE// ... hundreds of cycles of slack: decoupled look-back on the// previous tile's state, the stall that HIDES the fetch latency ...
tile_state.WaitForValid(...);
// (A) in-Load placement — what a BlockLoad template parameter does:BlockLoadKeysT(temp_storage).Load(d_keys_in + tile_offset, keys);
// ^ hint emitted inside Load(), immediately before the loads it// targets — ZERO lead time.// ...
}
// under the hood in case of (A):template <typename T, intBLOCK_DIM_X, intITEMS_PER_THREAD,
BlockLoadAlgorithm ALGORITHM, LoadPrefetch PREFETCH>
classBlockLoad
{
voidLoad(InputIteratorT block_src_it, T (&items)[ITEMS_PER_THREAD])
{
ifconstexpr (PREFETCH != LoadPrefetch::none)
{
// hint emitted here, unconditionally fused to the load below
detail::prefetch_tile<BLOCK_DIM_X, PREFETCH>(linear_tid, block_src_it, TILE_ITEMS);
}
LoadDirectBlocked(linear_tid, block_src_it, items); // ← the loads the hint targets,// issued on the very next instruction
}
};
Do we get any perf benefits from (A) emitting within the .Load() call right before the actual load operation? --> Answer: no
See full cross-perf comparison at the bottom of this document
2. "Where do we expose the prefetch enum interface?"
candidate solutions: DeviceAlgorithm template argument, environment, BlockLoad template argument (current design), Public tuning policy etc.
For the prefetch_tile() little does it matter if it's free or not, we can adjust it in any of the following options. But the place where we expose the Prefetch enabling/selection to the library users matters.
a) BlockLoad owns the tile geometry (BLOCK_THREADS × ITEMS_PER_THREAD).
b) Of course given that prefetch_tile() is a free function we can still keep the enum-tag in BlockLoad and query it in the agent still. Going with Opiton A here, doesn't mean we are forced to prefetch in-load. BlockLoad could might as well be used as a carrier for the prefetch tag and the load algorithm independently.
structReduceByKeyPolicy
{
int threads_per_block;
int items_per_thread;
// ...
LookbackDelayPolicy lookback_delay;
detail::LoadPrefetch load_prefetch = detail::LoadPrefetch::none;
// ...
}
pros:
a) Not all loads go through BlockLoad. Merge and radix agents load through other paths.
b) BlockLoad dictates the "how we load" and prefetch dictates the "when we load". The two are completely unrelated.
c) Placement becomes our job, per algorithm: we, the CCCL authors, decide where in each agent's schedule the hint pays off. The profitable issue point depends on the algorithm's schedule — e.g. multi-tile-loop agents want to prefetch tile N+1 while computing tile N, which is inexpressible from inside Load(tile N).
Performance cross comparison results between all options
For Q1 ("when") data is clear that we should prefetch early in the agent bodies. For Q2 ("where") I am highly inclined to go with the design where AlgorithmPolicy carries the tag (Option B).
Activity
cub::DeviceReduce::ReduceByKeyto BlockLoad prefetch and measure impact. #9566cub::DeviceSelect::Ifto BlockLoad prefetch and measure impact. #9567cub::detail::BlockPrefetch#9588BlockLoadalgorithms #9589cuda::ptx::prefetch()toBlockPrefetc#9616gonidelis commentedon Jul 8, 2026
Redesigning prefetch interface independently of
BlockLoadWe choose to expose the prefetch interface as a function (free or not)
which operates based on a tag-enum:
Two things to consider:
1. "When do we want for the prefetch hint to actually emit?"
Two options:
A) As part of the
.Load()operation, possibly right before the actual load executesB) As early as possible before
.Load()executes for the prefetch to have enough leeway to bring the data over.Do we get any perf benefits from (A) emitting within the
.Load()call right before the actual load operation? --> Answer: noSee full cross-perf comparison at the bottom of this document
2. "Where do we expose the prefetch
enuminterface?"candidate solutions: DeviceAlgorithm template argument, environment, BlockLoad template argument (current design), Public tuning policy etc.
For the
prefetch_tile()little does it matter if it's free or not, we can adjust it in any of the following options. But the place where we expose thePrefetchenabling/selection to the library users matters.Option A:
BlockLoadtemplate parameter - PR #9496pros:
a)
BlockLoadowns the tile geometry (BLOCK_THREADS×ITEMS_PER_THREAD).b) Of course given that
prefetch_tile()is a free function we can still keep the enum-tag inBlockLoadand query it in the agent still. Going with Opiton A here, doesn't mean we are forced to prefetch in-load.BlockLoadcould might as well be used as a carrier for the prefetch tag and the load algorithm independently.Option B: In
AlgorithmPolicystruct - PR #9723It all ignited when I read this review comment from @bernhardmgruber.
pros:
a) Not all loads go through BlockLoad.
Mergeandradixagents load through other paths.b)
BlockLoaddictates the "how we load" andprefetchdictates the "when we load". The two are completely unrelated.c) Placement becomes our job, per algorithm: we, the CCCL authors, decide where in each agent's schedule the hint pays off. The profitable issue point depends on the algorithm's schedule — e.g. multi-tile-loop agents want to prefetch tile N+1 while computing tile N, which is inexpressible from inside Load(tile N).
Performance cross comparison results between all options
Conclusion
For Q1 ("when") data is clear that we should prefetch early in the agent bodies. For Q2 ("where") I am highly inclined to go with the design where
AlgorithmPolicycarries the tag (Option B).