[英]Compiling CUDA with clang on math functions
用clang-11編譯下面的CUDA代碼helloWorld.cu ,
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)
...
請注意,匹配的 function 實際上被編譯器正確定位(即“math_functions.hpp:1079:31”),但被錯誤地推斷為“_device_” function 。
感謝您提前提供任何幫助。
您編寫的代碼是主機代碼,它在語法上不是有效的 C++。 該代碼不應編譯,並且編譯器行為是正確的。 為了編譯,代碼應該如下所示:
#include <algorithm>
int main() {
return std::max(1.0f, 2.0f);
}
也就是說,您必須實際包含標准庫 header,它定義了max
function,並且您必須使用正確的命名空間。 C++ 沒有內置max
function。 CUDA 可以。 你所看到的只是 clang CUDA 編譯軌跡的神器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.