forked from mikerabat/mrmath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadedMatrix.pas
534 lines (437 loc) · 19.6 KB
/
ThreadedMatrix.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
// ###################################################################
// #### This file is part of the mathematics library project, and is
// #### offered under the licence agreement described on
// #### http://www.mrsoft.org/
// ####
// #### Copyright:(c) 2011, Michael R. . All rights reserved.
// ####
// #### 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 ThreadedMatrix;
// ##############################################################
// #### Threaded versions of matrix operations
// ##############################################################
interface
uses Matrix, MatrixConst;
// ##############################################################
// #### Some overridden function which allows some multi threaded
// operations on the data
// todo: implement more functions multi threaded!
type
TThreadedMatrix = class(TDoubleMatrix)
protected
class function ResultClass : TDoubleMatrixClass; override;
class function ClassIdentifier : String; override;
public
procedure MultInPlace(Value : TDoubleMatrix); override;
function Mult(Value : TDoubleMatrix) : TDoubleMatrix; override;
procedure MultInPlaceT1(Value : TDoubleMatrix); override;
function MultT1(Value : TDoubleMatrix) : TDoubleMatrix; override;
procedure MultInPlaceT2(Value : TDoubleMatrix); override;
function MultT2(Value : TDoubleMatrix) : TDoubleMatrix; override;
procedure AddAndScaleInPlace(const aOffset, aScale : double); override;
function AddAndScale(const aOffset, aScale : double) : TDoubleMatrix; override;
procedure ElementwiseFuncInPlace(func : TMatrixFunc); overload; override;
function ElementwiseFunc(func : TMatrixFunc) : TDoubleMatrix; overload; override;
procedure ElementwiseFuncInPlace(func : TMatrixObjFunc); overload; override;
function ElementwiseFunc(func : TMatrixObjFunc) : TDoubleMatrix; overload; override;
procedure ElementwiseFuncInPlace(func : TMatrixMtxRefFunc); overload; override;
function ElementwiseFunc(func : TMatrixMtxRefFunc) : TDoubleMatrix; overload; override;
procedure ElementwiseFuncInPlace(func : TMatrixMtxRefObjFunc); overload; override;
function ElementwiseFunc(func : TMatrixMtxRefObjFunc) : TDoubleMatrix; overload; override;
// note: due to the multi threaded offset the add and sub functions seem to be
// faster if implemented single threaded
procedure AddInplace(Value : TDoubleMatrix); override;
function Add(Value : TDoubleMatrix) : TDoubleMatrix; override;
procedure SubInPlace(Value : TDoubleMatrix); override;
function Sub(Value : TDoubleMatrix) : TDoubleMatrix; override;
function Median(RowWise : boolean) : TDoubleMatrix; override;
procedure SortInPlace(RowWise : boolean); override;
// threaded lin alg functions
function InvertInPlace : TLinEquResult; override;
function Invert : TDoubleMatrix; override;
function SolveLinEQ(Value : TDoubleMatrix; numRefinements : integer = 0) : TDoubleMatrix; override;
procedure SolveLinEQInPlace(Value : TDoubleMatrix; numRefinements : integer = 0); override;
function Determinant: double; override;
function QR(out ecosizeR : TDoubleMatrix; out tau : TDoubleMatrix) : TQRResult; override;
function QRFull(out Q, R : TDoubleMatrix) : TQRResult; override;
function Cholesky(out Chol : TDoubleMatrix) : TCholeskyResult; overload; override;
class procedure InitThreadPool;
class procedure FinalizeThreadPool;
end;
implementation
uses SysUtils, ThreadedMatrixOperations, MtxThreadPool, ThreadedLinAlg, BlockSizeSetup,
Math;
{ TThreadedMatrix }
class function TThreadedMatrix.ClassIdentifier: String;
begin
Result := 'TMTX';
end;
class function TThreadedMatrix.ResultClass: TDoubleMatrixClass;
begin
Result := TThreadedMatrix;
end;
function TThreadedMatrix.Add(Value: TDoubleMatrix): TDoubleMatrix;
begin
assert((Width > 0) and (Height > 0), 'No data assigned');
assert((Value.Width = fSubWidth) and (Value.Height = fSubHeight), 'Dimension error');
Result := ResultClass.Create;
Result.SetWidthHeight(fSubWidth, fSubHeight);
ThrMatrixAdd(Result.StartElement, Result.LineWidth, StartElement, Value.StartElement, fSubWidth, fSubHeight, LineWidth, Value.LineWidth);
end;
function TThreadedMatrix.AddAndScale(const aOffset,
aScale: double): TDoubleMatrix;
begin
assert((width > 0) and (Height > 0), 'No data assigned');
Result := ResultClass.Create;
Result.Assign(self, True);
Result.AddAndScaleInPlace(aOffset, aScale);
end;
procedure TThreadedMatrix.AddAndScaleInPlace(const aOffset, aScale: double);
begin
assert((Width > 0) and (Height > 0), 'No data assigned');
ThrMatrixAddAndScale(StartElement, LineWidth, fSubWidth, fSubHeight, aOffset, aScale);
end;
procedure TThreadedMatrix.AddInplace(Value: TDoubleMatrix);
begin
assert((Width > 0) and (Height > 0), 'No data assigned');
// inplace matrix addition
assert((fSubWidth = Value.Width) and (fSubHeight = Value.Height), 'Matrix dimensions do not match');
ThrMatrixAdd(StartElement, LineWidth, StartElement, Value.StartElement, fSubWidth, fSubHeight, LineWidth, Value.LineWidth);
end;
function TThreadedMatrix.ElementwiseFunc(func: TMatrixFunc): TDoubleMatrix;
begin
assert((fSubWidth > 0) and (fSubHeight > 0), 'No data assigned');
Result := ResultClass.Create;
Result.Assign(self);
ThrMatrixFunc(Result.StartElement, Result.LineWidth, Result.Width, Result.Height, func);
end;
procedure TThreadedMatrix.ElementwiseFuncInPlace(func: TMatrixFunc);
begin
assert((fSubWidth > 0) and (fSubHeight > 0), 'No data assigned');
ThrMatrixFunc(StartElement, LineWidth, fSubWidth, fSubHeight, func);
end;
function TThreadedMatrix.ElementwiseFunc(func: TMatrixObjFunc): TDoubleMatrix;
begin
assert((fSubWidth > 0) and (fSubHeight > 0), 'No data assigned');
Result := ResultClass.Create;
Result.Assign(self);
ThrMatrixFunc(Result.StartElement, Result.LineWidth, Result.Width, Result.Height, func);
end;
procedure TThreadedMatrix.ElementwiseFuncInPlace(func: TMatrixObjFunc);
begin
assert((fSubWidth > 0) and (fSubHeight > 0), 'No data assigned');
ThrMatrixFunc(StartElement, LineWidth, fSubWidth, fSubHeight, func);
end;
function TThreadedMatrix.ElementwiseFunc(func: TMatrixMtxRefFunc): TDoubleMatrix;
begin
assert((fSubWidth > 0) and (fSubHeight > 0), 'No data assigned');
Result := ResultClass.Create;
Result.Assign(self);
ThrMatrixFunc(Result.StartElement, Result.LineWidth, Result.Width, Result.Height, func);
end;
function TThreadedMatrix.ElementwiseFunc(func: TMatrixMtxRefObjFunc): TDoubleMatrix;
begin
assert((fSubWidth > 0) and (fSubHeight > 0), 'No data assigned');
Result := ResultClass.Create;
Result.Assign(self);
ThrMatrixFunc(Result.StartElement, Result.LineWidth, Result.Width, Result.Height, func);
end;
procedure TThreadedMatrix.ElementwiseFuncInPlace(func: TMatrixMtxRefFunc);
begin
assert((fSubWidth > 0) and (fSubHeight > 0), 'No data assigned');
ThrMatrixFunc(StartElement, LineWidth, fSubWidth, fSubHeight, func);
end;
procedure TThreadedMatrix.ElementwiseFuncInPlace(func: TMatrixMtxRefObjFunc);
begin
assert((fSubWidth > 0) and (fSubHeight > 0), 'No data assigned');
ThrMatrixFunc(StartElement, LineWidth, fSubWidth, fSubHeight, func);
end;
class procedure TThreadedMatrix.FinalizeThreadPool;
begin
MtxThreadPool.FinalizeMtxThreadPool;
end;
class procedure TThreadedMatrix.InitThreadPool;
begin
MtxThreadPool.InitMtxThreadPool;
end;
function TThreadedMatrix.Invert: TDoubleMatrix;
begin
assert((Width > 0) and (Height > 0), 'No data assigned');
assert(fSubWidth = fSubHeight, 'Operation only allowed on square matrices');
Result := ResultClass.Create;
try
Result.Assign(Self, True);
if ThrMatrixInverse(Result.StartElement, Result.LineWidth, fSubWidth, fLinEQProgress) = leSingular then
raise ELinEQSingularException.Create('Singular matrix');
except
Result.Free;
raise;
end;
end;
function TThreadedMatrix.InvertInPlace: TLinEquResult;
var dt : TDoubleMatrix;
begin
assert((Width > 0) and (Height > 0), 'No data assigned');
assert(fSubWidth = fSubHeight, 'Operation only allowed on square matrices');
dt := ResultClass.Create(Width, Height);
try
dt.Assign(self, True);
Result := ThrMatrixInverse(dt.StartElement, dt.LineWidth, fSubWidth, fLinEQProgress);
if Result = leOk then
Assign(dt);
FreeAndNil(dt);
except
dt.Free;
raise;
end;
end;
function TThreadedMatrix.Median(RowWise: boolean): TDoubleMatrix;
begin
CheckAndRaiseError((Width > 0) and (Height > 0), 'No data assigned');
if RowWise then
begin
Result := ResultClass.Create(1, fSubHeight);
ThrMatrixMedian(Result.StartElement, Result.LineWidth, StartElement, LineWidth, fSubWidth, fSubHeight, RowWise);
end
else
begin
Result := ResultClass.Create(fSubWidth, 1);
ThrMatrixMedian(Result.StartElement, Result.LineWidth, StartElement, LineWidth, fSubWidth, fSubHeight, RowWise);
end;
end;
function TThreadedMatrix.Mult(Value: TDoubleMatrix): TDoubleMatrix;
begin
assert((Width > 0) and (Height > 0), 'No data assigned');
Result := ResultClass.Create(Value.Width, fSubHeight);
if (Value.Width = 1) and (Value.LineWidth = sizeof(double))
then
ThrMatrixVecMult(Result.StartElement, Result.LineWidth, StartElement, Value.StartElement, Width, Height, Value.Height, LineWidth)
else
ThrMatrixMult(Result.StartElement, Result.LineWidth, StartElement, Value.StartElement, Width, Height, Value.Width, Value.Height, LineWidth, Value.LineWidth);
end;
procedure TThreadedMatrix.MultInPlace(Value: TDoubleMatrix);
var res : TDoubleMatrix;
begin
assert((Width > 0) and (Height > 0), 'No data assigned');
assert((fOffsetX = 0) and (fOffsetY = 0) and (Width = fSubWidth) and (Height = fSubHeight), 'Operation only allowed on full matrices');
assert((fSubWidth = Value.Height), 'Dimension error');
res := ResultClass.Create(Value.Width, fSubHeight);
try
if (Value.Width = 1) and (Value.LineWidth = sizeof(double))
then
ThrMatrixVecMult(res.StartElement, res.LineWidth, StartElement, Value.StartElement, Width, Height, Value.Height, LineWidth)
else
ThrMatrixMult(res.StartElement, res.LineWidth, StartElement, Value.StartElement, Width, Height, Value.Width, Value.Height, LineWidth, Value.LineWidth);
Assign(res);
res.Free;
except
res.Free;
raise;
end;
end;
procedure TThreadedMatrix.MultInPlaceT1(Value: TDoubleMatrix);
var res : TDoubleMatrix;
begin
assert((Width > 0) and (Height > 0), 'No data assigned');
assert((fOffsetX = 0) and (fOffsetY = 0) and (Width = fSubWidth) and (Height = fSubHeight), 'Operation only allowed on full matrices');
assert((fSubHeight = Value.Height), 'Dimension error');
res := ResultClass.Create(Value.Width, fSubWidth);
try
ThrMatrixMultT1(res.StartElement, res.LineWidth, StartElement, Value.StartElement, Width, Height, Value.Width, Value.Height, LineWidth, Value.LineWidth);
Assign(res);
res.Free;
except
res.Free;
raise;
end;
end;
procedure TThreadedMatrix.MultInPlaceT2(Value: TDoubleMatrix);
var res : TDoubleMatrix;
begin
assert((Width > 0) and (Height > 0), 'No data assigned');
assert((fOffsetX = 0) and (fOffsetY = 0) and (Width = fSubWidth) and (Height = fSubHeight), 'Operation only allowed on full matrices');
assert((fSubWidth = Value.Width), 'Dimension error');
res := ResultClass.Create(Value.Height, fSubHeight);
try
ThrMatrixMultT2(res.StartElement, res.LineWidth, StartElement, Value.StartElement, Width, Height, Value.Width, Value.Height, LineWidth, Value.LineWidth);
Assign(res);
res.Free;
except
res.Free;
raise;
end;
end;
function TThreadedMatrix.MultT1(Value: TDoubleMatrix): TDoubleMatrix;
begin
assert((Width > 0) and (Height > 0), 'No data assigned');
Result := ResultClass.Create(Value.Width, fSubWidth);
ThrMatrixMultT1(Result.StartElement, Result.LineWidth, StartElement, Value.StartElement, Width, Height, Value.Width, Value.Height, LineWidth, Value.LineWidth);
end;
function TThreadedMatrix.MultT2(Value: TDoubleMatrix): TDoubleMatrix;
begin
assert((Width > 0) and (Height > 0), 'No data assigned');
Result := ResultClass.Create(Value.Height, fSubHeight);
ThrMatrixMultT2(Result.StartElement, Result.LineWidth, StartElement, Value.StartElement, Width, Height, Value.Width, Value.Height, LineWidth, Value.LineWidth);
end;
function TThreadedMatrix.QR(out ecosizeR, tau: TDoubleMatrix): TQRResult;
begin
ecosizeR := ResultClass.Create;
tau := ResultClass.Create(width, 1);
try
ecosizeR.Assign(self);
Result := ThrMatrixQRDecomp(ecosizeR.StartElement, ecosizeR.LineWidth, width, height, tau.StartElement, nil, QRBlockSize, fLinEQProgress);
except
FreeAndNil(ecosizeR);
FreeAndNil(tau);
raise;
end;
end;
function TThreadedMatrix.QRFull(out Q, R: TDoubleMatrix): TQRResult;
var tau : TDoubleMatrix;
tmp : TDoubleMatrix;
x, y : integer;
pdata : PConstDoubleArr;
begin
Q := nil;
R := nil;
// ###########################################
// #### First create a compact QR decomposition
Result := QR(R, Tau);
if Result <> qrOK then
begin
FreeAndNil(R);
exit;
end;
// ###########################################
// #### Calculation Q from the previous operation
tmp := ResultClass.Create;
try
tmp.Assign(R);
ThrMatrixQFromQRDecomp(tmp.StartElement, tmp.LineWidth, Width, Height, tau.StartElement, QRBlockSize, nil, fLinEQProgress);
// now assign only the relevant parts of Q (or just copy it if we have a square matrix)
if Width = height
then
Q := tmp
else
begin
// economy size Q:
tmp.SetSubMatrix(0, 0, Math.min(tmp.Width, tmp.Height), tmp.Height);
Q := ResultClass.Create;
Q.Assign(tmp, True);
tmp.Free;
tmp := R;
tmp.SetSubMatrix(0, 0, tmp.Width, Math.Min(tmp.Width, tmp.Height));
R := ResultClass.Create;
R.Assign(tmp, True);
tmp.Free;
end;
// clear R so we only have un upper triangle matrix
// zero out the parts occupied by Q
for y := 1 to R.Height - 1 do
begin
pData := PConstDoubleArr( R.StartElement );
inc(PByte(pData), y*R.LineWidth);
for x := 0 to Math.Min(r.Width, y) - 1 do
pData^[x] := 0;
end;
finally
Tau.Free;
end;
end;
function TThreadedMatrix.Cholesky(out Chol: TDoubleMatrix): TCholeskyResult;
var x, y : integer;
begin
CheckAndRaiseError((fSubWidth > 0) and (fSubWidth = fSubHeight), 'Cholesky decomposition is only allowed on square matrices');
chol := ResultClass.Create;
try
chol.Assign(Self);
// block wise cholesky decomposition
Result := ThrMatrixCholeskyDecomp(chol.StartElement, chol.LineWidth, chol.Width, 0, nil, fLinEQProgress);
if Result = crOk then
begin
// zero out the upper elements
for y := 0 to fSubWidth - 1 do
begin
for x := y + 1 to fSubWidth - 1 do
Chol[x, y] := 0;
end;
end
else
FreeAndNil(chol);
except
FreeAndNil(chol);
Result := crNoPositiveDefinite;
end;
end;
function TThreadedMatrix.SolveLinEQ(Value: TDoubleMatrix;
numRefinements: integer): TDoubleMatrix;
begin
// solves the System: A * x = b
// whereas A is the matrix stored in self, and be is the matrix in Value
// The result is a matrix having the size of Value.
assert((Width > 0) and (Height > 0), 'No data assigned');
assert((fSubWidth = fSubHeight) and (Value.Height = fSubHeight), 'Dimension error');
Result := ResultClass.Create(Value.Width, fSubHeight);
try
if ThrMatrixLinEQSolve(StartElement, LineWidth, fSubWidth, Value.StartElement,
Value.LineWidth, Result.StartElement,
Result.LineWidth, Value.Width, numRefinements, fLinEQProgress) = leSingular
then
raise ELinEQSingularException.Create('Matrix is singular');
except
FreeAndNil(Result);
raise;
end;
end;
procedure TThreadedMatrix.SolveLinEQInPlace(Value: TDoubleMatrix;
numRefinements: integer);
var dt : TDoubleMatrix;
begin
// solves the System: A * x = b
// whereas A is the matrix stored in self, and be is the matrix in Value
// The result is a matrix having the size of Value.
assert((Width > 0) and (Height > 0), 'No data assigned');
assert((fSubWidth = fSubHeight) and (Value.Height = fSubHeight), 'Dimension error');
dt := ResultClass.Create(Value.Width, fSubHeight);
try
if ThrMatrixLinEQSolve(StartElement, LineWidth, fSubWidth, Value.StartElement, Value.LineWidth,
dt.StartElement, dt.LineWidth, Value.Width,
numRefinements, fLinEQProgress) = leSingular
then
raise ELinEQSingularException.Create('Matrix is singular');
Assign(dt, False);
finally
dt.Free;
end;
end;
procedure TThreadedMatrix.SortInPlace(RowWise: boolean);
begin
CheckAndRaiseError((Width > 0) and (Height > 0), 'No data assigned');
ThrMatrixSort(StartElement, LineWidth, width, Height, RowWise);
end;
function TThreadedMatrix.Determinant: double;
begin
assert((Width > 0) and (Height = Width), 'Determinant only allowed on square matrices');
Result := ThrMatrixDeterminant(StartElement, LineWidth, fSubWidth);
end;
function TThreadedMatrix.Sub(Value: TDoubleMatrix): TDoubleMatrix;
begin
assert((Width > 0) and (Height > 0), 'No data assigned');
assert((Value.Width = fSubWidth) and (Value.Height = fSubHeight), 'Dimension error');
Result := ResultClass.Create(fSubWidth, fSubHeight);
ThrMatrixSub(Result.StartElement, Result.LineWidth, StartElement, Value.StartElement, fSubWidth, fSubHeight,
Width*sizeof(double), Value.LineWidth);
end;
procedure TThreadedMatrix.SubInPlace(Value: TDoubleMatrix);
begin
assert((Width > 0) and (Height > 0), 'No data assigned');
// inplace matrix substraction
assert((fSubWidth = Value.Width) and (fSubHeight = Value.Height), 'Matrix dimensions do not match');
ThrMatrixSub(StartElement, LineWidth, StartElement, Value.StartElement, fSubWidth, fSubHeight, LineWidth, Value.LineWidth);
end;
end.