为什么更改内核参数会耗尽我的资源?

时间:2012-11-19 08:11:23

标签: cuda pycuda

我在下面制作了一个非常简单的内核来练习CUDA。

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

def compile_kernel(kernel_code, kernel_name):
  mod = SourceModule(kernel_code)
  func = mod.get_function(kernel_name)
  return func

input_file = np.array(cv2.imread('clouds.jpg'))
height, width, channels = np.int32(input_file.shape)

my_kernel_code = """
  __global__ void my_kernel(int width, int height) {
    // This kernel trivially does nothing! Hurray!
  }
"""
kernel = compile_kernel(my_kernel_code, 'my_kernel')

if __name__ == '__main__':

  for i in range(0, 2):
    print 'o'
    kernel(width, height, block=(32, 32, 1), grid=(125, 71))

    # When I take this line away, the error goes bye bye.
    # What in the world?
    width -= 1

现在,如果我们运行上面的代码,执行就会继续执行for循环的第一次迭代。但是,在循环的第二次迭代期间,我收到以下错误。

Traceback (most recent call last):
  File "outOfResources.py", line 27, in <module>
    kernel(width, height, block=(32, 32, 1), grid=(125, 71))
  File "/software/linux/x86_64/epd-7.3-1-pycuda/lib/python2.7/site-packages/pycuda-2012.1-py2.7-linux-x86_64.egg/pycuda/driver.py", line 374, in function_call
    func._launch_kernel(grid, block, arg_buf, shared, None)
pycuda._driver.LaunchError: cuLaunchKernel failed: launch out of resources

如果我带走了width -= 1行,则错误就会消失。这是为什么?我不能第二次更改内核的参数吗?作为参考,这里是clouds.jpg

enter image description here

1 个答案:

答案 0 :(得分:3)

虽然错误消息不是特别有用,但请注意您需要传入正确转换的width变量。如下所示:

width = np.int32(width - 1)

应该有用。