我试图用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 Cuda: 11.4 Clang (尝试11/12/13)
我希望你能给我提示,如何让CUDA和Clang一起工作。到目前为止,我尝试过的东西有:不同的Clang版本( 11/12/13 ),不同的Cuda版本( 11.2/11.4 )。
发布于 2021-08-14 17:41:31
输出表明它成功编译,但未能链接:
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
^^^^^^^ ^^^^^^^就是这样。谢谢。
https://stackoverflow.com/questions/68441684
复制相似问题