编译 CUDA 与数学函数的叮当

Compiling CUDA with clang on math functions

本文关键字:函数 叮当 CUDA 编译      更新时间:2023-10-16

编译以下 CUDA 代码helloWorld.cu使用 clang-11

int main() {
return max(1.0f, 2.0f);
}

,使用命令clang++-11 -o helloWorld helloWorld.cu --cuda-gpu-arch=sm_75 -ldl -lrt -lcudart_static -pthread -L/usr/local/cuda/lib64,遇到错误:

helloWorld.cu:2:12: error: no matching function for call to 'max'
return max(1.0f, 2.0f);
^~~
/usr/lib/llvm-11/lib/clang/11.0.0/include/__clang_cuda_math.h:194:16: note: candidate function not viable: call to __device__ function from __host__ function
__DEVICE__ int max(int __a, int __b) { return __nv_max(__a, __b); }
...
/usr/local/cuda-10.2/include/crt/math_functions.hpp:1079:31: note: candidate function not viable: call to __device__ function from __host__ function
__MATH_FUNCTIONS_DECL__ float max(float a, float b)
...

请注意,编译器实际上正确定位了匹配函数(即"math_functions.hpp:1079:31"(,但被错误地推断为"_device_"函数。

提前感谢您的任何帮助。

您编写的代码是主机代码,C++语法上无效。该代码不应编译,并且编译器行为是正确的。为了编译,代码应如下所示:

#include <algorithm>
int main() {
return std::max(1.0f, 2.0f);
}

也就是说,您必须实际包含定义max函数的标准库标头,并且您必须使用正确的命名空间。C++没有内置的max功能。库达做到了。您所看到的只是 clang CUDA 编译轨迹的工件。