【问题标题】:Problems when running nvcc from command line从命令行运行 nvcc 时出现问题
【发布时间】:2012-10-02 08:53:42
【问题描述】:

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

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 文件):

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

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

C:\Users\Massimo\Desktop\Pluto>nvcc --cubin --use-local-env --cl-version 2010 vectorAdd_kernel.cu -keep -I "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include"

vectorAdd_kernel.cu

vectorAdd_kernel.cu

c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(29): error: invalid redeclaration of type name "size_t"

C:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/include\new(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\new(55): error: first parameter of allocation function must be of type "size_t"

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

【问题讨论】:

  • .cu 文件中还有什么?为什么需要-I VC/include 路径?为什么需要--use-local-env--cl-version

标签: c++ visual-c++ cuda nvcc


【解决方案1】:

VS 社区 2019:

为 VS 2019 打开 x64 原生工具命令提示符

**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.3.6
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community>cd c:\Users\AFP\Downloads\cuda_by_example\chapter03

c:\Users\AFP\Downloads\cuda_by_example\chapter03>nvcc hello_world.cu
hello_world.cu
   Creating library a.lib and object a.exp

c:\Users\AFP\Downloads\cuda_by_example\chapter03>

如果环境没有为 x64 初始化并且你已经打开 x86 Native Tools Command Prompt for VS 2019,运行:

**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.3.6
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86'

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community>cd c:\Users\AFP\Downloads\cuda_by_example\chapter03

c:\Users\AFP\Downloads\cuda_by_example\chapter03>nvcc hello_world.cu
hello_world.cu

YOU GET LOT OF ERRORS ....

75 errors detected in the compilation of "C:/Users/AFP/AppData/Local/Temp/tmpxft_00004504_00000000-12_hello_world.cpp1.ii".

c:\Users\AFP\Downloads\cuda_by_example\chapter03>"c:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x64
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.3.6
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

c:\Users\AFP\Downloads\cuda_by_example\chapter03>nvcc hello_world.cu
hello_world.cu
   Creating library a.lib and object a.exp

c:\Users\AFP\Downloads\cuda_by_example\chapter03>

【讨论】:

    【解决方案2】:

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

    nvcc -ccbin "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\bin\HostX86\x64"  -o add_cuda add_cuda.cu
    

    然后它工作正常。

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

    更新:

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

    "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" x64
    

    对于社区版,将路径中的“企业”替换为“社区”。

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

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

    nvcc  -o add_cuda add_cuda.cu
    

    【讨论】:

    • 谢谢,很棒的提示。读完这篇文章后,我尝试了 VS17 x64 本机工具命令行提示符。工作。
    • 另一种解决方案是简单地从开始菜单启动“x64 Native Tools Command Prompt for VS 2017”。
    • 对于使用 CLION 的我来说,可以选择不同版本的 Visual Studio。选择AMD64设置和这个答案一样
    【解决方案3】:

    我也遇到过类似的问题。

    SourceAnnotations.h 中构建中断的代码:

    #ifdef  _WIN64
    typedef unsigned __int64    size_t;
    #else
    typedef _W64 unsigned int   size_t;
    #endif
    

    我已经用这个--compiler-options "-D _WIN64" 添加了_WIN64 编译器符号。我的 nvcc 构建字符串如下所示:

    nvcc kernel.cu --cubin --compiler-options "-D _WIN64"
    

    【讨论】:

    • 您能否详细说明问题的原因,或者此解决方案如何解决问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-09
    相关资源
    最近更新 更多