[日本語]
使用opencl在GPU和CPU上并行计算。

LUX.GPU.OpenCL库
TOpenCL:TCLSYSTEM的Singleton
┃
TCLSystem:系统TCLPlatfos:平台列表TCLPlatfo:平台TCLExtenss:扩展名单TCLDevices:设备列表TCLDevice:设备TCLContexs:上下文列表TCLContex:上下文TCLQueuers:命令队列列表TCLQueuer:命令队列TCLArgumes:参数列表TCLBuffer:缓冲区TCLImager:图片TCLSamplr:采样器TCLLibrars:库列表
┃┗tcllibrar:TCLLibrar
TCLExecuts:可执行程序列表TCLExecut:可执行程序TCLBuildrs:构建列表TCLBuildr:构建TCLKernels:内核列表TCLKernel:内核TCLParames:参数列表TCLParame:参数
TOpenCL类是TCLSystem类的单身人士。 TCLSystem类自动检测主机上存在的所有计算设备。
“平台”对象( TCLPlatfo )表示每个设备供应商提供的环境。 TCLSystem类自动检测所有平台S,并将它们列在Platfos[]属性中。
Object PascalTOpenCL.Platfos.Count :Integer // Number of all platforms TOpenCL.Platfos[*] :TCLPlatfo // Array of all platforms
TCLPlatfo类提供有关特定平台作为属性的信息。
Object Pascal_Platfo := TOpenCL.Platfos[ 0 ]; // Selecting a specific platform _Platfo.Handle :T_cl_platform_id // Pointer _Platfo.Profile :String // Profile _Platfo.Version :String // Version _Platfo. Name :String // Name _Platfo.Vendor :String // Vendor Name _Platfo.Extenss.Count :Integer // Number of Extensions _Platfo.Extenss[*] :String // Array of Extensions
“设备”对象( TCLDevice )代表物理GPU或CPU。 TCLPlatfo类自动检测特定平台对象中的所有设备对象,并在Devices[]属性中枚举它们。
Object Pascal_Platfo.Devices.Count :Integer // Number of devices _Platfo.Devices[*] :TCLDevice // Array of devices
TCLDevice类通过其属性提供有关每个特定设备的详细信息。
Object Pascal_Device := _Platfo.Devices[ 0 ]; // Selecting a specific device _Device.Handle :T_cl_device_id // Pointer _Device.DEVICE_TYPE :T_cl_device_type // Type _Device.DEVICE_VENDOR_ID :T_cl_uint // Vendor ID _Device.DEVICE_NAME :String // Name _Device.DEVICE_VENDOR :String // Vendor _Device.DRIVER_VERSION :String // Driver Version _Device.DEVICE_PROFILE :String // Profile _Device.DEVICE_VERSION :String // Version
“上下文”对象( TCLContex )管理相关数据和程序的集合。 TCLContex类是从TCLPlatfo类实例化的。
Object Pascal_Contex := TCLContex.Create( _Platfo );
生成的TCLContex类在TCLPlatfo类的Contexs[]属性中注册。
Object Pascal_Platfo.Contexs.Count :Integer // Number of contexts _Platfo.Contexs[*] :TCLContex // Array of contexts ``
“命令队列”对象( TCLQueuer )管理发送到设备的命令。换句话说,它是上下文和设备之间的桥梁。 TCLQueuer类是用TCLContex和TCLDevice类创建的,作为参数。
Object Pascal_Queuer := TCLQueuer.Create( _Contex, _Device ); { or } _Queuer := _Contex.Queuers[ _Device ];
TCLContex类注册Queuers[]属性中的TCLQueuer对象。
Object Pascal_Contex.Queuers.Count :Integer // Number of command queue _Contex.Queuers[*] :TCLQueuer // Array of command queue
请注意,不同平台上的上下文和设备无法生成命令队列。
Object PascalP0 := TOpenCL.Platfos[ 0 ]; P1 := TOpenCL.Platfos[ 1 ]; P2 := TOpenCL.Platfos[ 2 ]; D00 := P0.Devices[ 0 ]; D01 := P0.Devices[ 1 ]; D02 := P0.Devices[ 2 ]; D10 := P1.Devices[ 0 ]; D20 := P2.Devices[ 0 ]; C0 := TCLContex.Create( P0 ); C1 := TCLContex.Create( P1 ); C2 := TCLContex.Create( P2 ); Q00 := TCLQueuer.Create( C0, D00 ); // OK Q01 := TCLQueuer.Create( C0, D01 ); // OK Q02 := TCLQueuer.Create( C0, D02 ); // OK Q10 := TCLQueuer.Create( C1, D00 ); // Error Q11 := TCLQueuer.Create( C1, D01 ); // Error Q12 := TCLQueuer.Create( C1, D02 ); // Error Q20 := TCLQueuer.Create( C2, D00 ); // Error Q21 := TCLQueuer.Create( C2, D10 ); // Error Q22 := TCLQueuer.Create( C2, D20 ); // OK
TCLArgume
TCLMemory
TCLBuffer
TCLImager
TCLSamplr
“内存”对象( TCLMemory )存储各种数据并与设备共享。 TCLMemory类是由TCLContex和TCLQueuer类创建的。 TCLMemory类是抽象的,并得出了TCLBuffer和TCLImager类。
TCLBuffer类存储任何“简单类型”或“记录类型”的数组。
如果要将以下结构类型的数组发送到设备,
OpenCL Ctypedef struct { int A ; double B ; } TItem ; kernel void Main ( global TItem * Buffer ) { ・・・ }
如下,生成TCLBuffer类。
Object PascalTItem = record A :Integer; B :Double; end ; _Buffer := TCLBuffer<TItem>.Create( _Contex, _Queuer );
通过Data属性读取和编写数组数据。数组数据必须是“映射PED”才能在阅读或写入之前与主机同步,并且使用后“ Unmap PED”与设备同步。
Object Pascal_Buffer.Count := 3 ; // Setting the number of elements _Buffer.Data.Map; // Synchronize data with host _Buffer.Data[ 0 ] := TItem.Create( 1 , 2.34 ); // Writing _Buffer.Data[ 1 ] := TItem.Create( 5 , 6.78 ); // Writing _Buffer.Data[ 2 ] := TItem.Create( 9 , 0.12 ); // Writing _Buffer.Data.Unmap; // Synchronize data with Device
“图像”对象( TCLImager )将像素阵列存储在1d到3D中。 3D Voxel数据也被认为是3D图像的类型。 TCLImager类是抽象的,并根据颜色通道的布局和位来得出各种类。
TCLImager
TCLImager1D
TCLImager1DxBGRAxUInt8
TCLImager1DxBGRAxUFix8
TCLImager1DxRGBAxUInt32
TCLImager1DxRGBAxSFlo32
TCLImager2D
TCLImager2DxBGRAxUInt8
TCLImager2DxBGRAxUFix8
TCLImager2DxRGBAxUInt32
TCLImager2DxRGBAxSFlo32
TCLImager3D
TCLImager3DxBGRAxUInt8
TCLImager3DxBGRAxUFix8
TCLImager3DxRGBAxUInt32
TCLImager3DxRGBAxSFlo32
类名称的第一部分表示图像的维度。
- tclimager
1Dx*x*
- 维度:
1D- tclimager
2Dx*x*
- 维度:
2D- tclimager
3Dx*x*
- 维度:
3D
类名称的第二部分代表图像的颜色通道顺序。
- tclimager
*xBGRAx*
- 颜色频道顺序:
BGRA- tclimager
*xRGBAx*
- 颜色频道
RGBA:
类名称的第三部分代表图像的颜色数据类型。
- tclimager
*x*xUInt8
- 设备侧数据类型:
uint8@ opencl c- 主机端数据类型:
UInt8 (Byte)@ delphi- tclimager
*x*xUFix8
- 设备侧数据类型:
float@ opencl c- 主机端数据类型:
UInt8 (Byte)@ delphi- tclimager
*x*xUInt32
- 设备侧数据类型:
uint@ opencl c- 主机端数据类型:
UInt32 (Cardinal)@ delphi- tclimager
*x*xSFlo32
- 设备侧数据类型:
float@ opencl c- 主机端数据类型:
Single@ delphi
'countx'/'y'/'z'属性设置了x/y/z方向的像素数。
Object Pascal_Imager := TCLDevIma3DxBGRAxUInt8.Create( _Contex, _Queuer ); _Imager.CountX := 100 ; // Number of pixels in the X direction _Imager.CountY := 200 ; // Number of pixels in the Y direction _Imager.CountZ := 300 ; // Number of pixels in the Z direction
Sampler对象( TCLSamplr )定义了在实数坐标中获取像素颜色的插值方法。
TCLSamplr类是用“ TCLCONTEX”类作为参数生成的。
Object Pascal_Samplr := TCLSamplr.Create( _Contex );
“程序”对象( TCLProgra )读取源代码并将其编译为可执行的二进制文件。 TCLProgra类是用TCLContex类创建的参数。 TCLProgra类是抽象的,并作为TCLLibrar和TCLExecut类的基类,具体取决于源代码的类型。
TCLLibrar类是一个程序,不包括直接执行的函数称为库类型。
Object Pascal_Librar := TCLLibrar.Create( _Contex ); _Librar.Source.LoadFromFile( ' Librar.cl ' ); // load Sourcecode
TCLExecut类是一个包含函数(内核s)的程序,直接执行。
Object Pascal_Execut := TCLExecut.Create( _Contex ); _Execut.Source.LoadFromFile( ' Execut.cl ' ); // load Sourcecode
“构建”( TCLBuildr )是程序执行的“动作”,但在我们的库中明确表示为类。
Object Pascal_Buildr := TCLBuildr.Create( _Execut, _Device ); { or } _Buildr := _Execut.Buildrs[ _Device ]; { or } _Buildr := _Execut.BuildTo( _Device );
内核对象(请参阅第2.8章)自动在运行时创建TCLBuildr类。但是,您可以通过在运行内核之前创建TCLBuildr对象来检查编译和链接错误。
Object Pascal_Buildr.Handle; // Run build _Buildr.CompileStatus :T_cl_build_status // Compile status _Buildr.CompileLog :String // Compile log _Buildr.LinkStatus :T_cl_build_status // Link status _Buildr.LinkLog :String // Link log
“内核”对象( TCLKernel )代表程序中的可执行函数。
OpenCL Ckernel void Main ( ・・・ ) { ・・・ }
TCLKernel类是从TCLExecut和TCLQueuer对象实例化的。
Object Pascal_Kernel := TCLKernel.Create( _Execut, ' Main ' , _Queuer ); { or } _Kernel := _Execut.Kernels.Add( ' Main ' , _Queuer );
通过TCLKernel类的“ Parames”属性,将内存对象链接到源代码中的参数。
Object Pascal_Kernel.Parames[ ' Buffer ' ] := _Buffer; // Connect to buffer _Kernel.Parames[ ' Imager ' ] := _Imager; // Connect to image _Kernel.Parames[ ' Samplr ' ] := _Samplr; // Connect to sampler
OPENCL程序反复运行,就像三重循环说话。
Object Pascal_Kernel.GloSizX := 100 ; // Number of loops in X direction _Kernel.GloSizY := 200 ; // Number of loops in Y direction _Kernel.GloSizZ := 300 ; // Number of loops in Z direction
您还可以指定最小循环指数。
Object Pascal_Kernel.GloMinX := 0 ; // Start index in X direction _Kernel.GloMinY := 0 ; // Start index in Y direction _Kernel.GloMinZ := 0 ; // Start index in Z direction _Kernel.GloMaxX := 100 - 1 ; // End index in X direction _Kernel.GloMaxY := 200 - 1 ; // End index in Y direction _Kernel.GloMaxZ := 300 - 1 ; // End index in Z direction
Object Pascal_Kernel.Run; // Run