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

前端 未结 2 603
清歌不尽
清歌不尽 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:26

    I don't know anything about CUDA but these are severe bugs:

    • You cannot convert from int** to void**. They are not compatible types. Casting doesn't solve the problem, but hides it.
    • &d_numbers gives the address of a pointer to pointer which is wrong. It is of type int***.

    Both of the above bugs result in undefined behavior. If your program somehow seems to works in some condition, that's just by pure (bad) luck only.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题