gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:00:48 24 4
gpt4 key购买 nike

示例代码:

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 的 include dirs 与运行时 python 不同,这不应该发生(据我所知)

有什么想法吗? (很抱歉这个问题很长)

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

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

最佳答案

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

关于python - PyCuda:可以导入模块,然后我不能...(PyCUDA 示例),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5595298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com