Bound CUDA texture reads zero

家住魔仙堡 提交于 2019-12-05 09:31:08

You are using cudaBindTexture to bind your texture to the memory allocated by cudaMalloc. In the kernel you are using tex2D function to read values from the texture. That is why it is reading zeros.

If you bind texture to linear memory using cudaBindTexture, it is read using tex1Dfetch inside the kernel.

tex2D is used to read only from those textures which are bound to pitch linear memory ( which is allocated by cudaMallocPitch ) using the function cudaBindTexture2D, or those textures which are bound to cudaArray using the function cudaBindTextureToArray

Here is the basic table, rest you can read from the programming guide:

Memory Type----------------- Allocated Using-----------------Bound Using-----------------------Read In The Kernel By

Linear Memory...................cudaMalloc........................cudaBindTexture.............................tex1Dfetch

Pitch Linear Memory.........cudaMallocPitch.............cudaBindTexture2D........................tex2D

cudaArray............................cudaMallocArray.............cudaBindTextureToArray.............tex1D or tex2D

3D cudaArray......................cudaMalloc3DArray........cudaBindTextureToArray.............tex3D

To add on, access using tex1Dfetch is based on integer indexing. However, the rest are indexed based on floating point, and you have to add +0.5 to get the exact value you want.

I'm curious why do you create float and bind to a float2 texture? It may gives ambiguous results. float2 is not 2D float texture. It can actually be used for representation of complex number.

typedef struct {float x; float y;} float2;

I think this tutorial will help you understand how to use texture memory in cuda. http://www.drdobbs.com/parallel/cuda-supercomputing-for-the-masses-part/218100902

The kernel you shown does not benefit much from using texture. however, if utilized properly, by exploiting locality, texture memory can improve the performance by quite a lot. Also, it is useful for interpolation.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!