-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathBlockedMult.pas
1731 lines (1399 loc) · 66 KB
/
BlockedMult.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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ###################################################################
// #### This file is part of the mathematics library project, and is
// #### offered under the licence agreement described on
// #### http://www.mrsoft.org/
// ####
// #### Copyright:(c) 2017, 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 BlockedMult;
interface
{$IFDEF FPC} {$MODESWITCH ADVANCEDRECORDS} {$ENDIF}
uses MatrixConst;
// ###################################################################
// #### Generic blocked multiplication routines
// blocked matrix mult + strassen multiplication functions
procedure GenericBlockMatrixMultiplication(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble;
width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt;
const LineWidth1, LineWidth2 : NativeInt; blockSize : NativeInt; op : TMatrixMultDestOperation = doNone; mem : PDouble = nil);
// calculates dest = mt1'*mt2
procedure GenericBlockMatrixMultiplicationT1(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt;
const LineWidth1, LineWidth2 : NativeInt; blockSize : NativeInt; op : TMatrixMultDestOperation; mem : Pdouble);
// calculates dest = mt1*mt2'
procedure GenericBlockMatrixMultiplicationT2(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt;
const LineWidth1, LineWidth2 : NativeInt; blockSize : NativeInt; op : TMatrixMultDestOperation; mem : Pdouble);
// this routine first performs a transposition on the second matrix before the multiplication is executed. This results
// normaly in quite a boost.
procedure BlockMatrixMultiplication(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt; op : TMatrixMultDestOperation = doNone); overload;
procedure BlockMatrixMultiplication(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt; blockSize : NativeInt; op : TMatrixMultDestOperation = doNone; mem : PDouble = nil); overload;
// calculates dest = mt1'*mt2
procedure BlockMatrixMultiplicationT1(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt;
const LineWidth1, LineWidth2 : NativeInt; blockSize : NativeInt; op : TMatrixMultDestOperation; mem : Pdouble);
// calculates dest = mt1*mt2'
procedure BlockMatrixMultiplicationT2(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt;
const LineWidth1, LineWidth2 : NativeInt; blockSize : NativeInt; op : TMatrixMultDestOperation; mem : Pdouble);
// this is a blockwise matrix multiplication routine which takes a limited cache into account.
// The routine tries to tile the matrix into 256x256 blocks (which seems to be a good approximation
// for a Core2 processor) which fits into the Level1 cache thus reduces
// cache misses.
procedure BlockMatrixMultiplicationDirect(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt; op : TMatrixMultDestOperation = doNone); overload;
procedure BlockMatrixMultiplicationDirect(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt; blockSize : NativeInt; op : TMatrixMultDestOperation = doNone); overload;
procedure BlockMatrixVectorMultiplication(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; height2 : NativeInt; const LineWidth1 : NativeInt); overload;
procedure BlockMatrixVectorMultiplication(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; height2 : NativeInt; const LineWidth1 : NativeInt; blockSize : NativeInt); overload;
// ################################################################
// #### Threaded versions
function UseInnerBlockMult( w, h : NativeInt) : boolean;
// calculates dest = mt1*mt2'
procedure ThrBlockMatrixMultiplicationT2(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt;
const LineWidth1, LineWidth2 : NativeInt; blockSize : NativeInt; op : TMatrixMultDestOperation; mem : Pdouble);
procedure ThrBlockMatrixMultiplication(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt; op : TMatrixMultDestOperation = doNone); overload;
procedure ThrBlockMatrixMultiplication(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt; blockSize : NativeInt; op : TMatrixMultDestOperation = doNone; mem : PDouble = nil); overload;
implementation
uses BlockSizeSetup, SimpleMatrixOperations, MatrixASMStubSwitch, Math,
{$IFDEF MSWINDOWS}
Windows,
{$ENDIF}
MtxThreadPool, ThreadedMatrixOperations, MathUtilFunc;
{$I 'mrMath_CPU.inc'}
{$IFNDEF MRMATH_NOASM}
procedure YieldProcessor; {$IFDEF FPC} assembler; {$ENDIF}
asm
PAUSE;
end;
procedure MemoryBarrier; {$IFDEF FPC} assembler; {$ENDIF}
{$IFDEF x64}
asm
push rax;
XCHG [rsp],rax;
POP rax;
end;
{$ELSE}
asm
push eax;
XCHG [esp],eax;
POP eax;
end;
{$ENDIF}
{$ELSE}
// MRMATH_NOASM for FPC - need to be adjusted for non x86 cores
procedure YieldProcessor;
begin
{$IFDEF MSWINDOWS}
SwitchToThread;
{$ENDIF}
// todo other platforms
end;
procedure MemoryBarrier;
begin
// todo: MemoryBarrier is only availabel as macro in winnt.h not in windows.pas
end;
{$ENDIF}
procedure BlockMatrixVectorMultiplication(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; height2 : NativeInt; const LineWidth1 : NativeInt); overload;
begin
BlockMatrixVectorMultiplication(dest, destLineWidth, mt1, mt2, width1, height1, height2, LineWidth1, BlockedVectorMatrixMultSize);
end;
procedure BlockMatrixVectorMultiplication(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; height2 : NativeInt; const LineWidth1 : NativeInt; blockSize : NativeInt); overload;
var h : NativeInt;
actBlk : PDouble;
multBlk : PDouble;
pA, pB : PDouble;
blkIdxY : NativeInt;
idx : NativeInt;
gamma : NativeInt;
pActBlk : PDouble;
pHelp : PDouble;
w1FitCacheSize : boolean;
h1FitCacheSize : boolean;
blkHeight : NativeInt;
gammaWidth : NativeInt;
ptrMem : Pointer;
begin
if (width1 = 0) or (height1 = 0) or (height2 = 0) then
exit;
assert((width1 = height2), 'Dimension error');
assert((LineWidth1 >= width1*sizeof(double)), 'Line widths do not match');
assert(BlockSize > 1, 'Error block size must be at least 2');
h1FitCacheSize := (height1 mod blockSize) = 0;
w1FitCacheSize := (width1 mod blockSize) = 0;
h := height1 div blockSize + NativeInt(not h1FitCacheSize) - 1;
gamma := width1 div blockSize + NativeInt(not w1FitCacheSize) - 1;
actBlk := MtxMallocAlign(2*blockSize*sizeof(double), ptrMem );
multBlk := actBlk;
inc(multBlk, blockSize);
blkHeight := blockSize;
for blkIdxY := 0 to h do
begin
if (blkIdxY = h) and not h1FitCacheSize then
blkHeight := (height1 mod blockSize);
FillChar(actBlk^, blockSize*sizeof(double), 0);
pa := mt1;
pb := mt2;
gammaWidth := blockSize;
for idx := 0 to gamma do
begin
if (idx = gamma) and not w1FitCacheSize then
gammaWidth := width1 mod blockSize;
if gammaWidth > 1
then
MatrixMult(multBlk, sizeof(double), pa, pb, gammaWidth, blkHeight, 1, gammaWidth, LineWidth1, sizeof(double))
else
GenericMtxMult(multBlk, sizeof(double), pa, pb, gammaWidth, blkHeight, 1, gammaWidth, LineWidth1, sizeof(double));
// treat the addition as vector add:
if blkHeight > 1
then
MatrixAdd(actBlk, sizeof(double), actBlk, multBlk, blkHeight, 1, sizeof(double), sizeof(double))
else
// intersting: the normal matrix addition is a bit faster (or just a tiny bit slower) than the asm version in this case!
GenericMtxAdd(actBlk, sizeof(double), actBlk, multBlk, 1, blkHeight, sizeof(double), sizeof(double));
inc(pa, gammaWidth);
inc(pb, gammaWidth);
end;
pHelp := Dest;
pActBlk := actBlk;
for idx := 0 to blkHeight - 1 do
begin
pHelp^ := pActBlk^;
inc(PByte(pHelp), destLineWidth);
inc(pActBlk);
end;
inc(PByte(mt1), blkHeight*LineWidth1);
inc(PByte(Dest), blkHeight*destLineWidth);
end;
FreeMem(ptrMem);
end;
procedure BlockMatrixMultiplicationDirect(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt; op : TMatrixMultDestOperation);
begin
BlockMatrixMultiplicationDirect(dest, destLineWidth, mt1, mt2, width1, height1, width2, height2, LineWidth1, LineWidth2, BlockMatrixCacheSize, op);
end;
procedure BlockMatrixMultiplicationDirect(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt; blockSize : NativeInt; op : TMatrixMultDestOperation);
var w, h : NativeInt;
blkIdxX : NativeInt;
actBlk : PDouble;
multBlk : PDouble;
pA, pB : PDouble;
blkIdxY : NativeInt;
idx : NativeInt;
gamma : NativeInt;
pDest : PDouble;
pMt2 : PDouble;
w1FitCacheSize : boolean;
w2FitCacheSize : boolean;
h1FitCacheSize : boolean;
blkHeight : NativeInt;
blkWidth : NativeInt;
gammaWidth : NativeInt;
sizeVal : NativeInt;
m1, m2 : Pointer;
blockByteSize : NativeInt;
begin
if (width1 = 0) or (width2 = 0) or (height1 = 0) or (height2 = 0) then
exit;
assert((width1 = height2), 'Dimension error');
assert((destLineWidth - Width2*sizeof(double) >= 0) and (LineWidth1 >= width1*sizeof(double)) and (LineWidth2 >= width2*sizeof(double)), 'Line widths do not match');
// estimate a good blocksize - one with a small number of zero columns (but as near as possible to cCacheMtxSize
if blockSize <= 0 then
begin
sizeVal := Max(width1, width2);
while sizeVal > cCacheMtxSize do
sizeVal := sizeVal shr 1;
blockSize := Min(Max(width1, width2), Max(64, sizeVal));
end;
h1FitCacheSize := (height1 mod blockSize) = 0;
w2FitCacheSize := (width2 mod blockSize) = 0;
w1FitCacheSize := (width1 mod blockSize) = 0;
h := height1 div blockSize + NativeInt(not h1FitCacheSize) - 1;
w := width2 div blockSize + NativeInt(not w2FitCacheSize) - 1;
gamma := width1 div blockSize + NativeInt(not w1FitCacheSize) - 1;
blockByteSize := blockSize*blockSize*sizeof(double);
actBlk := MtxMallocAlign(blockByteSize, m1);
multBlk := MtxMallocAlign(blockByteSize, m2);
blkHeight := blockSize;
for blkIdxY := 0 to h do
begin
if (blkIdxY = h) and not h1FitCacheSize then
blkHeight := (height1 mod blockSize);
pDest := dest;
inc(PByte(pDest), blkIdxY*blockSize*destLineWidth);
pMt2 := mt2;
blkWidth := blockSize;
for blkIdxX := 0 to w do
begin
if (blkIdxX = w) and not w2FitCacheSize then
blkWidth := (width2 mod blockSize);
MtxMemInit(actBlk, blockByteSize, 0 );
pa := mt1;
pb := pMt2;
gammaWidth := blockSize;
for idx := 0 to gamma do
begin
if (idx = gamma) and not w1FitCacheSize then
gammaWidth := width1 mod blockSize;
if blkWidth > 1 then
begin
MatrixMult(multBlk, blockSize*sizeof(double), pa, pb, gammaWidth, blkHeight, blkWidth, gammaWidth, LineWidth1, LineWidth2);
MatrixAdd(actBlk, blockSize*sizeof(double), actBlk, multBlk, blkWidth, blkHeight, blockSize*sizeof(double), blockSize*sizeof(double));
end
else
begin
GenericMtxMult(multBlk, blockSize*sizeof(double), pa, pb, gammaWidth, blkHeight, blkWidth, gammaWidth, LineWidth1, LineWidth2);
GenericMtxAdd(actBlk, blockSize*sizeof(double), actBlk, multBlk, blkWidth, blkHeight, blockSize*sizeof(double), blockSize*sizeof(double));
end;
inc(pa, gammaWidth);
inc(PByte(pb), gammaWidth*LineWidth2);
end;
// apply final operation such that we got the final result: Dest := Dest +- A*B ;
case op of
doNone: MatrixCopy(pDest, destLineWidth, actBlk, blockSize*sizeof(double), blkWidth, blkHeight);
doAdd: MatrixAdd(pDest, destLineWidth, pDest, actBlk, blkWidth, blkHeight, destLineWidth, blockSize*sizeof(double));
doSub: MatrixSub(pDest, destLineWidth, pDest, actBlk, blkWidth, blkHeight, destLineWidth, blockSize*sizeof(double));
end;
inc(pDest, blockSize);
inc(pMt2, blockSize);
end;
inc(PByte(mt1), blkHeight*LineWidth1);
end;
FreeMem(m1);
FreeMem(m2);
end;
procedure BlockMatrixMultiplication(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt; const LineWidth1, LineWidth2 : NativeInt; op : TMatrixMultDestOperation);
begin
BlockMatrixMultiplication(dest, destLineWidth, mt1, mt2, width1, height1, width2, height2, LineWidth1, LineWidth2, BlockMatrixCacheSize, op, nil);
end;
procedure BlockMatrixMultiplication(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt;
const LineWidth1, LineWidth2 : NativeInt; blockSize : NativeInt; op : TMatrixMultDestOperation; mem : Pdouble);
var w, h : NativeInt;
blkIdxX : NativeInt;
actBlk : PDouble;
multBlk : PDouble;
transBlk, copyBlk : PDouble;
pA, pB : PDouble;
blkIdxY : NativeInt;
idx : NativeInt;
gamma : NativeInt;
pDest : PDouble;
pMt2 : PDouble;
w1FitCacheSize : boolean;
w2FitCacheSize : boolean;
h1FitCacheSize : boolean;
blkHeight : NativeInt;
blkWidth : NativeInt;
gammaWidth : NativeInt;
blockByteSize : Cardinal;
blockLineSize : Cardinal;
isAligned : boolean;
ptrMem : Pointer;
begin
if (width1 = 0) or (width2 = 0) or (height1 = 0) or (height2 = 0) then
exit;
assert((width1 = height2), 'Dimension error');
assert((destLineWidth - Width2*sizeof(double) >= 0) and (LineWidth1 >= width1*sizeof(double)) and (LineWidth2 >= width2*sizeof(double)), 'Line widths do not match');
assert(blockSize > 1, 'Error blocksize must be at least 2');
if (blockSize > width1) and (not assigned(mem)) then
blockSize := Next2Pwr( width1, blockSize );
isAligned := (NativeUint(dest) and $00000001F) = 0;
h1FitCacheSize := (height1 mod blockSize) = 0;
w2FitCacheSize := (width2 mod blockSize) = 0;
w1FitCacheSize := (width1 mod blockSize) = 0;
h := height1 div blockSize + NativeInt(not h1FitCacheSize) - 1;
w := width2 div blockSize + NativeInt(not w2FitCacheSize) - 1;
gamma := width1 div blockSize + NativeInt(not w1FitCacheSize) - 1;
blockByteSize := blockSize*blockSize*sizeof(double);
actBlk := mem;
ptrMem := nil;
if not Assigned(mem) then
actBlk := MtxMallocAlign(BlockMultMemSize(blockSize), ptrMem );
multBlk := PDouble(NativeUint(actBlk) + blockByteSize);
transBlk := PDouble(NativeUint(actBlk) + 2*blockByteSize);
copyBlk := PDouble(NativeUint(actBlk) + 3*blockByteSize);
blockLineSize := blockSize*sizeof(double);
blkHeight := blockSize;
for blkIdxY := 0 to h do
begin
if (blkIdxY = h) and not h1FitCacheSize then
blkHeight := (height1 mod blockSize);
pDest := dest;
inc(PByte(pDest), blkIdxY*blockSize*destLineWidth);
pMt2 := mt2;
blkWidth := blockSize;
for blkIdxX := 0 to w do
begin
if (blkIdxX = w) and not w2FitCacheSize then
blkWidth := (width2 mod blockSize);
MtxMemInit(actBlk, blockByteSize, 0);
pa := mt1;
pb := pMt2;
gammaWidth := blockSize;
for idx := 0 to gamma do
begin
if (idx = gamma) and not w1FitCacheSize then
gammaWidth := width1 mod blockSize;
if (blkWidth > 3) and (blkHeight > 3) then
begin
MatrixTranspose(transBlk, blockLineSize, pb, LineWidth2, blkWidth, gammaWidth);
// it is faster to copy the block rather then multply it unaligned!
if (not isAligned) or ((LineWidth1 and $00000001F) <> 0) then
begin
MatrixCopy(copyBlk, blockLineSize, pa, LineWidth1, gammaWidth, blkHeight);
MatrixMultT2(multblk, blockLineSize, copyBlk, transBlk, gammaWidth, blkHeight, gammaWidth, blkWidth, blockLineSize, blockLineSize);
end
else
MatrixMultT2(multBlk, blockLineSize, pa, transBlk, gammaWidth, blkHeight, gammaWidth, blkWidth, LineWidth1, blockLineSize);
MatrixAdd(actBlk, blockLineSize, actBlk, multBlk, blkWidth, blkHeight, blockLineSize, blockLineSize);
end
else
begin
GenericMtxMult(multBlk, blockLineSize, pa, pb, gammaWidth, blkHeight, blkWidth, gammaWidth, LineWidth1, LineWidth2);
GenericMtxAdd(actBlk, blockLineSize, actBlk, multBlk, blkWidth, blkHeight, blockLineSize, blockLineSize);
end;
inc(pa, gammaWidth);
inc(PByte(pb), gammaWidth*LineWidth2);
end;
// apply final operation such that we got the final result: Dest := Dest +- A*B ;
case op of
doNone: MatrixCopy(pDest, destLineWidth, actBlk, blockLineSize, blkWidth, blkHeight);
doAdd: MatrixAdd(pDest, destLineWidth, pDest, actBlk, blkWidth, blkHeight, destLineWidth, blockLineSize);
doSub: MatrixSub(pDest, destLineWidth, pDest, actBlk, blkWidth, blkHeight, destLineWidth, blockLineSize);
end;
inc(pDest, blockSize);
inc(pMt2, blockSize);
end;
inc(PByte(mt1), blkHeight*LineWidth1);
end;
if Assigned(ptrMem) then
FreeMem(ptrMem);
end;
// calculates mt1'*mt2
procedure BlockMatrixMultiplicationT1(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt;
const LineWidth1, LineWidth2 : NativeInt; blockSize : NativeInt; op : TMatrixMultDestOperation; mem : Pdouble);
var w, w1 : NativeInt;
blkIdxX : NativeInt;
actBlk : PDouble;
multBlk : PDouble;
transBlk1, transBlk2 : PDouble;
pA, pB : PDouble;
blkIdxY : NativeInt;
idx : NativeInt;
gamma : NativeInt;
pDest : PDouble;
pMt2 : PDouble;
w1FitCacheSize : boolean;
w2FitCacheSize : boolean;
h1FitCacheSize : boolean;
blkWidth1 : NativeInt;
blkWidth : NativeInt;
gammaHeight : NativeInt;
blockByteSize : Cardinal;
blockLineSize : Cardinal;
ptrMem : Pointer;
begin
if (width1 = 0) or (width2 = 0) or (height1 = 0) or (height2 = 0) then
exit;
assert((height1 = height2), 'Dimension error');
assert((destLineWidth - Width2*sizeof(double) >= 0) and (LineWidth1 >= width1*sizeof(double)) and (LineWidth2 >= width2*sizeof(double)), 'Line widths do not match');
assert(blockSize > 1, 'Error blocksize must be at least 2');
if (blockSize > width1) and (not Assigned(mem)) then
blockSize := Next2Pwr( height1, blockSize );
h1FitCacheSize := (height1 mod blockSize) = 0;
w2FitCacheSize := (width2 mod blockSize) = 0;
w1FitCacheSize := (width1 mod blockSize) = 0;
w1 := width1 div blockSize + NativeInt(not w1FitCacheSize) - 1;
w := width2 div blockSize + NativeInt(not w2FitCacheSize) - 1;
gamma := height1 div blockSize + NativeInt(not h1FitCacheSize) - 1;
blockByteSize := blockSize*blockSize*sizeof(double);
actBlk := mem;
ptrMem := nil;
if not Assigned(mem) then
actBlk := MtxMallocAlign(BlockMultMemSize(blockSize), ptrMem );
multBlk := PDouble(NativeUint(actBlk) + blockByteSize);
transBlk1 := PDouble(NativeUint(actBlk) + 2*blockByteSize);
transBlk2 := PDouble(NativeUint(actBlk) + 3*blockByteSize);
blockLineSize := blockSize*sizeof(double);
blkWidth1 := blockSize;
for blkIdxY := 0 to w1 do
begin
if (blkIdxY = w1) and not w1FitCacheSize then
blkWidth1 := (width1 mod blockSize);
pDest := dest;
inc(PByte(pDest), blkIdxY*blockSize*destLineWidth);
pMt2 := mt2;
blkWidth := blockSize;
for blkIdxX := 0 to w do
begin
if (blkIdxX = w) and not w2FitCacheSize then
blkWidth := (width2 mod blockSize);
MtxMemInit(actBlk, blockByteSize, 0);
pa := mt1;
pb := pMt2;
gammaHeight := blockSize;
for idx := 0 to gamma do
begin
if (idx = gamma) and not h1FitCacheSize then
gammaHeight := height1 mod blockSize;
if (blkWidth > 3) and (blkWidth1 > 3) then
begin
MatrixTranspose(transBlk1, blockLineSize, pa, LineWidth1, blkWidth1, gammaHeight);
MatrixTranspose(transBlk2, blockLineSize, pb, LineWidth2, blkWidth, gammaHeight);
MatrixMultT2(multblk, blockLineSize, transBlk1, transBlk2, gammaHeight, blkWidth1, gammaHeight, blkWidth, blockLineSize, blockLineSize);
MatrixAdd(actBlk, blockLineSize, actBlk, multBlk, blkWidth, blkWidth1, blockLineSize, blockLineSize);
end
else
begin
GenericMtxTranspose(transBlk1, blockLineSize, pa, LineWidth1, blkWidth1, gammaHeight);
GenericMtxMult(multBlk, blockLineSize, transBlk1, pb, gammaHeight, blkWidth1, blkWidth, gammaHeight, blockLineSize, LineWidth2);
GenericMtxAdd(actBlk, blockLineSize, actBlk, multBlk, blkWidth, blkWidth1, blockLineSize, blockLineSize);
end;
inc(PByte(pa), gammaHeight*LineWidth1);
inc(PByte(pb), gammaHeight*LineWidth2);
end;
// apply final operation such that we got the final result: Dest := Dest +- A*B ;
case op of
doNone: MatrixCopy(pDest, destLineWidth, actBlk, blockLineSize, blkWidth, blkWidth1);
doAdd: MatrixAdd(pDest, destLineWidth, pDest, actBlk, blkWidth, blkWidth1, destLineWidth, blockLineSize);
doSub: MatrixSub(pDest, destLineWidth, pDest, actBlk, blkWidth, blkWidth1, destLineWidth, blockLineSize);
end;
inc(pDest, blockSize);
inc(pMt2, blockSize);
end;
inc(mt1, blkWidth1);
end;
if Assigned(ptrMem) then
FreeMem(ptrMem);
end;
// calculates mt1*mt2'
procedure BlockMatrixMultiplicationT2(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt;
const LineWidth1, LineWidth2 : NativeInt; blockSize : NativeInt; op : TMatrixMultDestOperation; mem : Pdouble);
var h1, h : NativeInt;
blkIdxX : NativeInt;
actBlk : PDouble;
multBlk : PDouble;
copyBlk : PDouble;
pA, pB : PDouble;
blkIdxY : NativeInt;
idx : NativeInt;
gamma : NativeInt;
pDest : PDouble;
pMt2 : PDouble;
w1FitCacheSize : boolean;
h2FitCacheSize : boolean;
h1FitCacheSize : boolean;
blkHeight : NativeInt;
blkHeight1 : NativeInt;
gammaWidth : NativeInt;
blockByteSize : Cardinal;
blockLineSize : Cardinal;
isAligned : boolean;
ptrMem : Pointer;
begin
if (width1 = 0) or (width2 = 0) or (height1 = 0) or (height2 = 0) then
exit;
assert((width1 = width2), 'Dimension error');
assert((destLineWidth - height2*sizeof(double) >= 0) and (LineWidth1 >= width1*sizeof(double)) and (LineWidth2 >= width2*sizeof(double)), 'Line widths do not match');
assert(blockSize > 1, 'Error blocksize must be at least 2');
// try to take a shortcut where blocked matrix mult is hindering performance
if (op = doNone) and (width1 <= blockSize) then
begin
MatrixMultT2( dest, destLineWidth, mt1, mt2, width1, height1, width2, height2, LineWidth1, LineWidth2 );
exit;
end;
if (blockSize > width1) and (not Assigned(mem)) then
blockSize := Next2Pwr( width1, blockSize );
isAligned := (NativeUint(dest) and $0000000F) = 0;
h1FitCacheSize := (height1 mod blockSize) = 0;
h2FitCacheSize := (height2 mod blockSize) = 0;
w1FitCacheSize := (width1 mod blockSize) = 0;
h := height1 div blockSize + NativeInt(not h1FitCacheSize) - 1;
h1 := height2 div blockSize + NativeInt(not h2FitCacheSize) - 1;
gamma := width1 div blockSize + NativeInt(not w1FitCacheSize) - 1;
blockByteSize := blockSize*blockSize*sizeof(double);
actBlk := mem;
ptrMem := nil;
if not Assigned(mem) then
actBlk := MtxMallocAlign(BlockMultMemSize(blockSize), ptrMem );
multBlk := PDouble(NativeUint(actBlk) + blockByteSize);
copyBlk := PDouble(NativeUint(actBlk) + 2*blockByteSize);
blockLineSize := blockSize*sizeof(double);
blkHeight := blockSize;
for blkIdxY := 0 to h do
begin
if (blkIdxY = h) and not h1FitCacheSize then
blkHeight := (height1 mod blockSize);
pDest := dest;
inc(PByte(pDest), blkIdxY*blockSize*destLineWidth);
pMt2 := mt2;
blkHeight1 := blockSize;
for blkIdxX := 0 to h1 do
begin
if (blkIdxX = h1) and not h2FitCacheSize then
blkHeight1 := (height2 mod blockSize);
MtxMemInit(actBlk, blockByteSize, 0);
pa := mt1;
pb := pMt2;
gammaWidth := blockSize;
for idx := 0 to gamma do
begin
if (idx = gamma) and not w1FitCacheSize then
gammaWidth := width1 mod blockSize;
if (blkHeight1 > 3) and (blkHeight > 3) then
begin
// it is faster to copy the block rather then multply it unaligned!
if (not isAligned) or ((LineWidth1 and $0000001F) <> 0) then
begin
MatrixCopy(copyBlk, blockLineSize, pa, LineWidth1, gammaWidth, blkHeight);
MatrixMultT2(multblk, blockLineSize, copyBlk, pb, gammaWidth, blkHeight, gammaWidth, blkHeight1, blockLineSize, LineWidth2);
end
else
MatrixMultT2(multBlk, blockLineSize, pa, pb, gammaWidth, blkHeight, gammaWidth, blkHeight1, LineWidth1, LineWidth2);
MatrixAdd(actBlk, blockLineSize, actBlk, multBlk, blkHeight1, blkHeight, blockLineSize, blockLineSize);
end
else
begin
GenericMtxMultTransp(multBlk, blockLineSize, pa, pb, gammaWidth, blkHeight, gammaWidth, blkHeight1, LineWidth1, LineWidth2);
GenericMtxAdd(actBlk, blockLineSize, actBlk, multBlk, blkHeight1, blkHeight, blockLineSize, blockLineSize);
end;
inc(pa, gammaWidth);
inc(pb, gammaWidth);
end;
// apply final operation such that we got the final result: Dest := Dest +- A*B ;
case op of
doNone: MatrixCopy(pDest, destLineWidth, actBlk, blockLineSize, blkHeight1, blkHeight);
doAdd: MatrixAdd(pDest, destLineWidth, pDest, actBlk, blkHeight1, blkHeight, destLineWidth, blockLineSize);
doSub: MatrixSub(pDest, destLineWidth, pDest, actBlk, blkHeight1, blkHeight, destLineWidth, blockLineSize);
end;
inc(pDest, blockSize);
inc(PByte(pMt2), blockSize*LineWidth2);
end;
inc(PByte(mt1), blkHeight*LineWidth1);
end;
if Assigned(ptrMem) then
FreeMem(ptrMem);
end;
// ########################################################
// #### Generic blocked routines
// ########################################################
procedure GenericBlockMatrixMultiplication(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble;
width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt;
const LineWidth1, LineWidth2 : NativeInt; blockSize : NativeInt; op : TMatrixMultDestOperation = doNone; mem : PDouble = nil);
var w, h : NativeInt;
blkIdxX : NativeInt;
actBlk : PDouble;
multBlk : PDouble;
pA, pB : PDouble;
blkIdxY : NativeInt;
idx : NativeInt;
gamma : NativeInt;
pDest : PDouble;
pMt2 : PDouble;
w1FitCacheSize : boolean;
w2FitCacheSize : boolean;
h1FitCacheSize : boolean;
blkHeight : NativeInt;
blkWidth : NativeInt;
gammaWidth : NativeInt;
sizeVal : NativeInt;
begin
if (width1 = 0) or (width2 = 0) or (height1 = 0) or (height2 = 0) then
exit;
assert((width1 = height2), 'Dimension error');
assert((destLineWidth - Width2*sizeof(double) >= 0) and (LineWidth1 >= width1*sizeof(double)) and (LineWidth2 >= width2*sizeof(double)), 'Line widths do not match');
// estimate a good blocksize - one with a small number of zero columns (but as near as possible to cCacheMtxSize
if blockSize <= 0 then
begin
sizeVal := Max(width1, width2);
while sizeVal > cCacheMtxSize do
sizeVal := sizeVal shr 1;
blockSize := Min(Max(width1, width2), Max(64, sizeVal));
end;
h1FitCacheSize := (height1 mod blockSize) = 0;
w2FitCacheSize := (width2 mod blockSize) = 0;
w1FitCacheSize := (width1 mod blockSize) = 0;
h := height1 div blockSize + NativeInt(not h1FitCacheSize) - 1;
w := width2 div blockSize + NativeInt(not w2FitCacheSize) - 1;
gamma := width1 div blockSize + NativeInt(not w1FitCacheSize) - 1;
if Assigned(mem)
then
actBlk := mem
else
GetMem(actBlk, BlockMultMemSize(blockSize));
multBlk := actBlk;
inc(multBlk, blockSize*blockSize);
blkHeight := blockSize;
for blkIdxY := 0 to h do
begin
if (blkIdxY = h) and not h1FitCacheSize then
blkHeight := (height1 mod blockSize);
pDest := dest;
inc(PByte(pDest), blkIdxY*blockSize*destLineWidth);
pMt2 := mt2;
blkWidth := blockSize;
for blkIdxX := 0 to w do
begin
if (blkIdxX = w) and not w2FitCacheSize then
blkWidth := (width2 mod blockSize);
FillChar(actBlk^, blockSize*blockSize*sizeof(double), 0);
pa := mt1;
pb := pMt2;
gammaWidth := blockSize;
for idx := 0 to gamma do
begin
if (idx = gamma) and not w1FitCacheSize then
gammaWidth := width1 mod blockSize;
GenericMtxMult(multBlk, blockSize*sizeof(double), pa, pb, gammaWidth, blkHeight, blkWidth, gammaWidth, LineWidth1, LineWidth2);
GenericMtxAdd(actBlk, blockSize*sizeof(double), actBlk, multBlk, blkWidth, blkHeight, blockSize*sizeof(double), blockSize*sizeof(double));
inc(pa, gammaWidth);
inc(PByte(pb), gammaWidth*LineWidth2);
end;
// build the result: dest = Dest +- A*B (according to the operator)
case op of
doNone: GenericMtxCopy(pDest, destLineWidth, ActBlk, blockSize*sizeof(double), blkWidth, blkHeight);
doAdd: GenericMtxAdd(pDest, destLineWidth, ActBlk, pDest, blkWidth, blkHeight, blockSize*sizeof(double), destLineWidth);
doSub: GenericMtxSub(pDest, destLineWidth, pDest, ActBlk, blkWidth, blkHeight, destLineWidth, blockSize*sizeof(double));
end;
inc(pDest, blockSize);
inc(pMt2, blockSize);
end;
inc(PByte(mt1), blkHeight*LineWidth1);
end;
if not Assigned(mem) then
FreeMem(actBlk);
end;
// calculates mt1'*mt2
procedure GenericBlockMatrixMultiplicationT1(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt;
const LineWidth1, LineWidth2 : NativeInt; blockSize : NativeInt; op : TMatrixMultDestOperation; mem : Pdouble);
var w, w1 : NativeInt;
blkIdxX : NativeInt;
actBlk : PDouble;
multBlk : PDouble;
transBlk1 : PDouble;
pA, pB : PDouble;
blkIdxY : NativeInt;
idx : NativeInt;
gamma : NativeInt;
pDest : PDouble;
pMt2 : PDouble;
w1FitCacheSize : boolean;
w2FitCacheSize : boolean;
h1FitCacheSize : boolean;
blkWidth1 : NativeInt;
blkWidth : NativeInt;
gammaHeight : NativeInt;
blockByteSize : Cardinal;
blockLineSize : Cardinal;
begin
if (width1 = 0) or (width2 = 0) or (height1 = 0) or (height2 = 0) then
exit;
assert((height1 = height2), 'Dimension error');
assert((destLineWidth - Width2*sizeof(double) >= 0) and (LineWidth1 >= width1*sizeof(double)) and (LineWidth2 >= width2*sizeof(double)), 'Line widths do not match');
assert(blockSize > 1, 'Error blocksize must be at least 2');
h1FitCacheSize := (height1 mod blockSize) = 0;
w2FitCacheSize := (width2 mod blockSize) = 0;
w1FitCacheSize := (width1 mod blockSize) = 0;
w1 := width1 div blockSize + NativeInt(not w1FitCacheSize) - 1;
w := width2 div blockSize + NativeInt(not w2FitCacheSize) - 1;
gamma := height1 div blockSize + NativeInt(not h1FitCacheSize) - 1;
blockByteSize := blockSize*blockSize*sizeof(double);
actBlk := mem;
if not Assigned(mem) then
GetMem(actBlk, BlockMultMemSize(blockSize));
multBlk := PDouble(NativeUint(actBlk) + blockByteSize);
transBlk1 := PDouble(NativeUint(actBlk) + 2*blockByteSize);
blockLineSize := blockSize*sizeof(double);
blkWidth1 := blockSize;
for blkIdxY := 0 to w1 do
begin
if (blkIdxY = w1) and not w1FitCacheSize then
blkWidth1 := (width1 mod blockSize);
pDest := dest;
inc(PByte(pDest), blkIdxY*blockSize*destLineWidth);
pMt2 := mt2;
blkWidth := blockSize;
for blkIdxX := 0 to w do
begin
if (blkIdxX = w) and not w2FitCacheSize then
blkWidth := (width2 mod blockSize);
FillChar(actBlk^, blockByteSize, 0);
pa := mt1;
pb := pMt2;
gammaHeight := blockSize;
for idx := 0 to gamma do
begin
if (idx = gamma) and not h1FitCacheSize then
gammaHeight := height1 mod blockSize;
GenericMtxTranspose(transBlk1, blockLineSize, pa, LineWidth1, blkWidth1, gammaHeight);
GenericMtxMult(multBlk, blockLineSize, transBlk1, pb, gammaHeight, blkWidth1, blkWidth, gammaHeight, blockLineSize, LineWidth2);
GenericMtxAdd(actBlk, blockLineSize, actBlk, multBlk, blkWidth, blkWidth1, blockLineSize, blockLineSize);
inc(PByte(pa), gammaHeight*LineWidth1);
inc(PByte(pb), gammaHeight*LineWidth2);
end;
// apply final operation such that we got the final result: Dest := Dest +- A*B ;
case op of
doNone: GenericMtxCopy(pDest, destLineWidth, actBlk, blockLineSize, blkWidth, blkWidth1);
doAdd: GenericMtxAdd(pDest, destLineWidth, pDest, actBlk, blkWidth, blkWidth1, destLineWidth, blockLineSize);
doSub: GenericMtxSub(pDest, destLineWidth, pDest, actBlk, blkWidth, blkWidth1, destLineWidth, blockLineSize);
end;
inc(pDest, blockSize);
inc(pMt2, blockSize);
end;
inc(mt1, blkWidth1);
end;
if not Assigned(mem) then
FreeMem(actBlk);
end;
// calculates mt1*mt2'
procedure GenericBlockMatrixMultiplicationT2(dest : PDouble; const destLineWidth : NativeInt; mt1, mt2 : PDouble; width1 : NativeInt; height1 : NativeInt; width2 : NativeInt; height2 : NativeInt;
const LineWidth1, LineWidth2 : NativeInt; blockSize : NativeInt; op : TMatrixMultDestOperation; mem : Pdouble);
var h1, h : NativeInt;
blkIdxX : NativeInt;
actBlk : PDouble;
multBlk : PDouble;
pA, pB : PDouble;
blkIdxY : NativeInt;
idx : NativeInt;
gamma : NativeInt;
pDest : PDouble;
pMt2 : PDouble;
w1FitCacheSize : boolean;
h2FitCacheSize : boolean;
h1FitCacheSize : boolean;
blkHeight : NativeInt;
blkHeight1 : NativeInt;
gammaWidth : NativeInt;
blockByteSize : Cardinal;
blockLineSize : Cardinal;
begin
if (width1 = 0) or (width2 = 0) or (height1 = 0) or (height2 = 0) then
exit;
assert((width1 = width2), 'Dimension error');
assert((destLineWidth - height2*sizeof(double) >= 0) and (LineWidth1 >= width1*sizeof(double)) and (LineWidth2 >= width2*sizeof(double)), 'Line widths do not match');
assert(blockSize > 1, 'Error blocksize must be at least 2');
h1FitCacheSize := (height1 mod blockSize) = 0;
h2FitCacheSize := (height2 mod blockSize) = 0;
w1FitCacheSize := (width1 mod blockSize) = 0;
h := height1 div blockSize + NativeInt(not h1FitCacheSize) - 1;
h1 := height2 div blockSize + NativeInt(not h2FitCacheSize) - 1;
gamma := width1 div blockSize + NativeInt(not w1FitCacheSize) - 1;
blockByteSize := blockSize*blockSize*sizeof(double);
actBlk := mem;
if not Assigned(mem) then
GetMem(actBlk, BlockMultMemSize(blockSize));
multBlk := PDouble(NativeUint(actBlk) + blockByteSize);
blockLineSize := blockSize*sizeof(double);
blkHeight := blockSize;
for blkIdxY := 0 to h do
begin
if (blkIdxY = h) and not h1FitCacheSize then
blkHeight := (height1 mod blockSize);
pDest := dest;
inc(PByte(pDest), blkIdxY*blockSize*destLineWidth);
pMt2 := mt2;
blkHeight1 := blockSize;
for blkIdxX := 0 to h1 do
begin
if (blkIdxX = h1) and not h2FitCacheSize then
blkHeight1 := (height2 mod blockSize);
FillChar(actBlk^, blockByteSize, 0);
pa := mt1;
pb := pMt2;
gammaWidth := blockSize;
for idx := 0 to gamma do
begin
if (idx = gamma) and not w1FitCacheSize then
gammaWidth := width1 mod blockSize;
GenericMtxMultTransp(multBlk, blockLineSize, pa, pb, gammaWidth, blkHeight, gammaWidth, blkHeight1, LineWidth1, LineWidth2);
GenericMtxAdd(actBlk, blockLineSize, actBlk, multBlk, blkHeight1, blkHeight, blockLineSize, blockLineSize);
inc(pa, gammaWidth);
inc(pb, gammaWidth);