Use texture reference in another file

I defined a texture reference in one file, let’s say in file A, as global variable

//File: A.cu

texture<float, 1, cudaReadModeElementType> texRef;

I bind the texRef to a cudaArray. I was trying to use this reference in file B by defining a external variable to texRef by

//File: B.cu

extern texture<float, 1, cudaReadModeElementType> texRef;

It turns out that in file B, the texRef is not the texRef I defined in file A anymore, or at least I did not the right result. But I put the exactly the same code which use the texRef in file B into file A, it give me correct result. Any thing I did wrong here?

When I read some post in the forum, I found that the texture reference is a static variable at the file scope. So it seems that it is not ok to extern this variable in another file. Any idea to share the texture variable in different file?

Indeed.

Don’t share the reference. Share the cudaArray between files. You need to bind indifidual texture references inside each compilation unit (binding is very cheap).

Thanks. By saying share the cudaArray between files. Do you mean

extern cudaArray *cuArray;

It works. Thanks, DrAndeerson42.

Whatever way works for you.

I follow an object oriented pardigm and never have any global variables. Instead, cuda arrays and device pointers are all allocated and managed in c++ classes an passed into functions in .cu files as parameters.

That’s a good idea. Thanks.