CUDA Thrust object shared between .cu and .cpp files

Also posted on SO, although so far no joy. It looks like recent changes to Thrust were made to tag the host_vector class name with the compile time CUDA architecture. This creates an issue (undefined reference) when linking shared objects compiled with nvcc or not (.cu vs .cpp files) that use Thrust objects, even host only ones like host_vector.

We have a large application that uses host_vector in many .cpp files, and compiling everything with nvcc is probably not an option, some of the files need moc (ie Qt).

To reproduce on Ubuntu 20.04.6 LTS with Cuda 12.6:

Main.cpp:

include <cuda_runtime.h>
include <thrust/host_vector.h>

bool cudaFunc(thrust::host_vector &vec);

int main()
{
thrust::host_vector vec;
cudaFunc(vec);
}

cudaFunc.cu:


include <cuda_runtime.h>
include <thrust/host_vector.h>

bool cudaFunc(thrust::host_vector &vec)
{
return true;
}

Compile with:

/usr/local/cuda/bin/nvcc -c cudaFunc.cu
g++ -I /usr/local/cuda/include -L /usr/local/cuda/targets/x86_64-linux/lib main.cpp cudaFunc.o -lcudart -ldl

will produce the error:

/usr/bin/ld: /tmp/cc5fFAJC.o: in function main:
main.cpp:(.text+0x30): undefined reference to cudaFunc(thrust::THRUST_200500___CUDA_ARCH_LIST___NS::host_vector<int, std::allocator >&)

This will link fine on Ubuntu 20 with Cuda 12.3.

Does anyone know if this is a bug? (so it might get fixed) or is compiling thrust objects in both .cu files and .cpp files not supported?

I’ve seen other similar observations. I don’t think it is a bug, if you are compiling for the CUDA backend.

You could file an issue if you wish.