-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmatprod.c
6175 lines (5318 loc) · 177 KB
/
matprod.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
/* MATPROD - A LIBRARY FOR MATRIX MULTIPLICATION
C Procedures for Matrix Multiplication Without Pipelining
Copyright (c) 2013, 2014, 2017, 2018 Radford M. Neal.
The matprod library is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/* IMPORTANT NOTE: The interface for the application-visible functions
defined here is documented in api-doc, and the implementation strategy
is documented in imp-doc. These documents should be consulted before
reading this code, and updated if this code is changed. */
#include <stdlib.h>
#include <stdint.h>
/* TUNABLE CACHE OPTIMIZATION CONSTANTS. These values are used to
divide computations into parts, so as to improve cache behaviour.
They are chosen based on the assumed sizes of the L1 and L2 caches.
See imp-doc for more information.
The values below should all be multiples of 8 in order to avoid any
changes to alignment. */
#define VEC_MAT_YROWS (512+256) /* # of rows to look at at once */
#define MAT_VEC_XROWS (1024+512) /* # of elements to compute in a part */
#define OUTER_ROWS (2048-128) /* # of rows to compute in a part */
#define MAT_MAT_XROWS (1024-64) /* # of rows to compute in a part */
#define MAT_MAT_XCOLS 32 /* # of columns in part, if # of rows is max */
#define TRANS1_XROWS (512+128) /* # of rows to look at at once */
#define TRANS1_XCOLS 48 /* # of columns in part, if # of rows is max */
#define TRANS2_XROWS (1024-64) /* # of rows to compute in a part */
#define TRANS2_XCOLS 32 /* # of columns in part, if # of rows is max */
#define TRANS12_ZELEM 2048 /* # of elements can store locally on stack */
#define DOUBLES_IN_LLC 95000 /* # of doubles assumed to fit in the
last-level cache with some space to spare */
/* SETUP TO FACILITATE INCLUSION AND USE OF MATPROD.C IN PAR-MATPROD.C.
If PAR_MATPROD is defined, SCOPE, AMTOUT, EXTRAD, and EXTRAN will
be defined before matprod.c is included. Otherwise, they are
defined here as nothing. EXTRAD declares extra arguments to the
matprod functions for use by AMTOUT, EXTRAN passes them along, and
EXTRAZ passes along null versions when more output should not be
signalled.
Also, matprod.h and perhaps matprod-app.h are included only if
PAR_MATPROD is not defined. */
#ifndef PAR_MATPROD
# ifdef MATPROD_APP_INCLUDED
# include "matprod-app.h"
# endif
# include "matprod.h"
# define SCOPE
# define AMTOUT(z) do {} while (0)
# define EXTRAD
# define EXTRAN
# define EXTRAZ
#endif
/* DEBUGGING FACILITIES. */
#define DEBUG_PRINTF 0 /* Set to 1 to enable printf of procedure args */
#if DEBUG_PRINTF
# ifdef PAR_MATPROD
# define debug_printf helpers_debug
# else
# include <stdio.h>
# define debug_printf printf
# endif
#endif
#define NDEBUG /* Define to disable assertion check */
#include <assert.h>
#define CHK_ALIGN(p) assert (((uintptr_t)(p)&(ALIGN-1)) == ALIGN_OFFSET)
/* SET UP ALIGNMENT DEFINITIONS. */
#ifndef ALIGN
# define ALIGN 1
#endif
#ifndef ALIGN_OFFSET
# define ALIGN_OFFSET 0
#endif
#if ALIGN < 0 || (ALIGN & (ALIGN-1)) != 0
# error "alignment must be a power of two"
#endif
#if ALIGN_OFFSET >= ALIGN
# error "alignment offset must be less than alignment"
#endif
#if ALIGN_OFFSET % 8 != 0
# error "alignment offset must be a multiple of eight"
#endif
#define ALIGN_FORWARD ((ALIGN - ALIGN_OFFSET) % ALIGN)
#if ALIGN >= 8 && __GNUC__ && !defined(NO_ASSUME_ALIGNED)
# define CAN_ASSUME_ALIGNED 1
# define ASSUME_ALIGNED(x,a,o) __builtin_assume_aligned(x,a,o)
#else
# define CAN_ASSUME_ALIGNED 0
# define ASSUME_ALIGNED(x,a,o) (x)
#endif
/* SET UP SIMD DEFINITIONS. */
#if __SSE2__ && !defined(DISABLE_SIMD_CODE)
# define CAN_USE_SSE2 1
#else
# define CAN_USE_SSE2 0
#endif
#if __SSE3__ && !defined(DISABLE_SIMD_CODE)
# define CAN_USE_SSE3 1
#else
# define CAN_USE_SSE3 0
#endif
#if __AVX__ && !defined(DISABLE_SIMD_CODE) && !defined(DISABLE_AVX_CODE)
# define CAN_USE_AVX 1
#else
# define CAN_USE_AVX 0
#endif
#if __AVX2__ && !defined(DISABLE_SIMD_CODE) && !defined(DISABLE_AVX_CODE)
# define CAN_USE_AVX2 1
#else
# define CAN_USE_AVX2 0
#endif
#if CAN_USE_SSE2 || CAN_USE_SSE3 || CAN_USE_AVX || CAN_USE_AVX2
# include <immintrin.h>
#endif
/* VERSIONS OF LOAD AND STORE TAKING ADVANTAGE OF KNOWN ALIGNMENT.
The loadA and storeA macros do an aligned load/store if ALIGN is
suitably large, assuming that any offset has been compensated for.
The loadAA and storeAA macro do an unalign load/store only if ALIGN
is suitably large and ALIGN_OFFSET mod the required alignment is
zero, as is appropriate for an address that is one of the arguments
plus a multiple of ALIGN. */
#define _mm_loadA_pd(w) \
(ALIGN>=16 ? _mm_load_pd(w) : _mm_loadu_pd(w))
#define _mm_storeA_pd(w,v) \
(ALIGN>=16 ? _mm_store_pd(w,v) : _mm_storeu_pd(w,v))
#define _mm256_loadA_pd(w) \
(ALIGN>=32 ? _mm256_load_pd(w) : _mm256_loadu_pd(w))
#define _mm256_storeA_pd(w,v) \
(ALIGN>=32 ? _mm256_store_pd(w,v) : _mm256_storeu_pd(w,v))
#define _mm_loadAA_pd(w) \
(ALIGN>=16 && ALIGN_OFFSET%16==0 ? _mm_load_pd(w) : _mm_loadu_pd(w))
#define _mm_storeAA_pd(w,v) \
(ALIGN>=16 && ALIGN_OFFSET%16==0 ? _mm_store_pd(w,v) : _mm_storeu_pd(w,v))
#define _mm256_loadAA_pd(w) \
(ALIGN>=32 && ALIGN_OFFSET==0 ? _mm256_load_pd(w) : _mm256_loadu_pd(w))
#define _mm256_storeAA_pd(w,v) \
(ALIGN>=32 && ALIGN_OFFSET==0 ? _mm256_store_pd(w,v) : _mm256_storeu_pd(w,v))
/* CONDITIONAL CAST FOR AVX VS. SSE. Macro to cast a variable to
__m128d if it is __m256d, which does nothing when AVX is not
available, and hence the variable will already be __m128d. This
facilitates sharing of code in AVX and SSE sections.
The cast128a version is for use when the AVX code is enabled only if
ENABLE_ALL_AVX_CODE is defined. */
#if CAN_USE_AVX || CAN_USE_AVX2
# define cast128(x) _mm256_castpd256_pd128(x)
#else
# define cast128(x) (x)
#endif
#if (CAN_USE_AVX || CAN_USE_AVX2) && ENABLE_ALL_AVX_CODE
# define cast128a(x) _mm256_castpd256_pd128(x)
#else
# define cast128a(x) (x)
#endif
/* MACRO TO SPLIT COUNT, ALIGNED. Given that M < c <= 2*M, with M a
multiple of 4, finds a value v s.t. v <= M and c-v <= M, and v is
a multiple of 4. */
#define SPLITC(c,M) (((c+3)>>1) & ~3) /* reasonable, but not necessarily best */
/* Set vector/matrix z with s elements to all zeros. This is a
degenerate special case of other operations. */
static void set_to_zeros (double * MATPROD_RESTRICT z, size_t s)
{
size_t i;
for (i = 0; i < s; i++)
{ z[i] = 0.0;
}
}
/* -------------------------------------------------------------------------- */
/* FILL_LOWER */
/* Fill the lower triangle of an n-by-n matrix from the upper
triangle. Fills two rows at once to improve cache performance. */
SCOPE void matprod_fill_lower (double * MATPROD_RESTRICT z, int n)
{
CHK_ALIGN(z);
z = ASSUME_ALIGNED (z, ALIGN, ALIGN_OFFSET);
/* Inner block because this is maybe required by "restrict" usage. */
{
/* Note that the first row requires no action, and hence can be
done or not as helps alignment. */
# if ALIGN_FORWARD & 8
double * MATPROD_RESTRICT zr = z + 1;
double * MATPROD_RESTRICT zc = z + n;
int i = 1;
# else
double * MATPROD_RESTRICT zr = z;
double * MATPROD_RESTRICT zc = z;
int i = 0;
# endif
/* Fill in two rows each time around this loop. */
while (i <= n-2)
{
double *zp = zr;
int j = 0;
while (j <= i-2)
{
# if CAN_USE_SSE2
{ _mm_storeA_pd (zp,
_mm_loadh_pd (_mm_load_sd(zc+j), zc+j+n));
_mm_storeu_pd (zp+n,
_mm_loadh_pd (_mm_load_sd(zc+j+1), zc+j+n+1));
}
# else /* non-SIMD code */
{ zp[0] = zc[j];
zp[1] = (zc+n)[j];
(zp+n)[0] = zc[j+1];
(zp+n)[1] = (zc+n)[j+1];
}
# endif
zp += n; zp += n;
j += 2;
}
if (j < i)
{
# if CAN_USE_SSE2
{ _mm_storeA_pd (zp, _mm_loadh_pd (_mm_load_sd(zc+j), zc+j+n));
}
# else /* non-SIMD code */
{ zp[0] = zc[j];
zp[1] = (zc+n)[j];
}
# endif
zp += n;
}
zp[1] = (zc+n)[i];
zc += n; zc += n;
zr += 2;
i += 2;
}
/* Fill in the last row, if not done above. */
if (i < n)
{ double *zp = zr;
int j = 0;
while (j < i)
{ zp[0] = zc[j];
zp += n;
j += 1;
}
}
}
}
/* -------------------------------------------------------------------------- */
/* SCALAR_VEC */
/* Multiply vector y of length m by scalar x, storing result in vector z. */
#define SCALAR_VEC_THRESH 1024 /* amount threshold for output pipelining */
SCOPE void matprod_scalar_vec (double x, double * MATPROD_RESTRICT y,
double * MATPROD_RESTRICT z, int m EXTRAD)
{
# if DEBUG_PRINTF
debug_printf("scalar_vec %f %p %p - %d\n",
x, y, z, m);
# endif
CHK_ALIGN(y); CHK_ALIGN(z);
y = ASSUME_ALIGNED (y, ALIGN, ALIGN_OFFSET);
z = ASSUME_ALIGNED (z, ALIGN, ALIGN_OFFSET);
/* Handle m of 0, 1, or 2 specially. */
if (m <= 2)
{ if (m == 2)
{ z[0] = x * y[0];
z[1] = x * y[1];
}
else if (m == 1)
{ z[0] = x * y[0];
}
return;
}
int i = 0; /* indexes y and z */
# if CAN_USE_SSE2 || CAN_USE_AVX
{
# if CAN_USE_AVX
__m256d X = _mm256_set1_pd (x);
# else
__m128d X = _mm_set1_pd (x);
# endif
# if ALIGN_FORWARD & 8
{ _mm_store_sd (z, _mm_mul_sd (cast128(X), _mm_load_sd(y)));
i += 1;
}
# endif
# if ALIGN_FORWARD & 16
{ _mm_storeA_pd (z+i, _mm_mul_pd (cast128(X), _mm_loadA_pd(y+i)));
i += 2;
}
# endif
# if CAN_USE_AVX
{
# ifdef PAR_MATPROD
{ while (i < m-SCALAR_VEC_THRESH)
{ int e = i+SCALAR_VEC_THRESH;
while (i < e)
{ _mm256_storeA_pd (z+i, _mm256_mul_pd (X, _mm256_loadA_pd(y+i)));
i += 4;
}
AMTOUT(z+i);
}
}
# endif
while (i <= m-4)
{ _mm256_storeA_pd (z+i, _mm256_mul_pd (X, _mm256_loadA_pd(y+i)));
i += 4;
}
}
# else /* CAN_USE_SSE2 */
{
# ifdef PAR_MATPROD
{ while (i < m-SCALAR_VEC_THRESH)
{ int e = i+SCALAR_VEC_THRESH;
while (i < e)
{ _mm_storeA_pd (z+i, _mm_mul_pd (cast128(X), _mm_loadA_pd(y+i)));
i += 2;
_mm_storeA_pd (z+i, _mm_mul_pd (cast128(X), _mm_loadA_pd(y+i)));
i += 2;
}
AMTOUT(z+i);
}
}
# endif
while (i <= m-4)
{ _mm_storeA_pd (z+i, _mm_mul_pd (cast128(X), _mm_loadA_pd(y+i)));
i += 2;
_mm_storeA_pd (z+i, _mm_mul_pd (cast128(X), _mm_loadA_pd(y+i)));
i += 2;
}
}
# endif
if (i <= m-2)
{ _mm_storeA_pd (z+i, _mm_mul_pd (cast128(X), _mm_loadA_pd(y+i)));
i += 2;
}
if (i < m)
{ _mm_store_sd (z+i, _mm_mul_sd (cast128(X), _mm_load_sd(y+i)));
}
}
# else /* non-SIMD code */
{
# ifdef PAR_MATPROD
{ while (i < m-SCALAR_VEC_THRESH)
{ int e = i+SCALAR_VEC_THRESH;
while (i < e)
{ z[i] = x * y[i];
i += 1;
}
AMTOUT(z+i);
}
}
# endif
while (i < m)
{ z[i] = x * y[i];
i += 1;
}
}
# endif
}
/* -------------------------------------------------------------------------- */
/* VEC_VEC */
/* Dot product of vectors x and y of length k, with result returned as the
function value. */
static double matprod_vec_vec_sub(double * MATPROD_RESTRICT x,
double * MATPROD_RESTRICT y, int k, double s);
SCOPE double matprod_vec_vec (double * MATPROD_RESTRICT x,
double * MATPROD_RESTRICT y, int k)
{
# if DEBUG_PRINTF
debug_printf("vec_vec %p %p - %d\n",
x, y, k);
# endif
CHK_ALIGN(x); CHK_ALIGN(y);
x = ASSUME_ALIGNED (x, ALIGN, ALIGN_OFFSET);
y = ASSUME_ALIGNED (y, ALIGN, ALIGN_OFFSET);
return matprod_vec_vec_sub (x, y, k, 0.0);
}
static double matprod_vec_vec_sub (double * MATPROD_RESTRICT x,
double * MATPROD_RESTRICT y, int k, double s)
{
# if DEBUG_PRINTF
debug_printf("vec_vec_sub %p %p %f - %d\n",
x, y, s, k);
# endif
/* Handle k = 0, 1, or 2 specially. */
if (k <= 2)
{ if (k == 2)
return s + x[0] * y[0] + x[1] * y[1];
else if (k == 1)
return s + x[0] * y[0];
else /* k <= 0 */
return s;
}
int i = 0;
/* Use an unrolled loop to add most products, perhaps using SSE2
or AVX instructions. */
# if CAN_USE_SSE2 || CAN_USE_AVX
{
__m128d S, A;
S = _mm_set_sd(s);
# if (ALIGN_FORWARD & 8)
S = _mm_add_sd (S, _mm_mul_sd (_mm_load_sd(x), _mm_load_sd(y)));
i += 1;
# endif
# if (ALIGN_FORWARD & 16)
A = _mm_mul_pd (_mm_loadA_pd(x+i), _mm_loadA_pd(y+i));
S = _mm_add_sd (A, S);
A = _mm_unpackhi_pd (A, A);
S = _mm_add_sd (A, S);
i += 2;
# endif
# if CAN_USE_AVX && ENABLE_ALL_AVX_CODE
{
__m256d AA;
while (i <= k-8)
{ AA = _mm256_mul_pd (_mm256_loadA_pd(x+i),
_mm256_loadA_pd(y+i));
A = cast128a(AA);
S = _mm_add_sd (A, S);
A = _mm_unpackhi_pd (A, A);
S = _mm_add_sd (A, S);
A = _mm256_extractf128_pd(AA,1);
S = _mm_add_sd (A, S);
A = _mm_unpackhi_pd (A, A);
S = _mm_add_sd (A, S);
AA = _mm256_mul_pd (_mm256_loadA_pd(x+i+4),
_mm256_loadA_pd(y+i+4));
A = cast128a(AA);
S = _mm_add_sd (A, S);
A = _mm_unpackhi_pd (A, A);
S = _mm_add_sd (A, S);
A = _mm256_extractf128_pd(AA,1);
S = _mm_add_sd (A, S);
A = _mm_unpackhi_pd (A, A);
S = _mm_add_sd (A, S);
i += 8;
}
if (i <= k-4)
{ AA = _mm256_mul_pd (_mm256_loadA_pd(x+i),
_mm256_loadA_pd(y+i));
A = cast128a(AA);
S = _mm_add_sd (A, S);
A = _mm_unpackhi_pd (A, A);
S = _mm_add_sd (A, S);
A = _mm256_extractf128_pd(AA,1);
S = _mm_add_sd (A, S);
A = _mm_unpackhi_pd (A, A);
S = _mm_add_sd (A, S);
i += 4;
}
}
# else /* CAN_USE_SSE2 */
{
while (i <= k-4)
{ A = _mm_mul_pd (_mm_loadA_pd(x+i), _mm_loadA_pd(y+i));
S = _mm_add_sd (A, S);
A = _mm_unpackhi_pd (A, A);
S = _mm_add_sd (A, S);
A = _mm_mul_pd (_mm_loadA_pd(x+i+2), _mm_loadA_pd(y+i+2));
S = _mm_add_sd (A, S);
A = _mm_unpackhi_pd (A, A);
S = _mm_add_sd (A, S);
i += 4;
}
}
# endif
if (i <= k-2)
{ A = _mm_mul_pd (_mm_loadA_pd(x+i), _mm_loadA_pd(y+i));
S = _mm_add_sd (A, S);
A = _mm_unpackhi_pd (A, A);
S = _mm_add_sd (A, S);
i += 2;
}
if (i < k)
{ A = _mm_mul_sd (_mm_load_sd(x+i), _mm_load_sd(y+i));
S = _mm_add_sd (A, S);
}
_mm_store_sd (&s, S);
}
# else /* non-SIMD code */
{
# if (ALIGN_FORWARD & 8)
s += x[0] * y[0];
i += 1;
# endif
# if (ALIGN_FORWARD & 16)
s += x[i] * y[i];
s += x[i+1] * y[i+1];
i += 2;
# endif
while (i <= k-4)
{ s += x[i+0] * y[i+0];
s += x[i+1] * y[i+1];
s += x[i+2] * y[i+2];
s += x[i+3] * y[i+3];
i += 4;
}
if (i <= k-2)
{ s += x[i+0] * y[i+0];
s += x[i+1] * y[i+1];
i += 2;
}
if (i < k)
{ s += x[i] * y[i];
}
}
# endif
return s;
}
/* -------------------------------------------------------------------------- */
/* VEC_MAT */
/* Product of row vector (x) of length k and k x m matrix (y), result
stored in z.
Cases where k is 0, 1, or 2 and where m is 0 or 1 are handled
specially. */
static void matprod_vec_mat_sub_yrows (double * MATPROD_RESTRICT x,
double * MATPROD_RESTRICT y,
double * MATPROD_RESTRICT z,
int k, int m, int yrows, int add EXTRAD);
static void matprod_vec_mat_k2 (double * MATPROD_RESTRICT x,
double * MATPROD_RESTRICT y,
double * MATPROD_RESTRICT z, int m);
SCOPE void matprod_vec_mat (double * MATPROD_RESTRICT x,
double * MATPROD_RESTRICT y,
double * MATPROD_RESTRICT z, int k, int m EXTRAD)
{
# if DEBUG_PRINTF
debug_printf("vec_mat %p %p %p - %d %d\n",
x, y, z, k, m);
# endif
CHK_ALIGN(x); CHK_ALIGN(y); CHK_ALIGN(z);
x = ASSUME_ALIGNED (x, ALIGN, ALIGN_OFFSET);
y = ASSUME_ALIGNED (y, ALIGN, ALIGN_OFFSET);
z = ASSUME_ALIGNED (z, ALIGN, ALIGN_OFFSET);
if (m <= 1)
{ if (m == 1)
{ z[0] = matprod_vec_vec (x, y, k);
}
return;
}
/* Specially handle cases where y has two or fewer rows. */
if (k <= 2)
{
if (k == 2)
{ matprod_vec_mat_k2 (x, y, z, m);
}
else if (k == 1)
{ matprod_scalar_vec (x[0], y, z, m EXTRAN);
}
else /* k == 0 */
{ set_to_zeros (z, m);
}
return;
}
/* The general case with k > 2. Calls matprod_vec_mat_sub_yrows to do parts
(only one part if y is a matrix with fewer than VEC_MAT_YROWS). */
int yrows = k;
int add = 0;
if (yrows > VEC_MAT_YROWS && m > 4)
{ while (yrows >= 2*VEC_MAT_YROWS)
{ matprod_vec_mat_sub_yrows(x, y, z, k, m, VEC_MAT_YROWS, add EXTRAZ);
x += VEC_MAT_YROWS;
y += VEC_MAT_YROWS;
yrows -= VEC_MAT_YROWS;
add = 1;
}
if (yrows > VEC_MAT_YROWS)
{ int nr = SPLITC (yrows, VEC_MAT_YROWS);
matprod_vec_mat_sub_yrows(x, y, z, k, m, nr, add EXTRAZ);
x += nr;
y += nr;
yrows -= nr;
add = 1;
}
}
matprod_vec_mat_sub_yrows (x, y, z, k, m, yrows, add EXTRAN);
}
/* Multiply the first 'yrows' elements of vector x with the first
'yrows' rows of matrix y, storing the result in z if 'add' is zero,
or adding the result to z if 'add' is non-zero. Note that x and y
may not be the start of the original vector/matrix. The k argument
is the number of rows in the original y, which is the amount to
step to go right to an element in the same row and the next column.
The m argument is the number of columns in y.
The same alignment assumptions hold for x, y, and z as with the
visible procedures.
Note that k and 'yrows' will be greater than 2. */
static void matprod_vec_mat_sub_yrows (double * MATPROD_RESTRICT x,
double * MATPROD_RESTRICT y,
double * MATPROD_RESTRICT z,
int k, int m, int yrows, int add EXTRAD)
{
# if DEBUG_PRINTF
debug_printf("vec_mat_sub_yrows %p %p %p - %d %d %d\n",
x, y, z, k, m, yrows);
# endif
assert (k > 2);
assert (yrows > 2);
CHK_ALIGN(x); CHK_ALIGN(y); CHK_ALIGN(z);
x = ASSUME_ALIGNED (x, ALIGN, ALIGN_OFFSET);
y = ASSUME_ALIGNED (y, ALIGN, ALIGN_OFFSET);
z = ASSUME_ALIGNED (z, ALIGN, ALIGN_OFFSET);
/* Each iteration of this loop computes four consecutive elements
of the result vector, by doing four dot products of x with
columns of y. Adjusts y, z, and m as it goes.
For SIMD code, the sums for the dot products are initialized to
from zero to three dot products, as helps alignment, plus the
current value in z, if 'add' is set. */
while (m >= 4)
{
int i = 0;
# if CAN_USE_AVX
{
__m256d S, B;
if (add)
S = _mm256_loadu_pd(z);
else
S = _mm256_setzero_pd();
# if ALIGN_FORWARD & 8
B = _mm256_set_pd ((y+k+k+k)[i], (y+k+k)[i], (y+k)[i], y[i]);
B = _mm256_mul_pd (_mm256_set1_pd(x[i]), B);
S = _mm256_add_pd (B, S);
i += 1;
# endif
# if ALIGN_FORWARD & 16
{
__m128d Y0, Y1, Y2, Y3;
__m256d T0, T1;
Y0 = _mm_loadA_pd(y+i);
Y1 = _mm_loadu_pd(y+i+k);
Y2 = _mm_loadA_pd(y+i+k+k);
Y3 = _mm_loadu_pd(y+i+k+k+k);
T0 = _mm256_castpd128_pd256 (Y0);
T0 = _mm256_insertf128_pd (T0, Y2, 1);
T1 = _mm256_castpd128_pd256 (Y1);
T1 = _mm256_insertf128_pd (T1, Y3, 1);
B = _mm256_unpacklo_pd (T0, T1);
B = _mm256_mul_pd (_mm256_set1_pd(x[i]), B);
S = _mm256_add_pd (B, S);
B = _mm256_unpackhi_pd (T0, T1);
B = _mm256_mul_pd (_mm256_set1_pd(x[i+1]), B);
S = _mm256_add_pd (B, S);
i += 2;
}
# endif
while (i <= yrows-4)
{ __m256d Y0, Y1, Y2, Y3;
__m256d T0, T1;
Y0 = _mm256_loadA_pd(y+i);
Y1 = _mm256_loadu_pd(y+i+k);
Y2 = _mm256_loadu_pd(y+i+k+k);
Y3 = _mm256_loadu_pd(y+i+k+k+k);
T0 = _mm256_permute2f128_pd (Y0, Y2, 0x20);
T1 = _mm256_permute2f128_pd (Y1, Y3, 0x20);
B = _mm256_unpacklo_pd (T0, T1);
B = _mm256_mul_pd (_mm256_set1_pd(x[i]), B);
S = _mm256_add_pd (B, S);
B = _mm256_unpackhi_pd (T0, T1);
B = _mm256_mul_pd (_mm256_set1_pd(x[i+1]), B);
S = _mm256_add_pd (B, S);
T0 = _mm256_permute2f128_pd (Y0, Y2, 0x31);
T1 = _mm256_permute2f128_pd (Y1, Y3, 0x31);
B = _mm256_unpacklo_pd (T0, T1);
B = _mm256_mul_pd (_mm256_set1_pd(x[i+2]), B);
S = _mm256_add_pd (B, S);
B = _mm256_unpackhi_pd (T0, T1);
B = _mm256_mul_pd (_mm256_set1_pd(x[i+3]), B);
S = _mm256_add_pd (B, S);
i += 4;
}
if (i <= yrows-2)
{ __m128d Y0, Y1, Y2, Y3;
__m256d T0, T1;
Y0 = _mm_loadA_pd(y+i);
Y1 = _mm_loadu_pd(y+i+k);
Y2 = _mm_loadA_pd(y+i+k+k);
Y3 = _mm_loadu_pd(y+i+k+k+k);
T0 = _mm256_castpd128_pd256 (Y0);
T0 = _mm256_insertf128_pd (T0, Y2, 1);
T1 = _mm256_castpd128_pd256 (Y1);
T1 = _mm256_insertf128_pd (T1, Y3, 1);
B = _mm256_unpacklo_pd (T0, T1);
B = _mm256_mul_pd (_mm256_set1_pd(x[i]), B);
S = _mm256_add_pd (B, S);
B = _mm256_unpackhi_pd (T0, T1);
B = _mm256_mul_pd (_mm256_set1_pd(x[i+1]), B);
S = _mm256_add_pd (B, S);
i += 2;
}
if (i < yrows)
{ B = _mm256_set_pd ((y+k+k+k)[i], (y+k+k)[i], (y+k)[i], y[i]);
B = _mm256_mul_pd (_mm256_set1_pd(x[i]), B);
S = _mm256_add_pd (B, S);
}
_mm256_storeu_pd (z, S);
}
# elif CAN_USE_SSE2 && ALIGN >= 16 /* works, but slower, when unaligned */
{
__m128d S0, S1;
if (add)
{ S0 = _mm_loadu_pd(z);
S1 = _mm_loadu_pd(z+2);
}
else
{ S0 = _mm_setzero_pd();
S1 = _mm_setzero_pd();
}
# if ALIGN_FORWARD & 8
{
__m128d P = _mm_set1_pd(x[i]);
S0 = _mm_add_pd(S0, _mm_mul_pd (P,
_mm_set_pd ((y+k)[i], y[i])));
S1 = _mm_add_pd(S1, _mm_mul_pd (P,
_mm_set_pd ((y+k+k+k)[i], (y+k+k)[i])));
i += 1;
}
# endif
if (ALIGN < 16 || (k & 1)) /* no alignment for second column */
{ while (i <= yrows-2)
{ __m128d T0, T1;
__m128d P = _mm_loadA_pd(x+i);
T0 = _mm_mul_pd (_mm_loadA_pd(y+i), P);
T1 = _mm_mul_pd (_mm_loadu_pd(y+i+k), P);
S0 = _mm_add_pd (_mm_unpacklo_pd(T0,T1), S0);
S0 = _mm_add_pd (_mm_unpackhi_pd(T0,T1), S0);
T0 = _mm_mul_pd (_mm_loadA_pd(y+i+k+k), P);
T1 = _mm_mul_pd (_mm_loadu_pd(y+i+k+k+k), P);
S1 = _mm_add_pd (_mm_unpacklo_pd(T0,T1), S1);
S1 = _mm_add_pd (_mm_unpackhi_pd(T0,T1), S1);
i += 2;
}
}
else /* second column has same 16-byte alignment as first */
{ while (i <= yrows-2)
{ __m128d T0, T1;
__m128d P = _mm_loadA_pd(x+i);
T0 = _mm_mul_pd (_mm_loadA_pd(y+i), P);
T1 = _mm_mul_pd (_mm_loadA_pd(y+i+k), P);
S0 = _mm_add_pd (_mm_unpacklo_pd(T0,T1), S0);
S0 = _mm_add_pd (_mm_unpackhi_pd(T0,T1), S0);
T0 = _mm_mul_pd (_mm_loadA_pd(y+i+k+k), P);
T1 = _mm_mul_pd (_mm_loadA_pd(y+i+k+k+k), P);
S1 = _mm_add_pd (_mm_unpacklo_pd(T0,T1), S1);
S1 = _mm_add_pd (_mm_unpackhi_pd(T0,T1), S1);
i += 2;
}
}
if (i < yrows)
{ __m128d P = _mm_set1_pd(x[i]);
S0 = _mm_add_pd (S0,
_mm_mul_pd (P, _mm_set_pd((y+k)[i],y[i])));
S1 = _mm_add_pd (S1,
_mm_mul_pd (P, _mm_set_pd((y+k+k+k)[i],(y+k+k)[i])));
}
_mm_storeu_pd (z, S0);
_mm_storeu_pd (z+2, S1);
}
# else /* non-SIMD code */
{
double s[4];
if (add)
{ s[0] = z[0];
s[1] = z[1];
s[2] = z[2];
s[3] = z[3];
}
else
s[0] = s[1] = s[2] = s[3] = 0.0;
while (i <= yrows-2)
{ s[0] += x[i+0] * y[i];
s[1] += x[i+0] * (y+k)[i];
s[2] += x[i+0] * (y+k+k)[i];
s[3] += x[i+0] * (y+k+k+k)[i];
s[0] += x[i+1] * y[i+1];
s[1] += x[i+1] * (y+k)[i+1];
s[2] += x[i+1] * (y+k+k)[i+1];
s[3] += x[i+1] * (y+k+k+k)[i+1];
i += 2;
}
if (i < yrows)
{ s[0] += x[i] * y[i];
s[1] += x[i] * (y+k)[i];
s[2] += x[i] * (y+k+k)[i];
s[3] += x[i] * (y+k+k+k)[i];
}
z[0] = s[0];
z[1] = s[1];
z[2] = s[2];
z[3] = s[3];
}
# endif
y += k; y += k; y += k; y += k;
z += 4;
m -= 4;
AMTOUT(z);
}
/* Compute the final few dot products left over from the loop above. */
if (m == 3) /* Do three more dot products */
{
int i = 0;
# if CAN_USE_AVX || CAN_USE_SSE2 && ALIGN >= 16 /* slower unaligned */
{
__m128d S, S2;
if (add)
{ S = _mm_loadu_pd(z);
S2 = _mm_load_sd(z+2);
}
else
{ S = _mm_setzero_pd();
S2 = _mm_setzero_pd();
}
# if ALIGN_FORWARD & 8
{ __m128d P = _mm_set1_pd(x[i]);
S = _mm_add_pd (S, _mm_mul_pd (P, _mm_set_pd ((y+k)[i], y[i])));
S2 = _mm_add_sd (S2, _mm_mul_sd (P, _mm_set_sd ((y+k+k)[i])));
i += 1;
}
# endif
# if ALIGN_FORWARD & 16
{ __m128d P = _mm_loadA_pd(x+i);
__m128d T0 = _mm_mul_pd (_mm_loadA_pd(y+i), P);
__m128d T1 = _mm_mul_pd (_mm_loadu_pd(y+i+k), P);
__m128d T2 = _mm_mul_pd (_mm_loadA_pd(y+i+k+k), P);
S = _mm_add_pd (_mm_unpacklo_pd(T0,T1), S);
S = _mm_add_pd (_mm_unpackhi_pd(T0,T1), S);