Issues with declaring texture in a header file

I have encountered the following problem: I have a .cu file which calls a kernel that utilizes a texture. For compilation reasons I have declared the texture and the kernel call in a header file which is included in the original .cu file. The implementation of the kernel is implemented in a separate .cu file. The problem occurs when I try to launch the kernel, cause then I get a launch failure for the kernel. After I while I realized that the failure occured when I tried access the texture. So, I just tried to include the .cu file with the implementation instead of the header and now all of a sudden it worked without a problem.

Comments?

Textures, constant memory declarations, kernels and other device objects are compiled and assembled at file scope. There is no CUDA linker, so you can’t link across different object files. As a result, you must declare the texture at the same scope as all kernels that use it.

Textures, constant memory declarations, kernels and other device objects are compiled and assembled at file scope. There is no CUDA linker, so you can’t link across different object files. As a result, you must declare the texture at the same scope as all kernels that use it.

If you don’t want to use your texture as a global variable in header files, there is a little trick I use, don’t know if it’s what you are looking for.

I declare the texture as global variable in my .cu, write a function getref to get the ref on that texture, declare the function in my header file and include this header and I can use it anywhere in my program :

*** .cu ***

texture<unsigned short, 2, cudaReadModeElementType> ref;

texture<unsigned short, 2, cudaReadModeElementType> &get_ref(void)

{

	return ref;

}

*** .h ***

texture<unsigned short, 2, cudaReadModeElementType> &get_ref(void);

*** .c ***

cudaBindTextureToArray(get_ref(), ref_ary);

If you don’t want to use your texture as a global variable in header files, there is a little trick I use, don’t know if it’s what you are looking for.

I declare the texture as global variable in my .cu, write a function getref to get the ref on that texture, declare the function in my header file and include this header and I can use it anywhere in my program :

*** .cu ***

texture<unsigned short, 2, cudaReadModeElementType> ref;

texture<unsigned short, 2, cudaReadModeElementType> &get_ref(void)

{

	return ref;

}

*** .h ***

texture<unsigned short, 2, cudaReadModeElementType> &get_ref(void);

*** .c ***

cudaBindTextureToArray(get_ref(), ref_ary);