Compile cuda code to shared library with texture

Hey guys, I want to compile my cuda code to a shared library. So it can be invoked by other c++ code.

As already know,

  • Different progresses invoking the shared library will copy the data segment and share the code segment with each other.

  • Threads in the same progress invoking the shared library will share the data segment and code segment with each other.

  • And C/C++ uses _Thread_local or thread_local to avoid the influences from the threads in the same progress.
  • So I try to add a thread_local to texture,

    thread_local texture<float, 2, cudaReadModeElementType> texSrc_32f

    But it returns an error,

    code=18(cudaErrorInvalidTexture) "cudaUnbindTexture(texSrc_32f)"

    Is there any key words to reach the target like “thread_local” in cuda?


    A texture reference is a special entity in CUDA. I’m not surprised this doesn’t work, although I haven’t explored it.

    What you’re suggesting might be possible with a texture object instead of a texture reference, although I haven’t tried that either.

    CUDA supports the thread_local C++11 keyword in host code.

    I am not sure what you are trying to accomplish. I think you might want to clarify where you refer to host threads and where to GPU threads.

    Any auto variable in device code is local to a GPU thread: it gets mapped to either registers or to local memory, where the word “local” in the term “local memory” means GPU-thread-local. So local memory is a thread-specific mapping of a section of global memory.

    Since textures are read-only data objects, I am not sure what influences between the threads in the same process you are concerned about. By definition, host-side threads share most of the resources (including memory) owned by the host-side process to which they belong, with the exception of registers and thread-local memory.

    Thanks for your answer.

    I have tried the texture object and it worked.