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

给你一囗甜甜゛ 提交于 2020-12-02 05:59:06

问题


As far as using nvcc, one needs to use the corresponding gcc (currently max. 5.4 I believe) in conjunction. This of course somewhat prevents one from using C++17 on the host side.

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, e.g. with the CUDA runtime)?


回答1:


Yes, as you already guessed the CUDA clang frontend is indeed ahead in C++ feature support, even in device code. It was already in the past, introducing C++14 features before NVCC which was mostly unnoticed by the community.

Take this C++17, unnecessarily modified if constexpr, snippet: 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

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/

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.

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.)




回答2:


Currently up to C++14 is supported in device code (Introduced in 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. Separate Compilation and Linking of CUDA C++ Device Code

Update: formatting and more info



来源:https://stackoverflow.com/questions/47959767/can-c17-be-used-together-with-cuda-using-clang

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!