How do I feed a 2-dimensional array into a kernel with pycuda?

懵懂的女人 提交于 2019-12-05 07:01:41

问题


I have created a numpy array of float32s with shape (64, 128), and I want to send it to the GPU. How do I do that? What arguments should my kernel function accept? float** myArray?

I have tried directly sending the array as it is to the GPU, but pycuda complains that objects are being accessed...


回答1:


Two dimensional arrays in numpy/PyCUDA are stored in pitched linear memory in row major order by default. So you only need to have a kernel something like this:

__global__
void kernel(float* a, int lda, ...)
{
    int r0 = threadIdx.y + blockDim.y * blockIdx.y;
    int r1 = threadIdx.x + blockDim.x * blockIdx.x;

    float val = a[r0 + r1*lda];

    ....
}

to access a numpy ndarray or PyCUDA gpuarray passed by reference to the kernel from Python.



来源:https://stackoverflow.com/questions/13282596/how-do-i-feed-a-2-dimensional-array-into-a-kernel-with-pycuda

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!