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)
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,
oclCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);
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.
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;
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.
second alternative is using buffers which are only linear. so you must calculate your 1D position from 2D like array[y*width+x]
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.