OpenCL:Open Computing Language,low level language with high performance
可以运行在各种硬件,CPU, GPU,甚至FPGA上,自身支持vector计算以及含有数学库,在不同硬件上运行时的精度是有保证的。
代码基于C99,普适性好,但是语言比较陈旧,嗑代码费劲。
Open Standard,大部分主流硬件厂商已支持。
和CUDA相比,OpenCL没有那么多的好用特性和工具,但是可以支持更多种类的硬件。
如果只是用于个人或者临时的项目,选用CUDA更加方便,时间成本更低。但如果想做一个自身的成熟产品,则使用OpenCL更加合适,因为硬件的选择面更广,产品可以不依赖N社硬件,更稳定。
优势
1,computationally intensive:
什么是computational intensity: math ops / memory ops
随机访问内存消耗大约数百 cpucycles,而浮点数乘法只消耗 3个左右,由此可知,计算比访问内存快几十倍量级。
我们可以根据算法代码的实际实现,推断出目前处理的 computational intensity。
2,data-parallel
各个数据点之间的处理相互独立(以图像为例)
3,single-precision
单精度和双精度在GPU上的计算时间成本是不同的,基本两倍,主要是因为数据本身的大小导致。
关键概念
1, global and local dimensions
global demensions
根据数据的维数分配核资源
gd里面的每个数据点都有一个独立的工作线程来处理数据,比如一个3维数据有几十M数据时,OpenCL会将这些数据均分到GPU的计算单元上,尽最大可能并行计算。
local dimension
将global任务均分到local 的工作组中。(local work group)
例:
gd: 100*512
ld: 10*8, 100*1, 50*2, 2*128
invalid ld: 10*10, 16*16
一个work group中的任务会被分配到一个相同的计算单元。
同一个工作组的数据对象是可以同步的。因为它们在同一个线程。
注意事项
1,work group是有大小限制的(~512)
2,work group invalid synchronization:不同的compute uni的同步成本很高。
(GPU体系结构,compute unit,texture units,L1 texture caches, local data shares)
3,OpenCL only supports global sync at the end of kernel exec. (CUDA is more flexible but less slower performance, global sync cost is high)
4, 要注意 work group的大小要和cpu核数保持一致,或者尽最大可能利用核资源
2, compute kernels
c99,intrinsics, utility functions
3, OpenCL architecture
platform, context, command queues
Data Movement
memory model:
host(host memory) 4-32G
|| PCIe, slow, 5-10G bw
device(
global memory, 0.25-12G
|| local data exchange, much faster, 50-200G bw
local memory binding compute unit, 16-48kB
|| private data exchange, fastest, 1000G bw
private memory(just registers)
OpenCL program
Setup:Device, context, command queue
Compilation: program, compile, create kernels??,
Create memory objs,
--> Enqueue writes, copy data to GPU
Set kernel args
--> Enqueue kernel exec
--> Enqueue reads, copy data from GPU
Wait for all to finish