Pycuda - 如何添加-ccbin clang-3.8

时间:2018-03-23 12:09:03

标签: python cuda nvcc pycuda

我目前正在尝试在Debian 9上使用PyCUDA。我已经设法让cuda工作,如果我运行:

nvcc -ccbin clang-3.8 file.cu

我正确编译了文件,我可以运行它。

然而,在我使用

安装了pycuda之后
apt-get install python-pycuda

从他们的网站上运行一个简单的例子:

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

但是我收到以下错误:

CompileError                              Traceback (most recent call last)
<ipython-input-1-8e16128de7f2> in <module>()
     10   dest[i] = a[i] * b[i];
     11 }
---> 12 """)
     13 
     14 multiply_them = mod.get_function("multiply_them")

/usr/lib/python2.7/dist-packages/pycuda/compiler.pyc in __init__(self, source, nvcc, options, keep, no_extern_c, arch, code, cache_dir, include_dirs)
    263 
    264         cubin = compile(source, nvcc, options, keep, no_extern_c,
--> 265                 arch, code, cache_dir, include_dirs)
    266 
    267         from pycuda.driver import module_from_buffer

/usr/lib/python2.7/dist-packages/pycuda/compiler.pyc in compile(source, nvcc, options, keep, no_extern_c, arch, code, cache_dir, include_dirs, target)
    253         options.append("-I"+i)
    254 
--> 255     return compile_plain(source, options, keep, nvcc, cache_dir, target)
    256 
    257 

/usr/lib/python2.7/dist-packages/pycuda/compiler.pyc in compile_plain(source, options, keep, nvcc, cache_dir, target)
    135         raise CompileError("nvcc compilation of %s failed" % cu_file_path,
    136                 cmdline, stdout=stdout.decode("utf-8", "replace"),
--> 137                 stderr=stderr.decode("utf-8", "replace"))
    138 
    139     if stdout or stderr:

CompileError: nvcc compilation of /tmp/tmpVgfyrm/kernel.cu failed
[command: nvcc --cubin -arch sm_61 -I/usr/local/lib/python2.7/dist-packages/pycuda-2017.1.1-py2.7-linux-x86_64.egg/pycuda/cuda kernel.cu]
[stderr:
ERROR: No supported gcc/g++ host compiler found, but clang-3.8 is available.
       Use 'nvcc -ccbin clang-3.8' to use that instead.
]

任何人都知道如何将-ccbin clang-3.8添加到pycuda ??

2 个答案:

答案 0 :(得分:1)

根据documentation,您可以通过两种方式为nvcc指定编译器选项

  1. 通过PYCUDA_DEFAULT_NVCC_FLAGS环境变量设置默认编译器选项。
  2. 通过使用SourceModule关键字传递的列表设置给定options=的编译器选项

答案 1 :(得分:1)

对于遇到问题的每个人来说,解决方案是使用options参数给出的b talonmies。我使用的代码如下:

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];
}
""", options=["-ccbin","clang-3.8"])

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

或使用:

pycuda.compiler.DEFAULT_NVCC_FLAG = ["-ccbin","clang-3.8"]