Using Buffers and Images Appropriately

On both CPU and Intel® Graphics devices, buffers usually perform better than images: more data transfers per read or write operation for buffers with much lower latency.

If your algorithm does not require linear data interpolation or specific border modes, consider using buffers instead of images. Still, if your legacy code uses images, or if you want to use the linear interpolation ability of the sampler, consider using the cl_khr_image2d_from_buffer extension which offers creating zero copy image aliases for the buffers.

To improve performance on the Intel Graphics, do the following:

  • Consider using images if you need linear interpolation between pixel values.
  • Consider using images for irregular access patterns. For example, use buffers when processing in memory (in row-major) order. Yet prefer image2D and texture sampling your access pattern is other than simple linear. For example, a kernel that reads diagonally or generally irregular positions.
  • Use local memory for explicit caching of data values rather than relying on sampler’s caches, as the caches do not support image write operations.
  • Notice however, that for (even mildly large) look-up tables the regular global memory is preferable over local memory.

  • Use constant samplers to be able to specify and optimize the sampling behavior in compile time.
  • Consider using the CL_ADDRESS_CLAMP_NONE as it is the fastest addressing mode, and use CL_ADDRESS_CLAMP_TO_EDGE rather than CL_ADDRESS_CLAMP.

In general, image2D sampling on the Intel Graphics offers:

  • Free type conversions. For example, uchar4 to uint4 (unavailable on CPU).
  • Automatic handling image boundaries (slower on the CPU).
  • Fast bilinear sampling (works slow on CPU; may vary between devices).

Notice that images are software-emulated on a CPU. So, make sure to choose the fastest interpolation mode that meets your needs. Specifically:

  • Nearest-neighbor filtering works well for most (interpolating) kernels.
  • Linear filtering might decrease CPU device performance.

See Also

Memory Access Overview
Applying Shared Local Memory (SLM)
Using Image2D From Buffer Extension

For more complete information about compiler optimizations, see our Optimization Notice.