Search code examples
cudamallocspacegranularity

Is there a way to know what's the extra space that cudaMalloc is going to reserve?


When I use cudaMalloc (100) it reserves more than 100 B (According to some users here it's due to granularity issues and housekeeping information.

Is it possible to determine how big this space will be based on the Bytes I need to reserve?

Thank you so much.

EDIT: I'll explain why I need to know.

I want to apply the convolution algorithm over huge images on the GPU. To do so, since there isn't enough memory on the GPU to hold it, I need to split the image in batches of rows an call the kernel several times.

In fact, I need to send 2 images, the OnlyRead matrix and the Results matrix.

I want to calcule a priori the max number of rows I can send to the device according to the amount of free memory.

The first cudaMalloc executes successfully, but the problem appears when trying to execute the second CudaMalloc since the first reserve took more Bytes than expected.

What I'm doing now is considering the free memory amount a 10% less than what it is... but that's just a magical number that came from nowhere..


Solution

  • "Is there a way to know what's the extra space that cudaMalloc is going to reserve?"

    Not without violating CUDA's platform guarantees, no. cudaMalloc() returns a pointer to the requested amount of memory. You can't make any assumptions about the amount of memory that happens to be valid after the end of the requested amount - the CUDA allocator already makes use of suballocators, and unlike CPU-based memory allocators, the data structures to track free lists etc. are not interleaved with the allocated memory. So for example, it would be unwise to assume that the CUDA runtime's guarantees about the alignment of the returned pointers mean anything other than that returned pointers will have a certain alignment.

    If you study the CUDA runtime's behavior, that will shed light on the behavior of that particular CUDA runtime, but the behavior may change with future releases and break your code.