OpenCL
lastmod 2021-04-10created 2014-03-1139 edits
OpenCL (Open Computing Language) is an open, general-purpose parallel computing framework. It lets you write programs that run on heterogeneous platforms made up of processors such as CPUs, GPUs, and DSPs. OpenCL includes OpenCL C, a C99-based language for writing kernel code, and an API for defining and controlling the platform. OpenCL provides task-based and data-based parallel computing.
OpenCL was created for reasons similar to those behind OpenGL and OpenAL. OpenGL and OpenAL are open industry standards for 3D computer graphics and computer audio, respectively. OpenCL is managed by the Khronos Group, a non-profit technology consortium.
Category
Elements
- Platform
- In OpenCL, a Platform consists of heterogeneous devices, one host, and the OpenCL framework. The heterogeneous devices are the familiar CPU, GPU, DSP, and so on. You can think of the Platform as what is used to query and select whether these heterogeneous devices are available and whether the OpenCL framework is available on the system. These queries and selections are performed in the host (application) program, and Context, Kernel, Queue, Buffer, etc. are created from the selected devices. When modularizing OpenCL, it is convenient to group and manage the Device, Program, Context, Kernel, etc. described below per Platform.
- Device
- Once you find an OpenCL platform on the system, the next thing to find is a compute device. In OpenCL, a Device is a set of units that can perform computation (Compute Units). A Compute Unit consists of one or more PEs (Processing Elements), and you can think of a GPU, CPU, etc. as a Compute Unit. Whereas the GPU used to be the component responsible for graphics rendering, now that the OpenCL specification is out, it has become a component with far greater computing power than the CPU. OpenCL Devices can be queried for a lot of information, and based on that information you can optimize the kernel, which is a set of instructions.
- Context
- The concept of a Context is very important in OpenCL. It allocates the Buffers and Command Queues that each device can use. However, the programmer rarely has to deal with managing the Context; you only need to know which devices a Context is associated with.
- Program
- In OpenCL, you can think of a Program as a set of Kernels. A common analogy compares a program to a dll and the functions in the dll to Kernels. A program can be built from source every time the application runs, or loaded from an already built binary. It is efficient to build from source every time during development and to distribute it as a binary for release.
- Kernel
- As explained above, a Kernel is an object that holds a set of instructions (functions). To actually run these functions, the kernel handles everything: passing arguments to the kernel function, executing it, receiving the result, and so on. If you have done 3D programming, think of it as the functions you used when writing shader programs in glsl or hlsl.
- Command Queue
- To run a Kernel on a specific device, put it into a Command Queue, which runs the Kernels in order.
Memory objects
Pinned memory
The pinned memory refers to a memory that as well as being in the device, exists in the host, so a DMA write is possible between these 2 memories. Increasing the copy performance. That is why it needs CL_MEM_ALLOC_HOST_PTR in the buffer creation params.
On the other hand, CL_MEM_USE_HOST_PTR will take a host pointer for buffer creation, it is unclear by the spec if this can or cannot be a pinned memory. But generally speaking, it should NOT be pinned memory created this way, since the host pointer has not been reserved by the OpenCL API and is not clear where it resides in memory.
OpenCL resource management
n_elements = 1024 * 1024;
mem = clCreateBuffer (context, CL_MEM_READ_WRITE,
n_elements * sizeof (float),
NULL, &errcode);
/* Launch kernel with one parameter */
clSetKernelArg (kernel, 0, sizeof (cl_mem), &mem);
clEnqueueNDRangeKernel (cmd_queue, kernel,
1, NULL, &n_elements, NULL,
0, NULL, &event);
/* Wait for end of execution and release all resources */
clWaitForEvents (1, &event));
clReleaseMemObject (mem);
clReleaseKernel (kernel);
clReleaseProgram (program);
clReleaseCommandQueue (cmd_queue);
clReleaseContext (context);
OpenCL device query
See also
Favorite site
- Wikipedia (en) Description of OpenCL
- Getting started with OpenCL, Part #1
- Stackoverflow: how to compile opencl project with kernels
- Stackoverflow: OpenCL compile on linux
- How to compile OpenCL example in GCC?
OpenCL Reference Pages
- OpenCL 1.0 Reference Pages
- OpenCL API 1.0 Quick Reference Card 2
- OpenCL Programming Guide for the CUDA Architecture 3
Intel
- Intel® SDK for OpenCL™ Applications
- Intel OpenCL Code Builder Support
- Simple Optimizations of OpenCL Code
- Getting the Most from OpenCL™ 1.2: How to Increase Performance by Minimizing Buffer Copies on Intel® Processor Graphics 5
NVIDIA
Guide
- [Recommended] Adventures in OpenCL Part 2: Particles with OpenGL 7
- Porting CUDA to OpenCL 8
- Using zero-copy buffers on integrated GPUs
- Simple opencl sample code and explanation
Tutorials
- 2013.04.19 - Basic concepts of GPGPU Computing.
- 2013.01.21 - OpenCL errors
- 2012.12.21 - global memory replay overhead
- 2012.11.07 - [CUDA] occupancy
- 2012.11.02 - NVIDIA clEnqueueReadBuffer non-blocking bug(?)
- 2012.10.15 - Intel opencl platform analyzer
- 2012.10.08 - DeviceQuery
- 2012.10.05 - OpenCL porting
- 2012.09.24 - Release
- 2012.09.24 - clCreateBuffer clEnqueueWriteBuffer clSetKernelArg clEnqueueReadBuffer
- 2012.09.21 - Easily compiling external OpenCL files
- 2012.09.21 - [OCL] #include cannot open source file "......cl"
- 2012.08.08 - Intel OpenCL visual studio settings
- 2012.06.20 - amd opencl __constant
- 2012.06.04 - standard functions in OpenCL
- 2012.06.02 - clBuildProgram (1)
- 2012.06.02 - OpenCL Address Spaces
- 2012.05.26 - clFinish, clFlush, block
- 2012.05.22 - clEnqueueCopyBufferRect vs clEnqueueWriteBufferRect
Hoororyn's Programming Story: OpenCL
- #1 Installing OpenCL. A brief explanation.
- #2 OpenCL tutorial. Creating the OpenCL interface. 13
- #3 OpenCL tutorial. Creating buffers and running kernels. 14
- #4 OpenCL tutorial. Learning the whole workflow before getting into image processing 15
- #5 OpenCL tutorial. Convolution Filter and Local Memory theory 16
- #6 OpenCL tutorial. Convolution Filter and Local Memory practice 17
- #7 OpenCL tutorial. Convolution Filter and Local Memory. Optimization techniques 18
- #8 OpenCL tutorial. Median Filter and Bitonic Sort 19
- #9 OpenCL tutorial. Histogram 20
References
-
OpenCL_Basics_-Flags_for_the_creating_memory_objects-_StreamHPC.pdf ↩
-
Opencl-quick-reference-card.pdf ↩
-
NVIDIA_OpenCL_ProgrammingGuide.pdf ↩
-
Opencl-1.1.pdf ↩
-
How_to_Increase_Performance_by_Minimizing_Buffer_Copies_on_Intel_Processor_Graphics.pdf ↩
-
1068_GTC09.pdf ↩
-
Adventures_in_OpenCL_Part_2_-_Particles_with_OpenGL.pdf ↩
-
Porting_CUDA_to_OpenCL_-_Documentation.pdf ↩
-
The_OpenCL_Programming_Book_-_3.3_First_OpenCL_Program.pdf ↩
-
Liminia_-OpenCL_Tutorials_1-_Quickstart.pdf ↩
-
Leemino_-_OpenCL_basic_2.pdf ↩
-
ColorCode_-Program_Language-_OpenCL.pdf ↩
-
Hoororyn_-_OpenCL_Tutorial_2.pdf ↩
-
Hoororyn_-_OpenCL_Tutorial_3.pdf ↩
-
Hoororyn_-_OpenCL_Tutorial_4.pdf ↩
-
Hoororyn_-_OpenCL_Tutorial_5.pdf ↩
-
Hoororyn_-_OpenCL_Tutorial_6.pdf ↩
-
Hoororyn_-_OpenCL_Tutorial_7.pdf ↩
-
Hoororyn_-_OpenCL_Tutorial_8.pdf ↩
-
Hoororyn_-_OpenCL_Tutorial_9.pdf ↩