3.2 OpenCL平台模型
OpenCL平台需要包含一个主处理器和一个或多个OpenCL设备。平台模型定义了host和device的角色,并且为device提供了一种抽象的硬件模型。一个device可以被划分成一个或多个计算单元,这些计算单元在之后能被分成一个或多个“处理单元”(processing elements)。具体的关系可见图3.1。

图3.1 OpenCL平台具有多个计算设备。每个计算设备都具有一个或多个计算单元。一个计算单元又由一个或多个处理元素(PEs)构成。系统中可以同时具有多个平台。例如,在一个系统中可以既有AMD的平台和Intel的平台。
平台模型是应用开发的重点,其保证了OpenCL代码的可移植性(在具有OpenCL能力的系统间)。即使只在一个系统中,这个系统也可以具有多个不同的OpenCL平台,这些平台可以被不同的应用所使用。平台模型的API允许一个OpenCL应用能够适应和选择对应的平台和计算设备,从而在相应平台和设备上运行应用。
应用可以使用OpenCL运行时API,选择对应提供商提供的对应平台。不过,平台上能指定和互动的设备,也只限于供应商提供的那些设备。例如,如果选择了A公司的平台,那么就不能使用B公司的GPU。不过,平台硬件并不需要由供应商独家提供。例如,AMD和Intel的实现可以使用其他公司的x86 CPU作为设备。
编程者写编写OpenCL C代码时,设备架构会被抽象成平台模型。供应商只需要将抽象的架构映射到对应的物理硬件上即可。平台模型定义了具有一组计算单元的设备,且每个计算单元的功能都是独立的。计算单元也可以划分成更多个处理单元。图3.1展示的就是这样的一种层级模型。举个例子,AMD Radeon R9 290X图形卡(device)包含44个向量处理器(计算单元)。每个计算单元都由4个16通道SIMD引擎,一共就有64个SIMD通道(处理单元)。Radeon R9 290X上每个SIMD通道都能处理一个标量指令。运行GPU设备能同时执行44x16x4=2816条指令。
3.2.1 平台和设备
clGetPlatformIDs()这个API就是查找制定系统上的可用OpenCL平台的集合。在具体的OpenCL程序中,这个API一般会调用两次,用来查询和获取到对应的平台信息。第一次调用这个API需要传入num_platforms作为数量参数,传入NULL作为平台参数。这样就能获取在该系统上有多少个平台可供使用。编程者可以开辟对应大小的空间(指针命名为platforms),来存放对应的平台对象(类型为 cl_platform_id)。第二次调用该API是,就可将platforms传入来获取对应数量的平台对象。平台查找完成后,使用clGetPlatformInfo()API可以查询对应供应商所提供的平台,然后决定使用哪个平台进行运行OpenCL程序。clGetPlatformIDs()这个API需要在其他API之前调用,3.6节中可以从矢量相加的源码中进一步了解。
cl_intclGetPlatformIDs(cl_uint num_entries,cl_platform_id *platforms,cl_uint *num_platforms)
当平台确定好之后,下一步就是要查询平台上可用的设备了。clGetDeviceIDs()API就是用来做这件事的,并且在使用流程上和clGetPlatformIDs()很类似。clGetDeviceIDs()多了平台对象和设备类型作为入参,不过也需要简单的三步就能创建device:第一,查询设备的数量;第二,分配对应数量的空间来存放设备对象;第三,选择期望使用的设备(确定设备对象)。device_type参数可以将设备限定为GPU(CL_DEVICE_TYPE_GPU),限定为CPU(CL_DEVICE_TYPE_CPU),或所有设备(CL_DEVICE_TYPE_ALL),当然还有其他选项。这些参数都必须传递给clGetDeviceIDs()。相较于平台的查询API,clGetDeviceInfo()API可用来查询每个设备的名称、类型和供应商。
cl_intclGetDeviceIDs(cl_platform_id platform,cl_device_type device_type,cl_uint num_entries,cl_device_id *devices,cl_uint *num_devices)
AMD的并行加速处理软件开发工具(APP SDK)中有一个clinfo的程序,其使用clGetPlatformInfo()和clGetDeviceInfo()来获取对应系统中的平台和设备信息。硬件信息,比如内存总量和总线带宽也是可以用该程序获取。在了解其他OpenCL特性之前,我们先休息一下,了解一下clinfo的输入,如图3.2。
译者机器的clinfo显示,译者和原书使用的AMD APP SDK版本不大一样。从观察上来看,原书应该隐藏了一些硬件显示。
Number of platforms: 3Platform Profile: FULL_PROFILEPlatform Version: OpenCL 1.2 CUDA 8.0.0Platform Name: NVIDIA CUDAPlatform Vendor: NVIDIA CorporationPlatform Extensions:cl_khr_global_int32_base_atomicscl_khr_global_int32_extended_atomicscl_khr_local_int32_base_atomicscl_khr_local_int32_extended_atomicscl_khr_fp64cl_khr_byte_addressable_storecl_khr_icd cl_khr_gl_sharingcl_nv_compiler_optionscl_nv_device_attribute_querycl_nv_pragma_unrollcl_nv_d3d10_sharingcl_khr_d3d10_sharingcl_nv_d3d11_sharingcl_nv_copy_optsPlatform Profile: FULL_PROFILEPlatform Version: OpenCL 1.2Platform Name: Intel(R) OpenCLPlatform Vendor: Intel(R) CorporationPlatform Extensions:cl_intel_dx9_media_sharingcl_khr_3d_image_writescl_khr_byte_addressable_storecl_khr_d3d11_sharingcl_khr_depth_imagescl_khr_dx9_media_sharingcl_khr_gl_sharingcl_khr_global_int32_base_atomicscl_khr_global_int32_extended_atomicscl_khr_icd cl_khr_local_int32_base_atomicscl_khr_local_int32_extended_atomicscl_khr_spirPlatform Profile: FULL_PROFILEPlatform Version: OpenCL 2.0 AMD-APP (1800.8)Platform Name: AMD Accelerated Parallel ProcessingPlatform Vendor: Advanced Micro Devices, Inc.Platform Extensions:cl_khr_icdcl_khr_d3d10_sharingcl_khr_d3d11_sharingcl_khr_dx9_media_sharingcl_amd_event_callbackcl_amd_offline_devicesPlatform Name: NVIDIA CUDANumber of devices: 1Device Type: CL_DEVICE_TYPE_GPUVendor ID: 10dehMax compute units: 4Max work items dimensions: 3Max work items[0]: 1024Max work items[1]: 1024Max work items[2]: 64Max work group size: 1024Preferred vector width char: 1Preferred vector width short: 1Preferred vector width int: 1Preferred vector width long: 1Preferred vector width float: 1Preferred vector width double: 1Native vector width char: 1Native vector width short: 1Native vector width int: 1Native vector width long: 1Native vector width float: 1Native vector width double: 1Max clock frequency: 862MhzAddress bits: 64Max memory allocation: 536870912Image support: YesMax number of images read arguments: 256Max number of images write arguments: 16Max image 2D width: 16384Max image 2D height: 16384Max image 3D width: 4096Max image 3D height: 4096Max image 3D depth: 4096Max samplers within kernel: 32Max size of kernel argument: 4352Alignment (bits) of base address: 4096Minimum alignment (bytes) for any datatype: 128Single precision floating point capabilityDenorms: YesQuiet NaNs: YesRound to nearest even: YesRound to zero: YesRound to +ve and infinity: YesIEEE754-2008 fused multiply-add: YesCache type: Read/WriteCache line size: 128Cache size: 65536Global memory size: 2147483648Constant buffer size: 65536Max number of constant args: 9Local memory type: ScratchpadLocal memory size: 49152Kernel Preferred work group size multiple: 32Error correction support: 0Unified memory for Host and Device: 0Profiling timer resolution: 1000Device endianess: LittleAvailable: YesCompiler available: YesExecution capabilities:Execute OpenCL kernels: YesExecute native function: NoQueue on Host properties:Out-of-Order: YesProfiling : YesPlatform ID: 000002D3A374DC10Name: GeForce GTX 765MVendor: NVIDIA CorporationDevice OpenCL C version: OpenCL C 1.2Driver version: 375.95Profile: FULL_PROFILEVersion: OpenCL 1.2 CUDAExtensions:cl_khr_global_int32_base_atomicscl_khr_global_int32_extended_atomicscl_khr_local_int32_base_atomicscl_khr_local_int32_extended_atomicscl_khr_fp64cl_khr_byte_addressable_storecl_khr_icdcl_khr_gl_sharingcl_nv_compiler_optionscl_nv_device_attribute_querycl_nv_pragma_unrollcl_nv_d3d10_sharingcl_khr_d3d10_sharingcl_nv_d3d11_sharingcl_nv_copy_optsPlatform Name: Intel(R) OpenCLNumber of devices: 2Device Type: CL_DEVICE_TYPE_GPUVendor ID: 8086hMax compute units: 20Max work items dimensions: 3Max work items[0]: 512Max work items[1]: 512Max work items[2]: 512Max work group size: 512Preferred vector width char: 1Preferred vector width short: 1Preferred vector width int: 1Preferred vector width long: 1Preferred vector width float: 1Preferred vector width double: 0Native vector width char: 1Native vector width short: 1Native vector width int: 1Native vector width long: 1Native vector width float: 1Native vector width double: 0Max clock frequency: 1150MhzAddress bits: 64Max memory allocation: 427189862Image support: YesMax number of images read arguments: 128Max number of images write arguments: 128Max image 2D width: 16384Max image 2D height: 16384Max image 3D width: 2048Max image 3D height: 2048Max image 3D depth: 2048Max samplers within kernel: 16Max size of kernel argument: 1024Alignment (bits) of base address: 1024Minimum alignment (bytes) for any datatype: 128Single precision floating point capabilityDenorms: NoQuiet NaNs: YesRound to nearest even: YesRound to zero: YesRound to +ve and infinity: YesIEEE754-2008 fused multiply-add: NoCache type: Read/WriteCache line size: 64Cache size: 262144Global memory size: 1708759450Constant buffer size: 65536Max number of constant args: 8Local memory type: ScratchpadLocal memory size: 65536Kernel Preferred work group size multiple: 32Error correction support: 0Unified memory for Host and Device: 1Profiling timer resolution: 80Device endianess: LittleAvailable: YesCompiler available: YesExecution capabilities:Execute OpenCL kernels: YesExecute native function: NoQueue on Host properties:Out-of-Order: NoProfiling : YesPlatform ID: 000002D3A374C760Name: Intel(R) HD Graphics 4600Vendor: Intel(R) CorporationDevice OpenCL C version: OpenCL C 1.2Driver version: 20.19.15.4531Profile: FULL_PROFILEVersion: OpenCL 1.2Extensions:cl_intel_acceleratorcl_intel_advanced_motion_estimationcl_intel_ctzcl_intel_d3d11_nv12_media_sharingcl_intel_dx9_media_sharingcl_intel_motion_estimationcl_intel_simultaneous_sharingcl_intel_subgroupscl_khr_3d_image_writescl_khr_byte_addressable_storecl_khr_d3d10_sharingcl_khr_d3d11_sharingcl_khr_depth_imagescl_khr_dx9_media_sharingcl_khr_gl_depth_imagescl_khr_gl_eventcl_khr_gl_msaa_sharingcl_khr_global_int32_base_atomicscl_khr_global_int32_extended_atomicscl_khr_gl_sharingcl_khr_icdcl_khr_image2d_from_buffercl_khr_local_int32_base_atomicscl_khr_local_int32_extended_atomicscl_khr_spirDevice Type: CL_DEVICE_TYPE_CPUVendor ID: 8086hMax compute units: 8Max work items dimensions: 3Max work items[0]: 8192Max work items[1]: 8192Max work items[2]: 8192Max work group size: 8192Preferred vector width char: 1Preferred vector width short: 1Preferred vector width int: 1Preferred vector width long: 1Preferred vector width float: 1Preferred vector width double: 1Native vector width char: 32Native vector width short: 16Native vector width int: 8Native vector width long: 4Native vector width float: 8Native vector width double: 4Max clock frequency: 2400MhzAddress bits: 64Max memory allocation: 2126515200Image support: YesMax number of images read arguments: 480Max number of images write arguments: 480Max image 2D width: 16384Max image 2D height: 16384Max image 3D width: 2048Max image 3D height: 2048Max image 3D depth: 2048Max samplers within kernel: 480Max size of kernel argument: 3840Alignment (bits) of base address: 1024Minimum alignment (bytes) for any datatype: 128Single precision floating point capabilityDenorms: YesQuiet NaNs: YesRound to nearest even: YesRound to zero: NoRound to +ve and infinity: NoIEEE754-2008 fused multiply-add: NoCache type: Read/WriteCache line size: 64Cache size: 262144Global memory size: 8506060800Constant buffer size: 131072Max number of constant args: 480Local memory type: GlobalLocal memory size: 32768Kernel Preferred work group size multiple: 128Error correction support: 0Unified memory for Host and Device: 1Profiling timer resolution: 427Device endianess: LittleAvailable: YesCompiler available: YesExecution capabilities:Execute OpenCL kernels: YesExecute native function: YesQueue on Host properties:Out-of-Order: YesProfiling : YesPlatform ID: 000002D3A374C760Name: Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHzVendor: Intel(R) CorporationDevice OpenCL C version: OpenCL C 1.2Driver version: 5.2.0.10094Profile: FULL_PROFILEVersion: OpenCL 1.2 (Build 10094)Extensions:cl_khr_icdcl_khr_global_int32_base_atomicscl_khr_global_int32_extended_atomicscl_khr_local_int32_base_atomicscl_khr_local_int32_extended_atomicscl_khr_byte_addressable_storecl_khr_depth_imagescl_khr_3d_image_writescl_intel_exec_by_local_threadcl_khr_spircl_khr_dx9_media_sharingcl_intel_dx9_media_sharingcl_khr_d3d11_sharingcl_khr_gl_sharingcl_khr_fp64Platform Name: AMD Accelerated Parallel ProcessingNumber of devices: 1Device Type: CL_DEVICE_TYPE_CPUVendor ID: 1002hBoard name:Max compute units: 8Max work items dimensions: 3Max work items[0]: 1024Max work items[1]: 1024Max work items[2]: 1024Max work group size: 1024Preferred vector width char: 16Preferred vector width short: 8Preferred vector width int: 4Preferred vector width long: 2Preferred vector width float: 8Preferred vector width double: 4Native vector width char: 16Native vector width short: 8Native vector width int: 4Native vector width long: 2Native vector width float: 8Native vector width double: 4Max clock frequency: 2394MhzAddress bits: 64Max memory allocation: 2147483648Image support: YesMax number of images read arguments: 128Max number of images write arguments: 64Max image 2D width: 8192Max image 2D height: 8192Max image 3D width: 2048Max image 3D height: 2048Max image 3D depth: 2048Max samplers within kernel: 16Max size of kernel argument: 4096Alignment (bits) of base address: 1024Minimum alignment (bytes) for any datatype: 128Single precision floating point capabilityDenorms: YesQuiet NaNs: YesRound to nearest even: YesRound to zero: YesRound to +ve and infinity: YesIEEE754-2008 fused multiply-add: YesCache type: Read/WriteCache line size: 64Cache size: 32768Global memory size: 8506060800Constant buffer size: 65536Max number of constant args: 8Local memory type: GlobalLocal memory size: 32768Max pipe arguments: 16Max pipe active reservations: 16Max pipe packet size: 2147483648Max global variable size: 1879048192Max global variable preferred total size: 1879048192Max read/write image args: 64Max on device events: 0Queue on device max size: 0Max on device queues: 0Queue on device preferred size: 0SVM capabilities:Coarse grain buffer: NoFine grain buffer: NoFine grain system: NoAtomics: NoPreferred platform atomic alignment: 0Preferred global atomic alignment: 0Preferred local atomic alignment: 0Kernel Preferred work group size multiple: 1Error correction support: 0Unified memory for Host and Device: 1Profiling timer resolution: 427Device endianess: LittleAvailable: YesCompiler available: YesExecution capabilities:Execute OpenCL kernels: YesExecute native function: YesQueue on Host properties:Out-of-Order: NoProfiling : YesQueue on Device properties:Out-of-Order: NoProfiling : NoPlatform ID: 00007FFB80F36D30Name: Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHzVendor: GenuineIntelDevice OpenCL C version: OpenCL C 1.2Driver version: 1800.8 (sse2,avx)Profile: FULL_PROFILEVersion: OpenCL 1.2 AMD-APP (1800.8)Extensions:cl_khr_fp64cl_amd_fp64cl_khr_global_int32_base_atomicscl_khr_global_int32_extended_atomicscl_khr_local_int32_base_atomicscl_khr_local_int32_extended_atomicscl_khr_int64_base_atomicscl_khr_int64_extended_atomicscl_khr_3d_image_writescl_khr_byte_addressable_storecl_khr_gl_sharingcl_ext_device_fissioncl_amd_device_attribute_querycl_amd_vec3cl_amd_printfcl_amd_media_opscl_amd_media_ops2cl_amd_popcntcl_khr_d3d10_sharingcl_khr_spircl_khr_gl_event
原书clinfo信息
Number of platforms: 1Platform Profile: FULL_PROFILEPlatform Version: OpenCL 2.0 AMD-APP (1642.5)Platform Name: AMD Accelerated Parallel ProcessingPlatform Vendor: Advanced Micro Devices, Inc.Platform Extensions:cl_khr_icdcl_khr_d3d10_sharingcl_khr_icdcl_amd_event_callbackcl_amd_offline_devicesPlatform Name: AMD Accelerated Parallel ProcessingNumber of devices: 2Vendor ID: 1002hDevice Type: CL_DEVICE_TYPE_GPUBoard name: AMD Radeon R9 200 SeriesDevice Topology: PCI[B#1, D#0, F#0]Max compute units: 40Max work group size: 256Native vector width int: 1Max clock frequency: 1000MhzMax memory allocation: 2505572352Image support: YesMax image 3D width: 2048Cache line size: 64Global memory size: 3901751296Platform ID: 0x7f54fb22cfd0Name: HawaiiVendor: Advanced Micro Devices, Inc.Device OpenCL C version: OpenCL C 2.0Driver version: 1642.5(VM)Profile: FULL_PROFILEVersion: OpenCL 2.0 AMD-APP (1642.5)Extensions:cl_khr_fp64_cl_amd_fp64cl_khr_global_int32_base_atomicscl_khr_global_int32_extended_atomicscl_khr_local_int32_base_atomicsDevice Type: CL_DEVICE_TYPE_CPUVendor ID: 1002hBoard name:Max compute units: 8Max work items dimensions: 3Max work items[0]: 1024Max work items[1]: 1024Name: AMD FX(tm)-8120 Eight-Core ProcessorVendor: AuthenticAMDDevice OpenCL C version: OpenCL C 1.2Driver version: 1642.5(sse2, avx, fma4)Profile: FULL_PROFILEVersion: OpenCL 1.2 (Build 10094)
图3.2 通过clinfo程序输出一些OpenCL平台和设备信息。我们能看到AMD平台上有两个设备(一个CPU和一个GPU)。这些信息都能通过平台API查询到。
