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

前端 未结 2 786
萌比男神i
萌比男神i 2021-02-19 08: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 u

2条回答
  •  不思量自难忘°
    2021-02-19 09:02

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

提交回复
热议问题