Skip to content

OpenCL

OpenCL(Open Computing Language)은 개방형 범용 병렬 컴퓨팅 프레임워크이다. CPU, GPU, DSP 등의 프로세서로 이루어진 이종 플랫폼에서 실행되는 프로그램을 작성할 수 있게 해 준다. OpenCL은 커널 코드를 작성하기 위한 C99 기반의 언어인 OpenCL C와 플랫폼을 정의하고 제어하기 위한 API를 포함하고 있다. OpenCL은 작업 기반(task-based) 및 데이터 기반(data-based) 병렬 컴퓨팅을 제공한다.

OpenCL이 만들어진 이유는 OpenGL이나 OpenAL이 만들어진 이유와 비슷하다. OpenGL과 OpenAL은 각각 3차원 컴퓨터 그래픽스 및 컴퓨터 오디오에 대한 산업계의 개방형 표준이다. OpenCL은 비영리 기술 컨소시엄인 크로노스 그룹(Khronos Group)에서 관리하고 있다.

Category

요소들

Platform
OpenCL 에서 Platform은 이종 디바이스들과 하나의 호스트, OpenCL 프레임워크로 이루어져 있습니다. 이종 디바이스들은 우리가 흔히 알고 있는 CPU, GPU, DSP 등등 입니다. Platform 은 이러한 이종 디바이스들이 사용 가능한지, OpenCL 프레임워크가 해당 시스템에서 사용가능한지를 질의하고 선택하기 위해 사용된다고 생각하시면 됩니다. 이러한 질의와 선택은 호스트(응용) 프로그램에서 수행되며, 선택된 디바이스들로부터 Context 나 Kernel, Queue, Buffer 등을 생성하게 됩니다. OpenCL 모듈화 작업을 할때 Platform 단위로 아래에서 설명하게 될 Device, Program, Context, Kernel 등을 모아서 관리하면 편합니다.
Device
시스템에 OpenCL 플랫폼을 찾게 되면, 그 다음으로 찾아야 하는 것이 계산 디바이스를 찾는 것입니다. OpenCL 에서 Device 란 개념은 계산을 수행할 수 있는 유닛(Compute Unit)들의 집합입니다. Compute Unit 은 한개이상의 PE(Processing Element)로 이루어져 있으며, GPU, CPU 등이 Compute Unit 이라고 생각하면 됩니다. 예전에 GPU는 그래픽 랜더링을 담당하는 부품이였다면, OpenCL 명세가 나온 현재는 CPU보다 월등한 계산능력을 가진 부품이 된 것이죠. OpenCL Device 들은 많은 정보를 질의 할 수 있는데 이러한 정보들을 바탕으로 명령어 집합인 커널을 최적화할 수 있습니다.
Context
OpenCL 에서 Context 의 개념은 매우 중요하다고 할 수 있습니다. 각 디바이스들에서 사용할 수 있는 Buffer 나 Command Queue를 할당해준다. 하지만 프로그래머가 Context를 관리하는 이슈는 적으며, 하나의 Context 가 어떤 디바이스들과 연관이 있는지만 알면 된다.
Program
OpenCL 에서 Program이란 개념은 Kernel 들의 집합이라고 생각하시면 됩니다. 흔히 dll 을 프로그램에 비유하고 dll 에 있는 함수들을 Kernel 에 비유합니다. 프로그램은 응용프로그램 실행할때마다 매번 소스로부터 빌드 할수도 있고, 이미 빌드되어진 바이너리로부터 읽어들일 수도 있습니다. 개발할때는 소스로 매번 빌드하고, 배포할때는 바이너리로 배포하면 효율적입니다.
Kernel
Kernel 이란 위에서도 설명하였지만, 명령어 집합(함수)을 가지고 있는 객체입니다. 이런 함수들을 실제 실행하기 위해서 커널 함수에 인자를 전달해주고 실행하고 결과값을 받는 등의 일체 행위를 커널에서 합니다. 3D 프로그래밍을 하셨던 분들은 glsl 이나 hlsl 로 쉐이더 프로그램을 만들때 사용한 함수라고 생각하시면 됩니다.
Command Queue
Kernel을 특정 디바이스에서 실행 시키기 위해서는 Command Queue 에 집어넣으면 순서대로 Kernel을 실행시킵니다.

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

후로린의 프로그래밍 이야기 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