Skip to content

[BUG]: cub::DeviceTransform fill with a large power-of-two value type reserves gigabytes of local memory #11758

Open

Description

@SamMausberg

Is this a duplicate?

Type of Bug

Runtime Error

Component

CUB

Describe the bug

A fill through cub::DeviceTransform::Fill or Generate with a value type whose size is a power of two reserves device memory in proportion to the value size. On a GH200, one thrust::fill with 4 KiB values leaves 17 GiB of device memory reserved, and with less than that free the fill fails with cudaErrorMemoryAllocation. The same path is taken by thrust::fill, thrust::uninitialized_fill for trivially copyable types, and thrust::device_vector<T>(n).

With no input streams and a power-of-two output size, the policy selector picks the vectorized transform kernel. That kernel stages ItemsPerThread results per thread in a local array (kernel_transform.cuh:280, ::cuda::__uninitialized_array<output_array_t, ItemsPerThread, ...> output), and the fill tuning for sm_90+ uses 16 items per thread (tuning_transform.cuh:349). The stack frame therefore grows with the value size, and on the first launch the driver reserves it for every resident thread: stack per thread x maximum resident threads per SM x number of SMs. For 4 KiB values (e.g. a 32x32 float matrix) on a GH200, with 132 SMs and 2048 threads per SM, that is 69616 B x 2048 x 132 = 17.5 GiB; the measured increase of 17.2 GiB is the same product after subtracting the default 1 KiB stack, which is reserved anyway. From 32 KiB values on, the stack exceeds the 512 KiB per-thread limit and the launch fails with cudaErrorInvalidValue.

Measured on a GH200 (sm_90, 97 GB), CTK 12.8, CCCL main 038f78b, with thrust::fill over 1000 elements of struct { unsigned char b[SIZE]; }:

value size stack per thread (cuobjdump --dump-resource-usage) device memory reserved after the first fill fails with free memory of
1 KiB 17392 B 4.1 GiB
2 KiB 34800 B 8.5 GiB
4 KiB 69616 B 17.2 GiB 10 GiB (passes with 20 GiB)
8 KiB 139248 B 34.8 GiB 30 GiB (passes with 40 GiB)
16 KiB 327664 B 82.2 GiB 60 GiB (passes with 90 GiB)
32 KiB exceeds 512 KiB launch fails with cudaErrorInvalidValue (with DeviceTransform::Generate; Fill does not compile because the value exceeds the kernel parameter space)

Values whose size is not a power of two (e.g. 5000 B) take the prefetch kernel and are not affected. #11756 lets Thrust's fill handle values larger than the kernel parameter limit without DeviceTransform, but values below that limit still take this path. Related but different: #6708, about 4 GB of unexpected device memory after thrust::transform on sm_120, caused by the TMA path.

How to Reproduce

// nvcc -std=c++17 -arch=sm_90 -I<cccl>/thrust -I<cccl>/cub -I<cccl>/libcudacxx/include -DSIZE=4096 repro.cu
#include <thrust/device_vector.h>
#include <thrust/fill.h>

#include <cstdio>
#include <cstdlib>

struct blob
{
  unsigned char b[SIZE];
};

int main(int argc, char** argv)
{
  thrust::device_vector<blob> v(1000, thrust::no_init);
  // optionally leave only argv[1] GiB of device memory free
  void* hog = nullptr;
  size_t free_b, total_b;
  cudaMemGetInfo(&free_b, &total_b);
  const size_t keep = argc > 1 ? static_cast<size_t>(std::atof(argv[1]) * (1ull << 30)) : free_b;
  if (free_b > keep)
  {
    cudaMalloc(&hog, free_b - keep);
  }
  cudaMemGetInfo(&free_b, &total_b);
  thrust::fill(v.begin(), v.end(), blob{}); // throws cudaErrorMemoryAllocation with too little free memory
  size_t free_after;
  cudaMemGetInfo(&free_after, &total_b);
  std::printf("free before %.1f GiB, after %.1f GiB\n", free_b / 1073741824.0, free_after / 1073741824.0);
  cudaFree(hog);
}

With SIZE=4096, ./a.out prints free before 93.9 GiB, after 76.7 GiB, and ./a.out 10 terminates with fill_n: failed inside CUB: cudaErrorMemoryAllocation: out of memory.

Expected behavior

Filling with a value of a few KiB should neither reserve gigabytes of device memory nor fail for lack of it. The selector could fall back to the prefetch kernel once the per-thread staging (ItemsPerThread * sizeof(value)) exceeds some size (the vectorized-or-prefetch selection at tuning_transform.cuh:453 and :492, and the fallback policy), or scale ItemsPerThread down with the value size.

Reproduction link

No response

Operating System

Ubuntu 22.04, aarch64

nvidia-smi output

NVIDIA GH200 480GB, driver 570.148.08

NVCC version

Cuda compilation tools, release 12.8, V12.8.93

Activity

  1. bernhardmgruber commented on Oct 7, 2026

    @bernhardmgruber
    Contributor

    I believe the current tuning for cub::DeviceTransform::Fill assumes simple types and will try to use some vectorization. I believe a simple first fix is to restrict the tuning to 1 item per thread if the output iterator's value type is getting too large. That should scale up to reasonably large types.

    #11756 seems to try to tackle data types larger than what we can pass to a kernel by value, which seems too esoteric to me, and I think such support is probably out of scope, because I can't imagine many other of our algorithms supporting this use case. If you have a realistic one, please let me know!

  2. SamMausberg commented on Oct 7, 2026

    @SamMausberg
    ContributorAuthor

    Ah, that makes sense. I will give it a try now. Thank you for the guidance.

  3. moved this from Todo to In Progress in CCCLon Oct 7, 2026
  4. SamMausberg commented on Oct 7, 2026

    @SamMausberg
    ContributorAuthor

    @bernhardmgruber I opened #11919 with your suggestion. This time on an RTX 5070 Ti.

    Your suggestion was better than both fixes I proposed, so thanks for the pointer 😂.

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

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

  • Status
    In Review

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions