-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdgemm-jornada-sse.c
237 lines (210 loc) · 7.39 KB
/
dgemm-jornada-sse.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
const char* dgemm_desc = "jornada1";
#define likely(x) __builtin_expect((x),1)
#define unlikely(x) __builtin_expect((x),0)
#define ALIGNED_BLOCKS
#define TRANSPOSE
#if !defined(BLOCK_SIZE2)
#define BLOCK_SIZE2 160
#endif
#if !defined(BLOCK_SIZE1)
#define BLOCK_SIZE1 40
#endif
#define min(a,b) (((a)<(b))?(a):(b))
//#include <xmmintrin.h>
#include <emmintrin.h>
#include <stdlib.h>
#include <string.h>
static int lda_A;
/* This auxiliary subroutine performs a smaller dgemm operation
* C := C + A * B
* where C is M-by-N, A is M-by-K, and B is K-by-N. */
static void do_block (int lda, int M, int N, int K, double* restrict A, double* restrict B, double* restrict C) {
//__builtin_prefetch(A + k*lda, 0, 1);
/* For each column j of B */
for (int j = 0; j < N; ++j) {
/* For each row i of A */
for (int i = 0; i < M; ++i) {
/* Compute C(i,j) */
double cij = C[i+j*lda];
for (int k = 0; k < K; ++k)
cij += A[i+k*lda] * B[k+j*lda];
C[i+j*lda] = cij;
}
}
}
//transposed version
static void do_block_T (int lda, int M, int N, int K, double* restrict A_T, double* restrict B, double* restrict C) {
/* For each column j of B */
int j_lda = 0;
for (int j = 0; j < N; ++j, j_lda+=lda) {
//__builtin_prefetch(B + j_lda, 0, 2);
//__builtin_prefetch(C + j_lda, 1, 2);
/* For each row i of A */
int i_lda_A = 0;
int ij_lda = j_lda;
for (int i = 0; i < M; ++i, i_lda_A+=lda_A, ij_lda++) {
//__builtin_prefetch(A_T + i_lda, 0, 2);
/* Compute C(i,j) */
double cij = C[ij_lda];
int ki_lda_A = i_lda_A;
int kj_lda = j_lda;
for (int k = 0; k < K; ++k) {
cij += A_T[ki_lda_A++] * B[kj_lda++];
}
C[ij_lda] = cij;
}
}
}
/* This auxiliary subroutine performs a smaller dgemm operation
* C := C + A * B
* where C is M-by-N, A is M-by-K, and B is K-by-N.
* This function assumes that the submatrix is size BLOCK_SIZE
*/
static void do_exact_block (int lda, double* restrict A, double* restrict B, double* restrict C) {
/* For each column j of B */
for (int j = 0; j < BLOCK_SIZE1; ++j) {
int j_lda = j * lda;
/* For each row i of A */
for (int i = 0; i < BLOCK_SIZE1; ++i) {
int ij_lda = i + j_lda;
/* Compute C(i,j) */
double cij = C[ij_lda];
for (int k = 0; k < BLOCK_SIZE1; ++k) {
cij += A[i+k*lda] * B[k+j_lda];
}
C[ij_lda] = cij;
}
}
}
//transposed version
static void do_exact_block_T (int lda, double* restrict A_T, double* restrict B, double* restrict C) {
/* For each column j of B */
int j_lda = 0;
int lda2 = lda;
for (int j = 0; j < BLOCK_SIZE1; j+=1, j_lda+=lda2) {
/* For each row i of A */
int i_lda_A = 0;
int ij_lda = j_lda;
for (int i = 0; i < BLOCK_SIZE1; i+=1, i_lda_A+=lda_A, ij_lda+=1) {
/* Compute C(i,j) */
double cij = C[ij_lda];
__m128d tmp_c = _mm_setzero_pd();
//__m128d tmp_c;
int ki_lda_A = i_lda_A;
int kj_lda = j_lda;
for (int k = 0; k < BLOCK_SIZE1; k+=2) {
__m128d a = _mm_load_pd( A_T + ki_lda_A );
__m128d b = _mm_loadu_pd( B + kj_lda );
/*
__m128d a = _mm_loadu_pd( A_T + ki_lda );
__m128d b = _mm_loadu_pd( B + kj_lda );
*/
tmp_c = _mm_add_pd(tmp_c, _mm_mul_pd(a, b));
ki_lda_A+=2;
kj_lda+=2;
//cij += A_T[ki_lda++] * B[kj_lda++];
//cij += A_T[ki_lda++] * B[kj_lda++];
}
double __attribute__ ((aligned (16))) c_[2];
//double t[2];
_mm_store_pd( c_, tmp_c );
C[ij_lda] = cij + c_[0] + c_[1];
}
}
}
static void L1_dgemm (int lda, int I, int J, int K, double* restrict A_T, double* restrict B, double* restrict C) {
/* For each block-column of B */
for (int j = 0; j < J; j += BLOCK_SIZE1)
/* For each block-row of A */
for (int i = 0; i < I; i += BLOCK_SIZE1)
/* Accumulate block dgemms into block of C */
for (int k = 0; k < K; k += BLOCK_SIZE1) {
//__builtin_prefetch(A_T + k + i*lda, 0, 2);
//__builtin_prefetch(B + k + j*lda, 0, 2);
//__builtin_prefetch(C + i + j*lda, 1, 2);
//__builtin_prefetch(C + i + j*lda, 0, 2);
if ( (i<=(I-BLOCK_SIZE1)) && (j<=(J-BLOCK_SIZE1)) && (k<=(K-BLOCK_SIZE1))){
//do_exact_block(lda, A + i + k*lda, B + k + j*lda, C + i + j*lda);
do_exact_block_T(lda, A_T + k + i*lda_A, B + k + j*lda, C + i + j*lda);
} else {
/* Correct block dimensions if block "goes off edge of" the matrix */
int I_ = min (BLOCK_SIZE1, I-i);
int J_ = min (BLOCK_SIZE1, J-j);
int K_ = min (BLOCK_SIZE1, K-k);
/* Perform individual block dgemm */
do_block_T(lda, I_, J_, K_, A_T + k + i*lda_A, B + k + j*lda, C + i + j*lda);
}
}
}
//size of submatrix == BLOCK_SIZE2
static void L1_dgemm_exact (int lda, double* restrict A_T, double* restrict B, double* restrict C) {
/* For each block-column of B */
for (int j = 0; j < BLOCK_SIZE2; j += BLOCK_SIZE1)
/* For each block-row of A */
for (int i = 0; i < BLOCK_SIZE2; i += BLOCK_SIZE1)
/* Accumulate block dgemms into block of C */
for (int k = 0; k < BLOCK_SIZE2; k += BLOCK_SIZE1) {
#ifdef ALIGNED_BLOCKS
do_exact_block_T(lda, A_T + k + i*lda_A, B + k + j*lda, C + i + j*lda);
#else
if ( (i<=(BLOCK_SIZE2-BLOCK_SIZE1)) && (j<=(BLOCK_SIZE2-BLOCK_SIZE1)) && (k<=(BLOCK_SIZE2-BLOCK_SIZE1)) ){
do_exact_block_T(lda, A_T + k + i*lda_A, B + k + j*lda, C + i + j*lda);
} else {
/* Correct block dimensions if block "goes off edge of" the matrix */
int I_ = min (BLOCK_SIZE1, BLOCK_SIZE2-i);
int J_ = min (BLOCK_SIZE1, BLOCK_SIZE2-j);
int K_ = min (BLOCK_SIZE1, BLOCK_SIZE2-k);
/* Perform individual block dgemm */
do_block_T(lda, I_, J_, K_, A_T + k + i*lda_A, B + k + j*lda, C + i + j*lda);
}
#endif
}
}
inline static void L2_dgemm (int lda, double* restrict A_T, double* restrict B, double* restrict C) {
/* For each block-column of B */
for (int j = 0; j < lda; j += BLOCK_SIZE2)
/* For each block-row of A */
for (int i = 0; i < lda; i += BLOCK_SIZE2)
/* Accumulate block dgemms into block of C */
for (int k = 0; k < lda; k += BLOCK_SIZE2) {
if ( (i<=(lda-BLOCK_SIZE2)) && (j<=(lda-BLOCK_SIZE2)) && (k<=(lda-BLOCK_SIZE2)) ){
//do_exact_block(lda, A + i + k*lda, B + k + j*lda, C + i + j*lda);
L1_dgemm_exact(lda, A_T + k + i*lda_A, B + k + j*lda, C + i + j*lda);
} else {
/* Correct block dimensions if block "goes off edge of" the matrix */
int I_ = min (BLOCK_SIZE2, lda-i);
int J_ = min (BLOCK_SIZE2, lda-j);
int K_ = min (BLOCK_SIZE2, lda-k);
/* Perform individual block dgemm */
//do_block_T(lda, M, N, K, A_T + k + i*lda, B + k + j*lda, C + i + j*lda);
L1_dgemm(lda, I_, J_, K_, A_T + k + i*lda_A, B + k + j*lda, C + i + j*lda);
}
}
}
/* This routine performs a dgemm operation
* C := C + A * B
* where A, B, and C are lda-by-lda matrices stored in column-major format.
* On exit, A and B maintain their input values. */
void square_dgemm (int lda, double* restrict A, double* restrict B, double* restrict C) {
double* restrict __attribute__((aligned(16))) A_T;
//transpose A
//TODO: blocked version?
#ifdef TRANSPOSE
//lda_A = (lda/2)*2;
//if (lda%2) lda_A += 1;
lda_A = ((lda+1)>>1)<<1;
posix_memalign((void**) &A_T, 16, sizeof(double)*lda_A*lda);
//A_T = (double*) malloc(sizeof(double)*lda*lda);
for (int i=0; i<lda; i++)
for (int j=0; j<lda; j++)
A_T[i + lda_A*j] = A[i*lda + j];
#endif
if (lda<=BLOCK_SIZE1) {
do_block_T(lda, lda, lda, lda, A_T, B, C);
} else if (lda<=BLOCK_SIZE2){
L1_dgemm(lda, lda, lda, lda, A_T, B, C);
} else {
L2_dgemm(lda, A_T, B, C);
}
free(A_T);
}