【问题标题】:Can int variables be transferred from host to device in PyCUDA?可以在 PyCUDA 中将 int 变量从主机传输到设备吗?
【发布时间】:2020-05-07 09:37:31
【问题描述】:
    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 没有名为 size 的属性”和“type int 没有名为 dtype 的属性”。此外,我收到一个名为“int 没有名为 astype 的属性”的错误。你能说明任何可能的原因吗?

【问题讨论】:

    标签: python cuda pycuda


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      • 2018-06-16
      • 2013-11-13
      • 2011-11-19
      相关资源
      最近更新 更多