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

问题描述 投票:1回答:1
    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()时,出现一条错误消息,提示“类型int没有名为size的属性”和“类型int没有称为dtype的属性”。另外,我得到一个名为“ int没有名为astype的属性”的错误。您能说明任何可能的原因吗?

python cuda pycuda
1个回答
0
投票

您收到python错误。您将N定义为N=dims[1],因此它只是一个单值整数。您也不能在整数上调用函数的大小,它们的大小也为1。类似地,您也无法检查int是哪种类型,因为好吧,它是int。您正在对cuda.mem_alloc的调用中进行此操作。

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

© www.soinside.com 2019 - 2024. All rights reserved.