Skip to content
Search

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

OpenCL Reference Pages

Intel

NVIDIA

Guide

Tutorials

  • [Recommended] The OpenCL Programming Book - FREE HTML VERSION
  • OpenCL Tutorials 1 - Quickstart
  • OpenCL Basics Part 2 11
  • [Recommended] ColorCode - 19 posts in 'Program Language/OpenCL' 12
    • 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

References


  1. OpenCL_Basics_-Flags_for_the_creating_memory_objects-_StreamHPC.pdf ↩

  2. Opencl-quick-reference-card.pdf ↩

  3. NVIDIA_OpenCL_ProgrammingGuide.pdf ↩

  4. Opencl-1.1.pdf ↩

  5. How_to_Increase_Performance_by_Minimizing_Buffer_Copies_on_Intel_Processor_Graphics.pdf ↩

  6. 1068_GTC09.pdf ↩

  7. Adventures_in_OpenCL_Part_2_-_Particles_with_OpenGL.pdf ↩

  8. Porting_CUDA_to_OpenCL_-_Documentation.pdf ↩

  9. The_OpenCL_Programming_Book_-_3.3_First_OpenCL_Program.pdf ↩

  10. Liminia_-OpenCL_Tutorials_1-_Quickstart.pdf ↩

  11. Leemino_-_OpenCL_basic_2.pdf ↩

  12. ColorCode_-Program_Language-_OpenCL.pdf ↩

  13. Hoororyn_-_OpenCL_Tutorial_2.pdf ↩

  14. Hoororyn_-_OpenCL_Tutorial_3.pdf ↩

  15. Hoororyn_-_OpenCL_Tutorial_4.pdf ↩

  16. Hoororyn_-_OpenCL_Tutorial_5.pdf ↩

  17. Hoororyn_-_OpenCL_Tutorial_6.pdf ↩

  18. Hoororyn_-_OpenCL_Tutorial_7.pdf ↩

  19. Hoororyn_-_OpenCL_Tutorial_8.pdf ↩

  20. Hoororyn_-_OpenCL_Tutorial_9.pdf ↩

author Zer0 sitemap