General-purpose programming on GPU


Giuseppe Bilotta, Eugenio Rustico, Alexis Hérault

DMI — Università di Catania
Sezione di Catania — INGV

OpenCL images

OpenCL images and samplers are the counterparts to CUDA Arrays and textures.

Images are memory objects (like buffers), and are allocated with the following syntax (in OpenCL 1.1):

cl_mem clCreateImage2D(cl_context ctx, cl_mem_flags flags,
           cl_image_format *fmt, size_t width, size_t height,
           size_t row_pitch, void *host_ptr, cl_int *error);

where the row pitch is the pitch in bytes of a single line in the host data (if host_ptr is not NULL).

Image formats

The image format specifies the format of the data:

typedef struct _cl_image_format {
          cl_channel_order image_channel_order;
          cl_channel_type image_channel_data_type;
} cl_image_format;

with the channel order specifying which channels are present in the data, and in which order they are returned (e.g. CL_R, CL_RG, CL_RGB, CL_RGBA or CL_ARGB), whereas the channel data type specifies the format (signed or unsigned integer, short, or float, normalized or unnormalized).

(For a full list of channel orders and data types, see the OpenCL reference)


Images are read using a sampler, which is just a set of flat that determine whether to normalize coordinates, how addressing beyond the image is handled (NONE, MIRROR, REPEAT) and whether texture filtering should be enabled (CLK_FILTER_LINEAR) or not (CLK_FILTER_NEAREST).

Image reading and writing

In contrast to CUDA, images can be read from and written to by kernels, but in a given kernel an image can only be read from, or written to (it is not possible to read and write the same image in a single kernel).

Reading and writing is always done with 4-components vector (uint4, int4 or float4), where only the channels defined by the image channel order are used.

Read functions (read_imagef, read_imagei, read_imageui) take as parameters an image, a sampler and the coordinates. The coordinates are either an int2 (unnormalized) or a float2 (normalized).

Write functions (write_imagef, write_imagei, write_imageui) take as parameters an image, the coordinates and the value to be written.

Example OpenCL program and kernel using OpenCL images for transposition can be found here and here.