Repository navigation
Description
Motivation
It's increasingly harder to reach SOL on newer GPU architectures, starting with A100 and H100, especially for simple kernels, like:
thrust::transform(..., thrust::plus{}), which basically load a few values and perform little compute. CUB algorithms already counter this by processing several elements per thread, but internal research hints at the necessity to further increase the amount of data in flight.
Use case
thrust::transform is an important primitive for many algorithms and also occurs in BabelStream, i highly relevant HPC benchmark often used to produce representative numbers to compare the performance of hardware architectures. We should therefore dedicate some effort to ensure thrust::transform performs well.
Approach
The main strategy is to have more "bytes in flight" when reading, with the concrete amount depending on the target architecture (tuning parameter). There are multiple ways to generate more loads. Again, internal research points to using either prefetching or the tensor memory accelerator (TMA, e.g. via memcpy_async) on newer architectures. Excessive unrolling and loading to registers works as well, but has the drawback of consuming large amount of registers for architectures requiring a large number of bytes in flight.
Address stability
For the loading strategy we have to consider the address stability of data items as well. Users sometimes rely on the ability to retrieve the index inside an input array from the reference of a loaded element:
transform(par, a, a + n, a, [a,b,c](const T& e) {
const auto i = &e – a; // &e expected to point into global memory
return e + b[i] + c[i];
});Such a user-provided function object inhibits any optimization which loads elements from global memory into registers or shared memory before passing them as arguments, thus only allowing prefetching as optimization. Address oblivious function objects can benefit from a larger variety of optimizations (like TMA or pipelined loading to registers.
Further concerns
Furthermore, the computational intensity and shared memory/register consumption of the user provided function object influence the loading strategy. Longer computations seem to require more data in flight. Shared memory is contested by TMA and user-side computation. Register pressure limits unrolling.
Status quo
thrust::transform (CUDA) is currently built on top of cub::DeviceFor::Bulk, which eventually dispatches independently of the uses data types or number of input and output streams. Because cub::DeviceFor::Bulk is index based, the involved input and output data streams are not visible and no tuning based on this information is possible. The situation is similar with cub::DeviceFor::ForEach et al.
Strategy
I propose to add a new CUB algorithm cub::DeviceTransform governing transformations of N input streams into a single output stream (maybe M output streams if use cases arrise) and rebasing thrust::transform on top of it.
Future tasks after merging cub::DeviceTransform
- Create
cub::DeviceTransform#2091 - Split BabelStream benchmarks by number if iterators
- Re-evaluate the prefetching fallback kernel and compare it with DeviceFor as fallback #2363
- Drop the fallback_for transform algorithm
- Port
thrust::transformto usecub::DeviceTransform#2263 - Readd, optimize and profile
LDGSTS-based transform kernel for Ampere #2361 - Add support for large
num_itemstocub::DeviceTransform#2714 - Roll out address stability optimization #2403
- Profile memcpy_async kernel on Ampere workstation GPUs
-
Refactor prefetching and TMA loading into new CUB block load algorithms for reuse (requires CUB block load redesign)We haveBlockLoadToSharedNow. - Port
thrust::transform_iftocub::DeviceTransformas well - Review Thrust and try to find more uses for
transform(especially replacingfor_each) - Benchmark and plot before/after performance of BabelStream based on
thrust::transform#2717 -
Evaluate the need for the unroll_staged kernel before A100 and for Ampere workstation GPUs -
Try to mark non-aliased input streams with__restrict____restrict__cannot be combined with PDL in the same kernel, since it introduces data races. So let's skip restrict. -
PortNot necessary for this issue.thrust::transform_ifwith stencil tocub::DeviceTransformas well - Tune
cub::DeviceTransform#7507
Metadata
Metadata
Assignees
Type
Projects
- StatusShow more project fieldsIn Progress
Activity
bernhardmgruber commentedon Jul 16, 2024
It turns out the C++ standard does not guarantee address stability of function arguments passed to user-provided callables in the context of parallel algorithms. See:
* https://eel.is/c++draft/algorithms.parallel#user-1
* https://eel.is/c++draft/algorithms.parallel#exec-3
The only exception are
for_eachandfor_each_n:* exception for for_each: https://eel.is/c++draft/alg.foreach#9
* exception for for_each_n: https://eel.is/c++draft/alg.foreach#25
IIRC from a discussion with @gevtushenko , this is consistent with what Thrust/CUB should promise. However, I could not find any solid hints in our documentation. We may thus break users when changing this behavior who relied on this effect by accident (see also: Hyrum's Law).
bernhardmgruber commentedon Jul 17, 2024
We discussed address stability again today and concluded the following:
cub::DeviceFor, with address-stable and unstable functions.bernhardmgruber commentedon Jul 18, 2024
Address stability: Because I just encountered it in the tests on my A6000. If we use a kernel serving parameters from shared memory and the user performs pointer arithmetic with a pointer to global memory, the kernel crashes (Release build) and the following error code is reported at the next
cudaDeviceSynchronize:That's at least better than a garbage result and the kernel continuing with wrong data.
ahendriksen commentedon Feb 26, 2026
Sorry for stream-of-consciousness. Have tried a few options to improve code generation.
It is very difficult to get good code-generation with ublkcp.
cp_async_bulk_predorcp_async_bulk_maskfunctions below improvescode generation considerably.
cuda::ptxahendriksen commentedon Feb 26, 2026
If we use the
__block_size__attribute on the kernel, we can remove aSHFL.SYNC. Example: https://cuda.godbolt.org/z/6sTP8acKPNote: this only works on recent compilers and it appears that not all target architectures are supported.
bernhardmgruber commentedon Sep 7, 2026
I asked
claudeto try the optimizations proposed by @ahendriksen and it found that the current version of our transform kernel already produces good SASS where the bulk copy is predicated. I didn't dive deep into doing the analysis myself though.Adding
__block_size__saved one instruction, aVOTEU.ALL, but it caused a massive regression due to different scheduling of CTAs I believe.miscco commentedon Sep 8, 2026
Reaching SoL one instruction at a time 🐱
bernhardmgruber commentedon Sep 8, 2026
I moved a few bullet items from this mega issues into independent issues, so we can follow-up later (or never):
cub::DeviceTransformublkcp path for mixing contiguous and non-contiguous iterators #11253thrust::transformfast-path #11254@gonidelis let's finish tuning and then we can complete this effort for now and close the issue.