I am trying to compile, link and execute a simple Cuda example using Clang instead of Gcc. The general idea behind using Clang is to allow c++20 in Host Code and more compiler optimizations using the llvm/clang stack.
I looked at the following sources: llvm docs google paper about gpucc This example is from the llvm documents about compiling cuda with clang
#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;
}
The Command to compile and link used is:
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
The output indicates that it successfully compiles but fails to link:
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)
The error seems to indicate that clang is doing multiple passes over the code to link and wrongfully includes main twice.
OS: Ubuntu 20.04 Kernel 5.40 Cuda: 11.4 Clang (tried 11/12/13)
I would be grateful for any hints how to get CUDA and Clang working together. Things I have tried so far: Different Clang versions 11/12/13. Different Cuda Versions 11.2/11.4.