-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcaldgemm_cal.cpp
2355 lines (2165 loc) · 84.9 KB
/
caldgemm_cal.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
/*
* CPU side of CALDGEMM implementation.
*
* Copyright 2015:
* - David Rohr ([email protected])
* - Matthias Bach ([email protected])
* - Matthias Kretz ([email protected])
*
* This file is part of CALDGEMM.
*
* CALDGEMM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CALDGEMM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with CALDGEMM. If not, see <http://www.gnu.org/licenses/>.
*/
#include "caldgemm_cal.h"
//#include "cal_fake.h"
#include "cmodules/util_adl.h"
#include "cmodules/affinity.h"
#ifndef _WIN32
#include <unistd.h>
#endif
#ifdef _NO_AFFINITY
#define sched_setaffinity(a, b, c)
#endif
#define ILKernelName ILKernel
#include "caldgemm.il"
#undef ILKernelName
#define ILKernelName ILKernelALPHA1
#define CALDGEMM_ALPHA1
#include "caldgemm.il"
#undef ILKernelName
#define ILKernelName ILKernelLinpack
#define CALDGEMM_LINPACK_KERNEL
#include "caldgemm.il"
#undef CALDGEMM_LINPACK_KERNEL
#undef CALDGEMM_ALPHA1
#undef ILKernelName
const char* caldgemm_cal::ILFakeKernel =
"il_ps_2_0\n"
"dcl_input_position_interp(linear_noperspective) vWinCoord0.xy__\n"
"dcl_output_generic o0\n"
"mov o0, vWinCoord0.0000\n"
"end\n"
;
const char* caldgemm_cal::ILConvertKernel =
"il_ps_2_0\n"
"dcl_input_position_interp(linear_noperspective) vWinCoord0.xy__\n"
"dcl_resource_id(0)_type(2d,unnorm)_fmtx(unknown)_fmty(unknown)_fmtz(unknown)_fmtw(unknown)\n"
"dcl_resource_id(1)_type(2d,unnorm)_fmtx(unknown)_fmty(unknown)_fmtz(unknown)_fmtw(unknown)\n"
"dcl_output_generic o0\n"
"dcl_output_generic o1\n"
"dcl_literal l0, 2.0, 1.0, 0.5, 0.0\n"
"sub r0.xy__, vWinCoord0.xy00, l0.zz00\n"
"mul r0.xy__, r0.xy00, l0.xy00\n"
"add r0.xy__, r0.xy00, l0.zz00\n"
"add r1.xy__, r0.xy00, l0.yw00\n"
"sample_resource(0)_sampler(0) r13, r0.xy\n"
"sample_resource(0)_sampler(0) r14, r1.xy\n"
"sample_resource(1)_sampler(1) r15, r0.xy\n"
"sample_resource(1)_sampler(1) r16, r1.xy\n"
"mov o0.xy, r13.xy\n"
"mov o0.z, r14.x\n"
"mov o0.w, r14.y\n"
"mov o1.xy, r15.xy\n"
"mov o1.z, r16.x\n"
"mov o1.w, r16.y\n"
"end\n"
;
//#define fprintf(file, ...) {fprintf(STD_OUT, "Thread %d ", gettid());fprintf(stderr, __VA_ARGS__);}
#define CHKERR(cmd, text) \
{ \
int tmp_cmd_result = cmd; \
if (tmp_cmd_result != CAL_RESULT_OK) {fprintf(STD_OUT, "Error %d: '%s' while " text "\n", tmp_cmd_result, calGetErrorString());return(1);} \
}
#define WAITFOREVENTA(ctx, event) { CALresult r; do { r = calCtxIsEventDone(ctx, event); if (r == CAL_RESULT_ERROR) { fprintf(STD_OUT, "Error while waiting for event\nError String: %s\n", calGetErrorString()); return(1);} } while (r == CAL_RESULT_PENDING);}
int caldgemm_cal::WaitForEvent(int eventnr, int devicenr, int lock)
{
if (Config->Debug) fprintf(STD_OUT, "\tWaiting for event from device %d obuffer %d...\n", devicenr, eventnr);
cpu_set_t tmpset, oldset;
bool needrepin = Config->RepinDuringActiveWaitForEvent && !Config->RepinMainThreadAlways && (Config->ParallelDMA == 0 || Config->ParallelDMA > matrix_n) && Config->AllocMapping[devicenr] != -1 && Config->AllocMapping[devicenr] != Config->PinMainThread;
if (needrepin)
{
sched_getaffinity(0, sizeof(oldset), &oldset);
CPU_ZERO(&tmpset);
CPU_SET(Config->AllocMapping[devicenr] + Config->CPUCoreOffset, &tmpset);
sched_setaffinity(0, sizeof(tmpset), &tmpset);
}
#ifdef CALDGEMM_USE_CAL_WAIT_FOR_EVENTS
if (lock) pthread_mutex_lock(&device_mutex[devicenr]);
if (Config->ThreadSaveDriver == -1) pthread_mutex_lock(&globalDriverLock);
#ifdef CALDGEMM_USE_CAL_WAIT_FOR_EVENTS_NO_POLL
CALuint flag = CAL_WAIT_LOW_CPU_UTILIZATION;
#else
CALuint flag = CAL_WAIT_POLLING;
#endif
CHKERR(calCtxWaitForEvents(ctxs[devicenr], events[devicenr][eventnr].events, events[devicenr][eventnr].nEvents, flag), "during calCtxWaitForEvents");
events[devicenr][eventnr].Reset();
if (Config->ThreadSaveDriver == -1) pthread_mutex_unlock(&globalDriverLock);
if (lock) pthread_mutex_unlock(&device_mutex[devicenr]);
#else
CALresult r = CAL_RESULT_OK;
do
{
if (lock) pthread_mutex_lock(&device_mutex[devicenr]);
if (Config->ThreadSaveDriver == -1) pthread_mutex_lock(&globalDriverLock);
for (int i = 0;i < events[devicenr][eventnr].nEvents;i++)
{
do
{
r = calCtxIsEventDone(ctxs[devicenr], events[devicenr][eventnr].events[i]);
} while (i && r == CAL_RESULT_PENDING);
if (i == 0 && r == CAL_RESULT_PENDING) break;
}
if (r == CAL_RESULT_OK) events[devicenr][eventnr].Reset();
if (Config->ThreadSaveDriver == -1) pthread_mutex_unlock(&globalDriverLock);
if (lock) pthread_mutex_unlock(&device_mutex[devicenr]);
if (r == CAL_RESULT_ERROR)
{
fprintf(STD_OUT, "Error while waiting for event\nError String: %s\n", calGetErrorString());
return(1);
}
else if (Config->SleepDuringActiveWait != -1 && r == CAL_RESULT_PENDING)
{
#ifdef _WIN32
Sleep(Config->SleepDuringActiveWait / 1000);
#else
usleep(Config->SleepDuringActiveWait);
#endif
}
} while (r == CAL_RESULT_PENDING);
#endif
if (needrepin)
{
sched_setaffinity(0, sizeof(oldset), &oldset);
}
if (Config->Debug) fprintf(STD_OUT, "\tDONE: Waiting for event from device %d obuffer %d...\n", devicenr, eventnr);
return(0);
}
#ifdef CALDGEMM_UNALIGNED_ADDRESSES
#define _mm_load_pd_use _mm_loadu_pd
#else
#define _mm_load_pd_use _mm_load_pd
#endif
#ifdef CALDGEMM_STREAMING_STORES_DIVIDE
#define _mm_store_pd_use _mm_stream_pd
#else
#define _mm_store_pd_use _mm_store_pd
#endif
caldgemm_cal::caldgemm_cal() : caldgemm()
{
adl_util_initialized = 0;
}
caldgemm_cal::~caldgemm_cal()
{
if (adl_util_initialized)
{
#ifndef _NO_ADL
adl_temperature_check_exit();
#endif
}
}
double caldgemm_cal::getMaxGPUTemperature()
{
#ifndef _NO_ADL
if (adl_util_initialized == 0)
{
if (adl_temperature_check_init()) return(-1.);
adl_util_initialized = 1;
}
double retVal;
if (adl_temperature_check_run(&retVal, !Config->Quiet)) return(-1.);
return(retVal);
#else
return(0.);
#endif
}
int caldgemm_cal::divideBuffer(BufferProperties* dst, double* src, int width, int height, int gpu_width, int gpu_height, int pitch, int numBuffers, bool transpose CALDGEMM_DIVBUFA)
{
const int gpu_pitch = dst[0].pitch * 2;
if (Config->SkipCPUProcessing) return(0);
//if (Config->Debug) fprintf(STD_OUT, "\t\tSRC=0x%llx, w: %d, h: %d, pitch: %d (gpuw: %d, gpuh: %d, transpose: %d)\n", (long long int) src, width, height, pitch, gpu_width, gpu_height, (int) transpose);
#if defined(CALDGEMM_DOUBLE_BUFFERS)
fprintf(STD_OUT, "ERROR: divideBuffer not implemented for current configuration, skipping\n");
return(0);
#endif
if (Config->DivideToGPU)
{
for (int i = 0;i < numBuffers;i++)
{
if (Config->ThreadSaveDriver == -1) pthread_mutex_lock(&globalDriverLock);
CHKERR(calResMap(&dst[i].ptr_void, &dst[i].pitch, dst[i].res, 0), "mapping input buffer for buffer division");
if (Config->ThreadSaveDriver == -1) pthread_mutex_unlock(&globalDriverLock);
if (((size_t) dst[i].ptr_void) & (vcpysize - 1))
{
fprintf(STD_OUT, "Invalid alignment\n");
return(1);
}
}
}
if (transpose)
{
#if !defined(CALDGEMM_44)
if (numBuffers <= 4)
{
for (int y = 0;y < width;y += 4)
{
double* saddr = src + (y * pitch);
double* saddr2 = src + ((y + 1) * pitch);
double* saddr3 = src + ((y + 2) * pitch);
double* saddr4 = src + ((y + 3) * pitch);
double* daddr = dst[0].ptr_double + y;
double* daddr2 = dst[1 % numBuffers].ptr_double + (1 / numBuffers) * gpu_width + y;
double* daddr3 = dst[2 % numBuffers].ptr_double + (2 / numBuffers) * gpu_width + y;
double* daddr4 = dst[3 % numBuffers].ptr_double + (3 / numBuffers) * gpu_width + y;
const int dpitch = 4 / numBuffers * gpu_width;
for (int i = 0;i < height;i += 4)
{
#ifdef CALDGEMM_USE_VEC_MEMCPY_PREFETCH
//Prefetching disabled as it currently has a negative performance impact
/*_mm_prefetch(saddr + 100, _MM_HINT_NTA);
_mm_prefetch(saddr2 + 100, _MM_HINT_NTA);
_mm_prefetch(saddr3 + 100, _MM_HINT_NTA);
_mm_prefetch(saddr4 + 100, _MM_HINT_NTA);*/
#endif
__m128d x1, x2, x3, x4, x5, x6, x7, x8, x9, x10;
x1 = _mm_load_pd_use(saddr);
x3 = _mm_load_pd_use(saddr + 2);
x2 = _mm_load_pd_use(saddr2);
x4 = _mm_load_pd_use(saddr2 + 2);
x5 = _mm_load_pd_use(saddr3);
x7 = _mm_load_pd_use(saddr3 + 2);
x6 = _mm_load_pd_use(saddr4);
x8 = _mm_load_pd_use(saddr4 + 2);
x9 = _mm_unpacklo_pd(x1, x2);
x10 = _mm_unpackhi_pd(x1, x2);
x1 = _mm_unpacklo_pd(x3, x4);
x2 = _mm_unpackhi_pd(x3, x4);
x3 = _mm_unpacklo_pd(x5, x6);
x4 = _mm_unpackhi_pd(x5, x6);
x5 = _mm_unpacklo_pd(x7, x8);
x6 = _mm_unpackhi_pd(x7, x8);
_mm_store_pd_use(daddr, x9);
_mm_store_pd_use(daddr2, x10);
_mm_store_pd_use(daddr + 2, x3);
_mm_store_pd_use(daddr2 + 2, x4);
_mm_store_pd_use(daddr3, x1);
_mm_store_pd_use(daddr4, x2);
_mm_store_pd_use(daddr3 + 2, x5);
_mm_store_pd_use(daddr4 + 2, x6);
saddr += 4;
saddr2 += 4;
saddr3 += 4;
saddr4 += 4;
daddr += dpitch;
daddr2 += dpitch;
daddr3 += dpitch;
daddr4 += dpitch;
}
}
}
else
#endif
#if (defined(CALDGEMM_44) & defined(CALDGEMM_TRANSPOSED_B))
assert((height & 3) == 0);
const int height_4 = height / 4;
for (int y=0; y < width; y += 4)
{
const double* __restrict__ saddr0 = &src[(y + 0) * pitch];
const double* __restrict__ saddr2 = &src[(y + 2) * pitch];
double* __restrict__ dstBank0 = &dst[0].ptr_double[y * 2];
double* __restrict__ dstBank1 = &dst[1].ptr_double[y * 2];
for (int i = 0; i < height_4; ++i)
{
double* __restrict__ daddr0 = &dstBank0[i * gpu_width * 2];
double* __restrict__ daddr1 = &dstBank1[i * gpu_width * 2];
const __m128d x0 = _mm_load_pd_use(&saddr0[0]);
const __m128d x1 = _mm_load_pd_use(&saddr0[pitch]);
const __m128d x2 = _mm_load_pd_use(&saddr2[0]);
const __m128d x3 = _mm_load_pd_use(&saddr2[pitch]);
saddr0 += 2;
saddr2 += 2;
const __m128d x4 = _mm_load_pd_use(&saddr0[0]);
const __m128d x5 = _mm_load_pd_use(&saddr0[pitch]);
const __m128d x6 = _mm_load_pd_use(&saddr2[0]);
const __m128d x7 = _mm_load_pd_use(&saddr2[pitch]);
saddr0 += 2;
saddr2 += 2;
_mm_store_pd_use(&daddr0[0], _mm_unpacklo_pd(x0, x1));
_mm_store_pd_use(&daddr0[2], _mm_unpackhi_pd(x0, x1));
_mm_store_pd_use(&daddr0[4], _mm_unpacklo_pd(x2, x3));
_mm_store_pd_use(&daddr0[6], _mm_unpackhi_pd(x2, x3));
_mm_store_pd_use(&daddr1[0], _mm_unpacklo_pd(x4, x5));
_mm_store_pd_use(&daddr1[2], _mm_unpackhi_pd(x4, x5));
_mm_store_pd_use(&daddr1[4], _mm_unpacklo_pd(x6, x7));
_mm_store_pd_use(&daddr1[6], _mm_unpackhi_pd(x6, x7));
}
}
#elif defined(CALDGEMM_44) & defined(CALDGEMM_TRANSPOSED_A) & !defined(CALDGEMM_SINGLE_BUFFER) &!defined(CALDGEMM_DOUBLE_BUFFERS)
#ifdef CALDGEMM_DIVIDE_TRANSPOSE_TWOPHASE
#ifdef CALDGEMM_DIVIDE_STATIC_BUFFER
double* __restrict__ tmpbuffer = tmpBuffer;
#else
double* __restrict__ tmpbuffer = allocDivideBuffer();
#endif
for (int k = 0;k < height;k += CALDGEMM_TRANSPOSE_BLOCKING)
{
for (int i = 0;i < width;i += 2)
{
double* __restrict__ daddr = &tmpbuffer[i];
const double* __restrict__ saddr = src + (i * pitch + k);
for (int y = 0;y < CALDGEMM_TRANSPOSE_BLOCKING;y += 2)
{
const __m128d x0 = _mm_load_pd_use(saddr);
const __m128d x1 = _mm_load_pd_use(&saddr[pitch]);
_mm_store_pd(daddr, _mm_unpacklo_pd(x0, x1));
_mm_store_pd(&daddr[width], _mm_unpackhi_pd(x0, x1));
saddr += 2;
daddr += 2 * width;
}
}
double* __restrict__ daddr = &dst[0].ptr_double[k * gpu_pitch];
double* __restrict__ daddr2 = &dst[1].ptr_double[k * gpu_pitch];
for (int y = 0; y < CALDGEMM_TRANSPOSE_BLOCKING; y++)
{
int count = dst[0].DataSize * width;
const double* __restrict__ saddr = tmpbuffer + (y * width);
#ifdef CALDGEMM_SHIFT_TEXTURE
if (y & 1)
{
daddr -= 2 * CALDGEMM_SHIFT_TEXTURE;
daddr2 -= 2 * CALDGEMM_SHIFT_TEXTURE;
}
else
{
daddr += 2 * CALDGEMM_SHIFT_TEXTURE;
daddr2 += 2 * CALDGEMM_SHIFT_TEXTURE;
}
#endif
for (int i = 0;i < count;i += 64)
{
#ifdef CALDGEMM_USE_VEC_MEMCPY_PREFETCH
_mm_prefetch(CAST_FOR_MMPREFETCH (saddr + 32), _MM_HINT_T1);
#endif
_mm_store_pd_use(daddr, _mm_load_pd_use(saddr));
_mm_store_pd_use(daddr2, _mm_load_pd_use(saddr + 2));
_mm_store_pd_use(daddr + 2, _mm_load_pd_use(saddr + 4));
_mm_store_pd_use(daddr2 + 2, _mm_load_pd_use(saddr + 6));
saddr += 8;
daddr += 4;
daddr2+= 4;
}
daddr += gpu_pitch - width / numBuffers;
daddr2 += gpu_pitch - width / numBuffers;
}
}
#ifndef CALDGEMM_DIVIDE_STATIC_BUFFER
freeDivideBuffer(tmpbuffer);
#endif
#elif !defined(CALDGEMM_SHIFT_TEXTURE) | CALDGEMM_SHIFT_TEXTURE == 0 //end of Two Phase transposition
for (int y = 0;y < width;y += 16)
{
const double* __restrict__ saddr0 = &src[(y + 0) * pitch];
const double* __restrict__ saddr2 = &src[(y + 2) * pitch];
const double* __restrict__ saddr4 = &src[(y + 4) * pitch];
const double* __restrict__ saddr6 = &src[(y + 6) * pitch];
double* __restrict__ dstBank0 = &dst[0].ptr_double[y / 2];
double* __restrict__ dstBank1 = &dst[1].ptr_double[y / 2];
for (int i = 0;i < height;i += 2)
{
double* __restrict__ daddr0 = &dstBank0[i * gpu_pitch];
double* __restrict__ daddr1 = &dstBank1[i * gpu_pitch];
{
const __m128d x0 = _mm_load_pd_use(&saddr0[0]);
const __m128d x1 = _mm_load_pd_use(&saddr0[pitch]);
const __m128d x2 = _mm_load_pd_use(&saddr2[0]);
const __m128d x3 = _mm_load_pd_use(&saddr2[pitch]);
const __m128d x4 = _mm_load_pd_use(&saddr4[0]);
const __m128d x5 = _mm_load_pd_use(&saddr4[pitch]);
const __m128d x6 = _mm_load_pd_use(&saddr6[0]);
const __m128d x7 = _mm_load_pd_use(&saddr6[pitch]);
_mm_store_pd_use(&daddr0[0], _mm_unpacklo_pd(x0, x1));
_mm_store_pd_use(&daddr0[gpu_pitch], _mm_unpackhi_pd(x0, x1));
_mm_store_pd_use(&daddr1[0], _mm_unpacklo_pd(x2, x3));
_mm_store_pd_use(&daddr1[gpu_pitch], _mm_unpackhi_pd(x2, x3));
_mm_store_pd_use(&daddr0[2], _mm_unpacklo_pd(x4, x5));
_mm_store_pd_use(&daddr0[gpu_pitch + 2], _mm_unpackhi_pd(x4, x5));
_mm_store_pd_use(&daddr1[2], _mm_unpacklo_pd(x6, x7));
_mm_store_pd_use(&daddr1[gpu_pitch + 2], _mm_unpackhi_pd(x6, x7));
}
{
const __m128d x0 = _mm_load_pd_use(&saddr0[8*pitch]);
const __m128d x1 = _mm_load_pd_use(&saddr0[9*pitch]);
const __m128d x2 = _mm_load_pd_use(&saddr2[8*pitch]);
const __m128d x3 = _mm_load_pd_use(&saddr2[9*pitch]);
const __m128d x4 = _mm_load_pd_use(&saddr4[8*pitch]);
const __m128d x5 = _mm_load_pd_use(&saddr4[9*pitch]);
const __m128d x6 = _mm_load_pd_use(&saddr6[8*pitch]);
const __m128d x7 = _mm_load_pd_use(&saddr6[9*pitch]);
_mm_store_pd_use(&daddr0[4], _mm_unpacklo_pd(x0, x1));
_mm_store_pd_use(&daddr0[gpu_pitch + 4], _mm_unpackhi_pd(x0, x1));
_mm_store_pd_use(&daddr1[4], _mm_unpacklo_pd(x2, x3));
_mm_store_pd_use(&daddr1[gpu_pitch + 4], _mm_unpackhi_pd(x2, x3));
_mm_store_pd_use(&daddr0[6], _mm_unpacklo_pd(x4, x5));
_mm_store_pd_use(&daddr0[gpu_pitch + 6], _mm_unpackhi_pd(x4, x5));
_mm_store_pd_use(&daddr1[6], _mm_unpacklo_pd(x6, x7));
_mm_store_pd_use(&daddr1[gpu_pitch + 6], _mm_unpackhi_pd(x6, x7));
}
saddr0 += 2;
saddr2 += 2;
saddr4 += 2;
saddr6 += 2;
}
}
#elif CALDGEMM_SHIFT_TEXTURE == 1 //end of: not defined CALDGEMM_SHIFT_TEXTURE
#ifdef CALDGEMM_DIVIDE_STATIC_BUFFER
double* __restrict__ tmpbuffer = tmpBuffer;
#else
double* __restrict__ tmpbuffer = allocDivideBuffer();
#endif
for (int i0 = 0;i0 < height;i0 += CALDGEMM_DIVIDE_BLOCKING)
{
for (int y = 0;y < width;y += 16)
{
const double* __restrict__ saddr0 = &src[(y + 0) * pitch + i0];
double* __restrict__ dstBank0 = &dst[0].ptr_double[y / 2];
double* __restrict__ dstBank1 = &dst[1].ptr_double[y / 2];
for (int i = i0;i < i0 + CALDGEMM_DIVIDE_BLOCKING;i += 2)
{
double* __restrict__ daddr0 = &dstBank0[(i) * gpu_pitch];
double* __restrict__ daddr1 = &dstBank1[(i) * gpu_pitch];
{
const __m128d x0 = _mm_load_pd_use(&saddr0[0]);
const __m128d x1 = _mm_load_pd_use(&saddr0[pitch]);
const __m128d x2 = _mm_load_pd_use(&saddr0[2*pitch]);
const __m128d x3 = _mm_load_pd_use(&saddr0[3*pitch]);
const __m128d x4 = _mm_load_pd_use(&saddr0[4*pitch]);
const __m128d x5 = _mm_load_pd_use(&saddr0[5*pitch]);
const __m128d x6 = _mm_load_pd_use(&saddr0[6*pitch]);
const __m128d x7 = _mm_load_pd_use(&saddr0[7*pitch]);
_mm_store_pd(&daddr0[0], _mm_load_pd_use(&tmpbuffer[2 * (i-i0)]));
_mm_store_pd(&daddr1[0], _mm_load_pd_use(&tmpbuffer[2 * (i-i0) + 2]));
_mm_store_pd_use(&daddr0[2], _mm_unpacklo_pd(x0, x1));
_mm_store_pd_use(&daddr0[gpu_pitch], _mm_unpackhi_pd(x0, x1));
_mm_store_pd_use(&daddr1[2], _mm_unpacklo_pd(x2, x3));
_mm_store_pd_use(&daddr1[gpu_pitch], _mm_unpackhi_pd(x2, x3));
_mm_store_pd_use(&daddr0[4], _mm_unpacklo_pd(x4, x5));
_mm_store_pd_use(&daddr0[gpu_pitch + 2], _mm_unpackhi_pd(x4, x5));
_mm_store_pd_use(&daddr1[4], _mm_unpacklo_pd(x6, x7));
_mm_store_pd_use(&daddr1[gpu_pitch + 2], _mm_unpackhi_pd(x6, x7));
}
{
const __m128d x0 = _mm_load_pd_use(&saddr0[8*pitch]);
const __m128d x1 = _mm_load_pd_use(&saddr0[9*pitch]);
const __m128d x2 = _mm_load_pd_use(&saddr0[10*pitch]);
const __m128d x3 = _mm_load_pd_use(&saddr0[11*pitch]);
const __m128d x4 = _mm_load_pd_use(&saddr0[12*pitch]);
const __m128d x5 = _mm_load_pd_use(&saddr0[13*pitch]);
const __m128d x6 = _mm_load_pd_use(&saddr0[14*pitch]);
const __m128d x7 = _mm_load_pd_use(&saddr0[15*pitch]);
_mm_store_pd_use(&daddr0[6], _mm_unpacklo_pd(x0, x1));
_mm_store_pd_use(&daddr0[gpu_pitch + 4], _mm_unpackhi_pd(x0, x1));
_mm_store_pd_use(&daddr1[6], _mm_unpacklo_pd(x2, x3));
_mm_store_pd_use(&daddr1[gpu_pitch + 4], _mm_unpackhi_pd(x2, x3));
_mm_store_pd_use(&tmpbuffer[2 * (i-i0)], _mm_unpacklo_pd(x4, x5));
_mm_store_pd_use(&daddr0[gpu_pitch + 6], _mm_unpackhi_pd(x4, x5));
_mm_store_pd_use(&tmpbuffer[2 * (i-i0) + 2], _mm_unpacklo_pd(x6, x7));
_mm_store_pd_use(&daddr1[gpu_pitch + 6], _mm_unpackhi_pd(x6, x7));
}
saddr0 += 2;
}
}
double* __restrict__ dstBank0 = &dst[0].ptr_double[width / 2];
double* __restrict__ dstBank1 = &dst[1].ptr_double[width / 2];
for (int i = i0;i < i0 + CALDGEMM_DIVIDE_BLOCKING;i += 2)
{
double* __restrict__ daddr0 = &dstBank0[(i) * gpu_pitch];
double* __restrict__ daddr1 = &dstBank1[(i) * gpu_pitch];
_mm_store_pd_use(&daddr0[0], _mm_load_pd_use(&tmpbuffer[2 * (i-i0)]));
_mm_store_pd_use(&daddr1[0], _mm_load_pd_use(&tmpbuffer[2 * (i-i0) + 2]));
#ifdef CALDGEMM_STREAMING_STORES_DIVIDE
__m128d empty;
_mm_store_pd_use(&daddr0[2], empty);
_mm_store_pd_use(&daddr1[2], empty);
_mm_store_pd_use(&daddr0[4], empty);
_mm_store_pd_use(&daddr1[4], empty);
_mm_store_pd_use(&daddr0[6], empty);
_mm_store_pd_use(&daddr1[6], empty);
#endif
}
}
#ifndef CALDGEMM_DIVIDE_STATIC_BUFFER
freeDivideBuffer(tmpbuffer);
#endif
#else //end of: CALDGEMM_SHIFT_TEXTURE < 2
#define CALDGEMM_SHIFT_TEXTURE_OFFSET (CALDGEMM_SHIFT_TEXTURE * 2)
for (int i = 0;i < height;i += 2)
{
double* __restrict__ daddr0 = &dst[0].ptr_double[i * gpu_pitch];
double* __restrict__ daddr1 = &dst[1].ptr_double[i * gpu_pitch];
const double* __restrict__ saddr0 = &src[i];
for (int y = 0;y < width;y += 4)
{
#ifdef CALDGEMM_USE_VEC_MEMCPY_PREFETCH
_mm_prefetch(CAST_FOR_MMPREFETCH (saddr0 + 4 * pitch), _MM_HINT_T1);
_mm_prefetch(CAST_FOR_MMPREFETCH (saddr0 + 5 * pitch), _MM_HINT_T1);
_mm_prefetch(CAST_FOR_MMPREFETCH (saddr0 + 6 * pitch), _MM_HINT_T1);
_mm_prefetch(CAST_FOR_MMPREFETCH (saddr0 + 7 * pitch), _MM_HINT_T1);
#endif
{
const __m128d x0 = _mm_load_pd_use(&saddr0[0]);
const __m128d x1 = _mm_load_pd_use(&saddr0[pitch]);
const __m128d x2 = _mm_load_pd_use(&saddr0[2 * pitch]);
const __m128d x3 = _mm_load_pd_use(&saddr0[3 * pitch]);
_mm_store_pd_use(&daddr0[0 + CALDGEMM_SHIFT_TEXTURE_OFFSET], _mm_unpacklo_pd(x0, x1));
_mm_store_pd_use(&daddr0[gpu_pitch], _mm_unpackhi_pd(x0, x1));
_mm_store_pd_use(&daddr1[0 + CALDGEMM_SHIFT_TEXTURE_OFFSET], _mm_unpacklo_pd(x2, x3));
_mm_store_pd_use(&daddr1[gpu_pitch], _mm_unpackhi_pd(x2, x3));
}
daddr0 += 2;
daddr1 += 2;
saddr0 += 4 * pitch;
}
}
#endif //CALDGEMM_SHIFT_TEXTURE
#else
for (int y=0; y < width; y += 2)
{
double* saddr = src + (y * pitch);
double* saddr2 = src + ((y + 1) * pitch);
for (int i = 0;i < height;i += 2)
{
#if defined(CALDGEMM_44) & defined(CALDGEMM_TRANSPOSED_B)
int bank = (i / 2) % 2;
double* daddr = dst[bank].ptr_double + (i / 4) * gpu_width * 2 + y * 2;
double* daddr2 = dst[bank].ptr_double + (i / 4) * gpu_width * 2 + y * 2 + 2;
#elif defined(CALDGEMM_44) & defined(CALDGEMM_TRANSPOSED_A)
//Col Interleaved Storage, Numbuffers is either 2 or 4, might be optimized in 2 branches
int bank = (y / 2) % numBuffers;
#ifdef CALDGEMM_DIAGONAL_TEXTURE
double* daddr = dst[bank].ptr_double + i * gpu_width / 2 + (((y / 2) & 0xFFFFFFFE) + 2 * i) % (gpu_width / 2);
double* daddr2 = dst[bank].ptr_double + (i + 1) * gpu_width / 2 + (((y / 2) & 0xFFFFFFFE) + 2 * i + 2) % (gpu_width / 2);
#else
double* daddr = dst[bank].ptr_double + (i * gpu_width / numBuffers + ((y / numBuffers) & 0xFFFFFFFE));
double* daddr2 = dst[bank].ptr_double + ((i + 1) * gpu_width / numBuffers + ((y / numBuffers) & 0xFFFFFFFE));
#endif
#else
//Standard Storage
int bank = (i) % numBuffers;
int bank2 = (i + 1) % numBuffers;
double* daddr = dst[bank].ptr_double + (i / numBuffers) * gpu_width + y;
double* daddr2 = dst[bank2].ptr_double + (i / numBuffers) * gpu_width + y;
#endif
#ifdef CALDGEMM_USE_VEC_MEMCPY_PREFETCH
_mm_prefetch(CAST_FOR_MMPREFETCH (saddr + 100), _MM_HINT_NTA);
_mm_prefetch(CAST_FOR_MMPREFETCH (saddr2 + 100), _MM_HINT_NTA);
#endif
__m128d x1, x2, x3, x4;
x1 = _mm_load_pd_use(saddr);
x2 = _mm_load_pd_use(saddr2);
x3 = _mm_unpacklo_pd(x1, x2);
x4 = _mm_unpackhi_pd(x1, x2);
_mm_store_pd_use(daddr, x3);
_mm_store_pd_use(daddr2, x4);
saddr += 2;
saddr2 += 2;
}
}
#endif
}
else
{
#if defined(CALDGEMM_44) & defined(CALDGEMM_TRANSPOSED_B)
//Row / Col Interleaved Storage with 2 rows stored in one col
for (int y = 0;y < height / 2;y++)
{
double* __restrict__ daddr = dst[y % 2].ptr_double + y / 2 * gpu_width * 2;
const double* __restrict__ saddr = src + 2 * y * pitch;
const double* __restrict__ saddr2 = src + (2 * y + 1) * pitch;
for (int i = 0;i < width;i += 4)
{
#ifdef CALDGEMM_USE_VEC_MEMCPY_PREFETCH
_mm_prefetch(CAST_FOR_MMPREFETCH (saddr + 60), _MM_HINT_NTA);
_mm_prefetch(CAST_FOR_MMPREFETCH (saddr2 + 60), _MM_HINT_NTA);
#endif
_mm_store_pd_use(daddr + 0, _mm_load_pd_use(saddr));
_mm_store_pd_use(daddr + 2, _mm_load_pd_use(saddr2));
_mm_store_pd_use(daddr + 4, _mm_load_pd_use(saddr + 2));
_mm_store_pd_use(daddr + 6, _mm_load_pd_use(saddr2 + 2));
daddr += 8;
saddr += 4;
saddr2 += 4;
}
}
#elif defined(CALDGEMM_44) & defined(CALDGEMM_TRANSPOSED_A)
//Col Interleaved Storage for transposed A with 4x4, 8x4 and 8x8 tiling
if (numBuffers == 4)
{
double* __restrict__ daddr = dst[0].ptr_double;
double* __restrict__ daddr2 = dst[1].ptr_double;
double* __restrict__ daddr3 = dst[2].ptr_double;
double* __restrict__ daddr4 = dst[3].ptr_double;
for (int y=0; y < height; y++)
{
int count = dst[0].DataSize * width;
const double* __restrict__ saddr = src + (y * pitch);
for (int i = 0;i < count;i += 256)
{
#ifdef CALDGEMM_USE_VEC_MEMCPY_PREFETCH
_mm_prefetch(CAST_FOR_MMPREFETCH (saddr + 30), _MM_HINT_NTA);
#endif
_mm_store_pd_use(daddr, _mm_load_pd_use(saddr));
_mm_store_pd_use(daddr2, _mm_load_pd_use(saddr + 2));
_mm_store_pd_use(daddr3, _mm_load_pd_use(saddr + 4));
_mm_store_pd_use(daddr4, _mm_load_pd_use(saddr + 6));
_mm_store_pd_use(daddr + 2, _mm_load_pd_use(saddr + 8));
_mm_store_pd_use(daddr2 + 2, _mm_load_pd_use(saddr + 10));
_mm_store_pd_use(daddr3 + 2, _mm_load_pd_use(saddr + 12));
_mm_store_pd_use(daddr4 + 2, _mm_load_pd_use(saddr + 14));
_mm_store_pd_use(daddr + 4, _mm_load_pd_use(saddr + 16));
_mm_store_pd_use(daddr2 + 4, _mm_load_pd_use(saddr + 18));
_mm_store_pd_use(daddr3 + 4, _mm_load_pd_use(saddr + 20));
_mm_store_pd_use(daddr4 + 4, _mm_load_pd_use(saddr + 22));
_mm_store_pd_use(daddr + 6, _mm_load_pd_use(saddr + 24));
_mm_store_pd_use(daddr2 + 6, _mm_load_pd_use(saddr + 26));
_mm_store_pd_use(daddr3 + 6, _mm_load_pd_use(saddr + 28));
_mm_store_pd_use(daddr4 + 6, _mm_load_pd_use(saddr + 30));
saddr += 32;
daddr += 8;
daddr2+= 8;
daddr3 += 8;
daddr4 += 8;
}
daddr += (gpu_width - width) / numBuffers;
daddr2 += (gpu_width - width) / numBuffers;
daddr3 += (gpu_width - width) / numBuffers;
daddr4 += (gpu_width - width) / numBuffers;
}
}
else if (numBuffers == 1)
{
double* __restrict__ daddr = dst[0].ptr_double;
for (int y=0; y < height; y++)
{
int count = dst[0].DataSize * width;
const double* __restrict__ saddr = src + (y * pitch);
#ifdef CALDGEMM_SHIFT_TEXTURE
if (y & 1)
{
daddr -= 2 * CALDGEMM_SHIFT_TEXTURE;
}
else
{
#if defined(CALDGEMM_STREAMING_STORES_DIVIDE) && CALDGEMM_SHIFT_TEXTURE == 1
__m128d empty;
_mm_store_pd_use(daddr, empty);
#endif
daddr += 2 * CALDGEMM_SHIFT_TEXTURE;
}
#endif
for (int i = 0;i < count;i += 64)
{
#ifdef CALDGEMM_USE_VEC_MEMCPY_PREFETCH
_mm_prefetch(CAST_FOR_MMPREFETCH (saddr + 32), _MM_HINT_NTA);
#endif
_mm_store_pd_use(daddr, _mm_load_pd_use(saddr));
_mm_store_pd_use(daddr + 2, _mm_load_pd_use(saddr + 2));
_mm_store_pd_use(daddr + 4, _mm_load_pd_use(saddr + 4));
_mm_store_pd_use(daddr + 6, _mm_load_pd_use(saddr + 6));
saddr += 8;
daddr += 8;
}
#if defined(CALDGEMM_STREAMING_STORES_DIVIDE) && CALDGEMM_SHIFT_TEXTURE == 1
if ((y & 1) == 0)
{
__m128d empty;
_mm_store_pd_use(daddr, empty);
_mm_store_pd_use(daddr + 2, empty);
_mm_store_pd_use(daddr + 4, empty);
}
#endif
daddr += gpu_pitch - width;
}
}
else
{
double* __restrict__ daddr = dst[0].ptr_double;
double* __restrict__ daddr2 = dst[1].ptr_double;
for (int y=0; y < height; y++)
{
int count = dst[0].DataSize * width;
const double* __restrict__ saddr = src + (y * pitch);
#ifdef CALDGEMM_SHIFT_TEXTURE
if (y & 1)
{
daddr -= 2 * CALDGEMM_SHIFT_TEXTURE;
daddr2 -= 2 * CALDGEMM_SHIFT_TEXTURE;
}
else
{
daddr += 2 * CALDGEMM_SHIFT_TEXTURE;
daddr2 += 2 * CALDGEMM_SHIFT_TEXTURE;
}
#endif
for (int i = 0;i < count;i += 64)
{
#ifdef CALDGEMM_USE_VEC_MEMCPY_PREFETCH
_mm_prefetch(CAST_FOR_MMPREFETCH (saddr + 32), _MM_HINT_NTA);
#endif
_mm_store_pd_use(daddr, _mm_load_pd_use(saddr));
_mm_store_pd_use(daddr2, _mm_load_pd_use(saddr + 2));
_mm_store_pd_use(daddr + 2, _mm_load_pd_use(saddr + 4));
_mm_store_pd_use(daddr2 + 2, _mm_load_pd_use(saddr + 6));
saddr += 8;
daddr += 4;
daddr2+= 4;
}
daddr += gpu_pitch - width / numBuffers;
daddr2 += gpu_pitch - width / numBuffers;
}
}
#else
// Array to store the position from which data will be filled in the various output buffers.
int* position = new int[numBuffers];
memset((void*) position, 0, numBuffers * sizeof(int));
for (int y=0; y < height; y++)
{
int bank = y % numBuffers;
double* daddr = dst[bank].ptr_double + position[bank];
double* saddr = src + (y * pitch);
int count = dst[bank].DataSize * width;
for (int i = 0;i < count;i += 64)
{
#ifdef CALDGEMM_USE_VEC_MEMCPY_PREFETCH
_mm_prefetch(CAST_FOR_MMPREFETCH (saddr + 100), _MM_HINT_NTA);
#endif
_mm_store_pd_use(daddr, _mm_load_pd_use(saddr));
_mm_store_pd_use(daddr + 2, _mm_load_pd_use(saddr + 2));
_mm_store_pd_use(daddr + 4, _mm_load_pd_use(saddr + 4));
_mm_store_pd_use(daddr + 6, _mm_load_pd_use(saddr + 6));
saddr += 8;
daddr += 8;
}
position[bank] += gpu_width;
}
delete[] position;
#endif
}
_mm_mfence();
if (Config->DivideToGPU)
{
for (int i = 0;i < numBuffers;i++)
{
if (Config->ThreadSaveDriver == -1) pthread_mutex_lock(&globalDriverLock);
CHKERR(calResUnmap(dst[i].res), "unmapping input buffer for buffer division");
if (Config->ThreadSaveDriver == -1) pthread_mutex_unlock(&globalDriverLock);
}
}
return(0);
}
#undef _mm_store_pd_use
#ifdef CALDGEMM_STREAMING_STORES_MERGE
#define _mm_store_pd_use _mm_stream_pd
#else
#define _mm_store_pd_use _mm_store_pd
#endif
int caldgemm_cal::mergeBuffers(double* dst, BufferProperties* src, int width, int height, int gpu_width, int gpu_height, int pitch, int numBuffers)
{
if (Config->SkipCPUProcessing) return(0);
#ifdef CALDGEMM_BENCHMARK_KERNEL
return(0);
#endif
if (Config->DstMemory == 'c' && !Config->KeepBuffersMapped)
{
for (unsigned int i = 0;i < dwBuffersC;i++)
{
if (Config->ThreadSaveDriver == -1) pthread_mutex_lock(&globalDriverLock);
CHKERR(calResMap(&src[i].ptr_void, &src[i].pitch, src[i].res, 0), "mapping output buffer for merging");
if (Config->ThreadSaveDriver == -1) pthread_mutex_unlock(&globalDriverLock);
if (((size_t) src[i].ptr_void) & (vcpysize - 1))
{
fprintf(STD_OUT, "Invalid alignment\n");
return(1);
}
}
}
#if defined(CALDGEMM_44) && !defined(CALDGEMM_USE_MEMEXPORT)
const unsigned long long int double_one = 0x3FF0000000000000; //1.0 in double
const unsigned long long int double_minus_one = 0xBFF0000000000000;
if (Config->Width == BufferWidth && reinterpret_cast<unsigned long long int &>(Beta) == double_one && reinterpret_cast<unsigned long long int &>(Alpha) == double_minus_one && (Config->ForceKernelVariant == -1 || Config->ForceKernelVariant == 2))
{
//Special Linpack Function
for (int y = 0;y < height;y++)
{
const int bank = y % 4;
const double* __restrict__ saddr = src[bank].ptr_double + (y / 4) * (gpu_width / 2);
const double* __restrict__ saddr2 = src[bank + 4].ptr_double + (y / 4) * (gpu_width / 2);
double* __restrict__ daddr = dst + (y * pitch);
//int count = src[bank].DataSize * width;
for (int i = 0;i < width;i += 8)
{
#ifdef CALDGEMM_USE_VEC_MEMCPY_PREFETCH
_mm_prefetch(CAST_FOR_MMPREFETCH (saddr + 50), _MM_HINT_NTA);
_mm_prefetch(CAST_FOR_MMPREFETCH (saddr2 + 50), _MM_HINT_NTA);
#if !defined(CALDGEMM_STREAMING_STORES_MERGE) | defined(CALDGEMM_PREFETCH_MERGE_STORES)
#ifndef _NO_AMD_CPU
_m_prefetchw(daddr + 50);
#else
_mm_prefetch(CAST_FOR_MMPREFETCH (daddr + 50), _MM_HINT_NTA);
#endif
#endif
#endif
_mm_store_pd_use(daddr, _mm_sub_pd(_mm_load_pd(daddr), _mm_load_pd(saddr)));
_mm_store_pd_use(daddr + 2, _mm_sub_pd(_mm_load_pd(daddr + 2), _mm_load_pd(saddr2)));
_mm_store_pd_use(daddr + 4, _mm_sub_pd(_mm_load_pd(daddr + 4), _mm_load_pd(saddr + 2)));
_mm_store_pd_use(daddr + 6, _mm_sub_pd(_mm_load_pd(daddr + 6), _mm_load_pd(saddr2 + 2)));
saddr += 4;
saddr2 += 4;
daddr += 8;
#ifdef CALDGEMM_MERGE_NOPS
for (int j = 0;j < CALDGEMM_MERGE_NOPS;j++)
{
#ifdef _WIN32
__asm
{
nop
nop
nop
nop
nop
nop
nop
nop
}
#else
asm volatile ("nop\nnop\nnop\nnop\nnop\nnop\nnop\nnop\n" : : : );
#endif
}
#endif
}
#ifdef CALDGEMM_MERGE_FLUSH
daddr = dst + (y * pitch);
for (int i = 0;i < width;i += 8) _mm_clflush(daddr + i);
#endif
}
}
else
#endif
{
// Array to store the position from which data will be pulled in from the input buffers
int* position = new int[numBuffers];
memset((void*) position, 0, numBuffers * sizeof(int));
for (int y=0; y < height; y++)
{
//CALDGEMM_44 Init
#if defined(CALDGEMM_44) & !defined(CALDGEMM_USE_MEMEXPORT)
int bank = y % 4;
double* saddr2 = src[bank + 4].ptr_double + position[bank];
double* paddr2 = src[(y + 1) % 4 + 4].ptr_double + position[(y + 1) % 4];
#else
int bank = y % numBuffers;
#endif
double* daddr = dst + (y * pitch);
double* saddr = src[bank].ptr_double + position[bank];
double* paddr = src[(y + 1) % 4].ptr_double + position[(y + 1) % 4];
int count = src[bank].DataSize * width;
#if defined(CALDGEMM_44) & !defined(CALDGEMM_USE_MEMEXPORT)
if (Config->KeepBuffersMapped)
{
#ifdef _WIN32
if (Beta == 0.)
#else
if (__fpclassify(Beta) == FP_ZERO)
#endif
{
//CALDGEMM_44 BETA=ZERO HACKED LIB
for (int i = 0;i < count;i += 64)
{
#ifdef CALDGEMM_USE_VEC_MEMCPY_PREFETCH
_mm_prefetch(CAST_FOR_MMPREFETCH (saddr + 50), _MM_HINT_NTA);
_mm_prefetch(CAST_FOR_MMPREFETCH (saddr2 + 50), _MM_HINT_NTA);
#endif
_mm_store_pd_use(daddr, _mm_load_pd(saddr));
_mm_store_pd_use(daddr + 2, _mm_load_pd(saddr2));
_mm_store_pd_use(daddr + 4, _mm_load_pd(saddr + 2));
_mm_store_pd_use(daddr + 6, _mm_load_pd(saddr2 + 2));
saddr += 4;
saddr2 += 4;
daddr += 8;
}
}
else
{
//CALDGEMM_44 GENERAL CASE ORIGINAL LIB
#undef _mm_store_pd_use
#define _mm_store_pd_use _mm_store_pd
__m128d beta = _mm_set1_pd(Beta);
for (int i = 0;i < count;i += 64)
{
#ifdef CALDGEMM_USE_VEC_MEMCPY_PREFETCH
_mm_prefetch(CAST_FOR_MMPREFETCH (saddr + 50), _MM_HINT_NTA);
_mm_prefetch(CAST_FOR_MMPREFETCH (saddr2 + 50), _MM_HINT_NTA);
#ifndef _NO_AMD_CPU
_m_prefetchw(daddr + 50);
#else