How can I use the "offset" returned by cudaBindTexture() in texture fetching? cudaBindText

Hello All,

I allocated one big chunk of memory using cudaMalloc() and I partioned/divided that into some parts…

When I bind one of the sub part of the memory by using cudaBindTexture() API, it is returning cudaErrorInvalidValue.

Below is the code…

[b]unsigned char* gpuMem=NULL;

cudaMalloc( (void**)&gpuMem, 10KB); // allocated 10 Kilo Bytes memory


unsigned short int* partOfGpuMem = (unsigned short int*)(gpuMem+2KB); // 2 kilo bytes is the offset

cudaError error = cudaBindTexture( 0, texHandle, partOfGpuMem, sizeof(unsigned short int)*srcW ); // Here srcW is 800 [/b]
Here error is cudaErrorInvalidValue.

I searched for this on internet, I found the usgae of cudaBindTexture() link…2791346fd2f2706

After that, I changed the code like below…

size_t offset;
cudaError error = cudaBindTexture( &offset, texHandle, partOfGpuMem, sizeof(unsigned short int)*srcW ); // Here srcW is 800

Here, “error” is cudaErrorInvalidValue and “offset” is 128.

The below is the discription of cudaBindTexture() from the above link

Since the hardware enforces an alignment requirement on texture base addresses, cudaBindTexture() returns in *offset a byte offset that must be applied to texture fetches in order to read from the desired memory. This offset must be divided by the texel size and passed to kernels that read from the texture so they can be applied to the tex1Dfetch() function

But Im not getting how to use “offset” in fetching and what is “texel size” and why I should devide offesrt by the texel size.

please help me on how to use the “offset” in texture fetching?.

The texel size is the size of the element of your texture. So if your texture contains float4 elements, you need

int index_offset = offset / sizeof(float4)

And then access element i from the texture using:

tex1Dfetch(texHandle, i + index_offset);

Dear All;

I am experiencing a similar problem as described here using the cudaBindTexture function. In my case, i get the cudaErrorInvalidValue at runtime also when binding to a subpart of the memory. I’m not sure what value is invalid? I’m guessing it is the 0 value passed as offset. I passed a NULL in my case. Can anyone confirm if this is the invalid value? Will using &offset solve the problem?

Thanks for helping.


I figure out that your pointer name is “partOfGpuMem”, I guess that the partOfGpuMem is an device pointer points to an offset of an allocated memory. If so, that’s why you got “cudaErrorInvalidValue”.

The pointer passed to cudaBindTexture should be the begin address of the allocated memory.

(in host part)

float* devArray;
size_t offset;
cudaMalloc((void**)&devArray,sizeof(float) * LENGTH);
// if partOfGpuMem is pointer got from devArray + x
// your tex1Dfetch should like tex1Dfetch(texHandle, ( x + offset ) / sizeof(float) )