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

Creating program objects


An OpenCL application can execute a function in parallel on a device using the kernel objects. There may be more than one kernel functions, which run in parallel in an application based on the hardware you have. An application can create multiple program objects each for a different context. Each of these program objects can have more than one kernel object. Each kernel in a program source string is identified by a __kernel qualifier. Let us first create a cl_program object.

Creating and building program objects

The OpenCL kernel programs needs to be built and linked at runtime. In OpenCL a program object can be created using the functions, clCreateProgramWithSource or clCreateProgramWithBinary. A program object is created once for a context in execution. Input to these functions is a source text string in ASCII or in binary format respectively. The program object is created for the devices associated with the OpenCL context. The clCreateProgramWithSource function...