This book is designed as a concise introduction to OpenCL programming for developers working on diverse domains. It covers all the major topics of OpenCL programming and illustrates them with code examples and explanations from different fields such as common algorithm, image processing, statistical computation, and machine learning. It also dedicates one chapter to Optimization techniques, where it discusses different optimization strategies on a single simple problem.
Parallel programming is a fast developing field today. As it is becoming increasingly difficult to increase the performance of a single core machine, hardware vendors see advantage in packing multiple cores in a single SOC. The GPU (Graphics Processor Unit) was initially meant for rendering better graphics which ultimately means fast floating point operation for computing pixel values. GPGPU (General purpose Graphics Processor Unit) is the technique of utilization of GPU for a general purpose computation. Since the GPU provides very high performance of floating point operations and data parallel computation, it is very well suited to be used as a co-processor in a computing system for doing data parallel tasks with high arithmetic intensity.
Before NVIDIA® came up with CUDA (Compute Unified Device Architecture) in February 2007, the typical GPGPU approach was to convert general problems' data parallel computation into some form of a graphics problem which is expressible by graphics programming APIs for the GPU. CUDA first gave a user friendly small extension of C language to write code for the GPU. But it was a proprietary framework from NVIDIA and was supposed to work on NVIDIA's GPU only.
With the growing popularity of such a framework, the requirement for an open standard architecture that would be able to support different kinds of devices from various vendors was becoming strongly perceivable. In June 2008, the Khronos compute working group was formed and they published OpenCL1.0 specification in December 2008. Multiple vendors gradually provided a tool-chain for OpenCL programming including NVIDIA OpenCL Drivers and Tools, AMD APP SDK, Intel® SDK for OpenCL application, IBM Server with OpenCL development Kit, and so on. Today OpenCL supports multi-core programming, GPU programming, cell and DSP processor programming, and so on.
In this book we discuss OpenCL with a few examples.
Chapter 1, Hello OpenCL, starts with a brief introduction to OpenCL and provides hardware architecture details of the various OpenCL devices from different vendors.
Chapter 2, OpenCL Architecture, discusses the various OpenCL architecture models.
Chapter 3, OpenCL Buffer Objects, discusses the common functions used to create an OpenCL memory object.
Chapter 4, OpenCL Images, gives an overview of functions for creating different types of OpenCL images.
Chapter 5, OpenCL Program and Kernel Objects, concentrates on the sequential steps required to execute a kernel.
Chapter 6, Events and Synchronization, discusses coarse grained and fine-grained events and their synchronization mechanisms.
Chapter 7, OpenCL C Programming, discusses the specifications and restrictions for writing an OpenCL compliant C kernel code.
Chapter 8, Basic Optimization Techniques with Case Studies, discusses various optimization techniques using a simple example of matrix multiplication.
Chapter 9, Image Processing and OpenCL, discusses Image Processing case studies. OpenCL implementations of Image filters and JPEG image decoding are provided in this chapter.
Chapter 10, OpenCL-OpenGL Interoperation, discusses OpenCL and OpenGL interoperation, which in its simple form means sharing of data between OpenGL and OpenCL in a program that uses both.
Chapter 11, Case studies – Regressions, Sort, and KNN, discusses general algorithm-like sorting. Besides this, case studies from Statistics (Linear and Parabolic Regression) and Machine Learning (K Nearest Neighbourhood) are discussed with their OpenCL implementations.
The prerequisite is proficiency in C language. Having a background of parallel programming would undoubtedly be advantageous, but it is not a requirement. Readers should find this book compact yet a complete guide for OpenCL programming covering most of the advanced topics. Emphasis is given to illustrate the key concept and problem-solution with small independent examples rather than a single large example. There are detailed explanations of the most of the APIs discussed and kernels for the case studies are presented.
Application developers from different domains intending to use OpenCL to accelerate their application can use this book to jump start. This book is also good for beginners in OpenCL and parallel programming.
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text are shown as follows: " Each OpenCL vendor, ships this library and the corresponding OpenCL.dll
or libOpenCL.so
library in its SDK."
A block of code is set as follows:
void saxpy(int n, float a, float *b, float *c) { for (int i = 0; i < n; ++i) y[i] = a*x[i] + y[i]; }
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
#include <CL/cl.h> #endif #define VECTOR_SIZE 1024 //OpenCL kernel which is run for every work item created. const char *saxpy_kernel = "__kernel \n" "void saxpy_kernel(float alpha, \n" " __global float *A, \n" " __global float *B, \n" " __global float *C) \n" "{ \n" " //Get the index of the work-item \n" " int index = get_global_id(0); \n" " C[index] = alpha* A[index] + B[index]; \n" "} \n"; int main(void) { int i;
Any command-line input or output is written as follows:
# cp /usr/src/asterisk-addons/configs/cdr_mysql.conf.sample /etc/asterisk/cdr_mysql.conf
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "clicking on the Next button moves you to the next screen".
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>
, and mention the book title via the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
You can contact us at <[email protected]>
if you are having a problem with any aspect of the book, and we will do our best to address it.