编译并链接Cuda和Clang以支持主机代码上的c++20



我正在尝试使用Clang而不是Gcc编译、链接和执行一个简单的Cuda示例。使用Clang的总体思想是允许在主机代码中使用c++20,并使用llvm/Clang堆栈进行更多编译器优化。

我查看了以下来源:llvm文档谷歌关于gpucc的论文这个例子来自llvm文档中关于用clang 编译cuda的内容

#include <iostream>
__global__ void axpy(float a, float* x, float* y) {
y[threadIdx.x] = a * x[threadIdx.x];
}
int main(int argc, char* argv[]) {
const int kDataLen = 4;
float a = 2.0f;
float host_x[kDataLen] = {1.0f, 2.0f, 3.0f, 4.0f};
float host_y[kDataLen];
// Copy input data to device.
float* device_x;
float* device_y;
cudaMalloc(&device_x, kDataLen * sizeof(float));
cudaMalloc(&device_y, kDataLen * sizeof(float));
cudaMemcpy(device_x, host_x, kDataLen * sizeof(float),
cudaMemcpyHostToDevice);
// Launch the kernel.
axpy<<<1, kDataLen>>>(a, device_x, device_y);
// Copy output data to host.
cudaDeviceSynchronize();
cudaMemcpy(host_y, device_y, kDataLen * sizeof(float),
cudaMemcpyDeviceToHost);
// Print the results.
for (int i = 0; i < kDataLen; ++i) {
std::cout << "y[" << i << "] = " << host_y[i] << "n";
}
cudaDeviceReset();
return 0;
}

使用的编译和链接命令是:

clang++-12 axpy.cu -o axpy --cuda-gpu-arch=sm_72     
-L/usr/local/cuda-11.4/lib64 -lcudart -ldl -lrt -pthread axpy.cu 
--cuda-path=/usr/local/cuda-11 --no-cuda-version-check

输出指示它成功编译但未能链接:

clang: warning: Unknown CUDA version. cuda.h: CUDA_VERSION=11040. Assuming the l                                                                                              atest supported version 10.1 [-Wunknown-cuda-version]
/usr/bin/ld: /tmp/axpy-35c781.o: in function `__device_stub__axpy(float, float*,                                                                                            float*)':
axpy.cu:(.text+0x0): multiple definition of `__device_stub__axpy(float, float*,                                                                                          float*)'; /tmp/axpy-c82a7d.o:axpy.cu:(.text+0x0): first defined here
/usr/bin/ld: /tmp/axpy-35c781.o: in function `main':
axpy.cu:(.text+0xa0): multiple definition of `main'; /tmp/axpy-c82a7d.o:axpy.cu:                                                                                          (.text+0xa0): first defined here
clang: error: linker command failed with exit code 1 (use -v to see invocation)

该错误似乎表明clang对要链接的代码进行了多次传递,并错误地包含了main两次。

操作系统:Ubuntu 20.04内核5.40库达:11.4Clang(2013年12月11日试用(

如果有任何关于如何让CUDA和Clang合作的提示,我将不胜感激。到目前为止我已经尝试过的东西:不同的Clang版本11/12/13。不同的Cuda版本11.2/11.4。

输出表示编译成功但链接失败:

axpy.cu:(.text+0xa0): multiple definition of `main';

这似乎已经在评论中进行了排序:

也许唯一的问题是在编译命令中传递axpy.cu两次。

clang++-12 axpy.cu -o axpy --cuda-gpu-arch=sm_72 -L/usr/local/cuda-11.4/lib64 -lcudart -ldl -lrt -pthread axpy.cu --cuda-path=/usr/local/cuda-11 --no-cuda-version-check
^^^^^^^                                                                                        ^^^^^^^

就这样。谢谢。

相关内容

最新更新