【问题标题】:pycuda "nvcc fatal : Cannot find compiler 'cl.exe' in PATH"pycuda“nvcc 致命:在 PATH 中找不到编译器 'cl.exe'”
【发布时间】:2017-04-12 21:32:16
【问题描述】:

操作系统:win10

VS:visual stadio2015 64bit

CUDA:CUDA8.0

python:python2.7.12 64bit (pycuda)

我关注了这个网站, https://documen.tician.de/pycuda/tutorial.html#getting-started

import  pycuda.driver as cuda
import pycuda.autoinit
from pycuda.compiler import SourceModule

import numpy

a = numpy.random.randn(4,4)
a = a.astype(numpy.float32)
a_gpu = cuda.mem_alloc(a.nbytes)
cuda.memcpy_htod(a_gpu,a)#transfer the data to the GPU

#executing a kernel
#function: write code to double each entry in a_gpu.
#we write the corresponding CUDA C code, and feed it into the constructor of pycuda.compiler.SourceModule
mod = SourceModule("""
    __global__ void doublify(float *a)
    {
        int idx = threadIdx.x + threadIdx.y*4;
        a[idx] *= 2;
    }
    """)

#If there aren’t any errors, the code is now compiled and loaded onto the device. We find a reference to our pycuda.driver.Function and call it, specifying a_gpu as the argument, and a block size of 4x4:
func = mod.get_function("doublify")
func(a_gpu, block=(4,4,1))

#Finally, we fetch the data back from the GPU and display it, together with the original a:
a_doubled = numpy.empty_like(a)
cuda.memcpy_dtoh(a_doubled, a_gpu)
print a_doubled
print a

但是,失败并出现错误:

Traceback (most recent call last):
  File "G:/myworkspace/python2.7/cuda/test.py", line 24, in <module>
    """)
  File "D:\python2.7\lib\site-packages\pycuda\compiler.py", line 265, in __init__
    arch, code, cache_dir, include_dirs)
  File "D:\python2.7\lib\site-packages\pycuda\compiler.py", line 255, in compile
    return compile_plain(source, options, keep, nvcc, cache_dir, target)
  File "D:\python2.7\lib\site-packages\pycuda\compiler.py", line 137, in compile_plain
    stderr=stderr.decode("utf-8", "replace"))
CompileError: nvcc compilation of c:\users\gl\appdata\local\temp\tmp8poxqp\kernel.cu failed
[command: nvcc --cubin -arch sm_50 -m64 -Id:\python2.7\lib\site-packages\pycuda\cuda kernel.cu]
[stdout:
nvcc fatal   : Cannot find compiler 'cl.exe' in PATH

]

有人说要将cl.exe的目录添加到环境中。我做到了,错误是一样的。我是 CUDA 的新手。我该如何解决这个问题?有什么建议吗?

我按照@citizenSNIPS 的建议做了:

添加cl.exe的路径,D:\vs2015\VC\bin。

INCLUDE = C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt.

LIB = C:\Program Files (x86)\Windows Kits\10\Lib\10.0.10240.0\ucrt\x64(我找不到 C:\Program Files (x86)\Windows Kits\10\Lib\ 10.0.10240.0\um\x64 在我的电脑上)。

有一个新的错误如下:

raceback (most recent call last):
  File "G:\myworkspace\python2.7\cuda\test.py", line 24, in <module>
    """)
  File "D:\python2.7\lib\site-packages\pycuda\compiler.py", line 265, in __init__
    arch, code, cache_dir, include_dirs)
  File "D:\python2.7\lib\site-packages\pycuda\compiler.py", line 255, in compile
    return compile_plain(source, options, keep, nvcc, cache_dir, target)
  File "D:\python2.7\lib\site-packages\pycuda\compiler.py", line 147, in compile_plain
    + (stdout+stderr).decode("utf-8", "replace"), stacklevel=4)
  File "D:\python2.7\lib\idlelib\run.py", line 36, in idle_showwarning_subproc
    message, category, filename, lineno, line))
  File "D:\python2.7\lib\idlelib\PyShell.py", line 65, in idle_formatwarning
    s += "%s: %s\n" % (category.__name__, message)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 147-168: ordinal not in range(128)

现在我正在解决这个问题,可能是因为我没有添加 C:\Program Files (x86)\Windows Kits\10\Lib\10.0.10240.0\um\x64?

【问题讨论】:

    标签: python cuda pycuda


    【解决方案1】:

    您也可以在 python 文件中添加cl.exe 的路径。缺点是如果您的 MSVS 版本更改,您将不得不更改它。

    例子:

    import os
    if (os.system("cl.exe")):
        os.environ['PATH'] += ';'+r"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\bin\HostX64\x64"
    if (os.system("cl.exe")):
        raise RuntimeError("cl.exe still not found, path probably incorrect")
    

    编辑:您需要运行与 CUDA 兼容的 MSVS 版本。 IE。 CUDA v9.0 不支持 MSVS2017,CUDA v9.1 仅支持 15.4 版本,不支持更高版本。通过从 Visual Studio 的本机工具命令提示符运行 nvcc.exe 来尝试它是否有效。

    【讨论】:

      【解决方案2】:

      您需要指定 cl.exe 的路径。

      1. 转到“控制面板\所有控制面板项\系统”并选择“高级系统设置”
      2. 选择“环境变量”。
      3. 在系统变量下,找到PATH,点击编辑,添加cl.exe的路径。应该是:

        C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\

      确保在安装 Visual Studio 时选择安装 c++ 编译器。默认情况下不安装它。如果没有,请重新运行 Visual Studio 安装程序并选择安装 c++ 编译器。

      完成后,您可能需要添加以下系统变量

      INCLUDE = C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt
      LIB = C:\Program Files (x86)\Windows Kits\10\Lib\10.0.10240.0\um\x64
            C:\Program Files (x86)\Windows Kits\10\Lib\10.0.10240.0\ucrt\x64
      

      看到这个帖子here

      【讨论】:

      • 非常感谢。我做了: 1.添加cl.exe的路径,D:\vs2015\VC\bin。 2. 包含 = C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt。 3. LIB = C:\Program Files (x86)\Windows Kits\10\Lib\10.0.10240.0\ucrt\x64(我找不到 C:\Program Files (x86)\Windows Kits\10\Lib\10.0 .10240.0\um\x64 在我的电脑中)。出现了一个新错误。
      • 现在新的错误已经解决,感谢您的帮助。
      • 你是如何解决这个错误的?我有同样的问题。谢谢
      • @Litwos 按照接受的答案。我写下答案并接受自己。
      • 我也试过了,还是不行。我尝试了各种不同的 VC(我有 MSVS 和 VC 运行时)
      【解决方案3】:

      我将这些代码添加到 .py 中

      import sys
      reload(sys)
      sys.setdefaultencoding('utf8')
      

      运行,没有错误

      【讨论】:

      • 您是使用 IDE 运行代码还是从终端调用?
      • 我使用 IDLE(python GUI)。
      • 对于 Python 3,重载已移至 importlib:from importlib import reload 然后 reload(sys),而 utf8 已经是 Python 3 的默认值。
      • 我试过了,还是不行,还是报错
      猜你喜欢
      • 2020-06-16
      • 2016-09-19
      • 2016-02-10
      • 1970-01-01
      • 2015-08-22
      • 1970-01-01
      • 2018-03-09
      • 2012-02-17
      • 1970-01-01
      相关资源
      最近更新 更多