forked from stecue/sgemm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscalar_noncblas_sgemm.cpp
More file actions
259 lines (248 loc) · 6.45 KB
/
Copy pathscalar_noncblas_sgemm.cpp
File metadata and controls
259 lines (248 loc) · 6.45 KB
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
#include <string.h>
const int bb_nCols = 52;
const int bb_nRows = 60;
struct noncblas_sgemm_prm_t {
int M;
int lda;
int ldc;
float alpha;
float bb[bb_nCols*bb_nRows];
};
static void scalar_noncblas_sgemm_core(
const noncblas_sgemm_prm_t* pPrm,
const float *A,
float *C)
{
int lda = pPrm->lda;
int ldc = pPrm->ldc;
int m;
for (m = 0; m < pPrm->M-1; A += lda*2, C += ldc*2, m += 2) {
for (int n = 0; n < bb_nCols; n += 2) {
const float *Bcol = &pPrm->bb[n];
float acc00 = 0;
float acc10 = 0;
float acc01 = 0;
float acc11 = 0;
for (int k = 0; k < bb_nRows; ++k) {
acc00 += A[k] * Bcol[0];
acc10 += A[k] * Bcol[1];
acc01 += A[k+lda] * Bcol[0];
acc11 += A[k+lda] * Bcol[1];
Bcol += bb_nCols;
}
C[n+0] += acc00*pPrm->alpha;
C[n+1] += acc10*pPrm->alpha;
C[ldc+n+0] += acc01*pPrm->alpha;
C[ldc+n+1] += acc11*pPrm->alpha;
}
}
if (m < pPrm->M) {
for (int n = 0; n < bb_nCols; n += 2) {
const float *Bcol = &pPrm->bb[n];
float acc00 = 0;
float acc10 = 0;
for (int k = 0; k < bb_nRows; ++k) {
acc00 += A[k] * Bcol[0];
acc10 += A[k] * Bcol[1];
Bcol += bb_nCols;
}
C[n+0] += acc00*pPrm->alpha;
C[n+1] += acc10*pPrm->alpha;
}
}
}
static void scalar_noncblas_sgemm_core_bottomRows(
const noncblas_sgemm_prm_t* pPrm,
const float *A,
float *C,
int nRows)
{
int lda = pPrm->lda;
int ldc = pPrm->ldc;
int m;
for (m = 0; m < pPrm->M-1; A += lda*2, C += ldc*2, m += 2) {
for (int n = 0; n < bb_nCols; n += 2) {
const float *Bcol = &pPrm->bb[n];
float acc00 = 0;
float acc10 = 0;
float acc01 = 0;
float acc11 = 0;
for (int k = 0; k < nRows; ++k) {
acc00 += A[k] * Bcol[0];
acc10 += A[k] * Bcol[1];
acc01 += A[k+lda] * Bcol[0];
acc11 += A[k+lda] * Bcol[1];
Bcol += bb_nCols;
}
C[n+0] += acc00*pPrm->alpha;
C[n+1] += acc10*pPrm->alpha;
C[ldc+n+0] += acc01*pPrm->alpha;
C[ldc+n+1] += acc11*pPrm->alpha;
}
}
if (m < pPrm->M) {
for (int n = 0; n < bb_nCols; n += 2) {
const float *Bcol = &pPrm->bb[n];
float acc00 = 0;
float acc10 = 0;
for (int k = 0; k < nRows; ++k) {
acc00 += A[k] * Bcol[0];
acc10 += A[k] * Bcol[1];
Bcol += bb_nCols;
}
C[n+0] += acc00*pPrm->alpha;
C[n+1] += acc10*pPrm->alpha;
}
}
}
static void scalar_noncblas_sgemm_core_rightmostColumns(
const noncblas_sgemm_prm_t* pPrm,
const float *A,
float *C,
int nCols,
int nRows)
{
int lda = pPrm->lda;
int ldc = pPrm->ldc;
int m;
for (m = 0; m < pPrm->M-1; A += lda*2, C += ldc*2, m += 2) {
int n;
for (n = 0; n < nCols-1; n += 2) {
const float *Bcol = &pPrm->bb[n];
float acc00 = 0;
float acc10 = 0;
float acc01 = 0;
float acc11 = 0;
for (int k = 0; k < nRows; ++k) {
acc00 += A[k] * Bcol[0];
acc10 += A[k] * Bcol[1];
acc01 += A[k+lda] * Bcol[0];
acc11 += A[k+lda] * Bcol[1];
Bcol += bb_nCols;
}
C[n+0] += acc00*pPrm->alpha;
C[n+1] += acc10*pPrm->alpha;
C[ldc+n+0] += acc01*pPrm->alpha;
C[ldc+n+1] += acc11*pPrm->alpha;
}
if (n < nCols) {
const float *Bcol = &pPrm->bb[n];
float acc00 = 0;
float acc01 = 0;
for (int k = 0; k < nRows; ++k) {
acc00 += A[k] * Bcol[0];
acc01 += A[k+lda] * Bcol[0];
Bcol += bb_nCols;
}
C[n+0] += acc00*pPrm->alpha;
C[ldc+n+0] += acc01*pPrm->alpha;
}
}
if (m < pPrm->M) {
int n;
for (n = 0; n < nCols-1; n += 2) {
const float *Bcol = &pPrm->bb[n];
float acc00 = 0;
float acc10 = 0;
for (int k = 0; k < nRows; ++k) {
acc00 += A[k] * Bcol[0];
acc10 += A[k] * Bcol[1];
Bcol += bb_nCols;
}
C[n+0] += acc00*pPrm->alpha;
C[n+1] += acc10*pPrm->alpha;
}
if (n < nCols) {
const float *Bcol = &pPrm->bb[n];
float acc00 = 0;
for (int k = 0; k < nRows; ++k) {
acc00 += A[k] * Bcol[0];
Bcol += bb_nCols;
}
C[n+0] += acc00*pPrm->alpha;
}
}
}
static void scalar_noncblas_sgemm_multC(
int M, int N,
float beta,
float *C, int ldc)
{
if (beta != 0) {
for (int m = 0; m < M; ++m) {
for (int n = 0; n < N; ++n)
C[n] *= beta;
C += ldc;
}
} else {
for (int m = 0; m < M; ++m) {
for (int n = 0; n < N; ++n)
C[n] = 0;
C += ldc;
}
}
}
void scalar_noncblas_sgemm(
int M, int N, int K,
float alpha,
const float *A, int lda,
const float *B, int ldb,
float beta,
float *C, int ldc)
{
scalar_noncblas_sgemm_multC(M, N, beta, C, ldc);
noncblas_sgemm_prm_t prm;
prm.M = M;
prm.lda = lda;
prm.ldc = ldc;
prm.alpha = alpha;
int n_Rsteps = K / bb_nRows;
int n_Csteps = N / bb_nCols;
int row = 0;
for (int ri = 0; ri < n_Rsteps; ++ri) {
int col = 0;
for (int ci = 0; ci < n_Csteps; ++ci) {
// process full rectangles
const float* bSrc = &B[row*ldb + col];
for (int i = 0; i < bb_nRows; ++i) {
memcpy(&prm.bb[bb_nCols*i], bSrc, bb_nCols*sizeof(*B));
bSrc += ldb;
}
scalar_noncblas_sgemm_core(&prm, &A[row], &C[col]);
col += bb_nCols;
}
if (col < N) {
// process rightmost rectangle of the full-height band
const float* bSrc = &B[row*ldb + col];
for (int i = 0; i < bb_nRows; ++i) {
memcpy(&prm.bb[bb_nCols*i], bSrc, (N-col)*sizeof(*B));
bSrc += ldb;
}
scalar_noncblas_sgemm_core_rightmostColumns(&prm, &A[row], &C[col], N-col, bb_nRows);
}
row += bb_nRows;
}
if (row < K) {
// bottom band
int col = 0;
for (int ci = 0; ci < n_Csteps; ++ci) {
// process full-width rectangles
const float* bSrc = &B[row*ldb + col];
for (int i = 0; i < K-row; ++i) {
memcpy(&prm.bb[bb_nCols*i], bSrc, bb_nCols*sizeof(*B));
bSrc += ldb;
}
scalar_noncblas_sgemm_core_bottomRows(&prm, &A[row], &C[col], K-row);
col += bb_nCols;
}
if (col < N) {
// process bottom-right corner rectangle
const float* bSrc = &B[row*ldb + col];
for (int i = 0; i < K-row; ++i) {
memcpy(&prm.bb[bb_nCols*i], bSrc, (N-col)*sizeof(*B));
bSrc += ldb;
}
scalar_noncblas_sgemm_core_rightmostColumns(&prm, &A[row], &C[col], N-col, K-row);
}
}
}