Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL

Chapter 1. Introducing OpenCL > The OpenCL standard and extensions - Pg. 13

The OpenCL standard and extensions clReleaseMemObject(res_buff); clReleaseKernel(kernel); clReleaseCommandQueue(queue); clReleaseProgram(program); clReleaseContext(context); return 0; } 13 This source file is long but straightforward. Most of the code is devoted to creating Open CL 's data structures, which obey a simple naming convention: the cl_context is called context , the cl_platform_id is called platform , the cl_device_id is called device , and so on. If you follow this convention, you can copy and paste most of your code from one host application to the next. In contrast, the creation of the cl_program and the cl_kernel structures changes from application to application. In listing 1.1, the application creates a kernel from a function in a file called More precisely, it reads the characters from mat- into a character array, creates a program from the character array, and compiles the program. Then it constructs a kernel from a function called matvec_mult . The kernel code in is much shorter than the host code in matvec.c. The single function, matvec_mult , performs the entire matrix-vector multiplication algo- rithm depicted in figure 1.3. Chapters 2 and 3 discuss how to code host applications like the one presented in listing 1.1. Chapters 4 and 5 explain how to code kernel functions like the one in the following listing. Listing 1.2 Performing the dot-product on the device: __kernel void matvec_mult(__global float4* matrix, __global float4* vector, __global float* result) { int i = get_global_id(0); result[i] = dot(matrix[i], vector[0]); } If you're eager to compile the code in these two listings and test the dot-product, I rec- ommend that you visit appendix A, which explains how to obtain and use Open CL 's development tools. But before you do this, you should have a top-level understanding of the Open CL standard, which we'll discuss next. 1.5 The OpenCL standard and extensions If you look through the Open CL website at, you'll find an important file called opencl-1.1.pdf. This contains the Open CL 1.1 specification, which provides a wealth of information about the language. It defines not only Open CL 's functions and data structures, but also the capabilities required by a ven- dor's development tools. In addition, it sets the criteria that all devices must meet to be considered compliant.