1

It seems that recent changes to Thrust (part of CUDA Toolkit) 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. I am not sure why they would do this.

We have a large application that uses host_vector in many .cpp files, anyone know a good way around this? I don't think compiling everything with nvcc is 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<int> &vec);

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

cudaFunc.cu

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

bool cudaFunc(thrust::host_vector<int> &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\<int\> \>&)'

collect2: error: ld returned 1 exit status

This will link fine on Ubuntu 20 with Cuda 12.3.

@paleonix Thank you for your reply. I downloaded cccl-2.6.1 and compiled with:

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

The error persists:

/usr/bin/ld: /tmp/cc966x8Y.o: in function `main':
main.cpp:(.text+0x30): undefined reference to `cudaFunc(thrust::THRUST_200601___CUDA_ARCH_LIST___NS::host_vector<int, std::allocator<int> >&)'
collect2: error: ld returned 1 exit status

Workaround:

I defined a new namespace for thrust objects, in my case mgxthrust, and replaced all the thrust::host_vector and other thrust names referenced in the application with mgxthrust::host_vector.

Then in a header file:

namespace mgx_thrust { namespace thrust {} }
#define THRUST_WRAPPED_NAMESPACE mgx_thrust
namespace mgxthrust = mgx_thrust::thrust;

Note this needs to be called before other thrust headers.

4
  • While this seems like a different issue to Unable to include thrust/host_vector.h and others with CUDA 12.5 it might also be fixed in a newer CCCL version. You can take a newer CCCL from GitHub - it should be compatible with older CUDA Toolkits. Commented Nov 1, 2024 at 20:06
  • @ paleonix I tried this, but I am not sure I am compiling the cpp correctly. Commented Nov 4, 2024 at 9:49
  • You seem to be using the development branch (main). I would rather use a release (they are tagged in Git). Commented Nov 4, 2024 at 10:55
  • @paleonix Thanks. The most recent release seems to be 2.6.1 and it now gives the same error. Commented Nov 4, 2024 at 11:28

1 Answer 1

2

It appears that the cuda architecture has been added to the namespace name for thrust objects (including host objects) in order to reduce problems with shared libraries compiled with different architectures: https://nvidia.github.io/cccl/cub/developer_overview.html#symbols-visibility https://github.com/NVIDIA/cccl/issues/2737

So not a bug per se, but rather an expected side effect of recent changes to address other issues.

Thanks to https://github.com/jrhemstad and https://forums.developer.nvidia.com/u/Robert_Crovella for the answer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.