1

示例代码:

import pycuda.autoinit
import pycuda.driver as drv
import numpy

from pycuda.compiler import SourceModule
mod = SourceModule("""
__global__ void multiply_them(float *dest, float *a, float *b)
{
  const int i = threadIdx.x;
  dest[i] = a[i] * b[i];
}
""")

multiply_them = mod.get_function("multiply_them")

a = numpy.random.randn(400).astype(numpy.float32)
b = numpy.random.randn(400).astype(numpy.float32)

dest = numpy.zeros_like(a)
multiply_them(
        drv.Out(dest), drv.In(a), drv.In(b),
        block=(400,1,1), grid=(1,1))

print dest-a*b

结果:

Traceback (most recent call last):
  File "test.py", line 12, in <module>
    """)
  File "build/bdist.linux-x86_64/egg/pycuda/compiler.py", line 238, in __init__
  File "build/bdist.linux-x86_64/egg/pycuda/compiler.py", line 223, in compile
  File "build/bdist.linux-x86_64/egg/pycuda/compiler.py", line 149, in _find_pycuda_include_path
ImportError: No module named pycuda

听起来很简单,所以让我们测试一下。

Python 2.7.1 (r271:86832, Feb 17 2011, 14:13:40) 
[GCC 4.3.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pycuda
>>> pycuda
<module 'pycuda' from '/home/abolster/lib/python2.7/site-packages/pycuda-0.94.2-py2.7-linux-x86_64.egg/pycuda/__init__.pyc'>
>>> 

好吧,这很奇怪……

长话短说,即使将文件逐行进入 python 控制台,在实际执行 mod=SourceModule() 行之前也不会出错。

(最终追溯,我保证)

/home/abolster/lib/python2.7/site-packages/pycuda-0.94.2-py2.7-linux-x86_64.egg/pycuda/compiler.pyc in _find_pycuda_include_path()
    147 def _find_pycuda_include_path():
    148     from imp import find_module
--> 149     file, pathname, descr = find_module("pycuda")
    150 
    151     # Who knew Python installation is so uniform and predictable?

ImportError: No module named pycuda

所以看起来 pycuda 的包含目录与运行时 python 不同,这不应该发生(据我了解)

有任何想法吗?(对不起,很长的问题)

Talonmies 提出了一个关于未找到 nvcc 的观点;除非 python 从我想不到的地方获取它的 envars,否则它没有理由不应该:

[bolster@dellgpu src]$ which nvcc
~/cuda/bin/nvcc
4

3 回答 3

3

更改为 Python 2.6 并重新安装相关模块解决了 OP 的问题。

于 2012-09-07T02:50:13.560 回答
1

您尝试运行的代码没有任何问题 - 它应该可以工作。我的猜测是找不到 nvcc。在尝试使用 pycuda.compiler 之前,请确保在您的环境中设置了 nvcc 可执行文件的路径。

于 2011-04-09T10:49:46.393 回答
0

我认为您没有从 nvidia 安装 CUDA 工具包并添加了

/usr/local/cuda/lib/

LD_LIBRARY_PATH

找到 pycuda 模块的 .so 并给我们输出:

>lld pycuda.so 
于 2011-04-09T07:58:12.053 回答