0

I'm having a crash in my CUDA code that I do not understand: it tells me that it's running out of memory when there is plenty left annd I'm not allocating very huge arrays.

Here is the code:

#include <iostream>


#define CHECK_CUDA_ERROR(val) check((val), #val, __FILE__, __LINE__)
template <typename T>
__device__ void check(T err, const char* const func, const char* const file, const int line) {
    if(err != cudaSuccess) {
        printf("CUDA Runtime Error at: %s : line %d\n", file, line);
        printf("%s\n", cudaGetErrorString(err));
    }
}


__device__ int* generate_spike_train() {
    const int n = 70000;
    int* spike_train;

    CHECK_CUDA_ERROR(cudaMalloc(&spike_train, n * sizeof(int))); // 70,000 * 4 = 0.267 MB.

    // Do stuff;

    return spike_train;
}


__global__ void run_simulations() {
    int* spike_train = generate_spike_train();
    
    // Do stuff.
    
    cudaFree(spike_train);
}


int main(int argc, char *argv[]) {
    const float to_MB = 1024.0f * 1024.0f;
    const int n_blocks = 30;
    const int n_threads = 2;
    size_t free, total;

    cudaMemGetInfo(&free, &total);
    printf("Memory left: %f / %f MB\n", free / to_MB, total / to_MB);

    run_simulations<<<n_blocks, n_threads>>>();
    cudaDeviceSynchronize();

    cudaMemGetInfo(&free, &total);
    printf("Memory left: %f / %f MB\n", free / to_MB, total / to_MB);
}

I'm compiling and running the code using:

$ nvcc -rdc=true test.cu
$ ./a.out
Memory left: 7791.625000 / 7982.437500 MB
CUDA Runtime Error at: test.cu : line 18
CUDA Runtime Error at: test.cu : line 18
CUDA Runtime Error at: test.cu : line 18
CUDA Runtime Error at: test.cu : line 18
CUDA Runtime Error at: test.cu : line 18
CUDA Runtime Error at: test.cu : line 18
CUDA Runtime Error at: test.cu : line 18
CUDA Runtime Error at: test.cu : line 18
CUDA Runtime Error at: test.cu : line 18
CUDA Runtime Error at: test.cu : line 18
CUDA Runtime Error at: test.cu : line 18
CUDA Runtime Error at: test.cu : line 18
CUDA Runtime Error at: test.cu : line 18
CUDA Runtime Error at: test.cu : line 18
out of memory
out of memory
out of memory
out of memory
out of memory
out of memory
out of memory
out of memory
out of memory
out of memory
out of memory
out of memory
out of memory
out of memory
Memory left: 7781.625000 / 7982.437500 MB

As you can see, I have plenty of VRAM (8 GB) and I'm only trying to allocate 16 MB total (counting all blocks and all threads).

I'm running on an RTX 2080 with CUDA 11.6 (drivers 520.61.05) on Ubuntu 20.04 LTS.

Thanks!

Drade
  • 157
  • 10

0 Answers0