可以使用clang将C ++ 17与CUDA一起使用吗? [英] Can C++17 be used together with CUDA using clang?

查看:133
本文介绍了可以使用clang将C ++ 17与CUDA一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就使用 nvcc 而言,需要结合使用相应的 gcc (我认为目前最多为5.4).当然,这在某种程度上阻止了人们在主机端使用C ++ 17.

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.

因为C ++ 17可以使用 clang 5 及更高版本进行编译(请参见),那么也可以编译cuda代码(请参见此处),是否可以同时使用C ++ 17和CUDA (或者是否有问题,例如CUDA运行时)?

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

推荐答案

是的,您已经猜到CUDA clang前端确实在C ++功能支持方面领先,甚至在设备代码方面也是如此.过去已经有过,在NVCC之前引入了C ++ 14功能,而这在大多数情况下是社区所不知道的.

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.

使用此C ++ 17,不必要地修改 if constexpr ,摘要:

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;
}

它已经与 clang ++ -std = c ++ 17 -x cuda 一起运行:尽管如此,在这个特定示例中,C ++ 17扩展的lambda和C ++ 14宽松的constexpr在现代C ++中非常重要,甚至在NVCC 8.0+标志的C ++ 11和C ++ 14模式下也添加了已启用这些功能: https://devblogs.nvidia.com/new-compiler-features-cuda-8/

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 if constexpr 构造并添加 -std时,即使没有 __ device __ 限定符,也可以使用NVCC 9.2进行编译= c ++ 14 --expt-relaxed-constexpr 标志.

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.

以下是有关 nvcc clang -x cuda 的设备端C ++标准支持的列表:

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

这篇关于可以使用clang将C ++ 17与CUDA一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆