cancel
Showing results for 
Search instead for 
Did you mean: 

OpenCL

sajis997
Adept I

Probable usage of texture memory

Hello forum,

The simulation i am trying to run represents data on a 2D grid. The natural representation for this grid on the CPU is array . The analog of array on the GPU is  the texture. So i believe that different type of data structure can be represented with 3/4 color channels. In my simulation i shall store the vector data represented as float2 in the texture and i am confused about its representation within color channels while creating the 2D texture and its access inside the kernel by sampler.

Some hint along with discussion will help me to get around this issue.

cl_mem initTexture(int width,int height)

{

   cl_int ciErrNum;

   cl_image_format image_format;

   image_format.image_channel_order = CL_RGBA;

   image_format.image_channel_data_type = CL_UNSIGNED_INT8;

   //create an empty image

   cl_mem imageTexture = clCreateImage2D(cxGPUContext,CL_MEM_READ_WRITE,

                                                   &image_format,width,height,0,NULL,

                                                   &ciErrNum);

   oclCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);

    

   return imageTexture;

}

The above snippet initialize the 2D texture. Please the texture will be containing the float2 type value which will be copied later from the host. Do i need to make any changes above to support float2 type?

What should i look out for while configuring the sampler ?

I shall be eagerly looking forward to your response.

Regards

Sajjad

0 Kudos
Reply
4 Replies
LeeHowes
Staff
Staff

Re: Probable usage of texture memory

The analogue of the array on the GPU is the array. The analogue of a malloced memory region that might be interpreted as an array is the buffer.

However, if you really want to use textures (whether they give a benefit or not is very application dependent and you might have to experiment) you will want to change the format to 2-channel floating point:

   image_format.image_channel_order = CL_RG;

   image_format.image_channel_data_type = CL_FLOAT;

sajis997
Adept I

Re: Probable usage of texture memory

Thanks for the hint!

Is there any better option than using the texture array for 2D grid ?

I am basically trying to port a CUDA application where they have used texture referencing from the array and the performance is not that bad.

I assume that it will remain same with OpenCL as well.

Regards

Sajjad

0 Kudos
Reply
nou
Exemplar

Re: Probable usage of texture memory

second alternative is using buffers which are only linear. so you must calculate your 1D position from 2D like array[y*width+x]

0 Kudos
Reply
himanshu_gautam
Grandmaster

Re: Probable usage of texture memory

Hi Sajis,

Textures are faster because of 2-D caches, which result in better cache-hit when the access pattern is tiled in nature.

If you require similar access pattern in your application, textures (or cl_image) objects should be the right choice.