简体   繁体   English

C++17 可以与使用 clang 的 CUDA 一起使用吗?

[英]Can C++17 be used together with CUDA using clang?

As far as using nvcc , one needs to use the corresponding gcc (currently max. 5.4 I believe) in conjunction.至于使用nvcc ,需要结合使用相应的gcc (目前最大 5.4 我相信)。 This of course somewhat prevents one from using C++17 on the host side.这当然在某种程度上阻止了在主机端使用 C++17。

Since C++17 can be compiled using clang 5 and upwards (see here ), and one can compile cuda code as well (see here ), is it possible to use both C++17 and CUDA at the same time (or can there be problems, eg with the CUDA runtime)?由于 C++17 可以使用clang 5及更高版本进行编译(请参阅此处),并且也可以编译 cuda 代码(请参阅此处),因此是否可以同时使用 C++17 和 CUDA (或可以有问题,例如 CUDA 运行时)?

Yes, as you already guessed the CUDA clang frontend is indeed ahead in C++ feature support, even in device code.是的,正如您已经猜到的,CUDA clang 前端在 C++ 功能支持方面确实领先,即使在设备代码方面也是如此。 It was already in the past, introducing C++14 features before NVCC which was mostly unnoticed by the community.它已经成为过去,在 NVCC 之前引入 C++14 功能,但社区几乎没有注意到。

Take this C++17, unnecessarily modified if constexpr , snippet:Fibo拿这个 C++17, if constexpr不必要的修改,片段:Fibo

#include <cuda_runtime.h>
#include <cstdio>

constexpr unsigned
fibonacci(const unsigned x) {
    if constexpr (false)
    {
        return 0u;
    }
    if( x <= 1 )
        return 1;
    return fibonacci(x - 1) + fibonacci(x - 2);
}

__global__
void k()
{
    constexpr unsigned arg = fibonacci(5);
    printf("%u", arg);
}

int main()
{
    k<<<1,1>>>();
    return 0;
}

It already runs with clang++ -std=c++17 -x cuda : https://cuda.godbolt.org/z/GcIqeW它已经与clang++ -std=c++17 -x cudahttps : clang++ -std=c++17 -x cuda

Nevertheless, for this specific example, C++17 extended lambdas and C++14 relaxed constexpr are that important in modern C++, that even in C++11 and C++14 mode of NVCC 8.0+ flags were added to enable those features already: https://devblogs.nvidia.com/new-compiler-features-cuda-8/尽管如此,对于这个特定的例子,C++17 扩展的 lambdas 和 C++14 宽松的 constexpr 在现代 C++ 中很重要,即使在 C++11 和 C++14 模式下,NVCC 8.0+ 标志也被添加来启用这些功能已经: https : //devblogs.nvidia.com/new-compiler-features-cuda-8/

That means the above example compiles for example with NVCC 9.2 even without __device__ qualifiers when removing the demonstrating C++17 if constexpr construct and adding -std=c++14 --expt-relaxed-constexpr flags.这意味着在删除演示 C++17 if constexpr构造并添加-std=c++14 --expt-relaxed-constexpr标志时,即使没有__device__限定符,上面的示例也可以使用 NVCC 9.2 进行编译。

Here is a list about C++ standard support on the device side for nvcc and clang -x cuda : https://gist.github.com/ax3l/9489132#device-side-c-standard-support (NVCC 11.0 supports device-side C++17 now.)以下是有关nvccclang -x cuda设备端 C++ 标准支持的列表: https ://gist.github.com/ax3l/9489132#device-side-c-standard-support(NVCC 11.0 支持设备端现在是 C++17。)

Currently up to C++14 is supported in device code (Introduced in CUDA 9 )目前设备代码最多支持 C++14(在CUDA 9 中引入)

--std {c++03|c++11|c++14}

Options for Specifying Behavior of Compiler/Linker 用于指定编译器/链接器行为的选项

However, if your host is only using C++17, it should be possible to use separate compilation and link them with library.但是,如果您的主机仅使用 C++17,则应该可以使用单独的编译并将它们与库链接。 Separate Compilation and Linking of CUDA C++ Device Code CUDA C++设备代码的单独编译链接

Update: formatting and more info更新:格式和更多信息

I've encountered this same problem, and here's a simple example for anyone who meets the same problem too.我遇到了同样的问题,这里有一个简单的例子,供任何遇到同样问题的人使用。

Assume that there're 3 files: main.cpp cuda_calling.cpp cuda_calling.h , in which, the main.cpp uses c++17 or what so ever that your nvcc does not support.假设有 3 个文件: main.cpp cuda_calling.cpp cuda_calling.h ,其中main.cpp使用c++17或您的nvcc不支持的任何内容。 As the name suggested, the cuda_calling.cpp must be compiled by nvcc .顾名思义, cuda_calling.cpp必须由nvcc编译。

First, build cuda_calling.cpp as a lib (as in the real case, there may be many files calling cuda. Otherwise, it's OK to build as an object file ie cuda_calling.o ).首先,将cuda_calling.cpp构建为 lib(在实际情况下,可能有很多文件在调用 cuda。否则,构建为目标文件即cuda_calling.o )。 The output file is assumed as lib_cudacalling.a .输出文件假定为lib_cudacalling.a Then build the main.cpp and the whole project as a cmake file:然后将main.cpp和整个项目构建为一个 cmake 文件:

cmake_minimun_required(VERSION 3.5)
project(separate)
add_compile_options(-std=c++17)
find_package(CUDA REQUIRED)

add_library(${PROJECT_NAME}.o OBJECT main.cpp) # build as an object file first
add_executable(${PROJECT_NAME} $<TARGET_OBJECTS:${PROJECT_NAME}.o>)

target_link_libraries(${PROJECT_NAME} cuda_calling) # you may need to specify the path

You can see that the key point is to build them separately .你可以看到,关键的一点是单独构建它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM