PyCuda:可以导入模块,然后我不能......(PyCUDA 示例)

发布于 2024-10-31 14:06:42 字数 2160 浏览 1 评论 0原文

示例代码:

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 从我想不到的地方获取它的环境变量,否则没有理由不这样做:

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

Example code:

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

Results:

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

Sounds simple enough, so lets test this.

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'>
>>> 

Ok, thats weird...

Long story short, even stepping through the file line by line into the python console, nothing goes wrong until the actual execution of the mod=SourceModule() line.

(Final Traceback, I promise)

/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

So it looks like pycuda is getting different include dirs than runtime python, which shouldn't happen (as i understand it)

Any ideas? (Sorry for the long question)

Talonmies borought up a point about nvcc not being found; unless python is getting its envars from somewhere I can't think of, there's no reason it shouldn't :

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪冰清 2024-11-07 14:06:42

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

Changing to Python 2.6 and reinstalling relevant modules fixed the problem for the OP.

眼波传意 2024-11-07 14:06:42

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

There is nothing wrong with the code you are trying to run - it should work. My guess is that nvcc cannot be found. Make sure that the path to the nvcc executable is set in your environment before you try using pycuda.compiler.

暖阳 2024-11-07 14:06:42

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

/usr/local/cuda/lib/

LD_LIBRARY_PATH

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

>lld pycuda.so 

I think you did not install the CUDA toolkit from nvidia and added the

/usr/local/cuda/lib/

to

LD_LIBRARY_PATH

find the the .so of the pycuda module and give us the output of:

>lld pycuda.so 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文