CUDA/C - Using malloc in kernel functions gives strange results

前端 未结 2 619
清歌不尽
清歌不尽 2021-01-07 16:04

I\'m new to CUDA/C and new to stack overflow. This is my first question.

I\'m trying to allocate memory dynamically in a kernel function, but the results are unexpec

2条回答
  •  日久生厌
    2021-01-07 16:30

    In-kernel memory allocation draws memory from a statically allocated runtime heap. At larger sizes, you are exceeding the size of that heap and then your two kernels are attempting to read and write from uninitialised memory. This produces a runtime error on the device and renders the results invalid. You would already know this if you either added correct API error checking on the host side, or ran your code with the cuda-memcheck utility.

    The solution is to ensure that the heap size is set to something appropriate before trying to run a kernel. Adding something like this:

     size_t heapsize = sizeof(int) * size_t(N_CELLE) * size_t(2*L_CELLE);
     cudaDeviceSetLimit(cudaLimitMallocHeapSize, heapsize);
    

    to your host code before any other API calls, should solve the problem.

提交回复
热议问题