-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathCompute.pas
303 lines (240 loc) · 8.64 KB
/
Compute.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright 2014 Asbjørn Heid
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
unit Compute;
interface
uses
Compute.Common,
Compute.ExprTrees,
Compute.OpenCL,
Compute.Future.Detail;
type
Expr = Compute.ExprTrees.Expr;
TLogProc = Compute.OpenCL.TLogProc;
ComputeDeviceSelection = (PreferCPUDevice, PreferGPUDevice);
Buffer<T> = record
strict private
FCmdQueue: Compute.OpenCL.CLCommandQueue;
FBuffer: Compute.OpenCL.CLBuffer;
function GetNumElements: UInt64;
function GetSize: UInt64;
private
property CmdQueue: Compute.OpenCL.CLCommandQueue read FCmdQueue;
property Buffer: Compute.OpenCL.CLBuffer read FBuffer;
public
class function Create(const NumElements: UInt64): Buffer<T>; overload; static;
class function Create(const InitialData: TArray<T>): Buffer<T>; overload; static;
procedure CopyTo(const DestBuffer: Buffer<T>);
procedure SwapWith(var OtherBuffer: Buffer<T>);
function ToArray: TArray<T>;
property NumElements: UInt64 read GetNumElements;
property Size: UInt64 read GetSize;
// underlying buffer
property Handle: Compute.OpenCL.CLBuffer read FBuffer;
end;
Future<T> = record
strict private
FImpl: IFuture<T>;
function GetDone: boolean;
function GetValue: T;
private
class function CreateReady(const Value: T): Future<T>; static;
property Impl: IFuture<T> read FImpl;
public
class operator Implicit(const Impl: IFuture<T>): Future<T>;
// makes a ready-future
class operator Implicit(const Value: T): Future<T>;
procedure SwapWith(var OtherFuture: Future<T>);
procedure Wait;
property Done: boolean read GetDone;
property Value: T read GetValue;
end;
function Constant(const Value: double): Expr.Constant;
function Variable(const Name: string): Expr.Variable;
function ArrayVariable(const Name: string; const Count: integer): Expr.ArrayVariable;
function Func1(const Name: string; const FuncBody: Expr): Expr.Func1;
function Func2(const Name: string; const FuncBody: Expr): Expr.Func2;
function Func3(const Name: string; const FuncBody: Expr): Expr.Func3;
function _1: Expr.LambdaParam;
function _2: Expr.LambdaParam;
function _3: Expr.LambdaParam;
procedure InitializeCompute(const DeviceSelection: ComputeDeviceSelection = PreferGPUDevice; const LogProc: TLogProc = nil; const DebugLogProc: TLogProc = nil);
function AsyncTransform(const InputBuffer: Buffer<double>; const Expression: Expr): Future<Buffer<double>>; overload;
// queues the async transform to execute as soon as the input buffer is ready, output buffer is returned in the future
function AsyncTransform(const InputBuffer, OutputBuffer: Future<Buffer<double>>; const Expression: Expr): Future<Buffer<double>>; overload;
// two inputs
function AsyncTransform(const InputBuffer1, InputBuffer2, OutputBuffer: Future<Buffer<double>>; const Expression: Expr): Future<Buffer<double>>; overload;
// three inputs
function AsyncTransform(const InputBuffer1, InputBuffer2, InputBuffer3, OutputBuffer: Future<Buffer<double>>; const Expression: Expr): Future<Buffer<double>>; overload;
function AsyncTransform(const Input: TArray<double>; const Expression: Expr): Future<TArray<double>>; overload;
function Transform(const Input: TArray<double>; const Expression: Expr): TArray<double>;
implementation
uses
Winapi.Windows, System.SysUtils, System.Math,
Compute.Detail;
function Constant(const Value: double): Expr.Constant;
begin
result := Compute.ExprTrees.Constant(Value);
end;
function Variable(const Name: string): Expr.Variable;
begin
result := Compute.ExprTrees.Variable(Name);
end;
function ArrayVariable(const Name: string; const Count: integer): Expr.ArrayVariable;
begin
result := Compute.ExprTrees.ArrayVariable(Name, Count);
end;
function Func1(const Name: string; const FuncBody: Expr): Expr.Func1;
begin
result := Compute.ExprTrees.Func1(Name, FuncBody);
end;
function Func2(const Name: string; const FuncBody: Expr): Expr.Func2;
begin
result := Compute.ExprTrees.Func2(Name, FuncBody);
end;
function Func3(const Name: string; const FuncBody: Expr): Expr.Func3;
begin
result := Compute.ExprTrees.Func3(Name, FuncBody);
end;
function _1: Expr.LambdaParam;
begin
result := Compute.ExprTrees._1;
end;
function _2: Expr.LambdaParam;
begin
result := Compute.ExprTrees._2;
end;
function _3: Expr.LambdaParam;
begin
result := Compute.ExprTrees._3;
end;
{ Future<T> }
class function Future<T>.CreateReady(const Value: T): Future<T>;
begin
result.FImpl := TReadyFutureImpl<T>.Create(Value);
end;
function Future<T>.GetDone: boolean;
begin
result := Impl.Done;
end;
function Future<T>.GetValue: T;
begin
result := Impl.Value;
end;
class operator Future<T>.Implicit(const Value: T): Future<T>;
begin
result := CreateReady(Value);
end;
procedure Future<T>.SwapWith(var OtherFuture: Future<T>);
var
f: IFuture<T>;
begin
f := OtherFuture.FImpl;
OtherFuture.FImpl := FImpl;
FImpl := f;
end;
class operator Future<T>.Implicit(const Impl: IFuture<T>): Future<T>;
begin
result.FImpl := Impl;
end;
procedure Future<T>.Wait;
begin
Impl.Wait;
end;
procedure InitializeCompute(const DeviceSelection: ComputeDeviceSelection; const LogProc, DebugLogProc: TLogProc);
begin
Algorithms.Initialize(DeviceSelection, LogProc, DebugLogProc);
end;
{ Buffer<T> }
class function Buffer<T>.Create(const NumElements: UInt64): Buffer<T>;
begin
result.FCmdQueue := Algorithms.CmdQueue;
result.FBuffer := Algorithms.Context.CreateHostBuffer(BufferAccessReadWrite, SizeOf(T) * NumElements);
end;
class function Buffer<T>.Create(const InitialData: TArray<T>): Buffer<T>;
var
size: UInt64;
begin
size := SizeOf(T) * Length(InitialData);
result.FCmdQueue := Algorithms.CmdQueue;
result.FBuffer := Algorithms.Context.CreateHostBuffer(BufferAccessReadWrite, size, InitialData);
end;
function Buffer<T>.GetNumElements: UInt64;
begin
result := FBuffer.Size div SizeOf(T);
end;
function Buffer<T>.GetSize: UInt64;
begin
result := FBuffer.Size;
end;
procedure Buffer<T>.SwapWith(var OtherBuffer: Buffer<T>);
var
t: CLBuffer;
begin
t := OtherBuffer.FBuffer;
OtherBuffer.FBuffer := FBuffer;
FBuffer := t;
end;
function Buffer<T>.ToArray: TArray<T>;
var
len: UInt64;
begin
len := NumElements;
SetLength(result, len);
FCmdQueue.EnqueueReadBuffer(FBuffer, BufferCommandBlocking, 0, len * SizeOf(T), result, []);
end;
procedure Buffer<T>.CopyTo(const DestBuffer: Buffer<T>);
var
event: CLEvent;
begin
event := DestBuffer.CmdQueue.EnqueueCopyBuffer(
Buffer,
DestBuffer.Buffer,
0, 0,
Min(Buffer.Size, DestBuffer.Buffer.Size), []);
event.Wait;
end;
function AsyncTransform(const InputBuffer: Buffer<double>; const Expression: Expr): Future<Buffer<double>>;
var
outputBuffer: Buffer<double>;
begin
outputBuffer := Buffer<double>.Create(InputBuffer.NumElements);
result := AsyncTransform(InputBuffer, outputBuffer, Expression);
end;
function AsyncTransform(const Input: TArray<double>; const Expression: Expr): Future<TArray<double>>;
var
output: TArray<double>;
begin
SetLength(output, Length(Input));
result := Algorithms.Transform(Input, output, Expression);
end;
function Transform(const Input: TArray<double>; const Expression: Expr): TArray<double>;
var
f: Future<TArray<double>>;
begin
f := AsyncTransform(Input, Expression);
result := f.Value;
end;
function AsyncTransform(const InputBuffer, OutputBuffer: Future<Buffer<double>>; const Expression: Expr): Future<Buffer<double>>;
begin
result := Algorithms.Transform(InputBuffer.Impl, 0, InputBuffer.Impl.PeekValue.NumElements, OutputBuffer.Impl, Expression);
end;
function AsyncTransform(const InputBuffer1, InputBuffer2, OutputBuffer: Future<Buffer<double>>; const Expression: Expr): Future<Buffer<double>>;
begin
result := Algorithms.Transform([InputBuffer1.Impl, InputBuffer2.Impl], 0, InputBuffer1.Impl.PeekValue.NumElements, OutputBuffer.Impl, Expression);
end;
function AsyncTransform(const InputBuffer1, InputBuffer2, InputBuffer3, OutputBuffer: Future<Buffer<double>>; const Expression: Expr): Future<Buffer<double>>;
begin
result := Algorithms.Transform([InputBuffer1.Impl, InputBuffer2.Impl, InputBuffer3.Impl], 0, InputBuffer1.Impl.PeekValue.NumElements, OutputBuffer.Impl, Expression);
end;
end.