[日本語]
使用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