使用pycuda在python中调用cuda内核时出错

问题描述 投票:0回答:1

我正在尝试使用 pycuda 在 python 脚本中调用 cuda 内核。 在主cuda kuda内核内部,调用了不同的cuda内核。 使用以下命令即可完成编译,没有任何错误:

nvcc -shared -rdc=true -o test.so test.cu -Xcompiler -fPIC

但是,当我尝试运行 python 脚本时,出现以下错误:

raise CompileError(
pycuda.driver.CompileError: nvcc compilation of /tmp/tmpkyzts09l/kernel.cu failed
[command: nvcc --cubin -arch sm_86 -I/home/user/wrapper_test/wrapper/lib/python3.8/site-packages/pycuda/cuda kernel.cu]
[stderr:
kernel.cu(111): error: kernel launch from __device__ or __global__ functions requires separate compilation mode

kernel.cu(145): error: kernel launch from __device__ or __global__ functions requires separate compilation mode

我该如何解决这个问题。 预先感谢您的帮助。

python c++ c cuda pycuda
1个回答
0
投票

这是cuda代码:

__global__ void square(float *array, int n) {
    if (array[0] == 0)
    {
        array[0] = 5;
        array[1] = 6;
        array[2] = 7;
    }
     }
__global__ void square_kernel(float *array, int n) {
    square<<<1,1>>>(array, n);
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx < n) {
        array[idx] = array[idx] * array[idx];
    } }

这是Python代码:

import numpy as np
import pycuda.driver as cuda
import pycuda.autoinit
from pycuda.compiler import SourceModule

# Load the CUDA module
module = SourceModule(open('test.cu', 'r').read())

# Get a reference to the CUDA kernel function
square_kernel = module.get_function("square_kernel")

# Create data on the CPU
data = np.array([1.0, 2.0, 3.0, 4.0], dtype=np.float32)
n = len(data)

# Allocate GPU memory
gpu_data = cuda.mem_alloc(data.nbytes)

# Transfer data from CPU to GPU
cuda.memcpy_htod(gpu_data, data)

# Define block and grid sizes
block_size = (1024, 1, 1)
grid_size = ((n + block_size[0] - 1) // block_size[0], 1)

# Launch the CUDA kernel
square_kernel(gpu_data, np.int32(n), block=block_size, grid=grid_size)

# Wait for kernel to finish
cuda.Context.synchronize()

# Transfer the result back from GPU to CPU
cuda.memcpy_dtoh(data, gpu_data)

# Print the result
print("Original data:", data)

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