I’m confused about some basic aspects of how textures work. According to the documentation, I’m supposed to first create a texture reference:
texture<float4, 1, cudaReadModeElementType> texRef;
Then after I bind it to the appropriate memory, I use it inside a kernel to access the texture:
float4 value = tex1Dfetch(texRef, x);
Even a brief examination shows that something bizarre is going on here. texRef is declared without any modifiers; that is, it exists in host memory. Yet there I am referring to it inside a kernel! That can’t possibly work, and yet it does. It seems that texRef is two different symbols at once, one in host memory and one in device memory, and nvcc is doing something behind the scenes to make it look like a single variable. What is really going on here?
The reason I need to understand is that I’m writing multithreaded code. Two different threads might be executing the same kernel at the same time, but with different textures. It seems that when one thread calls cudaBindTexture() to bind a texture to the reference, that will mess up the other thread. What is the correct way of handling this?