PyCUDA 中可以将 int 变量从主机传输到设备吗?

Can int variables be transferred from host to device in PyCUDA?

    import pycuda.driver as cuda
    import pycuda.autoinit
    from pycuda.compiler import SourceModule
    import numpy as np
    dims=img_in.shape
    rows=dims[0]
    columns=dims[1]
    channels=dims[2]
    #To be used in CUDA Device
    N=columns
    #create output image matrix
    img_out=np.zeros([rows,cols,channels])

     #Convert img_in pixels to 8-bit int
     img_in=img_in.astype(np.int8)
     img_out=img_out.astype(np.int8)

     #Allocate memory for input image,output image and N
     img_in_gpu = cuda.mem_alloc(img_in.size * img_in.dtype.itemsize)
     img_out_gpu= cuda.mem_alloc(img_out.size * img_out.dtype.itemsize)
     N=cuda.mem_alloc(N.size*N.dtype.itemsize)

     #Transfer both input and now empty(output) image matrices from host to device
     cuda.memcpy_htod(img_in_gpu, img_in)
     cuda.memcpy_htod(img_out_gpu, img_out)
     cuda.memcpy_htod(N_out_gpu, N)
     #CUDA Device
     mod=SourceModule("""
                      __global__ void ArCatMap(int *img_in,int *img_out,int *N)
                      {
                         int col = threadIdx.x + blockIdx.x * blockDim.x;
                         int row = threadIdx.y + blockIdx.y * blockDim.y;

                         int img_out_index=col + row * N;
                         int i=(row+col)%N;
                         int j=(row+2*col)%N;
                         img_out[img_out_index]=img_in[]

                      }""")

                     func = mod.get_function("ArCatMap")

                     #for i in range(1,385):
                     func(out_gpu, block=(4,4,1))

                     cuda_memcpy_dtoh(img_out,img_in)
                     cv2_imshow(img_out)

我这里有一张 512 X 512 的图片。我正在尝试使用 numpy.astype 将输入图像 img_in 的所有元素转换为 8 位 int。输出图像矩阵 img_out 也是如此。当我尝试使用 cuda.mem_alloc() 时,我收到一条错误消息,指出 'type int has no attribute called size' 和 'type int has no attribute called dtype'。另外,我收到一个名为 'int has no attribute called astype' 的错误。你能说出任何可能的原因吗?

您遇到 python 错误。您将 N 定义为 N=dims[1],因此它只是一个单值整数。您也不能对整数调用函数 size,它们的大小也是 1。同样,您不能检查 int 是哪种类型,因为它是一个 int。您正在调用 cuda.mem_alloc 时这样做。

你不需要为单个int分配内存,你可以按值传递它。将内核定义为 __global__ void ArCatMap(int *img_in,int *img_out,int N) 而不是传递指针。