-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblas.html
604 lines (519 loc) · 30.3 KB
/
blas.html
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
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>BLAS</title>
<link rel="stylesheet" type="text/css" href="/htmlize.css"/>
<link rel="stylesheet" type="text/css" href="./htmlize.css"/>
<link rel="stylesheet" type="text/css" href="../htmlize.css"/>
<link rel="stylesheet" type="text/css" href="../../htmlize.css"/>
<link rel="stylesheet" type="text/css" href="/readtheorg.css"/>
<link rel="stylesheet" type="text/css" href="./readtheorg.css"/>
<link rel="stylesheet" type="text/css" href="../readtheorg.css"/>
<link rel="stylesheet" type="text/css" href="../../readtheorg.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS_HTML"></script>
<link rel="stylesheet" type="text/css" href="/main.css" media="screen" />
<link rel="stylesheet" type="text/css" href="../main.css" media="screen" />
<link rel="stylesheet" type="text/css" href="../../main.css" media="screen" />
<link rel="stylesheet" type="text/css" href="./main.css" media="screen" />
<link rel = "icon" href = "/icon.png" type = "image/x-icon">
</head>
<body>
<div id="content" class="content">
<h1 class="title">BLAS</h1>
<div id="table-of-contents" role="doc-toc">
<h2>Table of Contents</h2>
<div id="text-table-of-contents" role="doc-toc">
<ul>
<li><a href="#org000002b">1. BLAS</a>
<ul>
<li><a href="#org0000028">1.1. gemm</a>
<ul>
<li><a href="#org0000001">1.1.1. 拆分方法</a></li>
<li><a href="#org0000004">1.1.2. 关于 M_c, N_c, K_c, M_r, N_r 的取值</a></li>
<li><a href="#org0000008">1.1.3. 关于 pack</a></li>
<li><a href="#org000000b">1.1.4. openblas gemm kernel</a></li>
<li><a href="#org000000f">1.1.5. openblas gemm kernel with SIMD</a></li>
<li><a href="#org0000013">1.1.6. m/n/k 三重循环的选择</a></li>
<li><a href="#org0000016">1.1.7. benchmark</a></li>
<li><a href="#org0000025">1.1.8. openblas 代码结构</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-org000002b" class="outline-2">
<h2 id="org000002b"><span class="section-number-2">1.</span> BLAS</h2>
<div class="outline-text-2" id="text-1">
</div>
<div id="outline-container-org0000028" class="outline-3">
<h3 id="org0000028"><span class="section-number-3">1.1.</span> gemm</h3>
<div class="outline-text-3" id="text-1-1">
</div>
<div id="outline-container-org0000001" class="outline-4">
<h4 id="org0000001"><span class="section-number-4">1.1.1.</span> 拆分方法</h4>
<div class="outline-text-4" id="text-1-1-1">
<p>
<a href="https://www.cs.utexas.edu/users/flame/pubs/GotoTOMS_final.pdf">https://www.cs.utexas.edu/users/flame/pubs/GotoTOMS_final.pdf</a>
</p>
<div id="org0000000" class="figure">
<p><img src="extra/gotoblas.png" alt="gotoblas.png" />
</p>
</div>
<p>
A 为 M x K, B 为 K x N
</p>
<ol class="org-ol">
<li>A, B 拆分成较小的 A' (M_c x K_c) 和 B' (K_c x N_c), A' 在 l2 cache, B'在 l3 cache</li>
<li>A', B' 拆分为更小的 A'' (M_r x K_c) 和 B'' (K_c x N_r), B'' 在 l1 cache,
M_r/N_r 的典型值为 2</li>
<li>计算 A'' x B'' 时使用了 `列 dot 行`的形式 , 所以 A' 需要以 M_r 为单位纵向
pack, B' 需要以 N_r 为单位横向 pack, 以便读数据时能利用 l1 cache</li>
</ol>
</div>
</div>
<div id="outline-container-org0000004" class="outline-4">
<h4 id="org0000004"><span class="section-number-4">1.1.2.</span> 关于 M_c, N_c, K_c, M_r, N_r 的取值</h4>
<div class="outline-text-4" id="text-1-1-2">
<ul class="org-ul">
<li>K_c 和 N_c 取决于 l3 cache</li>
<li>K_c 和 M_c 取决于 l2 cache</li>
<li>K_c 和 M_r, N_r 取决于 l1 cache</li>
</ul>
<p>
openblas 使用 GEBP 类型的 kernel, N_c 通常较大, 例如 4K. M_c/K_c 较小, 例如
128/256.
</p>
</div>
</div>
<div id="outline-container-org0000008" class="outline-4">
<h4 id="org0000008"><span class="section-number-4">1.1.3.</span> 关于 pack</h4>
<div class="outline-text-4" id="text-1-1-3">
<p>
假设 M_c=N_c=K_c = 4, M_r=N_r=2, openblas pack 的结果为:
</p>
<pre class="example" id="org0000007">
pack 之前:
A': (矩阵的值, 非内存中的布局)
0.170828 0.577304 0.873904 0.732520
0.749902 0.785799 0.745095 0.260222
0.096372 0.692194 0.446046 0.394294
0.870465 0.368766 0.353728 0.776790
B':
0.845035 0.455825 0.568585 0.275509
0.575788 0.109947 0.959066 0.260361
0.715539 0.545228 0.867719 0.924095
0.083004 0.390687 0.163190 0.435923
pack 之后:
A'':
(gdb) x /8fg ba
0x7fffefd7d000: 0.17082803610628972 0.74990198048496381
0x7fffefd7d010: 0.57730350679510778 0.78579925883967405
0x7fffefd7d020: 0.87390407686180893 0.74509509845006505
0x7fffefd7d030: 0.73251963200253911 0.26022200108288018
B'':
(gdb) x /8fg bb
0x7fffefd9d000: 0.84503513758028603 0.45582512865975744
0x7fffefd9d010: 0.57578820048277635 0.10994681418064545
0x7fffefd9d020: 0.71553859516866325 0.54522802381657343
0x7fffefd9d030: 0.083004246073876686 0.39068657064867551
</pre>
</div>
</div>
<div id="outline-container-org000000b" class="outline-4">
<h4 id="org000000b"><span class="section-number-4">1.1.4.</span> openblas gemm kernel</h4>
<div class="outline-text-4" id="text-1-1-4">
<p>
openblas SGEMMKERNEL 计算的是 packed A' x packed B', 所以 gemm kernel 需要先通过
SGEMMONCOPY/SGEMMOTCOPY 进行 pack, 且 COPY 和 SGEMMKERNEL 需要对应相同的
M_r/N_r, 否则数据的布局会对应不上.
</p>
<p>
openblas 中的 param.h 中定义了以上几个值:
</p>
<ul class="org-ul">
<li>SGEMM_DEFAULT_P 对应 M_c</li>
<li>SGEMM_DEFAULT_Q 对应 K_c</li>
<li>SGEMM_DEFAULT_R 对应 N_c</li>
<li>SGEMM_DEFAULT_UNROLL_M 对应 M_r</li>
<li>SGEMM_DEFAULT_UNROLL_N 对应 N_r</li>
</ul>
<p>
以 gemmkernel_2x2.c 为例:
</p>
<div class="org-src-container">
<pre class="src src-c"><span class="org-type">int</span> <span class="org-function-name">CNAME</span>(
<span class="org-type">BLASLONG</span> <span class="org-variable-name">bm</span>, <span class="org-type">BLASLONG</span> <span class="org-variable-name">bn</span>, <span class="org-type">BLASLONG</span> <span class="org-variable-name">bk</span>, <span class="org-type">FLOAT</span> <span class="org-variable-name">alpha</span>, <span class="org-type">IFLOAT</span>* <span class="org-variable-name">ba</span>, <span class="org-type">IFLOAT</span>* <span class="org-variable-name">bb</span>,
<span class="org-type">FLOAT</span>* <span class="org-variable-name">C</span>, <span class="org-type">BLASLONG</span> <span class="org-variable-name">ldc</span>) {
<span class="org-type">BLASLONG</span> <span class="org-variable-name">i</span>, <span class="org-variable-name">j</span>, <span class="org-variable-name">k</span>;
<span class="org-type">FLOAT</span> *<span class="org-variable-name">C0</span>, *<span class="org-variable-name">C1</span>;
<span class="org-type">IFLOAT</span> *<span class="org-variable-name">ptrba</span>, *<span class="org-variable-name">ptrbb</span>;
<span class="org-type">FLOAT</span> <span class="org-variable-name">res0</span>, <span class="org-variable-name">res1</span>, <span class="org-variable-name">res2</span>, <span class="org-variable-name">res3</span>;
<span class="org-type">IFLOAT</span> <span class="org-variable-name">load0</span>, <span class="org-variable-name">load1</span>, <span class="org-variable-name">load2</span>, <span class="org-variable-name">load3</span>, <span class="org-variable-name">load4</span>, <span class="org-variable-name">load5</span>, <span class="org-variable-name">load6</span>, <span class="org-variable-name">load7</span>;
<span class="org-comment-delimiter">/* </span><span class="org-comment">NOTE: ba 即 packed A', bb 则 packed B'</span><span class="org-comment-delimiter"> */</span>
<span class="org-comment-delimiter">/* </span><span class="org-comment">NOTE: bn 即 N_c, /2 表示 N_r 为 2</span><span class="org-comment-delimiter"> */</span>
<span class="org-keyword">for</span> (j = 0; j < bn / 2; j += 1) {
C0 = C;
C1 = C0 + ldc;
<span class="org-comment-delimiter">/* </span><span class="org-comment">NOTE: ptrba 相当于 A''</span><span class="org-comment-delimiter"> */</span>
ptrba = ba;
<span class="org-keyword">for</span> (i = 0; i < bm / 2; i += 1) {
<span class="org-comment-delimiter">/* </span><span class="org-comment">NOTE: bm 即 M_c, M_r=2</span><span class="org-comment-delimiter"> */</span>
<span class="org-comment-delimiter">/* </span><span class="org-comment">NOTE: ptrbb 相当于 B''</span><span class="org-comment-delimiter"> */</span>
ptrbb = bb;
res0 = 0;
res1 = 0;
res2 = 0;
res3 = 0;
<span class="org-comment-delimiter">/* </span><span class="org-comment">NOTE: bk 即 K_c, /4 表示计算 A'' x B'' 时针对 K_c 循环展开 4 次</span>
<span class="org-comment-delimiter"> */</span>
<span class="org-keyword">for</span> (k = 0; k < bk / 4; k += 1) {
<span class="org-comment-delimiter">/* </span><span class="org-comment">NOTE: 计算 A'' x B'', 每次得到 4 个值 (因为每次 A'' 的一列</span>
<span class="org-comment"> * dot B'' 的一行, 结果为 2x2 矩阵")</span><span class="org-comment-delimiter"> */</span>
load0 = ptrba[2 * 0 + 0];
load1 = ptrbb[2 * 0 + 0];
res0 = res0 + BF16TOF32(load0) * BF16TOF32(load1);
load2 = ptrba[2 * 0 + 1];
res1 = res1 + BF16TOF32(load2) * BF16TOF32(load1);
<span class="org-comment-delimiter">/* </span><span class="org-comment">...</span><span class="org-comment-delimiter"> */</span>
ptrba = ptrba + 8;
ptrbb = ptrbb + 8;
}
<span class="org-comment-delimiter">/* </span><span class="org-comment">NOTE: 循环展开时 K_c 不能被 4 整除</span><span class="org-comment-delimiter"> */</span>
<span class="org-keyword">for</span> (k = 0; k < (bk & 3); k += 1) {
load0 = ptrba[2 * 0 + 0];
load1 = ptrbb[2 * 0 + 0];
<span class="org-comment-delimiter">/* </span><span class="org-comment">...</span><span class="org-comment-delimiter"> */</span>
}
res0 = res0 * alpha;
C0[0] = C0[0] + res0;
res1 = res1 * alpha;
<span class="org-comment-delimiter">/* </span><span class="org-comment">...</span><span class="org-comment-delimiter"> */</span>
C0 = C0 + 2;
C1 = C1 + 2;
}
<span class="org-comment-delimiter">/* </span><span class="org-comment">NOTE: M_c 不能被 2 整除</span><span class="org-comment-delimiter"> */</span>
<span class="org-keyword">for</span> (i = 0; i < (bm & 1); i += 1) {
ptrbb = bb;
res0 = 0;
res1 = 0;
<span class="org-keyword">for</span> (k = 0; k < bk; k += 1) {
load0 = ptrba[0 + 0];
load1 = ptrbb[2 * 0 + 0];
res0 = res0 + BF16TOF32(load0) * BF16TOF32(load1);
<span class="org-comment-delimiter">/* </span><span class="org-comment">...</span><span class="org-comment-delimiter"> */</span>
}
res0 = res0 * alpha;
<span class="org-comment-delimiter">/* </span><span class="org-comment">...</span><span class="org-comment-delimiter"> */</span>
C1 = C1 + 1;
}
k = (bk << 1);
bb = bb + k;
i = (ldc << 1);
C = C + i;
}
<span class="org-comment-delimiter">/* </span><span class="org-comment">NOTE: N_c 不能被 2 整除</span><span class="org-comment-delimiter"> */</span>
* / <span class="org-keyword">for</span> (j = 0; j < (bn & 1); j += 1) {
C0 = C;
ptrba = ba;
<span class="org-keyword">for</span> (i = 0; i < bm / 2; i += 1) {
ptrbb = bb;
res0 = 0;
res1 = 0;
<span class="org-keyword">for</span> (k = 0; k < bk; k += 1) {
load0 = ptrba[2 * 0 + 0];
load1 = ptrbb[0 + 0];
res0 = res0 + BF16TOF32(load0) * BF16TOF32(load1);
<span class="org-comment-delimiter">/* </span><span class="org-comment">...</span><span class="org-comment-delimiter"> */</span>
}
res0 = res0 * alpha;
C0[0] = C0[0] + res0;
<span class="org-comment-delimiter">/* </span><span class="org-comment">...</span><span class="org-comment-delimiter"> */</span>
}
<span class="org-keyword">for</span> (i = 0; i < (bm & 1); i += 1) {
ptrbb = bb;
res0 = 0;
<span class="org-comment-delimiter">/* </span><span class="org-comment">...</span><span class="org-comment-delimiter"> */</span>
}
k = (bk << 0);
bb = bb + k;
C = C + ldc;
}
<span class="org-keyword">return</span> 0;
}
</pre>
</div>
</div>
</div>
<div id="outline-container-org000000f" class="outline-4">
<h4 id="org000000f"><span class="section-number-4">1.1.5.</span> openblas gemm kernel with SIMD</h4>
<div class="outline-text-4" id="text-1-1-5">
<p>
以 <code>sgemm_kernel_16x4_c910v.c</code> 为例, 最内层 micro kernel 需要计算 k_c 个 `(16
x 1) x (1 x 4)`, 针对 k_c 同样使用循环展开, 针对 (16 x 1) x (1 x 4), 则使用如下的计算方法:
</p>
<pre class="example" id="org000000e">
// A: 16x1 B: 1x4
[1
2
3
4 x [a b c d]
.
.
16]
// 使用 v16~v31 保存计算结果, 因为结果为 16x4
v0 = [1, 2, 3, 4]
v1 = [a, a, a, a]
v16 += v0*v1
v2 = [5, 6, 7, 8]
v17 += v0*v2
...
v0 = [13, 14, 15, 16]
v1 = [d, d, d, d]
v31 += v0*v1
</pre>
<p>
计算 k_c 次后保存 v16~v31 到 c 矩阵
</p>
</div>
</div>
<div id="outline-container-org0000013" class="outline-4">
<h4 id="org0000013"><span class="section-number-4">1.1.6.</span> m/n/k 三重循环的选择</h4>
<div class="outline-text-4" id="text-1-1-6">
<p>
最简单的 gemm 实现如下所示:
</p>
<div class="org-src-container">
<pre class="src src-c"><span class="org-keyword">for</span> (<span class="org-type">int</span> <span class="org-variable-name">m</span> = 0; m < M; m++) {
<span class="org-keyword">for</span> (<span class="org-type">int</span> <span class="org-variable-name">n</span> = 0; n < N; n++) {
<span class="org-keyword">for</span> (<span class="org-type">int</span> <span class="org-variable-name">k</span> = 0; k < K; k++) {
C[m, n] += A[m, k] * B[k, n];
}
}
}
</pre>
</div>
<p>
实际上三重循环的顺序可以任意调换, 不影响计算结果, 例如:
</p>
<div class="org-src-container">
<pre class="src src-c"><span class="org-keyword">for</span> (<span class="org-type">int</span> <span class="org-variable-name">n</span> = 0; n < N; n++) {
<span class="org-keyword">for</span> (<span class="org-type">int</span> <span class="org-variable-name">m</span> = 0; m < M; m++) {
<span class="org-keyword">for</span> (<span class="org-type">int</span> <span class="org-variable-name">k</span> = 0; k < K; k++) {
C[m, n] += A[m, k] * B[k, n];
}
}
}
</pre>
</div>
<p>
但它们的性能并不相同.
</p>
<p>
假设 A/B/C 均为 column major, 则访问 <code>A[m,k]</code> 相当于 <code>A[k*M+m]</code>, 所以从 locality
的角度出发, 遍历时我们应尽量改动 m 而不是 k.
</p>
<p>
例如:
</p>
<div class="org-src-container">
<pre class="src src-c"><span class="org-type">C</span>[m, n] += A[m, k] * B[k, n];
</pre>
</div>
<ul class="org-ul">
<li>m: 两次出现在 M[x,y] 的 x 位置 (C[m,n], A[m,k])</li>
<li>k: 一次出现在 x 位置, 一次出现在 y 位置</li>
<li>n: 两次出现在 y 位置</li>
</ul>
<p>
因此对于 column major 的 gemm, 最佳的三重循环的写法是 nkm 形式, 即:
</p>
<div class="org-src-container">
<pre class="src src-c"><span class="org-keyword">for</span> (<span class="org-type">int</span> <span class="org-variable-name">n</span> = 0; n < N; n++) {
<span class="org-keyword">for</span> (<span class="org-type">int</span> <span class="org-variable-name">k</span> = 0; k < K; k++) {
<span class="org-keyword">for</span> (<span class="org-type">int</span> <span class="org-variable-name">m</span> = 0; m < M; m++) {
C[m, n] += A[m, k] * B[k, n];
}
}
}
</pre>
</div>
<p>
使用 <a href="https://github.com/flame/blislab/tree/master">blislab</a> 的测试的结果:
</p>
<pre class="example" id="org0000012">
# nkm
~/source/blislab/step0> sudo perf stat -e L1-dcache-loads,L1-dcache-load-misses, \
L1-dcache-stores,cycles,instructions,branches \
test/test_bl_dgemm.x 1000 1000 1000
1000 1000 1000 2.10 inf
Performance counter stats for 'test/test_bl_dgemm.x 1000 1000 1000':
3,049,074,610 L1-dcache-loads (83.17%)
126,865,835 L1-dcache-load-misses # 4.16% of all L1-dcache accesses (83.17%)
1,029,504,921 L1-dcache-stores (83.35%)
2,169,687,341 cycles (67.14%)
8,171,486,214 instructions # 3.77 insn per cycle (83.57%)
1,031,403,187 branches (83.17%)
0.999021070 seconds time elapsed
0.982300000 seconds user
0.016037000 seconds sys
# mkn
~/source/blislab/step0#> sudo perf stat -e L1-dcache-loads,L1-dcache-load-misses, \
L1-dcache-stores,cycles,instructions,branches \
test/test_bl_dgemm.x 1000 1000 1000
1000 1000 1000 0.17 inf
Performance counter stats for 'test/test_bl_dgemm.x 1000 1000 1000':
3,057,403,526 L1-dcache-loads (83.34%)
2,448,489,282 L1-dcache-load-misses # 80.08% of all L1-dcache accesses (83.33%)
1,033,077,016 L1-dcache-stores (83.33%)
25,482,801,053 cycles (66.67%)
9,233,529,254 instructions # 0.36 insn per cycle (83.33%)
1,038,037,398 branches (83.33%)
11.616521657 seconds time elapsed
11.582296000 seconds user
0.027986000 seconds sys
</pre>
</div>
</div>
<div id="outline-container-org0000016" class="outline-4">
<h4 id="org0000016"><span class="section-number-4">1.1.7.</span> benchmark</h4>
<div class="outline-text-4" id="text-1-1-7">
<p>
使用 blislab 的 test_bl_dgemm.c 在 skylakex 平台上测试, 大致的 benchmark 数据为:
</p>
<ol class="org-ol">
<li>原始的 nkm 循环是 1 gflops</li>
<li>openblas 的 generic 实现 (使用 gotoblas 的拆分方法以利用 cache) 是 4 gflops</li>
<li>openblas 的 simd 实现是 20 gflops</li>
</ol>
</div>
</div>
<div id="outline-container-org0000025" class="outline-4">
<h4 id="org0000025"><span class="section-number-4">1.1.8.</span> openblas 代码结构</h4>
<div class="outline-text-4" id="text-1-1-8">
<p>
以 sgemm_ 为例:
</p>
<div class="org-src-container">
<pre class="src src-c"><span class="org-type">int</span> <span class="org-function-name">sgemm_</span>(
<span class="org-type">char</span> *<span class="org-variable-name">transa</span>, <span class="org-type">char</span> *<span class="org-variable-name">transb</span>, <span class="org-type">integer</span> *<span class="org-variable-name">m</span>, <span class="org-type">integer</span> *<span class="org-variable-name">n</span>, <span class="org-type">integer</span> *<span class="org-variable-name">k</span>, <span class="org-type">real</span> *<span class="org-variable-name">alpha</span>,
<span class="org-type">real</span> *<span class="org-variable-name">a</span>, <span class="org-type">integer</span> *<span class="org-variable-name">lda</span>, <span class="org-type">real</span> *<span class="org-variable-name">b</span>, <span class="org-type">integer</span> *<span class="org-variable-name">ldb</span>, <span class="org-type">real</span> *<span class="org-variable-name">beta</span>, <span class="org-type">real</span> *<span class="org-variable-name">c</span>,
<span class="org-type">integer</span> *<span class="org-variable-name">ldc</span>)
</pre>
</div>
</div>
<div id="outline-container-org0000019" class="outline-5">
<h5 id="org0000019"><span class="section-number-5">1.1.8.1.</span> interface</h5>
<div class="outline-text-5" id="text-1-1-8-1">
<p>
gemm.c 中对应的代码为:
</p>
<div class="org-src-container">
<pre class="src src-c"><span class="org-type">void</span> <span class="org-function-name">CNAME</span>(
<span class="org-keyword">enum</span> <span class="org-type">CBLAS_ORDER</span> <span class="org-variable-name">order</span>, <span class="org-keyword">enum</span> <span class="org-type">CBLAS_TRANSPOSE</span> <span class="org-variable-name">TransA</span>,
<span class="org-keyword">enum</span> <span class="org-type">CBLAS_TRANSPOSE</span> <span class="org-variable-name">TransB</span>, <span class="org-type">blasint</span> <span class="org-variable-name">m</span>, <span class="org-type">blasint</span> <span class="org-variable-name">n</span>, <span class="org-type">blasint</span> <span class="org-variable-name">k</span>, <span class="org-type">FLOAT</span> <span class="org-variable-name">alpha</span>,
<span class="org-type">IFLOAT</span> *<span class="org-variable-name">a</span>, <span class="org-type">blasint</span> <span class="org-variable-name">lda</span>, <span class="org-type">IFLOAT</span> *<span class="org-variable-name">b</span>, <span class="org-type">blasint</span> <span class="org-variable-name">ldb</span>, <span class="org-type">FLOAT</span> <span class="org-variable-name">beta</span>, <span class="org-type">FLOAT</span> *<span class="org-variable-name">c</span>,
<span class="org-type">blasint</span> <span class="org-variable-name">ldc</span>) {
<span class="org-comment-delimiter">/* </span><span class="org-comment">...</span><span class="org-comment-delimiter"> */</span>
}
</pre>
</div>
<p>
其中 `CNAME` 通过编译宏替换为 `sgemm_`, openblas 会使用相同的 <code>interface/gemm.c</code>
和不同的编译宏编译出 <code>{s,d,...}gemm_</code> 等函数
</p>
<div class="org-src-container">
<pre class="src src-makefile"><span class="org-comment-delimiter"># </span><span class="org-comment">Makefile@interface</span>
<span class="org-makefile-targets">sgemm.$(</span><span class="org-variable-name"><span class="org-makefile-targets">SUFFIX</span></span><span class="org-makefile-targets">) sgemm.$(</span><span class="org-variable-name"><span class="org-makefile-targets">PSUFFIX</span></span><span class="org-makefile-targets">)</span> : gemm.c ../param.h
$(<span class="org-variable-name">CC</span>) -c $(<span class="org-variable-name">CFLAGS</span>) $<span class="org-constant"><</span> -o $(<span class="org-variable-name">@F</span>)
<span class="org-makefile-targets">dgemm.$(</span><span class="org-variable-name"><span class="org-makefile-targets">SUFFIX</span></span><span class="org-makefile-targets">) dgemm.$(</span><span class="org-variable-name"><span class="org-makefile-targets">PSUFFIX</span></span><span class="org-makefile-targets">)</span> : gemm.c ../param.h
$(<span class="org-variable-name">CC</span>) -c $(<span class="org-variable-name">CFLAGS</span>) $<span class="org-constant"><</span> -o $(<span class="org-variable-name">@F</span>)
<span class="org-comment-delimiter"># </span><span class="org-comment">Makefile.system</span>
<span class="org-comment-delimiter"># </span><span class="org-comment">关于 `*F`: https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html</span>
<span class="org-variable-name">CCOMMON_OPT</span> += -DASMNAME=$(<span class="org-variable-name">FU</span>)$(<span class="org-variable-name">*F</span>) -DASMFNAME=$(<span class="org-variable-name">FU</span>)$(<span class="org-variable-name">*F</span>)$(<span class="org-variable-name">BU</span>) -DNAME=$(<span class="org-variable-name">*F</span>)$(<span class="org-variable-name">BU</span>) \
<span class="org-variable-name">-DCNAME</span>=$(<span class="org-variable-name">*F</span>) -DCHAR_NAME=\"$(<span class="org-variable-name">*F</span>)$(<span class="org-variable-name">BU</span>)\" -DCHAR_CNAME=\"$(<span class="org-variable-name">*F</span>)\"
<span class="org-comment-delimiter"># </span><span class="org-comment">Makefile.tail</span>
<span class="org-makefile-targets">$(</span><span class="org-variable-name"><span class="org-makefile-targets">SBLASOBJS</span></span><span class="org-makefile-targets">) $(</span><span class="org-variable-name"><span class="org-makefile-targets">SBLASOBJS_P</span></span><span class="org-makefile-targets">)</span> : override <span class="org-variable-name">CFLAGS</span> += -UDOUBLE -UCOMPLEX
<span class="org-makefile-targets">$(</span><span class="org-variable-name"><span class="org-makefile-targets">DBLASOBJS</span></span><span class="org-makefile-targets">) $(</span><span class="org-variable-name"><span class="org-makefile-targets">DBLASOBJS_P</span></span><span class="org-makefile-targets">)</span> : override <span class="org-variable-name">CFLAGS</span> += -DDOUBLE -UCOMPLEX
</pre>
</div>
</div>
</div>
<div id="outline-container-org000001f" class="outline-5">
<h5 id="org000001f"><span class="section-number-5">1.1.8.2.</span> driver</h5>
<div class="outline-text-5" id="text-1-1-8-2">
<p>
gemm.c 中的 sgemm_ 会根据 transa, transb, 是否需要多线程等调用 driver 中的函数,
例如 sgemm_nn, sgemm_nt, sgemm_thread_nn, sgemm_thread_nt 等, 其中 nn, nt 表示输入的矩阵是否经过 transpose
</p>
<p>
driver 和 interface 类似, 也是使用同一个 <code>level3.c</code> 和编译宏编译出不同的
<code>{s,d,...}gemm_{nn,nt,...}</code>
</p>
<div class="org-src-container">
<pre class="src src-makefile"><span class="org-makefile-targets">sgemm_nn.$(</span><span class="org-variable-name"><span class="org-makefile-targets">PSUFFIX</span></span><span class="org-makefile-targets">)</span> : gemm.c level3.c ../../param.h
$(<span class="org-variable-name">CC</span>) $(<span class="org-variable-name">PFLAGS</span>) $(<span class="org-variable-name">BLOCKS</span>) -c -UDOUBLE -UCOMPLEX -DNN $<span class="org-constant"><</span> -o $(<span class="org-variable-name">@F</span>)
<span class="org-makefile-targets">sgemm_nt.$(</span><span class="org-variable-name"><span class="org-makefile-targets">PSUFFIX</span></span><span class="org-makefile-targets">)</span> : gemm.c level3.c ../../param.h
$(<span class="org-variable-name">CC</span>) $(<span class="org-variable-name">PFLAGS</span>) $(<span class="org-variable-name">BLOCKS</span>) -c -UDOUBLE -UCOMPLEX -DNT $<span class="org-constant"><</span> -o $(<span class="org-variable-name">@F</span>)
<span class="org-makefile-targets">dgemm_nn.$(</span><span class="org-variable-name"><span class="org-makefile-targets">PSUFFIX</span></span><span class="org-makefile-targets">)</span> : gemm.c level3.c ../../param.h
$(<span class="org-variable-name">CC</span>) $(<span class="org-variable-name">PFLAGS</span>) $(<span class="org-variable-name">BLOCKS</span>) -c -DDOUBLE -UCOMPLEX -DNN $<span class="org-constant"><</span> -o $(<span class="org-variable-name">@F</span>)
</pre>
</div>
<p>
除了 <code>level3.c</code>, driver 中还有一个 <code>level3_thread.c</code>, 用来实现多线程版本的
driver, 例如:
</p>
<div class="org-src-container">
<pre class="src src-makefile"><span class="org-makefile-targets">sgemm_thread_nn.$(</span><span class="org-variable-name"><span class="org-makefile-targets">SUFFIX</span></span><span class="org-makefile-targets">)</span> : gemm.c level3_thread.c ../../param.h
$(<span class="org-variable-name">CC</span>) $(<span class="org-variable-name">CFLAGS</span>) $(<span class="org-variable-name">BLOCKS</span>) -c -DTHREADED_LEVEL3 -UDOUBLE -UCOMPLEX -DNN $<span class="org-constant"><</span> -o $(<span class="org-variable-name">@F</span>)
</pre>
</div>
<p>
sgemm_nn 中的代码需要完成矩阵的拆分和 pack. 对于多线程的 sgemm_thread_nn, 需要把任务拆分成多个 job 由 blas server 调用 sgemm_nn 去处理.
</p>
</div>
<div id="outline-container-org000001c" class="outline-6">
<h6 id="org000001c"><span class="section-number-6">1.1.8.2.1.</span> blas server</h6>
<div class="outline-text-6" id="text-1-1-8-2-1">
<p>
sgemm_thread_nn 把任务拆分成多个 job 后由 blas server 的 <code>exec_blas</code> 去调度执行,
后者有两个实现:
</p>
<ol class="org-ol">
<li>基于 pthread 的 <code>blas_server.c</code></li>
<li>基于 openmp 的 <code>blas_server_omp.c</code></li>
</ol>
</div>
</div>
</div>
<div id="outline-container-org0000022" class="outline-5">
<h5 id="org0000022"><span class="section-number-5">1.1.8.3.</span> kernel</h5>
<div class="outline-text-5" id="text-1-1-8-3">
<p>
以 RISCV64_GENERIC 为例, <code>OpenBLAS/kernel/riscv64/RISCV64_GENERIC</code> 中定义了
`SGEMMKERNEL = ../generic/gemmkernel_2x2.c`.
</p>
<p>
OpenBLAS kernel 相当于 gotoblas 论文中 micro-kernel 及其外面的两层循环 (即 GEBP
(GEneral Block times Panel) kernel, 其中 M_c x K_c 称为 block, K_c x N_c 称为
pannel), 不需要考虑 `M_c/N_c/K_c` 的拆分, pack 以及多线程的处理, 但需要自己处理
`M_r/N_r` 的拆分
</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div id="postamble" class="status">
<p class="author">Author: <a href="mailto:sunway@dogdog.run">sunway@dogdog.run</a><br />
Date: 2023-08-10 Thu 11:20<br />
Last updated: 2023-08-15 Tue 19:12</p>
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/"><img alt="知识共享许可协议" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png" /></a>
</div>
</body>
</html>