pycuda/nvcc遇到的几个报错解决记录

pycuda/nvcc遇到的几个报错解决记录

环境

windows 11
torch 2.2.1
cuda 12.1
vscode 1.94.2
pycuda 2024.1
MSVC19.41(或称VS 2022 17.10,Visual Studio 2022 17.10)
未安装visual studio

报错类型1:Cannot find compiler ‘cl.exe’ in PATH

错误

pycuda执行compiler.SourceModule(kernel_code),试图调用nvcc编译cu代码时报错,找不到cl.exe。

[stdout:
nvcc fatal   : Cannot find compiler 'cl.exe' in PATH
]

解决方案:

如果cl.exe未安装,则需要安装visual studio 生成工具(可参考此链接或类似的帖子),此时在

C:\Program Files (x86)\Microsoft Visual Studio\2022\VC\Tools\MSVC\xx.xx.xxxxx\bin\Hostx64\x86

下会有cl.exe。

将C:\Program Files (x86)\Microsoft Visual Studio\2022\VC\Tools\MSVC\xx.xx.xxxxx\bin\Hostx64\x86添加到环境变量中,然后在cmd执行cl命令测试,应有如下输出:

C:\Users\Administrator>cl
用于 x86 的 Microsoft (R) C/C++ 优化编译器 xx.xx.xxxxx版
版权所有(C) Microsoft Corporation。保留所有权利。

用法: cl [ 选项... ] 文件名... [ /link 链接选项... ]

此时,系统环境变量配置完毕。然后重启vscode刷新环境变量。

若重启vscode后仍找不到cl.exe,则使用win+r运行cmd,再执行code命令,从命令行打开vscode:

Microsoft Windows [版本 10.0.22631.4317]
(c) Microsoft Corporation。保留所有权利。

C:\Users\Administrator>code

此时vscode将继承全部的环境变量。可以在vscode中点击终端-新建终端,执行cl命令测试;若配置正确,应和前文中cmd处的输出相同。
注意,如果有使用utools,注意通过utools启动的程序是不会刷新环境变量的,需要关闭utools-启动utools。这里有点小坑。

报错类型2: unsupported Microsoft Visual Studio version

错误

已经安装了vs2022,但nvcc报错,提示vs版本不对。

fatal error C1189: #error:  
-- unsupported Microsoft Visual Studio version! 
Only the versions between 2017 and 2022 (inclusive) are supported! 
The nvcc flag '-allow-unsupported-compiler' can be used to override this version check; however, using an unsupported host compiler may cause compilation failure or incorrect run time execution. Use at your own risk.

解决方案

原因是vs版本过高,或者cuda版本过低。
CUDA 12.4 是第一个识别和支持 MSVC 19.40(又名 VS 2022 17.10)的版本。此前的cuda版本检测到MSVC版本过高时将直接报错。
如果保持VS版本不变,可以将cuda升级到12.4或以后的版本。
如果保持cuda版本不变,可以将MSVC回退到v14.39(VS 2022 17.9)或之前。
此外,此链接提示cuda 12.2与vs 2022 17.9.2可以兼容。

参考

  1. https://stackoverflow.com/questions/78515942/cuda-compatibility-with-visual-studio-2022-version-17-10
  2. https://devblogs.microsoft.com/cppblog/msvc-toolset-minor-version-number-14-40-in-vs-2022-v17-10/
  3. https://mwell.tech/archives/12662
  4. https://www.codenong.com/42003849/
  5. https://docs.nvidia.com/cuda/archive/12.4.1/cuda-installation-guide-microsoft-windows/index.html
  6. https://discuss.tvm.apache.org/t/windows-cuda-error-host-compiler-targets-unsupported-os/3711
  7. https://www.cnblogs.com/shrimp-can/p/5567518.html
  8. https://forums.developer.nvidia.com/t/nvcc-fatal-host-compiler-targets-unsupported-os/83401
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值