-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslave.c
4343 lines (3908 loc) · 186 KB
/
slave.c
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
#include <crts.h>
#include <simd.h>
#include <slave.h>
#include <stdio.h>
#include <stdlib.h>
#include "args.h"
__cross void *para_cross = NULL;
__cross void *para_cross_1 = NULL;
__cross void *para_cross_2 = NULL;
#define thread_num (64 * 6)
#define sgemm_8_8_8(A, B, C, K, N)\
({\
asm volatile(\
"vlds $32, 0(%0)\n"/*A0*/\
"s4addl %3, %0, $1\n"/*&A1 at $1*/\
"vlds $40, 0(%1)\n"/*B0*/\
"s8addl %4, %2, $5\n"/*&C1 at $5*/\
"vldd $42, 0(%2)\n"/*C0*/\
"s4addl %3, $1, $2\n"/*&A2 at $2*/\
"vlds $33, 0($1)\n"/*A1*/\
"s8addl %4, $5, $6\n"/*&C2 at $6*/\
"vldd $43, 0($5)\n"/*C1*/\
"s4addl %3, $2, $3\n"/*&A3 at $3*/\
"vlds $34, 0($2)\n"/*A2*/\
"s8addl %4, $6, $7\n"/*&C3 at $7*/\
"vldd $44, 0($6)\n"/*C2*/\
"s4addl %3, $3, $4\n"/*&A4 at $4*/\
"vlds $35, 0($3)\n"/*A3*/\
"s8addl %4, $7, $8\n"/*&C4 at $8*/\
"ldi $9,7($31)\n" /*get interval*/\
"s4addl %3, $4, $1\n"/*&A5 at $1*/\
"wcsr $9, 0x92\n" /*set interval*/\
"s8addl %4, $8, $5\n"/*&C5 at $5*/\
"vldd $45, 0($7)\n"/*C3*/\
"s4addl %3, $1, $2\n"/*&A6 at $2*/\
"vlds $36, 0($4)\n"/*A4*/\
"s8addl %4, $5, $6\n"/*&C6 at $6*/\
"vldd $46, 0($8)\n"/*C4*/\
"s4addl %3, $2, $3\n"/*&A7 at $3*/\
"vlds $37, 0($1)\n"/*A5*/\
"s8addl %4, $6, $7\n"/*&C7 at $7*/\
"vldd $47, 0($5)\n"/*C5*/\
"vlenma $32, $40, $42, $42\n"\
"vlds $38, 0($2)\n"/*A6*/\
"vlenma $33, $40, $43, $43\n"\
"vldd $48, 0($6)\n"/*C6*/\
"vlenma $34, $40, $44, $44\n"\
"vlds $39, 0($3)\n"/*A7*/\
"vlenma $35, $40, $45, $45\n"\
"vldd $49, 0($7)\n"/*C7*/\
"vlenma $36, $40, $46, $46\n"\
"s4addl %4, %1, $9\n"/*&B1 at $9*/\
"vlenma $37, $40, $47, $47\n"\
"vlds $41, 0($9)\n"/*B1*/\
"vlenma $38, $40, $48, $48\n"\
"s4addl %4, $9, $10\n"/*&B2 at $10*/\
"vlenma $39, $40, $49, $49\n"\
"ldi $1,0(%2)\n" /*get CSR_VLENMAS_STADDR*/\
"vlenma $32, $41, $42, $42\n"\
"vlds $40, 0($10)\n"/*B2*/\
"vlenma $33, $41, $43, $43\n"\
"wcsr $1, 0x90\n" /*set CSR_VLENMAS_STADDR*/\
"vlenma $34, $41, $44, $44\n"\
"s8addl %4, $31, $2\n"\
"vlenma $35, $41, $45, $45\n"\
"wcsr $2, 0x91\n"\
"vlenma $36, $41, $46, $46\n"\
"s4addl %4, $10, $9\n"/*&B3 at $9*/\
"vlenma $37, $41, $47, $47\n"\
"vlenma $38, $41, $48, $48\n"\
"s4addl %4, $9, $10\n"/*&B4 at $10*/\
"vlenma $39, $41, $49, $49\n"\
"vlenma $32, $40, $42, $42\n"\
"vlds $41, 0($9)\n"/*B3*/\
"vlenma $33, $40, $43, $43\n"\
"s4addl %4, $10, $9\n"/*&B5 at $9*/\
"vlenma $34, $40, $44, $44\n"\
"vlenma $35, $40, $45, $45\n"\
"vlenma $36, $40, $46, $46\n"\
"vlenma $37, $40, $47, $47\n"\
"vlenma $38, $40, $48, $48\n"\
"vlenma $39, $40, $49, $49\n"\
"vlenma $32, $41, $42, $42\n"\
"vlds $40, 0($10)\n"/*B4*/\
"vlenma $33, $41, $43, $43\n"\
"s4addl %4, $9, $10\n"/*B6 at $10*/\
"vlenma $34, $41, $44, $44\n"\
"vlenma $35, $41, $45, $45\n"\
"vlenma $36, $41, $46, $46\n"\
"vlenma $37, $41, $47, $47\n"\
"vlenma $38, $41, $48, $48\n"\
"vlenma $39, $41, $49, $49\n"\
"vlenma $32, $40, $42, $42\n"\
"vlds $41, 0($9)\n"/*B5*/\
"vlenma $33, $40, $43, $43\n"\
"s4addl %4, $10, $9\n"/*B7 at $9*/\
"vlenma $34, $40, $44, $44\n"\
"vlenma $35, $40, $45, $45\n"\
"vlenma $36, $40, $46, $46\n"\
"vlenma $37, $40, $47, $47\n"\
"vlenma $38, $40, $48, $48\n"\
"vlenma $39, $40, $49, $49\n"\
"vlenma $32, $41, $42, $42\n"\
"vlds $40, 0($10)\n"/*B6*/\
"vlenma $33, $41, $43, $43\n"\
"vlenma $34, $41, $44, $44\n"\
"vlenma $35, $41, $45, $45\n"\
"vlenma $36, $41, $46, $46\n"\
"vlenma $37, $41, $47, $47\n"\
"vlenma $38, $41, $48, $48\n"\
"vlenma $39, $41, $49, $49\n"\
"vlenma $32, $40, $42, $42\n"\
"vlds $41, 0($9)\n"/*B7*/\
"vlenma $33, $40, $43, $43\n"\
"vlenma $34, $40, $44, $44\n"\
"vlenma $35, $40, $45, $45\n"\
"vlenma $36, $40, $46, $46\n"\
"vlenma $37, $40, $47, $47\n"\
"vlenma $38, $40, $48, $48\n"\
"vlenma $39, $40, $49, $49\n"\
"vlenmas $32, $41, $42, $42\n"\
"vlenmas $33, $41, $43, $43\n"\
"vlenmas $34, $41, $44, $44\n"\
"vlenmas $35, $41, $45, $45\n"\
"vlenmas $36, $41, $46, $46\n"\
"vlenmas $37, $41, $47, $47\n"\
"vlenmas $38, $41, $48, $48\n"\
"vlenmas $39, $41, $49, $49\n"\
:\
:"r"(A),"r"(B),"r"(C),"r"(K),"r"(N)\
:"$32","$33","$34","$35","$36","$37","$38","$39",/*A*/\
"$40","41",/*B*/\
"$42","$43","$44","$45","$46","$47","$48","$49",/*C*/\
"$1","$2","$3","$4","$5","$6","$7","$8","$9","$10",\
"memory");\
0;\
})
#define SWAP_8_8(a1, a2, a3, a4, a5, a6, a7, a8) \
{ \
floatv8 b1 = simd_vshfs(a1, a2, shuffle1); \
floatv8 b2 = simd_vshfs(a1, a2, shuffle2); \
floatv8 b3 = simd_vshfs(a3, a4, shuffle1); \
floatv8 b4 = simd_vshfs(a3, a4, shuffle2); \
floatv8 b5 = simd_vshfs(a5, a6, shuffle1); \
floatv8 b6 = simd_vshfs(a5, a6, shuffle2); \
floatv8 b7 = simd_vshfs(a7, a8, shuffle1); \
floatv8 b8 = simd_vshfs(a7, a8, shuffle2); \
floatv8 c1 = simd_vshfs(b1, b3, shuffle3); \
floatv8 c2 = simd_vshfs(b1, b3, shuffle4); \
floatv8 c3 = simd_vshfs(b2, b4, shuffle3); \
floatv8 c4 = simd_vshfs(b2, b4, shuffle4); \
floatv8 c5 = simd_vshfs(b5, b7, shuffle3); \
floatv8 c6 = simd_vshfs(b5, b7, shuffle4); \
floatv8 c7 = simd_vshfs(b6, b8, shuffle3); \
floatv8 c8 = simd_vshfs(b6, b8, shuffle4); \
a1 = simd_vshfs(c1, c5, shuffle5); \
a2 = simd_vshfs(c1, c5, shuffle6); \
a3 = simd_vshfs(c2, c6, shuffle5); \
a4 = simd_vshfs(c2, c6, shuffle6); \
a5 = simd_vshfs(c3, c7, shuffle5); \
a6 = simd_vshfs(c3, c7, shuffle6); \
a7 = simd_vshfs(c4, c8, shuffle5); \
a8 = simd_vshfs(c4, c8, shuffle6); \
}
inline void sw_slave_gemm_copy_all_back_add(const int ThreadsStart, const int ThreadsEnd,
const int sli_A,
const float* A, const float* Ap,
const int H, const int He,
const int W, const int We){
const int ThreadsNum = ThreadsEnd - ThreadsStart + 1;
const int myid = CRTS_cgn * 64 + CRTS_tid - ThreadsStart;
if(ThreadsNum <= 0){
printf("sw_slave_gemm_copy_all ThreadsNum Error!\n");
return;
}
/* if(H == He && W == We){
printf("sw_slave_gemm_copy_all Don't Need Copy All!\n");
return;
} */
if(sli_A == 0){
printf("sw_slave_gemm_copy_all_back_add sli_A == 0!\n Please use sw_slave_gemm_copy_all_back");
return;
}
if(myid < 0){
//printf("_MYID %d sw_slave_gemm_copy_all myid %d return!\n", CRTS_cgn * 64 + CRTS_tid, myid);
return;
}
const int counts_Q = H;
const int local_count = (counts_Q + ThreadsNum - 1) / ThreadsNum;
const int local_start = myid * local_count;
const int local_end = ((local_start + local_count > counts_Q) ? counts_Q : (local_start + local_count));
const int local_rows = local_end - local_start;
if(local_start >= counts_Q || myid >= ThreadsNum){
return;
}
int blk_Q = (sli_A * local_rows * We * sizeof(float) < 200 * 1024) ? local_rows : 200 * 1024 / (sli_A * We * sizeof(float));
int rem_Q = blk_Q;
if(blk_Q != local_rows){
rem_Q = local_rows % blk_Q;
}
float* local_Q = ldm_malloc(sizeof(float) * sli_A * blk_Q * We);
//printf("myid %d H %d He %d blk_Q %d rem_Q %d local_start %d local_rows %d local_end %d\n", myid, H, He, blk_Q, rem_Q, local_start, local_rows, local_end);
for(int local_now = local_start; local_now < local_end; local_now += blk_Q){
int curr_Q = blk_Q;
if(local_now + blk_Q < H){//right zeros only
curr_Q = local_end - local_now < blk_Q ? rem_Q : blk_Q;
#ifdef _SWOPS_DEBUG
printf("local_now %d blk_Q %d curr_Q %d\n", local_now, blk_Q, curr_Q);
#endif
athread_dma_get_stride(local_Q,
Ap + local_now * We,
sizeof(float) * sli_A * curr_Q * We,
sizeof(float) * curr_Q * We,
sizeof(float) * (He * We - curr_Q * We));
for(int s = 1; s < sli_A; s++){
for(int m = 0; m < curr_Q; m++){
for(int n = 0; n < W; n++){
local_Q[m * We + n] += local_Q[s * curr_Q * We + m * We + n];
}
}
}
for(int m = 0; m < curr_Q; m++){
for(int n = 0; n < W; n++){
local_Q[m * W + n] = local_Q[m * We + n];
}
}
athread_dma_put(A + local_now * W,
local_Q,
sizeof(float) * curr_Q * W);
}
else if(local_now + blk_Q >= H && local_now < H){//right and bottom zeros There are some problems
curr_Q = local_end - local_now < blk_Q ? rem_Q : blk_Q;
int res_Q = H - local_now;
#ifdef _SWOPS_DEBUG
printf("local_now %d blk_Q %d curr_Q %d res_Q %d\n", local_now, blk_Q, curr_Q, res_Q);
#endif
athread_dma_get_stride(local_Q,
Ap + local_now * We,
sizeof(float) * sli_A * res_Q * We,
sizeof(float) * res_Q * We,
sizeof(float) * (He * We - res_Q * We));
for(int s = 1; s < sli_A; s++){
for(int m = 0; m < res_Q; m++){
for(int n = 0; n < W; n++){
local_Q[m * We + n] += local_Q[s * res_Q * We + m * We + n];
}
}
}
for(int m = 0; m < res_Q; m++){
for(int n = 0; n < W; n++){
local_Q[m * W + n] = local_Q[m * We + n];
}
}
athread_dma_put(A + local_now * W,
local_Q,
sizeof(float) * res_Q * W);
}
}
ldm_free(local_Q, sizeof(float) * sli_A * blk_Q * We);
}
inline void sw_slave_gemm_copy_all_back(const int ThreadsStart, const int ThreadsEnd,
const float* A, const float* Ap,
const int H, const int He,
const int W, const int We){
const int ThreadsNum = ThreadsEnd - ThreadsStart + 1;
const int myid = CRTS_cgn * 64 + CRTS_tid - ThreadsStart;
if(ThreadsNum <= 0){
printf("sw_slave_gemm_copy_all ThreadsNum Error!\n");
return;
}
/* if(H == He && W == We){
printf("sw_slave_gemm_copy_all Don't Need Copy All!\n");
return;
} */
if(myid < 0){
//printf("_MYID %d sw_slave_gemm_copy_all myid %d return!\n", CRTS_cgn * 64 + CRTS_tid, myid);
return;
}
const int counts_Q = H;
const int local_count = (counts_Q + ThreadsNum - 1) / ThreadsNum;
const int local_start = myid * local_count;
const int local_end = ((local_start + local_count > counts_Q) ? counts_Q : (local_start + local_count));
const int local_rows = local_end - local_start;
if(local_start >= counts_Q || myid >= ThreadsNum){
return;
}
int blk_Q = (local_rows * We * sizeof(float) < 200 * 1024) ? local_rows : 200 * 1024 / (We * sizeof(float));
int rem_Q = blk_Q;
if(blk_Q != local_rows){
rem_Q = local_rows % blk_Q;
}
float* local_Q = ldm_malloc(sizeof(float) * blk_Q * We);
//printf("myid %d H %d He %d blk_Q %d rem_Q %d local_start %d local_rows %d local_end %d\n", myid, H, He, blk_Q, rem_Q, local_start, local_rows, local_end);
for(int local_now = local_start; local_now < local_end; local_now += blk_Q){
int curr_Q = blk_Q;
if(local_now + blk_Q < H){//right zeros only
curr_Q = local_end - local_now < blk_Q ? rem_Q : blk_Q;
athread_dma_get(local_Q,
Ap + local_now * We,
sizeof(float) * curr_Q * We);
for(int m = 0; m < curr_Q; m++){
for(int n = 0; n < W; n++){
local_Q[m * W + n] = local_Q[m * We + n];
}
}
athread_dma_put(A + local_now * W,
local_Q,
sizeof(float) * curr_Q * W);
}
else if(local_now + blk_Q >= H && local_now < H){//right and bottom zeros There are some problems
curr_Q = local_end - local_now < blk_Q ? rem_Q : blk_Q;
int res_Q = H - local_now;
#ifdef _SWOPS_DEBUG
printf("local_now %d blk_Q %d curr_Q %d res_Q %d\n", local_now, blk_Q, curr_Q, res_Q);
#endif
athread_dma_get(local_Q,
Ap + local_now * We,
sizeof(float) * res_Q * We);
for(int m = 0; m < res_Q; m++){
for(int n = 0; n < W; n++){
local_Q[m * W + n] = local_Q[m * We + n];
}
}
athread_dma_put(A + local_now * W,
local_Q,
sizeof(float) * res_Q * W);
}
}
ldm_free(local_Q, sizeof(float) * blk_Q * We);
}
inline void sw_slave_gemm_copy_all(const int ThreadsStart, const int ThreadsEnd,
const float* A, const float* Ap,
const int H, const int He,
const int W, const int We){
const int ThreadsNum = ThreadsEnd - ThreadsStart + 1;
const int myid = CRTS_cgn * 64 + CRTS_tid - ThreadsStart;
if(ThreadsNum <= 0){
printf("sw_slave_gemm_copy_all ThreadsNum Error!\n");
return;
}
/* if(H == He && W == We){
//printf("sw_slave_gemm_copy_all Don't Need Copy All!\n");
return;
} */
if(myid < 0){
//printf("_MYID %d sw_slave_gemm_copy_all myid %d return!\n", CRTS_cgn * 64 + CRTS_tid, myid);
return;
}
const int counts_Q = He;
const int local_count = (counts_Q + ThreadsNum - 1) / ThreadsNum;
const int local_start = myid * local_count;
const int local_end = ((local_start + local_count > counts_Q) ? counts_Q : (local_start + local_count));
const int local_rows = local_end - local_start;
if(local_start >= counts_Q || myid >= ThreadsNum){
return;
}
int blk_Q = (local_rows * We * sizeof(float) < 200 * 1024) ? local_rows : 200 * 1024 / (We * sizeof(float));
int rem_Q = blk_Q;
if(blk_Q != local_rows){
rem_Q = local_rows % blk_Q;
}
float* local_Q = ldm_malloc(sizeof(float) * blk_Q * We);
//printf("myid %d H %d He %d blk_Q %d rem_Q %d local_start %d local_rows %d local_end %d\n", myid, H, He, blk_Q, rem_Q, local_start, local_rows, local_end);
for(int local_now = local_start; local_now < local_end; local_now += blk_Q){
int curr_Q = blk_Q;
if(local_now + blk_Q < H){//right zeros only
curr_Q = local_end - local_now < blk_Q ? rem_Q : blk_Q;
athread_dma_get(local_Q,
A + local_now * W,
sizeof(float) * curr_Q * W);
for(int m = curr_Q - 1; m >= 0; m--){
for(int n = W - 1; n >= 0; n--){
local_Q[m * We + n] = local_Q[m * W + n];
}
for(int n = We - 1; n >= W; n--){
local_Q[m * We+ n] = 0;
}
}
athread_dma_put(Ap + local_now * We,
local_Q,
sizeof(float) * curr_Q * We);
}
else if(local_now + blk_Q >= H && local_now < H){//right and bottom zeros There are some problems
curr_Q = local_end - local_now < blk_Q ? rem_Q : blk_Q;
int res_Q = H - local_now;
#ifdef _SWOPS_DEBUG
printf("local_now %d blk_Q %d curr_Q %d res_Q %d\n", local_now, blk_Q, curr_Q, res_Q);
#endif
athread_dma_get(local_Q,
A + local_now * W,
sizeof(float) * res_Q * W);
for(int m = res_Q - 1; m >= 0; m--){//copy right zeros
for(int n = W - 1; n >= 0; n--){
local_Q[m * We + n] = local_Q[m * W + n];
}
for(int n = We - 1; n >= W; n--){
local_Q[m * We+ n] = 0;
}
}
for(int m = curr_Q - 1; m >= res_Q; m--){//copy bottom zeros
for(int n = 0; n < We; n++){
local_Q[m * We + n] = 0;
}
}
athread_dma_put(Ap + local_now * We,
local_Q,
sizeof(float) * curr_Q * We);
}
else if(local_now >= H){//bottom zeros only
curr_Q = local_end - local_now < blk_Q ? rem_Q : blk_Q;
for(int m = curr_Q - 1; m >= 0; m--){//copy bottom zeros
for(int n = We; n >= 0; n--){
local_Q[m * We + n] = 0;
}
}
athread_dma_put(Ap + local_now * We,
local_Q,
sizeof(float) * curr_Q * We);
}
}
ldm_free(local_Q, sizeof(float) * blk_Q * We);
}
inline void sw_slave_gemm_copy_all_back_cgn(const int CGN_id,
const float* A, const float* Ap,
const int H, const int He,
const int W, const int We){
if(CRTS_cgn != CGN_id){
return;
}
const int myid = CRTS_tid;
/* if(H == He && W == We){
printf("sw_slave_gemm_copy_all Don't Need Copy All!\n");
return;
} */
if(myid < 0){
//printf("_MYID %d sw_slave_gemm_copy_all myid %d return!\n", CRTS_cgn * 64 + CRTS_tid, myid);
return;
}
const int counts_Q = H;
const int local_count = (counts_Q + 64 - 1) / 64;
const int local_start = myid * local_count;
const int local_end = ((local_start + local_count > counts_Q) ? counts_Q : (local_start + local_count));
const int local_rows = local_end - local_start;
if(local_start >= counts_Q){
return;
}
int blk_Q = (local_rows * We * sizeof(float) < 200 * 1024) ? local_rows : 200 * 1024 / (We * sizeof(float));
int rem_Q = blk_Q;
if(blk_Q != local_rows){
rem_Q = local_rows % blk_Q;
}
float* local_Q = ldm_malloc(sizeof(float) * blk_Q * We);
//printf("myid %d H %d He %d blk_Q %d rem_Q %d local_start %d local_rows %d local_end %d\n", myid, H, He, blk_Q, rem_Q, local_start, local_rows, local_end);
for(int local_now = local_start; local_now < local_end; local_now += blk_Q){
int curr_Q = blk_Q;
if(local_now + blk_Q < H){//right zeros only
curr_Q = local_end - local_now < blk_Q ? rem_Q : blk_Q;
athread_dma_get(local_Q,
Ap + local_now * We,
sizeof(float) * curr_Q * We);
for(int m = 0; m < curr_Q; m++){
for(int n = 0; n < W; n++){
local_Q[m * W + n] = local_Q[m * We + n];
}
}
athread_dma_put(A + local_now * W,
local_Q,
sizeof(float) * curr_Q * W);
}
else if(local_now + blk_Q >= H && local_now < H){//right and bottom zeros There are some problems
curr_Q = local_end - local_now < blk_Q ? rem_Q : blk_Q;
int res_Q = H - local_now;
#ifdef _SWOPS_DEBUG
printf("local_now %d blk_Q %d curr_Q %d res_Q %d\n", local_now, blk_Q, curr_Q, res_Q);
#endif
athread_dma_get(local_Q,
Ap + local_now * We,
sizeof(float) * res_Q * We);
for(int m = 0; m < res_Q; m++){
for(int n = 0; n < W; n++){
local_Q[m * W + n] = local_Q[m * We + n];
}
}
athread_dma_put(A + local_now * W,
local_Q,
sizeof(float) * res_Q * W);
}
}
ldm_free(local_Q, sizeof(float) * blk_Q * We);
}
inline void sw_slave_gemm_copy_all_cgn(const int CGN_id,
const float* A, const float* Ap,
const int H, const int He,
const int W, const int We){
if(CRTS_cgn != CGN_id){
return;
}
const int myid = CRTS_tid;
/* if(H == He && W == We){
printf("sw_slave_gemm_copy_all Don't Need Copy All!\n");
return;
} */
if(myid < 0){
//printf("_MYID %d sw_slave_gemm_copy_all myid %d return!\n", CRTS_cgn * 64 + CRTS_tid, myid);
return;
}
const int counts_Q = He;
const int local_count = (counts_Q + 64 - 1) / 64;
const int local_start = myid * local_count;
const int local_end = ((local_start + local_count > counts_Q) ? counts_Q : (local_start + local_count));
const int local_rows = local_end - local_start;
if(local_start >= counts_Q){
return;
}
int blk_Q = (local_rows * We * sizeof(float) < 200 * 1024) ? local_rows : 200 * 1024 / (We * sizeof(float));
int rem_Q = blk_Q;
if(blk_Q != local_rows){
rem_Q = local_rows % blk_Q;
}
float* local_Q = ldm_malloc(sizeof(float) * blk_Q * We);
//printf("myid %d H %d He %d blk_Q %d rem_Q %d local_start %d local_rows %d local_end %d\n", myid, H, He, blk_Q, rem_Q, local_start, local_rows, local_end);
for(int local_now = local_start; local_now < local_end; local_now += blk_Q){
int curr_Q = blk_Q;
if(local_now + blk_Q < H){//right zeros only
curr_Q = local_end - local_now < blk_Q ? rem_Q : blk_Q;
athread_dma_get(local_Q,
A + local_now * W,
sizeof(float) * curr_Q * W);
for(int m = curr_Q - 1; m >= 0; m--){
for(int n = W - 1; n >= 0; n--){
local_Q[m * We + n] = local_Q[m * W + n];
}
for(int n = We - 1; n >= W; n--){
local_Q[m * We+ n] = 0;
}
}
athread_dma_put(Ap + local_now * We,
local_Q,
sizeof(float) * curr_Q * We);
}
else if(local_now + blk_Q >= H && local_now < H){//right and bottom zeros There are some problems
curr_Q = local_end - local_now < blk_Q ? rem_Q : blk_Q;
int res_Q = H - local_now;
#ifdef _SWOPS_DEBUG
printf("local_now %d blk_Q %d curr_Q %d res_Q %d\n", local_now, blk_Q, curr_Q, res_Q);
#endif
athread_dma_get(local_Q,
A + local_now * W,
sizeof(float) * res_Q * W);
for(int m = res_Q - 1; m >= 0; m--){//copy right zeros
for(int n = W - 1; n >= 0; n--){
local_Q[m * We + n] = local_Q[m * W + n];
}
for(int n = We - 1; n >= W; n--){
local_Q[m * We+ n] = 0;
}
}
for(int m = curr_Q - 1; m >= res_Q; m--){//copy bottom zeros
for(int n = 0; n < We; n++){
local_Q[m * We + n] = 0;
}
}
athread_dma_put(Ap + local_now * We,
local_Q,
sizeof(float) * curr_Q * We);
}
else if(local_now >= H){//bottom zeros only
curr_Q = local_end - local_now < blk_Q ? rem_Q : blk_Q;
for(int m = curr_Q - 1; m >= 0; m--){//copy bottom zeros
for(int n = We; n >= 0; n--){
local_Q[m * We + n] = 0;
}
}
athread_dma_put(Ap + local_now * We,
local_Q,
sizeof(float) * curr_Q * We);
}
}
ldm_free(local_Q, sizeof(float) * blk_Q * We);
}
void sw_slave_gemm_copy_all_f32(sw_gemmPara *_){
sw_gemmPara *para = (sw_gemmPara *)para_cross;
const float *src_A = para->A;
const float *src_Ap = para->Ap;
const float *src_B = para->B;
const float *src_Bp = para->Bp;
const float *src_C = para->C;
const float *src_Cp = para->Cp;
const int M = para->M;
const int Ms = para->Ms;
const int Me = para->Me;
const int N = para->N;
const int Ns = para->Ns;
const int Ne = para->Ne;
const int K = para->K;
const int Ks = para->Ks;
const int Ke = para->Ke;
const int blk_M = para->blk_M;
const int blk_N = para->blk_N;
const int blk_K = para->blk_K;
sw_slave_gemm_copy_all(0, 383, src_A, src_Ap,
M, Me,
K, Ke);
athread_ssync_node();
sw_slave_gemm_copy_all_back(0, 383, src_B, src_Ap,
M, Me,
K, Ke);
athread_ssync_node();
}
inline void sw_slave_gemm_copy_all_H_32_to_64(const int CGN_id,
const float* A, const float* Ap,
const int H_pad, const int W_pad,
const int H, const int Hs, const int He, const int blk_H,
const int W, const int Ws, const int We, const int blk_W){
if(CRTS_cgn != CGN_id){
return;
}
if(H_pad && W_pad){
printf("sw_slave_gemm_copy_all_32_to_64 error\n");
return;
}
if(We > 30720){
printf("error in copy border, We: %d larger than 30720", We);
return;
}
if(H_pad){
if(CRTS_tid < 32){
const int CPY_tid = CRTS_tid;
float* start_A = A + CPY_tid * W;
float* start_Ap = Ap + CPY_tid * We;
float* local_A = ldm_malloc(sizeof(float) * We);
athread_dma_get(local_A,
start_A,
sizeof(float) * W);
for(int i = W; i < We; i++){
local_A[i] = 0;
}
athread_dma_put(start_Ap,
local_A,
sizeof(float) * We);
ldm_free(local_A, sizeof(float) * We);
}
else{
const int CPY_tid = CRTS_tid - 32;
float* start_A = A + (CPY_tid + 32) * W;
float* start_Ap = Ap + (CPY_tid + 32) * We;
float* local_A = ldm_malloc(sizeof(float) * We);
for(int i = 0; i < We; i++){
local_A[i] = 0;
}
athread_dma_put(start_Ap,
local_A,
sizeof(float) * We);
ldm_free(local_A, sizeof(float) * We);
}
}
}
inline void sw_slave_gemm_copy_border_back_f32_cgn(const int CGN_id,
const float* A, const float* Ap,
const int H, const int Hs, const int He, const int blk_H,
const int W, const int Ws, const int We, const int blk_W){
if(CRTS_cgn != CGN_id){
return;
}
if(blk_W > 30720){
if(_MYID == 0){
printf("error in sw_slave_gemm_copy_border_f32_cgn\n blk_W: %d > 30720",blk_W);
}
}
int HW_size = H * W;
if(CRTS_tid < 32){
if(Ws == We){
return;
}
const int CPY_tid = CRTS_tid;
const int Cols_P = Hs / 32;
const float* start_A = A + CPY_tid * Cols_P * W + Ws;
const float* start_Ap = Ap + CPY_tid * Cols_P * We + Ws;
const int blk_P = Cols_P * blk_W * sizeof(float) < (200 * 1024) ? Cols_P : (200 * 1024) / (blk_W * sizeof(float));//larger than 220 * 1024 will cause memory access error
const int num_P = (Cols_P + blk_P - 1) / blk_P;
const int rem_blk_P = Cols_P - num_P * blk_P == 0 ? blk_P : Cols_P - (num_P - 1) * blk_P;
const int rem_blk_W = W - Ws;
const int local_P_size = blk_P * blk_W;
int curr_blk_P = blk_P;
int next_blk_P = blk_P;
float* local_P = ldm_malloc(blk_P * blk_W * sizeof(float));
/* if(local_P == 0){
printf("local_P ldm_malloc error!!! %d\n", _MYID);
return;
} */
for(int curr_P = 0; curr_P < num_P; curr_P++){
curr_blk_P = curr_P < num_P - 1 ? blk_P : rem_blk_P;
for(int i = 0; i < local_P_size; i++){
local_P[i] = 0;
}
athread_dma_get_stride(local_P,
start_Ap + blk_P * curr_P * We,
sizeof(float) * curr_blk_P * blk_W,
sizeof(float) * blk_W,
sizeof(float) * (We - blk_W));
for(int m = 0; m < curr_blk_P; m++){
for(int n = 0; n < rem_blk_W; n++){
local_P[m * rem_blk_W + n] = local_P[m * blk_W + n];
}
}
athread_dma_put_stride(start_A + curr_P * blk_P * W,//error here
local_P,
sizeof(float) * curr_blk_P * rem_blk_W,
sizeof(float) * rem_blk_W,
sizeof(float) * (W - rem_blk_W));
}
ldm_free(local_P,blk_P * blk_W * sizeof(float));
}
else{
if(Hs == He){
return;
}
//printf("Cores %d Copying Border Back\n", CRTS_tid);
const int CPY_tid = CRTS_tid - 32;// 0 ~ 32
const int Cols_Q = blk_H / 32;
const int num_Q = Cols_Q;
const int rem_blk_H = H - Hs;
const float* start_A = A + Hs * W + CPY_tid * Cols_Q * W;
const float* start_Ap = Ap + Hs * We + CPY_tid * Cols_Q * We;
float* local_Q = ldm_malloc(We * sizeof(float));
if(We > 30720){
printf("error in copy border, We: %d larger than 30720", We);
return;
}
for(int curr_Q = 0; curr_Q < num_Q; curr_Q++){
if(CPY_tid * Cols_Q + curr_Q < rem_blk_H ){//not zero only
athread_dma_get(local_Q,
start_Ap + curr_Q * We,
sizeof(float) * We);
athread_dma_put(start_A + curr_Q * W,
local_Q,
sizeof(float) * W);
}
}
ldm_free(local_Q, We * sizeof(float));
}
}
//H the hight dim, W the low dim
//run on CRTS_cgn
inline void sw_slave_gemm_copy_border_f32_cgn(const int CGN_id,
const float* src, const float* dst,
const int H, const int Hs, const int He, const int blk_H,
const int W, const int Ws, const int We, const int blk_W){
if(CRTS_cgn != CGN_id){
return;
}
if(blk_W > 30720){
if(_MYID == 0){
printf("error in sw_slave_gemm_copy_border_f32_cgn\n blk_W: %d > 30720",blk_W);
}
}
int HW_size = H*W;
if(CRTS_tid < 32){
//do dstp
if(Ws == We){
return;
}
const int CPY_tid = CRTS_tid;// 0 ~ 31
const int Cols_P = Hs / 32;
const float* src_P = src + CPY_tid * Cols_P * W + Ws;
const float* dst_P = dst + CPY_tid * Cols_P * We + Ws;
const int blk_P = Cols_P * blk_W * sizeof(float) < (200 * 1024) ? Cols_P : (200 * 1024) / (blk_W * sizeof(float));//larger than 220 * 1024 will cause memory access error
const int num_P = (Cols_P + blk_P - 1) / blk_P;
const int rem_blk_P = Cols_P - num_P * blk_P == 0 ? blk_P : Cols_P - (num_P - 1) * blk_P;
const int rem_blk_W = W - Ws;
const int local_P_size = blk_P * blk_W;
int curr_blk_P = blk_P;
int next_blk_P = blk_P;
float* local_P = ldm_malloc(blk_P * blk_W * sizeof(float));
/* if(local_P == 0){
printf("local_P ldm_malloc error!!! %d\n", _MYID);
return;
} */
for(int curr_P = 0; curr_P < num_P; curr_P++){
curr_blk_P = curr_P < num_P - 1 ? blk_P : rem_blk_P;
for(int i = 0; i < local_P_size; i++){
local_P[i] = 0;
}
athread_dma_get_stride(local_P,
src_P + blk_P * curr_P * W, //src_P
sizeof(float) * curr_blk_P * rem_blk_W,
sizeof(float) * rem_blk_W,
sizeof(float) * (W - rem_blk_W));
for(int m = curr_blk_P - 1; m >= 0; m--){
for(int n = rem_blk_W - 1; n >= 0; n--){
local_P[m * blk_W + n] = local_P[m * rem_blk_W + n];
}
for(int n = blk_W - 1; n >= rem_blk_W; n--){
local_P[m * blk_W + n] = 0;
}
}
athread_dma_put_stride(dst_P + curr_P * blk_P * We,//error here
local_P,
sizeof(float) * curr_blk_P * blk_W,
sizeof(float) * blk_W,
sizeof(float) * (We - blk_W));
}
ldm_free(local_P,blk_P * blk_W * sizeof(float));
}
else{
if(Hs == He){
return;
}
const int CPY_tid = CRTS_tid - 32;// 0 ~ 32
const int Cols_Q = blk_H / 32;
const int num_Q = Cols_Q;
const int rem_blk_H = H - Hs;
const float* src_Q = src + Hs * W + CPY_tid * Cols_Q * W;
const float* dst_Q = dst + Hs * We + CPY_tid * Cols_Q * We;
float* local_Q = ldm_malloc(We * sizeof(float));
if(We > 30720){
printf("error in copy border, We: %d larger than 30720", We);
}
for(int curr_Q = 0; curr_Q < num_Q; curr_Q++){
if(CPY_tid * Cols_Q + curr_Q < rem_blk_H ){//not zero only
athread_dma_get(local_Q,
src_Q + curr_Q * W,
sizeof(float) * W);
for(int i = W; i < We; i++){
local_Q[i] = 0;
}
athread_dma_put(dst_Q + curr_Q * We,
local_Q,
sizeof(float) * We);
}
else{
for(int i = 0; i < We; i++){
local_Q[i] = 0;
}
athread_dma_put(dst_Q + curr_Q * We,
local_Q,
sizeof(float) * We);
}
}
ldm_free(local_Q, We * sizeof(float));
}
}
void sw_slave_gemm_crr_asm_cgn(const int CGN_id,
const float* A, const float* Ap,
const float* B, const float* Bp,
const float* C, const float* Cp,
const int M, const int Ms, const int Me, const int blk_M,
const int N, const int Ns, const int Ne, const int blk_N,
const int K, const int Ks, const int Ke, const int blk_K){
if(CRTS_cgn != CGN_id){
return;
}
const int cid = CRTS_tid % 8;
const int rid = CRTS_tid / 8;
const int Md = blk_M/8;
const int Nd = blk_N/8;
const int Kd = blk_K/8;
const int num_M = (M + blk_M - 1) / blk_M;
const int num_N = (N + blk_N - 1) / blk_N;
const int num_K = (K + blk_K - 1) / blk_K;
const int rem_blk_M = num_M * blk_M - M == 0 ? blk_M : M - (num_M-1) * blk_M;
const int rem_blk_N = num_N * blk_N - N == 0 ? blk_N : N - (num_N-1) * blk_N;
const int rem_blk_K = num_K * blk_K - K == 0 ? blk_K : K - (num_K-1) * blk_K;
const int local_A_size = blk_M * blk_K / 64;
const int local_B_size = blk_K * blk_N / 64;
const int local_C_size = blk_M * blk_N / 64;
float* local_A = (float*)ldm_malloc(sizeof(float) * 2 * blk_M * blk_K / 64);
float* local_B = (float*)ldm_malloc(sizeof(float) * 2 * blk_K * blk_N / 64);
double* local_C = (double*)ldm_malloc(sizeof(double) * blk_M * blk_N / 64);
float* trans_A = (float*)ldm_malloc(sizeof(float) * blk_M * blk_K / 64);
float* local_A_dma = (float*)ldm_malloc(sizeof(float) * 2 * blk_M * blk_K / 64);
float* local_B_dma = (float*)ldm_malloc(sizeof(float) * 2 * blk_K * blk_N / 64);
float* local_C_dma = (float*)ldm_malloc(sizeof(float) * blk_M * blk_N / 64);
const float* start_A = A + cid * blk_K/8 * M + rid * blk_M/8;
const float* start_B = B + rid * blk_K/8 * N + cid * blk_N/8;
const float* start_C = C + rid * blk_M/8 * N + cid * blk_N/8;
const float* start_Ap = Ap + cid * blk_K/8 * Me + rid * blk_M/8;
const float* start_Bp = Bp + rid * blk_K/8 * Ne + cid * blk_N/8;
const float* start_Cp = Cp + rid * blk_M/8 * Ne + cid * blk_N/8;
float* next_A = A;
float* next_B = B;
float* next_C = C;
float* curr_C = C;
int A_step = M - blk_M/8;
int B_step = N - blk_N/8;
int C_step = N - blk_N/8;
int curr_M = M;
int curr_N = N;
int curr_K = K;
int curr_blk_M = blk_M;
int curr_blk_N = blk_N;
int curr_blk_K = blk_K;
int next_A_offset = 0;
int next_B_offset = 0;
int next_C_offset = 0;
int next_blk_M = blk_M;
int next_blk_N = blk_N;
int next_blk_K = blk_K;
volatile athread_rply_t dma_get_A = 0, dma_get_B = 0, dma_put_C = 0;
volatile athread_rply_t rma_local_A = 0, rma_local_B = 0;
volatile athread_rply_t rma_A[8] = {0,0,0,0,0,0,0,0}, rma_B[8] = {0,0,0,0,0,0,0,0};
volatile int double_buffer_A = 0, double_buffer_B = 0; //for rma
volatile int double_buffer_DMA = 0;
/* if(CRTS_tid ==0){
printf("CGN %d\nM %d Ms %d Me %d\nN %d Ns %d Ne %d\nK %d Ks %d Ke %d\n",
CRTS_cgn, M, Ms, Me, N, Ns, Ne, K, Ks, Ke);
} */
athread_dma_iget_stride(local_A_dma + (1 - double_buffer_DMA) * local_A_size,
start_A,
sizeof(float) * local_A_size,
sizeof(float) * blk_M/8,
sizeof(float) * A_step,
&dma_get_A);
athread_dma_iget_stride(local_B_dma + (1 - double_buffer_DMA) * local_B_size,
start_B,
sizeof(float) * local_B_size,
sizeof(float) * blk_N/8,
sizeof(float) * B_step,
&dma_get_B);
athread_dma_wait_value(&dma_get_A, 1);