Book Image

OpenCL Programming by Example

Book Image

OpenCL Programming by Example

Overview of this book

Research in parallel programming has been a mainstream topic for a decade, and will continue to be so for many decades to come. Many parallel programming standards and frameworks exist, but only take into account one type of hardware architecture. Today computing platforms come with many heterogeneous devices. OpenCL provides royalty free standard to program heterogeneous hardware. This guide offers you a compact coverage of all the major topics of OpenCL programming. It explains optimization techniques and strategies in-depth, using illustrative examples and also provides case studies from diverse fields. Beginners and advanced application developers will find this book very useful. Beginning with the discussion of the OpenCL models, this book explores their architectural view, programming interfaces and primitives. It slowly demystifies the process of identifying the data and task parallelism in diverse algorithms. It presents examples from different domains to show how the problems within different domains can be solved more efficiently using OpenCL. You will learn about parallel sorting, histogram generation, JPEG compression, linear and parabolic regression and k-nearest neighborhood, a clustering algorithm in pattern recognition. Following on from this, optimization strategies are explained with matrix multiplication examples. You will also learn how to do an interoperation of OpenGL and OpenCL. "OpenCL Programming by Example" explains OpenCL in the simplest possible language, which beginners will find it easy to understand. Developers and programmers from different domains who want to achieve acceleration for their applications will find this book very useful.
Table of Contents (18 chapters)
OpenCL Programming by Example
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Case study – Histogram calculation


In the section Histogram calculation in Chapter 3, Buffers and Image Objects, we discussed about the naive implementation of histogram computation of an image. We read an input image file and pass the pixel buffer to the OpenCL device to compute the histogram of the image. By now you must have observed that this implementation is not so optimized which involves sequential reads. In this section we will try to optimize this implementation by making use of atomic_inc OpenCL C built-in and make use of coalesced reads and writes to the global and local memory. Take a look at the following kernel:

#define BIN_SIZE            256
#define ELEMENTS_TO_PROCESS 256
__kernel
void histogram_kernel(__global const uint* data,
                  __global uint* binResultR,
                  __global uint* binResultG,
                  __global uint* binResultB)
{
  __local int sharedArrayR[BIN_SIZE];
  __local int sharedArrayG[BIN_SIZE];
  __local int sharedArrayB[BIN_SIZE...