/
transpose.cpp
2128 lines (1926 loc) · 87.8 KB
/
transpose.cpp
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
/**
* \file
* Compute the tensor transposition
*/
#include <tuple>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <cmath>
#include <chrono>
#include <float.h>
#include <stdio.h>
#include <assert.h>
#ifdef _OPENMP
#include <omp.h>
#endif
#include "../include/utils.h"
#include "../include/macros.h"
#include "../include/compute_node.h"
#include "../include/utils.h"
#include "../include/hptt_types.h"
#include "../include/plan.h"
#include "../include/transpose.h"
namespace hptt {
template <typename floatType, int betaIsZero, bool conjA>
struct micro_kernel
{
static void execute(const floatType* __restrict__ A, const size_t lda, floatType* __restrict__ B, const size_t ldb, const floatType alpha, const floatType beta)
{
constexpr int n = (REGISTER_BITS/8) / sizeof(floatType);
if( betaIsZero )
for(int j=0; j < n; ++j)
for(int i=0; i < n; ++i)
if( conjA )
B[i + j * ldb] = alpha * conj(A[i * lda + j]);
else
B[i + j * ldb] = alpha * A[i * lda + j];
else
for(int j=0; j < n; ++j)
for(int i=0; i < n; ++i)
if( conjA )
B[i + j * ldb] = alpha * conj(A[i * lda + j]) + beta * B[i + j * ldb];
else
B[i + j * ldb] = alpha * A[i * lda + j] + beta * B[i + j * ldb];
}
};
template<typename floatType>
static void streamingStore( floatType* out, const floatType *in )
{
constexpr int n = REGISTER_BITS/8/sizeof(floatType);
for(int i=0; i < n; ++i)
out[i] = in[i];
}
#ifdef HPTT_ARCH_AVX
#include <immintrin.h>
template<typename floatType>
static INLINE void prefetch(const floatType* A, const int lda)
{
constexpr int blocking_micro_ = REGISTER_BITS/8 / sizeof(floatType);
for(int i=0;i < blocking_micro_; ++i)
_mm_prefetch((char*)(A + i * lda), _MM_HINT_T2);
}
template <int betaIsZero, bool conjA>
struct micro_kernel<double, betaIsZero, conjA>
{
static void execute(const double* __restrict__ A, const size_t lda, double* __restrict__ B, const size_t ldb, const double alpha ,const double beta)
{
__m256d reg_alpha = _mm256_set1_pd(alpha); // do not alter the content of B
__m256d reg_beta = _mm256_set1_pd(beta); // do not alter the content of B
//Load A
__m256d rowA0 = _mm256_loadu_pd((A +0*lda));
__m256d rowA1 = _mm256_loadu_pd((A +1*lda));
__m256d rowA2 = _mm256_loadu_pd((A +2*lda));
__m256d rowA3 = _mm256_loadu_pd((A +3*lda));
//4x4 transpose micro kernel
__m256d r4, r34, r3, r33;
r33 = _mm256_shuffle_pd( rowA2, rowA3, 0x3 );
r3 = _mm256_shuffle_pd( rowA0, rowA1, 0x3 );
r34 = _mm256_shuffle_pd( rowA2, rowA3, 0xc );
r4 = _mm256_shuffle_pd( rowA0, rowA1, 0xc );
rowA0 = _mm256_permute2f128_pd( r34, r4, 0x2 );
rowA1 = _mm256_permute2f128_pd( r33, r3, 0x2 );
rowA2 = _mm256_permute2f128_pd( r33, r3, 0x13 );
rowA3 = _mm256_permute2f128_pd( r34, r4, 0x13 );
//Scale A
rowA0 = _mm256_mul_pd(rowA0, reg_alpha);
rowA1 = _mm256_mul_pd(rowA1, reg_alpha);
rowA2 = _mm256_mul_pd(rowA2, reg_alpha);
rowA3 = _mm256_mul_pd(rowA3, reg_alpha);
//Load B
if( !betaIsZero )
{
__m256d rowB0 = _mm256_loadu_pd((B +0*ldb));
__m256d rowB1 = _mm256_loadu_pd((B +1*ldb));
__m256d rowB2 = _mm256_loadu_pd((B +2*ldb));
__m256d rowB3 = _mm256_loadu_pd((B +3*ldb));
rowB0 = _mm256_add_pd( _mm256_mul_pd(rowB0, reg_beta), rowA0);
rowB1 = _mm256_add_pd( _mm256_mul_pd(rowB1, reg_beta), rowA1);
rowB2 = _mm256_add_pd( _mm256_mul_pd(rowB2, reg_beta), rowA2);
rowB3 = _mm256_add_pd( _mm256_mul_pd(rowB3, reg_beta), rowA3);
//Store B
_mm256_storeu_pd((B + 0 * ldb), rowB0);
_mm256_storeu_pd((B + 1 * ldb), rowB1);
_mm256_storeu_pd((B + 2 * ldb), rowB2);
_mm256_storeu_pd((B + 3 * ldb), rowB3);
} else {
//Store B
_mm256_storeu_pd((B + 0 * ldb), rowA0);
_mm256_storeu_pd((B + 1 * ldb), rowA1);
_mm256_storeu_pd((B + 2 * ldb), rowA2);
_mm256_storeu_pd((B + 3 * ldb), rowA3);
}
}
};
template <int betaIsZero, bool conjA>
struct micro_kernel<float, betaIsZero, conjA>
{
static void execute(const float* __restrict__ A, const size_t lda, float* __restrict__ B, const size_t ldb, const float alpha ,const float beta)
{
__m256 reg_alpha = _mm256_set1_ps(alpha); // do not alter the content of B
__m256 reg_beta = _mm256_set1_ps(beta); // do not alter the content of B
//Load A
__m256 rowA0 = _mm256_loadu_ps((A +0*lda));
__m256 rowA1 = _mm256_loadu_ps((A +1*lda));
__m256 rowA2 = _mm256_loadu_ps((A +2*lda));
__m256 rowA3 = _mm256_loadu_ps((A +3*lda));
__m256 rowA4 = _mm256_loadu_ps((A +4*lda));
__m256 rowA5 = _mm256_loadu_ps((A +5*lda));
__m256 rowA6 = _mm256_loadu_ps((A +6*lda));
__m256 rowA7 = _mm256_loadu_ps((A +7*lda));
//8x8 transpose micro kernel
__m256 r121, r139, r120, r138, r71, r89, r70, r88, r11, r1, r55, r29, r10, r0, r54, r28;
r28 = _mm256_unpacklo_ps( rowA4, rowA5 );
r54 = _mm256_unpacklo_ps( rowA6, rowA7 );
r0 = _mm256_unpacklo_ps( rowA0, rowA1 );
r10 = _mm256_unpacklo_ps( rowA2, rowA3 );
r29 = _mm256_unpackhi_ps( rowA4, rowA5 );
r55 = _mm256_unpackhi_ps( rowA6, rowA7 );
r1 = _mm256_unpackhi_ps( rowA0, rowA1 );
r11 = _mm256_unpackhi_ps( rowA2, rowA3 );
r88 = _mm256_shuffle_ps( r28, r54, 0x44 );
r70 = _mm256_shuffle_ps( r0, r10, 0x44 );
r89 = _mm256_shuffle_ps( r28, r54, 0xee );
r71 = _mm256_shuffle_ps( r0, r10, 0xee );
r138 = _mm256_shuffle_ps( r29, r55, 0x44 );
r120 = _mm256_shuffle_ps( r1, r11, 0x44 );
r139 = _mm256_shuffle_ps( r29, r55, 0xee );
r121 = _mm256_shuffle_ps( r1, r11, 0xee );
rowA0 = _mm256_permute2f128_ps( r88, r70, 0x2 );
rowA1 = _mm256_permute2f128_ps( r89, r71, 0x2 );
rowA2 = _mm256_permute2f128_ps( r138, r120, 0x2 );
rowA3 = _mm256_permute2f128_ps( r139, r121, 0x2 );
rowA4 = _mm256_permute2f128_ps( r88, r70, 0x13 );
rowA5 = _mm256_permute2f128_ps( r89, r71, 0x13 );
rowA6 = _mm256_permute2f128_ps( r138, r120, 0x13 );
rowA7 = _mm256_permute2f128_ps( r139, r121, 0x13 );
//Scale A
rowA0 = _mm256_mul_ps(rowA0, reg_alpha);
rowA1 = _mm256_mul_ps(rowA1, reg_alpha);
rowA2 = _mm256_mul_ps(rowA2, reg_alpha);
rowA3 = _mm256_mul_ps(rowA3, reg_alpha);
rowA4 = _mm256_mul_ps(rowA4, reg_alpha);
rowA5 = _mm256_mul_ps(rowA5, reg_alpha);
rowA6 = _mm256_mul_ps(rowA6, reg_alpha);
rowA7 = _mm256_mul_ps(rowA7, reg_alpha);
//Load B
if( !betaIsZero )
{
__m256 rowB0 = _mm256_loadu_ps((B +0*ldb));
__m256 rowB1 = _mm256_loadu_ps((B +1*ldb));
__m256 rowB2 = _mm256_loadu_ps((B +2*ldb));
__m256 rowB3 = _mm256_loadu_ps((B +3*ldb));
__m256 rowB4 = _mm256_loadu_ps((B +4*ldb));
__m256 rowB5 = _mm256_loadu_ps((B +5*ldb));
__m256 rowB6 = _mm256_loadu_ps((B +6*ldb));
__m256 rowB7 = _mm256_loadu_ps((B +7*ldb));
rowB0 = _mm256_add_ps( _mm256_mul_ps(rowB0, reg_beta), rowA0);
rowB1 = _mm256_add_ps( _mm256_mul_ps(rowB1, reg_beta), rowA1);
rowB2 = _mm256_add_ps( _mm256_mul_ps(rowB2, reg_beta), rowA2);
rowB3 = _mm256_add_ps( _mm256_mul_ps(rowB3, reg_beta), rowA3);
rowB4 = _mm256_add_ps( _mm256_mul_ps(rowB4, reg_beta), rowA4);
rowB5 = _mm256_add_ps( _mm256_mul_ps(rowB5, reg_beta), rowA5);
rowB6 = _mm256_add_ps( _mm256_mul_ps(rowB6, reg_beta), rowA6);
rowB7 = _mm256_add_ps( _mm256_mul_ps(rowB7, reg_beta), rowA7);
//Store B
_mm256_storeu_ps((B + 0 * ldb), rowB0);
_mm256_storeu_ps((B + 1 * ldb), rowB1);
_mm256_storeu_ps((B + 2 * ldb), rowB2);
_mm256_storeu_ps((B + 3 * ldb), rowB3);
_mm256_storeu_ps((B + 4 * ldb), rowB4);
_mm256_storeu_ps((B + 5 * ldb), rowB5);
_mm256_storeu_ps((B + 6 * ldb), rowB6);
_mm256_storeu_ps((B + 7 * ldb), rowB7);
} else {
_mm256_storeu_ps((B + 0 * ldb), rowA0);
_mm256_storeu_ps((B + 1 * ldb), rowA1);
_mm256_storeu_ps((B + 2 * ldb), rowA2);
_mm256_storeu_ps((B + 3 * ldb), rowA3);
_mm256_storeu_ps((B + 4 * ldb), rowA4);
_mm256_storeu_ps((B + 5 * ldb), rowA5);
_mm256_storeu_ps((B + 6 * ldb), rowA6);
_mm256_storeu_ps((B + 7 * ldb), rowA7);
}
}
};
template<>
void streamingStore<float>( float* out, const float*in ){
_mm256_stream_ps(out, _mm256_loadu_ps(in));
}
template<>
void streamingStore<double>( double* out, const double*in ){
_mm256_stream_pd(out, _mm256_loadu_pd(in));
}
#else
template<typename floatType>
static INLINE void prefetch(const floatType* A, const int lda) { }
#endif
#ifdef HPTT_ARCH_ARM
#include <arm_neon.h>
template <int betaIsZero, bool conjA>
struct micro_kernel<float, betaIsZero>
{
static void execute(const float* __restrict__ A, const size_t lda, float* __restrict__ B, const size_t ldb, const float alpha ,const float beta)
{
float32x4_t reg_alpha = vdupq_n_f32(alpha);
float32x4_t reg_beta = vdupq_n_f32(beta);
//Load A
float32x4_t rowA0 = vld1q_f32((A +0*lda));
float32x4_t rowA1 = vld1q_f32((A +1*lda));
float32x4_t rowA2 = vld1q_f32((A +2*lda));
float32x4_t rowA3 = vld1q_f32((A +3*lda));
//4x4 transpose micro kernel
float32x4x2_t t0,t1,t2,t3;
t0 = vuzpq_f32(rowA0, rowA2);
t1 = vuzpq_f32(rowA1, rowA3);
t2 = vtrnq_f32(t0.val[0], t1.val[0]);
t3 = vtrnq_f32(t0.val[1], t1.val[1]);
//Scale A
rowA0 = vmulq_f32(t2.val[0], reg_alpha);
rowA1 = vmulq_f32(t3.val[0], reg_alpha);
rowA2 = vmulq_f32(t2.val[1], reg_alpha);
rowA3 = vmulq_f32(t3.val[1], reg_alpha);
//Load B
if( !betaIsZero )
{
float32x4_t rowB0 = vld1q_f32((B +0*ldb));
float32x4_t rowB1 = vld1q_f32((B +1*ldb));
float32x4_t rowB2 = vld1q_f32((B +2*ldb));
float32x4_t rowB3 = vld1q_f32((B +3*ldb));
rowB0 = vaddq_f32( vmulq_f32(rowB0, reg_beta), rowA0);
rowB1 = vaddq_f32( vmulq_f32(rowB1, reg_beta), rowA1);
rowB2 = vaddq_f32( vmulq_f32(rowB2, reg_beta), rowA2);
rowB3 = vaddq_f32( vmulq_f32(rowB3, reg_beta), rowA3);
//Store B
vst1q_f32((B + 0 * ldb), rowB0);
vst1q_f32((B + 1 * ldb), rowB1);
vst1q_f32((B + 2 * ldb), rowB2);
vst1q_f32((B + 3 * ldb), rowB3);
} else {
//Store B
vst1q_f32((B + 0 * ldb), rowA0);
vst1q_f32((B + 1 * ldb), rowA1);
vst1q_f32((B + 2 * ldb), rowA2);
vst1q_f32((B + 3 * ldb), rowA3);
}
}
};
#endif
#ifdef HPTT_ARCH_IBM
//#include <altivec.h> //vector conflicts with std::vector (TODO)
//
//template <int betaIsZero>
//struct micro_kernel<float, betaIsZero>
//{
// static void execute(const float* __restrict__ A, const size_t lda, float* __restrict__ B, const size_t ldb, const float alpha ,const float beta)
// {
// vector float reg_alpha = vec_splats(alpha);
//
// //Load A
// vector float rowA0 = vec_ld(0,const_cast<float*>(A+0*lda));
// vector float rowA1 = vec_ld(0,const_cast<float*>(A+1*lda));
// vector float rowA2 = vec_ld(0,const_cast<float*>(A+2*lda));
// vector float rowA3 = vec_ld(0,const_cast<float*>(A+3*lda));
//
// //4x4 transpose micro kernel
// vector float aa = (vector float) {2, 3, 2.5, 3.5};
// vector float bb = (vector float) {2.25, 3.25, 2.75, 3.75};
// vector float cc = (vector float) {2, 2.25, 3, 3.25};
// vector float dd = (vector float) {2.5, 2.75, 3.5, 3.75};
//
// vector float r010 = vec_perm(rowA0,rowA1, aa); //0,4,2,6
// vector float r011 = vec_perm(rowA0,rowA1, bb); //1,5,3,7
// vector float r230 = vec_perm(rowA2,rowA3, aa); //8,12,10,14
// vector float r231 = vec_perm(rowA2,rowA3, bb); //9,13,11,15
//
// rowA0 = vec_perm(r010, r230, cc); //0,4,8,12
// rowA1 = vec_perm(r011, r231, cc); //1,5,9,13
// rowA2 = vec_perm(r010, r230, dd); //2,6,10,14
// rowA3 = vec_perm(r011, r231, dd); //3,7,11,15
//
// //Scale A
// rowA0 = vec_mul(rowA0, reg_alpha);
// rowA1 = vec_mul(rowA1, reg_alpha);
// rowA2 = vec_mul(rowA2, reg_alpha);
// rowA3 = vec_mul(rowA3, reg_alpha);
//
// if( !betaIsZero )
// {
// vector float reg_beta = vec_splats(beta);
// //Load B
// vector float rowB0 = vec_ld(0,const_cast<float*>(B+0*ldb));
// vector float rowB1 = vec_ld(0,const_cast<float*>(B+1*ldb));
// vector float rowB2 = vec_ld(0,const_cast<float*>(B+2*ldb));
// vector float rowB3 = vec_ld(0,const_cast<float*>(B+3*ldb));
//
// rowB0 = vec_madd( rowB0, reg_beta, rowA0);
// rowB1 = vec_madd( rowB1, reg_beta, rowA1);
// rowB2 = vec_madd( rowB2, reg_beta, rowA2);
// rowB3 = vec_madd( rowB3, reg_beta, rowA3);
//
// //Store B
// vec_st(rowB0, 0, B + 0 * ldb);
// vec_st(rowB1, 0, B + 1 * ldb);
// vec_st(rowB2, 0, B + 2 * ldb);
// vec_st(rowB3, 0, B + 3 * ldb);
// } else {
// //Store B
// vec_st(rowA0, 0, B + 0 * ldb);
// vec_st(rowA1, 0, B + 1 * ldb);
// vec_st(rowA2, 0, B + 2 * ldb);
// vec_st(rowA3, 0, B + 3 * ldb);
// }
// }
//};
#endif
template<int betaIsZero, typename floatType, bool conjA>
static INLINE void macro_kernel_scalar(const floatType* __restrict__ A, const size_t lda, int blockingA,
floatType* __restrict__ B, const size_t ldb, int blockingB,
const floatType alpha ,const floatType beta)
{
#ifdef DEBUG
assert( blockingA > 0 && blockingB > 0);
#endif
if( betaIsZero )
for(int j=0; j < blockingA; ++j)
for(int i=0; i < blockingB; ++i)
if( conjA )
B[i + j * ldb] = alpha * conj(A[i * lda + j]);
else
B[i + j * ldb] = alpha * A[i * lda + j];
else
for(int j=0; j < blockingA; ++j)
for(int i=0; i < blockingB; ++i)
if( conjA )
B[i + j * ldb] = alpha * conj(A[i * lda + j]) + beta * B[i + j * ldb];
else
B[i + j * ldb] = alpha * A[i * lda + j] + beta * B[i + j * ldb];
}
template<int blockingA, int blockingB, int betaIsZero, typename floatType, bool useStreamingStores_, bool conjA>
static INLINE void macro_kernel(const floatType* __restrict__ A, const floatType* __restrict__ Anext, const size_t lda,
floatType* __restrict__ B, const floatType* __restrict__ Bnext, const size_t ldb,
const floatType alpha ,const floatType beta)
{
constexpr int blocking_micro_ = REGISTER_BITS/8 / sizeof(floatType);
constexpr int blocking_ = blocking_micro_ * 4;
const bool useStreamingStores = useStreamingStores_ && betaIsZero && (blockingB*sizeof(floatType))%64 == 0 && ((uint64_t)B)%32 == 0 && (ldb*sizeof(floatType))%32 == 0;
floatType *Btmp = B;
size_t ldb_tmp = ldb;
floatType buffer[blockingA * blockingB];// __attribute__((aligned(64)));
if( (useStreamingStores_ && useStreamingStores) ){
Btmp = buffer;
ldb_tmp = blockingB;
}
if( blockingA == blocking_ && blockingB == blocking_ )
{
if( !(useStreamingStores_ && useStreamingStores) )
prefetch<floatType>(Bnext + (0 * ldb_tmp + 0), ldb_tmp);
prefetch<floatType>(Anext + (0 * lda + 0), lda);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (0 * lda + 0), lda, Btmp + (0 * ldb_tmp + 0), ldb_tmp , alpha , beta);
prefetch<floatType>(Anext + (blocking_micro_ * lda + 0), lda);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (blocking_micro_ * lda + 0), lda, Btmp + (0 * ldb_tmp + blocking_micro_), ldb_tmp , alpha , beta);
if( !(useStreamingStores_ && useStreamingStores) )
prefetch<floatType>(Bnext + (0 * ldb_tmp + 2*blocking_micro_), ldb_tmp);
prefetch<floatType>(Anext + (2*blocking_micro_ * lda + 0), lda);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (2*blocking_micro_ * lda + 0), lda, Btmp + (0 * ldb_tmp + 2*blocking_micro_), ldb_tmp , alpha , beta);
prefetch<floatType>(Anext + (3*blocking_micro_ * lda + 0), lda);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (3*blocking_micro_ * lda + 0), lda, Btmp + (0 * ldb_tmp + 3*blocking_micro_), ldb_tmp , alpha , beta);
if( !(useStreamingStores_ && useStreamingStores) )
prefetch<floatType>(Bnext + (blocking_micro_ * ldb_tmp + 0), ldb_tmp);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (0 * lda + blocking_micro_), lda, Btmp + (blocking_micro_ * ldb_tmp + 0), ldb_tmp , alpha , beta);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (blocking_micro_ * lda + blocking_micro_), lda, Btmp + (blocking_micro_ * ldb_tmp + blocking_micro_), ldb_tmp , alpha , beta);
if( !(useStreamingStores_ && useStreamingStores) )
prefetch<floatType>(Bnext + (blocking_micro_ * ldb_tmp + 2*blocking_micro_), ldb_tmp);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (2*blocking_micro_ * lda + blocking_micro_), lda, Btmp + (blocking_micro_ * ldb_tmp + 2*blocking_micro_), ldb_tmp , alpha , beta);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (3*blocking_micro_ * lda + blocking_micro_), lda, Btmp + (blocking_micro_ * ldb_tmp + 3*blocking_micro_), ldb_tmp , alpha , beta);
if( !(useStreamingStores_ && useStreamingStores) )
prefetch<floatType>(Bnext + (2*blocking_micro_ * ldb_tmp + 0), ldb_tmp);
prefetch<floatType>(Anext + (0 * lda + 2*blocking_micro_), lda);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (0 * lda + 2*blocking_micro_), lda, Btmp + (2*blocking_micro_ * ldb_tmp + 0), ldb_tmp , alpha , beta);
prefetch<floatType>(Anext + (blocking_micro_ * lda + 2*blocking_micro_), lda);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (blocking_micro_ * lda + 2*blocking_micro_), lda, Btmp + (2*blocking_micro_ * ldb_tmp + blocking_micro_), ldb_tmp , alpha , beta);
if( !(useStreamingStores_ && useStreamingStores) )
prefetch<floatType>(Bnext + (2*blocking_micro_ * ldb_tmp + 2*blocking_micro_), ldb_tmp);
prefetch<floatType>(Anext + (2*blocking_micro_ * lda + 2*blocking_micro_), lda);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (2*blocking_micro_ * lda + 2*blocking_micro_), lda, Btmp + (2*blocking_micro_ * ldb_tmp + 2*blocking_micro_), ldb_tmp , alpha , beta);
prefetch<floatType>(Anext + (3*blocking_micro_ * lda + 2*blocking_micro_), lda);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (3*blocking_micro_ * lda + 2*blocking_micro_), lda, Btmp + (2*blocking_micro_ * ldb_tmp + 3*blocking_micro_), ldb_tmp , alpha , beta);
if( !(useStreamingStores_ && useStreamingStores) )
prefetch<floatType>(Bnext + (3*blocking_micro_ * ldb_tmp + 0), ldb_tmp);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (0 * lda + 3*blocking_micro_), lda, Btmp + (3*blocking_micro_ * ldb_tmp + 0), ldb_tmp , alpha , beta);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (blocking_micro_ * lda + 3*blocking_micro_), lda, Btmp + (3*blocking_micro_ * ldb_tmp + blocking_micro_), ldb_tmp , alpha , beta);
if( !(useStreamingStores_ && useStreamingStores) )
prefetch<floatType>(Bnext + (3*blocking_micro_ * ldb_tmp + 2*blocking_micro_), ldb_tmp);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (2*blocking_micro_ * lda + 3*blocking_micro_), lda, Btmp + (3*blocking_micro_ * ldb_tmp + 2*blocking_micro_), ldb_tmp , alpha , beta);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (3*blocking_micro_ * lda + 3*blocking_micro_), lda, Btmp + (3*blocking_micro_ * ldb_tmp + 3*blocking_micro_), ldb_tmp , alpha , beta);
}else if( blockingA == 2*blocking_micro_ && blockingB == blocking_ ) {
if( !(useStreamingStores_ && useStreamingStores) )
prefetch<floatType>(Bnext + (0 * ldb_tmp + 0), ldb_tmp);
prefetch<floatType>(Anext + (0 * lda + 0), lda);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (0 * lda + 0), lda, Btmp + (0 * ldb_tmp + 0), ldb_tmp , alpha , beta);
prefetch<floatType>(Anext + (blocking_micro_ * lda + 0), lda);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (blocking_micro_ * lda + 0), lda, Btmp + (0 * ldb_tmp + blocking_micro_), ldb_tmp , alpha , beta);
if( !(useStreamingStores_ && useStreamingStores) )
prefetch<floatType>(Bnext + (0 * ldb_tmp + 2*blocking_micro_), ldb_tmp);
prefetch<floatType>(Anext + (2*blocking_micro_ * lda + 0), lda);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (2*blocking_micro_ * lda + 0), lda, Btmp + (0 * ldb_tmp + 2*blocking_micro_), ldb_tmp , alpha , beta);
prefetch<floatType>(Anext + (3*blocking_micro_ * lda + 0), lda);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (3*blocking_micro_ * lda + 0), lda, Btmp + (0 * ldb_tmp + 3*blocking_micro_), ldb_tmp , alpha , beta);
if( !(useStreamingStores_ && useStreamingStores) )
prefetch<floatType>(Bnext + (blocking_micro_ * ldb_tmp + 0), ldb_tmp);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (0 * lda + blocking_micro_), lda, Btmp + (blocking_micro_ * ldb_tmp + 0), ldb_tmp , alpha , beta);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (blocking_micro_ * lda + blocking_micro_), lda, Btmp + (blocking_micro_ * ldb_tmp + blocking_micro_), ldb_tmp , alpha , beta);
if( !(useStreamingStores_ && useStreamingStores) )
prefetch<floatType>(Bnext + (blocking_micro_ * ldb_tmp + 2*blocking_micro_), ldb_tmp);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (2*blocking_micro_ * lda + blocking_micro_), lda, Btmp + (blocking_micro_ * ldb_tmp + 2*blocking_micro_), ldb_tmp , alpha , beta);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (3*blocking_micro_ * lda + blocking_micro_), lda, Btmp + (blocking_micro_ * ldb_tmp + 3*blocking_micro_), ldb_tmp , alpha , beta);
}else if( blockingA == blocking_ && blockingB == 2*blocking_micro_) {
if( !(useStreamingStores_ && useStreamingStores) )
prefetch<floatType>(Bnext + (0 * ldb_tmp + 0), ldb_tmp);
prefetch<floatType>(Anext + (0 * lda + 0), lda);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (0 * lda + 0), lda, Btmp + (0 * ldb_tmp + 0), ldb_tmp , alpha , beta);
prefetch<floatType>(Anext + (blocking_micro_ * lda + 0), lda);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (blocking_micro_ * lda + 0), lda, Btmp + (0 * ldb_tmp + blocking_micro_), ldb_tmp , alpha , beta);
if( !(useStreamingStores_ && useStreamingStores) )
prefetch<floatType>(Bnext + (blocking_micro_ * ldb_tmp + 0), ldb_tmp);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (0 * lda + blocking_micro_), lda, Btmp + (blocking_micro_ * ldb_tmp + 0), ldb_tmp , alpha , beta);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (blocking_micro_ * lda + blocking_micro_), lda, Btmp + (blocking_micro_ * ldb_tmp + blocking_micro_), ldb_tmp , alpha , beta);
if( !(useStreamingStores_ && useStreamingStores) )
prefetch<floatType>(Bnext + (2*blocking_micro_ * ldb_tmp + 0), ldb_tmp);
prefetch<floatType>(Anext + (0 * lda + 2*blocking_micro_), lda);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (0 * lda + 2*blocking_micro_), lda, Btmp + (2*blocking_micro_ * ldb_tmp + 0), ldb_tmp , alpha , beta);
prefetch<floatType>(Anext + (blocking_micro_ * lda + 2*blocking_micro_), lda);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (blocking_micro_ * lda + 2*blocking_micro_), lda, Btmp + (2*blocking_micro_ * ldb_tmp + blocking_micro_), ldb_tmp , alpha , beta);
if( !(useStreamingStores_ && useStreamingStores) )
prefetch<floatType>(Bnext + (3*blocking_micro_ * ldb_tmp + 0), ldb_tmp);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (0 * lda + 3*blocking_micro_), lda, Btmp + (3*blocking_micro_ * ldb_tmp + 0), ldb_tmp , alpha , beta);
micro_kernel<floatType,betaIsZero, conjA>::execute(A + (blocking_micro_ * lda + 3*blocking_micro_), lda, Btmp + (3*blocking_micro_ * ldb_tmp + blocking_micro_), ldb_tmp , alpha , beta);
} else {
//invoke micro-transpose
if(blockingA > 0 && blockingB > 0 )
micro_kernel<floatType,betaIsZero, conjA>::execute(A, lda, Btmp, ldb_tmp , alpha , beta);
//invoke micro-transpose
if(blockingA > 0 && blockingB > blocking_micro_ )
micro_kernel<floatType,betaIsZero, conjA>::execute(A + blocking_micro_ * lda, lda, Btmp + blocking_micro_, ldb_tmp , alpha , beta);
//invoke micro-transpose
if(blockingA > 0 && blockingB > 2*blocking_micro_ )
micro_kernel<floatType,betaIsZero, conjA>::execute(A + 2*blocking_micro_ * lda, lda, Btmp + 2*blocking_micro_, ldb_tmp , alpha , beta);
//invoke micro-transpose
if(blockingA > 0 && blockingB > 3*blocking_micro_ )
micro_kernel<floatType,betaIsZero, conjA>::execute(A + 3*blocking_micro_ * lda, lda, Btmp + 3*blocking_micro_, ldb_tmp , alpha , beta);
//invoke micro-transpose
if(blockingA > blocking_micro_ && blockingB > 0 )
micro_kernel<floatType,betaIsZero, conjA>::execute(A + blocking_micro_, lda, Btmp + blocking_micro_ * ldb_tmp, ldb_tmp , alpha , beta);
//invoke micro-transpose
if(blockingA > blocking_micro_ && blockingB > blocking_micro_ )
micro_kernel<floatType,betaIsZero, conjA>::execute(A + blocking_micro_ + blocking_micro_ * lda, lda, Btmp + blocking_micro_ + blocking_micro_ * ldb_tmp, ldb_tmp , alpha , beta);
//invoke micro-transpose
if(blockingA > blocking_micro_ && blockingB > 2*blocking_micro_ )
micro_kernel<floatType,betaIsZero, conjA>::execute(A + blocking_micro_ + 2*blocking_micro_ * lda, lda, Btmp + 2*blocking_micro_ + blocking_micro_ * ldb_tmp, ldb_tmp , alpha , beta);
//invoke micro-transpose
if(blockingA > blocking_micro_ && blockingB > 3*blocking_micro_ )
micro_kernel<floatType,betaIsZero, conjA>::execute(A + blocking_micro_ + 3*blocking_micro_ * lda, lda, Btmp + 3*blocking_micro_ + blocking_micro_ * ldb_tmp, ldb_tmp , alpha , beta);
//invoke micro-transpose
if(blockingA > 2*blocking_micro_ && blockingB > 0 )
micro_kernel<floatType,betaIsZero, conjA>::execute(A + 2*blocking_micro_, lda, Btmp + 2*blocking_micro_ * ldb_tmp, ldb_tmp , alpha , beta);
//invoke micro-transpose
if(blockingA > 2*blocking_micro_ && blockingB > blocking_micro_ )
micro_kernel<floatType,betaIsZero, conjA>::execute(A + 2*blocking_micro_ + blocking_micro_ * lda, lda, Btmp + blocking_micro_ + 2*blocking_micro_ * ldb_tmp, ldb_tmp , alpha , beta);
//invoke micro-transpose
if(blockingA > 2*blocking_micro_ && blockingB > 2*blocking_micro_ )
micro_kernel<floatType,betaIsZero, conjA>::execute(A + 2*blocking_micro_ + 2*blocking_micro_ * lda, lda, Btmp + 2*blocking_micro_ + 2*blocking_micro_ * ldb_tmp, ldb_tmp , alpha , beta);
//invoke micro-transpose
if(blockingA > 2*blocking_micro_ && blockingB > 3*blocking_micro_ )
micro_kernel<floatType,betaIsZero, conjA>::execute(A + 2*blocking_micro_ + 3*blocking_micro_ * lda, lda, Btmp + 3*blocking_micro_ + 2*blocking_micro_ * ldb_tmp, ldb_tmp , alpha , beta);
//invoke micro-transpose
if(blockingA > 3*blocking_micro_ && blockingB > 0 )
micro_kernel<floatType,betaIsZero, conjA>::execute(A + 3*blocking_micro_, lda, Btmp + 3*blocking_micro_ * ldb_tmp, ldb_tmp , alpha , beta);
//invoke micro-transpose
if(blockingA > 3*blocking_micro_ && blockingB > blocking_micro_ )
micro_kernel<floatType,betaIsZero, conjA>::execute(A + 3*blocking_micro_ + blocking_micro_ * lda, lda, Btmp + blocking_micro_ + 3*blocking_micro_ * ldb_tmp, ldb_tmp , alpha , beta);
//invoke micro-transpose
if(blockingA > 3*blocking_micro_ && blockingB > 2*blocking_micro_ )
micro_kernel<floatType,betaIsZero, conjA>::execute(A + 3*blocking_micro_ + 2*blocking_micro_ * lda, lda, Btmp + 2*blocking_micro_ + 3*blocking_micro_ * ldb_tmp, ldb_tmp , alpha , beta);
//invoke micro-transpose
if(blockingA > 3*blocking_micro_ && blockingB > 3*blocking_micro_ )
micro_kernel<floatType,betaIsZero, conjA>::execute(A + 3*blocking_micro_ + 3*blocking_micro_ * lda, lda, Btmp + 3*blocking_micro_ + 3*blocking_micro_ * ldb_tmp, ldb_tmp , alpha , beta);
}
// write buffer to main-memory via non-temporal stores
if( (useStreamingStores_ && useStreamingStores) )
for( int i = 0; i < blockingA; i++){
for( int j = 0; j < blockingB; j+=blocking_micro_)
streamingStore<floatType>(B + i * ldb + j, buffer + i * ldb_tmp + j);
}
}
template<int betaIsZero, typename floatType, bool conjA>
void transpose_int_scalar( const floatType* __restrict__ A, int sizeStride1A,
floatType* __restrict__ B, int sizeStride1B, const floatType alpha, const floatType beta, const ComputeNode* plan)
{
const int32_t end = plan->end;
const size_t lda = plan->lda;
const size_t ldb = plan->ldb;
if( plan->next->next != nullptr ){
// recurse
int i = plan->start;
if( lda == 1)
transpose_int_scalar<betaIsZero, floatType, conjA>( &A[i*lda], end - plan->start, &B[i*ldb], sizeStride1B, alpha, beta, plan->next);
else if( ldb == 1)
transpose_int_scalar<betaIsZero, floatType, conjA>( &A[i*lda], sizeStride1A, &B[i*ldb], end - plan->start, alpha, beta, plan->next);
else
for(; i < end; i++)
transpose_int_scalar<betaIsZero, floatType, conjA>( &A[i*lda], sizeStride1A, &B[i*ldb], sizeStride1B, alpha, beta, plan->next);
}else{
// macro-kernel
const size_t lda_macro = plan->next->lda;
const size_t ldb_macro = plan->next->ldb;
int i = plan->start;
const size_t scalarRemainder = plan->end - plan->start;
if( scalarRemainder > 0 ){
if( lda == 1)
macro_kernel_scalar<betaIsZero,floatType, conjA>(&A[i*lda], lda_macro, scalarRemainder, &B[i*ldb], ldb_macro, sizeStride1B, alpha, beta);
else if( ldb == 1)
macro_kernel_scalar<betaIsZero,floatType, conjA>(&A[i*lda], lda_macro, sizeStride1A, &B[i*ldb], ldb_macro, scalarRemainder, alpha, beta);
}
}
}
template<int blockingA, int blockingB, int betaIsZero, typename floatType, bool useStreamingStores, bool conjA>
void transpose_int( const floatType* __restrict__ A, const floatType* __restrict__ Anext,
floatType* __restrict__ B, const floatType* __restrict__ Bnext, const floatType alpha, const floatType beta,
const ComputeNode* plan)
{
const int32_t end = plan->end - (plan->inc - 1);
const int32_t inc = plan->inc;
const size_t lda = plan->lda;
const size_t ldb = plan->ldb;
constexpr int blocking_micro_ = REGISTER_BITS/8 / sizeof(floatType);
constexpr int blocking_ = blocking_micro_ * 4;
if( plan->next->next != nullptr ){
// recurse
int32_t i;
for(i = plan->start; i < end; i+= inc)
{
if( i + inc < end )
transpose_int<blockingA, blockingB, betaIsZero, floatType, useStreamingStores, conjA>( &A[i*lda], &A[(i+1)*lda], &B[i*ldb], &B[(i+1)*ldb], alpha, beta, plan->next);
else
transpose_int<blockingA, blockingB, betaIsZero, floatType, useStreamingStores, conjA>( &A[i*lda], Anext, &B[i*ldb], Bnext, alpha, beta, plan->next);
}
// remainder
if( blocking_/2 >= blocking_micro_ && (i + blocking_/2) <= plan->end ){
if( lda == 1)
transpose_int<blocking_/2, blockingB, betaIsZero, floatType, useStreamingStores, conjA>( &A[i*lda], Anext, &B[i*ldb], Bnext, alpha, beta, plan->next);
else if( ldb == 1)
transpose_int<blockingA, blocking_/2, betaIsZero, floatType, useStreamingStores, conjA>( &A[i*lda], Anext, &B[i*ldb], Bnext, alpha, beta, plan->next);
i+=blocking_/2;
}
if( blocking_/4 >= blocking_micro_ && (i + blocking_/4) <= plan->end ){
if( lda == 1)
transpose_int<blocking_/4, blockingB, betaIsZero, floatType, useStreamingStores, conjA>( &A[i*lda], Anext, &B[i*ldb], Bnext, alpha, beta, plan->next);
else if( ldb == 1)
transpose_int<blockingA, blocking_/4, betaIsZero, floatType, useStreamingStores, conjA>( &A[i*lda], Anext, &B[i*ldb], Bnext, alpha, beta, plan->next);
i+=blocking_/4;
}
const size_t scalarRemainder = plan->end - i;
if( scalarRemainder > 0 ){
if( lda == 1)
transpose_int_scalar<betaIsZero, floatType, conjA>( &A[i*lda], scalarRemainder, &B[i*ldb], -1, alpha, beta, plan->next);
else
transpose_int_scalar<betaIsZero, floatType, conjA>( &A[i*lda], -1, &B[i*ldb], scalarRemainder, alpha, beta, plan->next);
}
} else {
const size_t lda_macro = plan->next->lda;
const size_t ldb_macro = plan->next->ldb;
// invoke macro-kernel
int32_t i;
for(i = plan->start; i < end; i+= inc)
if( i + inc < end )
macro_kernel<blockingA, blockingB, betaIsZero,floatType, useStreamingStores, conjA>(&A[i*lda], &A[(i+1)*lda], lda_macro, &B[i*ldb], &B[(i+1)*ldb], ldb_macro, alpha, beta);
else
macro_kernel<blockingA, blockingB, betaIsZero,floatType, useStreamingStores, conjA>(&A[i*lda], Anext, lda_macro, &B[i*ldb], Bnext, ldb_macro, alpha, beta);
// remainder
if( blocking_/2 >= blocking_micro_ && (i + blocking_/2) <= plan->end ){
if( lda == 1)
macro_kernel<blocking_/2, blockingB, betaIsZero,floatType, useStreamingStores, conjA>(&A[i*lda], Anext, lda_macro, &B[i*ldb], Bnext, ldb_macro, alpha, beta);
else if( ldb == 1)
macro_kernel<blockingA, blocking_/2, betaIsZero,floatType, useStreamingStores, conjA>(&A[i*lda], Anext, lda_macro, &B[i*ldb], Bnext, ldb_macro, alpha, beta);
i+=blocking_/2;
}
if( blocking_/4 >= blocking_micro_ && (i + blocking_/4) <= plan->end ){
if( lda == 1)
macro_kernel<blocking_/4, blockingB, betaIsZero,floatType, useStreamingStores, conjA>(&A[i*lda], Anext, lda_macro, &B[i*ldb], Bnext, ldb_macro, alpha, beta);
else if( ldb == 1)
macro_kernel<blockingA, blocking_/4, betaIsZero,floatType, useStreamingStores, conjA>(&A[i*lda], Anext, lda_macro, &B[i*ldb], Bnext, ldb_macro, alpha, beta);
i+=blocking_/4;
}
const size_t scalarRemainder = plan->end - i;
if( scalarRemainder > 0 ){
if( lda == 1)
macro_kernel_scalar<betaIsZero,floatType, conjA>(&A[i*lda], lda_macro, scalarRemainder, &B[i*ldb], ldb_macro, blockingB, alpha, beta);
else if( ldb == 1)
macro_kernel_scalar<betaIsZero,floatType, conjA>(&A[i*lda], lda_macro, blockingA, &B[i*ldb], ldb_macro, scalarRemainder, alpha, beta);
}
}
}
template<int betaIsZero, typename floatType, bool useStreamingStores, bool conjA>
void transpose_int_constStride1( const floatType* __restrict__ A, floatType* __restrict__ B, const floatType alpha, const floatType beta,
const ComputeNode* plan)
{
const int32_t end = plan->end - (plan->inc - 1);
constexpr int32_t inc = 1; // TODO
const size_t lda = plan->lda;
const size_t ldb = plan->ldb;
if( plan->next != nullptr )
for(int i = plan->start; i < end; i+= inc)
// recurse
transpose_int_constStride1<betaIsZero, floatType, useStreamingStores, conjA>( &A[i*lda], &B[i*ldb], alpha, beta, plan->next);
else
if( !betaIsZero )
{
for(int32_t i = plan->start; i < end; i+= inc)
if( conjA )
B[i] = alpha * conj(A[i]) + beta * B[i];
else
B[i] = alpha * A[i] + beta * B[i];
} else {
if( useStreamingStores)
if( conjA )
#pragma vector nontemporal
for(int32_t i = plan->start; i < end; i+= inc)
B[i] = alpha * conj(A[i]);
else
#pragma vector nontemporal
for(int32_t i = plan->start; i < end; i+= inc)
B[i] = alpha * A[i];
else
if( conjA )
for(int32_t i = plan->start; i < end; i+= inc)
B[i] = alpha * conj(A[i]);
else
for(int32_t i = plan->start; i < end; i+= inc)
B[i] = alpha * A[i];
}
}
template<typename floatType>
Transpose<floatType>::Transpose( const int *sizeA,
const int *perm,
const int *outerSizeA,
const int *outerSizeB,
const int dim,
const floatType *A,
const floatType alpha,
floatType *B,
const floatType beta,
const SelectionMethod selectionMethod,
const int numThreads,
const int *threadIds,
const bool useRowMajor) :
A_(A),
B_(B),
alpha_(alpha),
beta_(beta),
dim_(-1),
numThreads_(numThreads),
masterPlan_(nullptr),
selectionMethod_(selectionMethod),
maxAutotuningCandidates_(-1),
selectedParallelStrategyId_(-1),
selectedLoopOrderId_(-1),
conjA_(false)
{
#ifdef _OPENMP
omp_init_lock(&writelock);
#endif
int tmpPerm[dim];
int tmpSizeA[dim];
int tmpOuterSizeA[dim];
int tmpOuterSizeB[dim];
accountForRowMajor(sizeA, outerSizeA, outerSizeB, perm,
tmpSizeA, tmpOuterSizeA, tmpOuterSizeB, tmpPerm, dim, useRowMajor);
sizeA_.resize(dim);
perm_.resize(dim);
outerSizeA_.resize(dim);
outerSizeB_.resize(dim);
lda_.resize(dim);
ldb_.resize(dim);
if(threadIds){
// compact threadIds. E.g., 1, 7, 5 -> local_id(1) = 0, local_id(7) = 2, local_id(5) = 1
for(int i=0; i < numThreads; ++i)
threadIds_.push_back(threadIds[i]);
std::sort(threadIds_.begin(), threadIds_.end());
}else{
for(int i=0; i < numThreads; ++i)
threadIds_.push_back(i);
}
verifyParameter(tmpSizeA, tmpPerm, tmpOuterSizeA, tmpOuterSizeB, dim);
// initializes dim_, outerSizeA, outerSizeB, sizeA and perm
skipIndices(tmpSizeA, tmpPerm, tmpOuterSizeA, tmpOuterSizeB, dim);
fuseIndices();
// initializes lda_ and ldb_
computeLeadingDimensions();
// create plan
this->createPlan();
}
template<typename floatType>
Transpose<floatType>::Transpose(const Transpose<floatType> &other) : A_(other.A_), B_(other.B_),
alpha_(other.alpha_),
beta_(other.beta_),
dim_(other.dim_),
numThreads_(other.numThreads_),
masterPlan_(other.masterPlan_),
selectionMethod_(other.selectionMethod_),
selectedParallelStrategyId_(other.selectedParallelStrategyId_),
selectedLoopOrderId_(other.selectedLoopOrderId_),
maxAutotuningCandidates_(other.maxAutotuningCandidates_),
sizeA_(other.sizeA_),
perm_(other.perm_),
outerSizeA_(other.outerSizeA_),
outerSizeB_(other.outerSizeB_),
lda_(other.lda_),
ldb_(other.ldb_),
threadIds_(other.threadIds_),
conjA_(other.conjA_)
{
#ifdef _OPENMP
omp_init_lock(&writelock);
#endif
}
template<typename floatType>
Transpose<floatType>::~Transpose() {
#ifdef _OPENMP
omp_destroy_lock(&writelock);
#endif
}
template<typename floatType>
void Transpose<floatType>::executeEstimate(const Plan *plan) noexcept
{
if( plan == nullptr ) {
fprintf(stderr,"[HPTT] ERROR: plan has not yet been created.\n");
exit(-1);
}
constexpr bool useStreamingStores = false;
const int numTasks = plan->getNumTasks();
#ifdef _OPENMP
#pragma omp parallel for num_threads(numThreads_) if(numThreads_ > 1)
#endif
for( int taskId = 0; taskId < numTasks; taskId++)
if ( perm_[0] != 0 ) {
auto rootNode = plan->getRootNode_const( taskId );
if( std::abs(beta_) < getZeroThreshold<floatType>() ) {
if( conjA_ )
transpose_int<blocking_,blocking_,1,floatType, useStreamingStores, true>( A_,A_, B_, B_, 0.0, 1.0, rootNode);
else
transpose_int<blocking_,blocking_,1,floatType, useStreamingStores, false>( A_,A_, B_, B_, 0.0, 1.0, rootNode);
} else {
if( conjA_ )
transpose_int<blocking_,blocking_,0,floatType, useStreamingStores, true>( A_,A_, B_, B_, 0.0, 1.0, rootNode);
else
transpose_int<blocking_,blocking_,0,floatType, useStreamingStores, false>( A_,A_, B_, B_, 0.0, 1.0, rootNode);
}
} else {
auto rootNode = plan->getRootNode_const( taskId );
if( std::abs(beta_) < getZeroThreshold<floatType>() ) {
if( conjA_ )
transpose_int_constStride1<1,floatType, useStreamingStores, true>( A_, B_, 0.0, 1.0, rootNode);
else
transpose_int_constStride1<1,floatType, useStreamingStores, false>( A_, B_, 0.0, 1.0, rootNode);
}else{
if( conjA_ )
transpose_int_constStride1<0,floatType, useStreamingStores, true>( A_, B_, 0.0, 1.0, rootNode);
else
transpose_int_constStride1<0,floatType, useStreamingStores, false>( A_, B_, 0.0, 1.0, rootNode);
}
}
}
template<int betaIsZero, typename floatType, bool useStreamingStores, bool spawnThreads, bool conjA>
static void axpy_1D( const floatType* __restrict__ A, floatType* __restrict__ B, const int myStart, const int myEnd, const floatType alpha, const floatType beta, int numThreads)
{
if( !betaIsZero )
{
HPTT_DUPLICATE(spawnThreads,
for(int32_t i = myStart; i < myEnd; i++)
if( conjA )
B[i] = alpha * conj(A[i]) + beta * B[i];
else
B[i] = alpha * A[i] + beta * B[i];
)
} else {
if( useStreamingStores)
#pragma vector nontemporal
HPTT_DUPLICATE(spawnThreads,
for(int32_t i = myStart; i < myEnd; i++)
if( conjA )
B[i] = alpha * conj(A[i]);
else
B[i] = alpha * A[i];
)
else
HPTT_DUPLICATE(spawnThreads,
for(int32_t i = myStart; i < myEnd; i++)
if( conjA )
B[i] = alpha * conj(A[i]);
else
B[i] = alpha * A[i];
)
}
}
template<int betaIsZero, typename floatType, bool useStreamingStores, bool spawnThreads, bool conjA>
static void axpy_2D( const floatType* __restrict__ A, const int lda,
floatType* __restrict__ B, const int ldb,
const int n0, const int myStart, const int myEnd, const floatType alpha, const floatType beta, int numThreads)
{
if( !betaIsZero )
{
HPTT_DUPLICATE(spawnThreads,
for(int32_t j = myStart; j < myEnd; j++)
for(int32_t i = 0; i < n0; i++)
if( conjA )
B[i + j * ldb] = alpha * conj(A[i + j * lda]) + beta * B[i + j * ldb];
else
B[i + j * ldb] = alpha * A[i + j * lda] + beta * B[i + j * ldb];
)
} else {
if( useStreamingStores)
HPTT_DUPLICATE(spawnThreads,
for(int32_t j = myStart; j < myEnd; j++)
_Pragma("vector nontemporal")
for(int32_t i = 0; i < n0; i++)
if( conjA )
B[i + j * ldb] = alpha * conj(A[i + j * lda]);
else
B[i + j * ldb] = alpha * A[i + j * lda];
)
else
HPTT_DUPLICATE(spawnThreads,
for(int32_t j = myStart; j < myEnd; j++)
for(int32_t i = 0; i < n0; i++)
if( conjA )
B[i + j * ldb] = alpha * conj(A[i + j * lda]);
else
B[i + j * ldb] = alpha * A[i + j * lda];
)
}
}
template<typename floatType>
template<bool spawnThreads>
void Transpose<floatType>::getStartEnd(int n, int &myStart, int &myEnd) const
{
#ifdef _OPENMP
int myLocalThreadId = getLocalThreadId(omp_get_thread_num());
#else
int myLocalThreadId = 0;
#endif
if(myLocalThreadId == -1 ) // skip those threads which do not participate in this plan
{
myStart = n;
myEnd = n;
return;
}
if(spawnThreads){ // worksharing will be handled by the OpenMP runtime
myStart = 0;
myEnd = n;
return;
}
const int workPerThread = (n + numThreads_-1)/numThreads_;
myStart = std::min(n, myLocalThreadId * workPerThread);
myEnd = std::min(n, (myLocalThreadId+1) * workPerThread);
}
template<typename floatType>
int Transpose<floatType>::getLocalThreadId(int myThreadId) const
{
int myLocalId = -1;
for(int i=0; i < numThreads_; ++i)
if(myThreadId == threadIds_[i])
myLocalId = i;
return myLocalId;
}
template<typename floatType>
template<bool useStreamingStores, bool spawnThreads, bool betaIsZero>
void Transpose<floatType>::execute_expert() noexcept
{
if( masterPlan_ == nullptr ) {
fprintf(stderr,"[HPTT] ERROR: master plan has not yet been created.\n");
exit(-1);
}
int myStart = 0;
int myEnd = 0;
if( dim_ == 1)
{
getStartEnd<spawnThreads>(sizeA_[0], myStart, myEnd);
if( conjA_ )
axpy_1D<betaIsZero, floatType, useStreamingStores, spawnThreads, true>( A_, B_, myStart, myEnd, alpha_, beta_, numThreads_ );
else
axpy_1D<betaIsZero, floatType, useStreamingStores, spawnThreads, false>( A_, B_, myStart, myEnd, alpha_, beta_, numThreads_ );
return;
}
else if( dim_ == 2 && perm_[0] == 0)
{