-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
371 lines (296 loc) · 11.3 KB
/
main.cpp
File metadata and controls
371 lines (296 loc) · 11.3 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
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
//------------------------------------------------------------------------------------
// Matrix multiplication code adapted from an SME programming example provided
// by Arm Ltd.
//
// Assumptions :
// - Input and Output matricies are in row-major
// - nbr in matLeft (M): any
// - nbc in matLeft, nbr in matRight (K): any K > 2
// - nbc in matRight (N): any
//------------------------------------------------------------------------------------
#include <iostream>
#include <random>
#include <chrono>
#include <cstdint>
#include <stdlib.h>
#include <Accelerate/Accelerate.h>
#define ERROR_TOLERANCE 0.0002f
void matmul_ref(const uint64_t rows_l, const uint64_t cols_l,const uint64_t cols_r, const float * input_left,
const float * input_right, float * output) {
for (uint64_t x = 0; x < rows_l; ++x) {
for (uint64_t y = 0; y < cols_r; ++y) {
float acc = 0.0f;
for (uint64_t z = 0; z < cols_l; ++z) {
acc += input_left[(x * cols_l) + z] * input_right[(z * cols_r) + y];
}
output[(x * cols_r) + y] = acc;
}
}
}
void random_matrix( int m, int n, float *a)
{
double drand48();
//#pragma omp parallel for num_threads(num)
for(int i =0 ; i < m * n; i ++)
a[i]= 2.0 * (float)drand48( ) - 1.0 + 0.0000001 * (i/m);
}
uint64_t sme_cntw() {
uint64_t cnt;
asm volatile(" smstart sm \n" // smstart sm
" cntw %[res]\n"
" smstop sm \n" // smstop sm
: [res] "=r"(cnt)
:
: "p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7", "p8", "p9",
"p10", "p11", "p12", "p13", "p14", "p15", "z0", "z1", "z2",
"z3", "z4", "z5", "z6", "z7", "z8", "z9", "z10", "z11", "z12",
"z13", "z14", "z15", "z16", "z17", "z18", "z19", "z20", "z21",
"z22", "z23", "z24", "z25", "z26", "z27", "z28", "z29", "z30",
"z31");
//cnt = 16;
return cnt;
}
extern "C"{
extern void sgemm_direct_sme1_2VLx2VL(const uint64_t rows_l, const uint64_t cols_l,
const uint64_t cols_r, const float * input_left,
const float * input_right, float * output);
extern void sgemm_direct_sme1_preprocess(const uint64_t rows, const uint64_t cols,
const float * a, float * a_mod);
extern void sgemm_direct_sme2_preprocess(const uint64_t rows, const uint64_t cols,
const float * a, float * a_mod);
extern void sgemm_direct_sme2_2VLx2VL(const uint64_t rows_l, const uint64_t cols_l,
const uint64_t cols_r, const float * input_left,
const float * input_right, float * output);
}
#define L1_CACHE_SIZE 131072 // 128 KB
#define LOAD_WIDTH (16 * 2) // 每次加载 32 个 float(128B)
#define FLOAT_SIZE 4
const int cacheK = (L1_CACHE_SIZE / FLOAT_SIZE / LOAD_WIDTH) * 8;
float cacheN[1][LOAD_WIDTH];
float cacheM[cacheK][LOAD_WIDTH];
float *cacheC;
void init_C() {
for (size_t i = 0; i < LOAD_WIDTH; i++) {
cacheN[0][i] = 2.0 * (float)drand48( ) - 1.0 + 0.0000001 * (i/LOAD_WIDTH);
}
for (size_t i = 0; i < cacheK; i++) {
for (size_t j = 0; j < LOAD_WIDTH; j++) {
cacheM[i][j] = 2.0 * (float)drand48( ) - 1.0 + 0.0000001 * (i/LOAD_WIDTH);
}
}
posix_memalign( (void**) &cacheC, 128, cacheK * LOAD_WIDTH * sizeof(float));
for (size_t i = 0; i < cacheK * LOAD_WIDTH; i++) {
cacheC[i] = (float)i/16.52525;
}
}
void func1() {
float *ptr = &cacheN[0][0];
asm volatile("smstart" :::);
for (int i = 0; i < cacheK; i++) {
asm volatile(
"ld1w {z0.s}, p0/z, [%[src]]\n"
:
: [src] "r"(ptr)
: "p0", "z0"
);
}
asm volatile("smstop" :::);
}
void func2() {
asm volatile("smstart" :::);
for (size_t i = 0; i < cacheK; i++) {
float *ptr = &cacheM[i][0];
asm volatile(
"ld1w {z0.s}, p0/z, [%[src]]\n"
:
: [src] "r"(ptr)
: "p0","z0"
);
}
asm volatile("smstop" :::);
}
void func3() {
asm volatile("smstart" :::);
for (size_t i = 0; i < cacheK; i++) {
float *ptr = &cacheC[i * LOAD_WIDTH]; // 大跨度访问
asm volatile(
"ld1w {z0.s}, p0/z, [%[src]]\n"
:
: [src] "r"(ptr)
: "p0","z0"
);
}
asm volatile("smstop" :::);
}
void benchmark(const char *name, void (*func)()) {
std::chrono::steady_clock::time_point l_start = std::chrono::steady_clock::now();
func();
std::chrono::steady_clock::time_point l_end = std::chrono::steady_clock::now();
double elapsed = std::chrono::duration_cast< std::chrono::duration<double> >( l_end - l_start ).count();
double total_bytes = cacheK * LOAD_WIDTH * FLOAT_SIZE;
double bandwidth = total_bytes / elapsed / 1e9; // GB/s
printf("%s:\n Time: %.6f sec\n Bandwidth: %.2f GB/s\n\n", name, elapsed, bandwidth);
}
int main(int argc, char **argv) {
init_C(); // 分配并初始化大数组
benchmark("func1 (same address load)", func1);
benchmark("func2 (linear load)", func2);
benchmark("func3 (stride load)", func3);
free(cacheC);
int M, N, K, M_mod,iterations,num_threads;
// /* Size parameters */
// if (argc == 5) {
// iterations = strtoul(argv[1], NULL, 0);
// M = strtoul(argv[2], NULL, 0);
// N = strtoul(argv[3], NULL, 0);
// K = strtoul(argv[4], NULL, 0);
// num_threads = 1;
// } else {
// /* Default: 128x128x128 */
// M = 128;
// N = 128;
// K = 128;
// iterations = 1;
// num_threads = 1;
// }
M = 32;
N = 32;
K = 8192;
iterations = 1000;
if (!(K > 2)) {
printf("Invalid Matrix Dimensions. Please ensure that :\n\t- K dimension "
"is Greater-Than 2 \n\nRuntime args ==> ./BINARY_NAME iterations M "
"K N\n");
exit(1);
}
printf("Left Matrix Dimensions = %d x %d\nRight Matrix Dimensions = %d x "
"%d\nSME Loop iterations = %d\n\n",
M, K, K, N, iterations);
/* Calculate M of transformed matLeft. */
uint64_t vl_elms = sme_cntw();
printf("%lu\n", vl_elms);
M_mod = ceil((double)M / (double)vl_elms) * vl_elms; // ceil(M/cntw)*cntw
printf("M_mod = %d\n", M_mod);
pthread_set_qos_class_self_np( QOS_CLASS_USER_INTERACTIVE, 0 );
float *matLeft = NULL;
posix_memalign( (void**) &matLeft, 128, M * K * sizeof(float) );
float *matLeft_mod = NULL;
posix_memalign( (void**) &matLeft_mod, 128, M_mod * K * sizeof(float) );
float *matRight = NULL;
posix_memalign( (void**) &matRight, 128,K * N * sizeof(float) );
float *matResult_ref = NULL;
posix_memalign( (void**) &matResult_ref, 128, M * N * sizeof(float) );
float *matResult_opt = NULL;
posix_memalign( (void**) &matResult_opt, 128, M * N * sizeof(float) );
random_matrix(M, K, matLeft);
random_matrix(K, N, matRight);
// for(int i=0;i<M*K; i++){
// printf(" %.2f ",matLeft[i]);
// }
// printf("\n");
// printf("\n");
// for(int j=0;j< M;j++){
// for(int z=0;z< K;z++){
// printf(" %.2f ",matLeft[z + j * K]);
// }
// printf("\n");
// }
// printf("\n");
// printf("\n");
// sgemm_direct_sme1_preprocess(M, K, matLeft, matLeft_mod);
// for(int i=0;i<M*K; i++){
// printf(" %.2f ",matLeft_mod[i]);
// }
// printf("\n");
// printf("\n");
// for(int j=0;j< M;j++){
// for(int z=0;z< K;z++){
// printf(" %.2f ",matLeft_mod[z + j * K]);
// }
// printf("\n");
// }
// set up dispatch
// dispatch_queue_attr_t l_attr = dispatch_queue_attr_make_with_qos_class( DISPATCH_QUEUE_CONCURRENT,
// QOS_CLASS_USER_INTERACTIVE,
// 0 );
// dispatch_queue_t l_queue = dispatch_queue_create( "bench_queue", l_attr );
// dispatch_group_t l_group = dispatch_group_create();
double ops = (double )M * N * K * 1.0e-09 * 2;
//Calculate matrix multiply naively
printf("Calculating Reference Matrix Multiply...\n");
std::chrono::steady_clock::time_point l_start = std::chrono::steady_clock::now();
// for( int l_td = 0; l_td < num_threads; l_td++ ) {
// dispatch_group_async( l_group,
// l_queue,
// ^{ for (int i = 0; i < iterations; i++) {
// matmul_ref(M, K, N, matLeft, matRight, matResult_ref); }
// } );
// }
// dispatch_group_wait( l_group, DISPATCH_TIME_FOREVER );
for (int i = 0; i < iterations; i++) {
//matmul_ref(M, K, N, matLeft, matRight, matResult_ref);
cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, M, N, K, 1.0, matLeft, K, matRight, N, 0.0, matResult_ref, N);
}
std::chrono::steady_clock::time_point l_end = std::chrono::steady_clock::now();
double cost = std::chrono::duration_cast< std::chrono::duration<double> >( l_end - l_start ).count() / iterations;
printf("SGEMM: M= %d N=%d K=%d cost=%lf Gflops = %lf \n", M, N, K, cost, ops/cost);
printf("Done\n");
// for (int i = 0; i < 3; i++) {
// sgemm_direct_sme1_preprocess(M, K, matLeft, matLeft_mod);
// sgemm_direct_sme1_2VLx2VL(M, K, N, matLeft_mod, matRight, matResult_opt);
// }
// Looping multiple times to isolate performance to this function call
printf("Calculating %d iterations of SME Matrix Multiply...\n", iterations);
l_start = std::chrono::steady_clock::now();
//#pragma omp parallel for num_threads(10)
// for( int l_td = 0; l_td < num_threads; l_td++ ) {
// dispatch_group_async( l_group,
// l_queue,
// ^{
// for (int i = 0; i < iterations; i++) {
// sgemm_direct_sme1_preprocess(M, K, matLeft, matLeft_mod);
// sgemm_direct_sme1_2VLx2VL(M, K, N, matLeft_mod, matRight, matResult_opt);
// }
// } );
// }
// dispatch_group_wait( l_group, DISPATCH_TIME_FOREVER );
for (int i = 0; i < iterations; i++) {
sgemm_direct_sme2_preprocess(M, K, matLeft, matLeft_mod);
sgemm_direct_sme2_2VLx2VL(M, K, N, matLeft_mod, matRight, matResult_opt);
}
l_end = std::chrono::steady_clock::now();
cost = std::chrono::duration_cast< std::chrono::duration<double> >( l_end - l_start ).count() / iterations;
printf("SGEMMxsme: M= %d N=%d K=%d cost=%lf ops=%f Gflops = %lf \n", M, N, K, cost, ops, ops/cost);
printf("Done\n\n");
//clang -march=armv9-a+sme+sme2 -O0 -c matmul_opt.s preprocess.s
//clang -O3 -c main.c -o main.o
//clang -march=armv9-a+sme+sme2 -O0 -c matmul_opt.s preprocess.s
//clang main.o matmul_opt.o preprocess.o -o matmul
printf("ERROR TOLERANCE = %.4f%%\n", (float)ERROR_TOLERANCE);
int error = 0;
for (uint64_t i = 0; i < M * N; i++) {
if (fabsf(matResult_ref[i] - matResult_opt[i]) > fabsf((float)ERROR_TOLERANCE * matResult_ref[i])) {
error = 1;
printf("%lu, %f, %f\n", i, matResult_ref[i],matResult_opt[i]);
}
}
// for (uint64_t i = 0; i < M*N; i++) {
// printf("%lu, %f, %f\n", i, matResult_ref[i],matResult_opt[i]);
// }
// if (error)
// {
// printf("FAILED\n");
// printf("100 , %f, %f\n", matResult_ref[100],matResult_opt[100]);
// }
// else
// {
// printf("PASS\n");
// printf("100 , %f, %f\n", matResult_ref[100],matResult_opt[100]);
// }
free(matLeft);
free(matLeft_mod);
free(matRight);
free(matResult_ref);
free(matResult_opt);
return 0;
}