Skip to content

[FEA]: Add a CUB BlockLoad using prefetching #3126

Open
2 of 5 issues completed
Open

Description

@bernhardmgruber

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.

Sub-issues

Sub-issues

2 of 5 Issues completed
Loading 5 sub-issues

Activity

  1. added
    cub
    on Dec 11, 2024
  2. moved this from Todo to In Progress in CCCLon May 26, 2026
  3. moved this from In Progress to Todo in CCCLon May 26, 2026
  4. moved this from Todo to In Progress in CCCLon Jun 16, 2026
  5. gonidelis commented on Jul 8, 2026

    @gonidelis
    Member

    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:

    enum class LoadPrefetch : 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 — simplified
    void ConsumeTile(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, int BLOCK_DIM_X, int ITEMS_PER_THREAD,
              BlockLoadAlgorithm ALGORITHM, LoadPrefetch PREFETCH>
    class BlockLoad
    {
      void Load(InputIteratorT block_src_it, T (&items)[ITEMS_PER_THREAD])
      {
        if constexpr (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.

    Option A: BlockLoad template parameter - PR #9496

    template <typename T, int BLOCK_DIM_X, int ITEMS_PER_THREAD,
              BlockLoadAlgorithm ALGORITHM, /*...,*/ LoadPrefetch PREFETCH>
    class BlockLoad
    { ...

    pros:

    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.

    Option B: In AlgorithmPolicy struct - PR #9723

    It all ignited when I read this review comment from @bernhardmgruber.

    struct ReduceByKeyPolicy
    {
      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

    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 AlgorithmPolicy carries the tag (Option B).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

cub

Type

No type

Projects

  • Status
    In Progress

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions