从命令行运行 nvcc 时出现问题

2022-01-10 00:00:00 cuda visual-c++ c++ nvcc

我需要从命令行使用 nvcc 编译一个 cuda .cu 文件.该文件是vectorAdd_kernel.cu",包含以下代码:

I need to compile a cuda .cu file using nvcc from command line. The file is "vectorAdd_kernel.cu" and contains the following piece of code:

extern "C" __global__ void VecAdd_kernel(const float* A, const float* B, float* C, int N)
{
    int i = blockDim.x * blockIdx.x + threadIdx.x;
    if (i < N)
        C[i] = A[i] + B[i];
}

我使用了以下命令(我需要获取一个 .cubin 文件):

I used the following command (I need to get a .cubin file):

nvcc --cubin --use-local-env --cl-version 2010 -keep -I "C:Program Files (x86)Microsoft Visual Studio 10.0VCinclude" vectorAdd_kernel.cu

编译器创建文件 vectorAdd_kernel.cpp4.ii 和 vectorAdd_kernel.cpp1.ii 然后停止并输出以下输出:

The compiler creates files vectorAdd_kernel.cpp4.ii and vectorAdd_kernel.cpp1.ii then it stops with the following output:

C:UsersMassimoDesktopPluto>nvcc --cubin --use-local-env --cl-version 2010 vectorAdd_kernel.cu -keep -I "C:Program Files (x86)Microsoft Visual Studio 10.0VCinclude"

vectorAdd_kernel.cu

vectorAdd_kernel.cu

c:program files (x86)microsoft visual studio 10.0vcincludecodeanalysissourceannotations.h(29): error: invalid redeclaration of type name "size_t"

C:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/include
ew(51): error: first parameter of allocation function must be of type## Heading ## "size_t"

C:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/include
ew(55): error: first parameter of allocation function must be of type "size_t"

你能帮我解决这个问题吗?

Can you please help me in solving this issue?

推荐答案

我刚刚在 Visual Studio 2017 和 Cuda v9.0 中尝试使用 nvcc 从命令行编译时遇到了这个问题.经过长时间的会议后,我意识到我的 Visual Studio 命令行工具已设置为使用 x86 目录中的 cl.exe 而不是 x64.有很多方法可以解决它,一种方法是覆盖它寻找其编译器工具的目录 - 例如:

I just encountered this in Visual Studio 2017 and Cuda v9.0 trying to compile from the command line with nvcc. After a lengthy session I realised my Visual Studio Command line tools were setup to use cl.exe from the x86 director instead of the x64. There are a number of ways to resolve it, one way is to override the directory it looks for its compiler tools with - for example as in :

nvcc -ccbin "C:Program Files (x86)Microsoft Visual Studio2017EnterpriseVCToolsMSVC14.11.25503inHostX86x64"  -o add_cuda add_cuda.cu

然后它工作正常.

我还要提到我使用 git 工具中的 which.exe 实用程序来确定它正在访问的 cl.exe 版本,但是 where 命令 - Windows 原生 - 也可以.

I'll also mention that I used the which.exe utility from the git tools to figure out what version of cl.exe it was accessing, but the where command - native to windows - works as well.

另一种方法(可能是更好的方法)处理此问题的方法是将 Visual Studio 环境变量正确设置为企业版的 64 位,如下所示:

Another way - probably a better way - to handle this is to just set the Visual Studio environment variables correctly to 64 bits like this for the Enterprise edition:

"C:Program Files (x86)Microsoft Visual Studio2017EnterpriseVCAuxiliaryBuildvcvarsall.bat" x64

对于社区版,将路径中的企业"替换为社区".

For the Community edition substitute "Community" for "Enterprise" in the path.

您还可以使用(例如)--vcvars_ver=14.0 选择工具集,该工具集选择 14.0 工具集,这是使用 15.5 版本的 Visual Studio 编译 CUDA 9.1 所必需的.

You can also select the toolset with (for example) --vcvars_ver=14.0 which selects the 14.0 toolset, necessary to compile CUDA 9.1 with the 15.5 version of Visual Studio.

然后你可以用这个简单地构建:

Then you can build simply with this:

nvcc  -o add_cuda add_cuda.cu

相关文章