From: Kazushige Goto <goto@statabo.rim.or.jp>
Subject: how to optimize dgemm? No.3
Date: Sat, 19 Dec 1998 00:06:36 +0900 [thread overview]
Message-ID: <19981219000636T.goto@statabo.rim.or.jp> (raw)
In-Reply-To: <19981209231455P.goto@statabo.rim.or.jp>
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 8395 bytes --]
This is a 3rd part document of DGEMM.
5. Copying 2
(a) 1st step
I use stack temporary array s_a(Matrix A). And use these matrix in
most inner loop.
<< 28 MFlops, 93494 DTB misses>> <- I added DTB miss information.
#define P 40
#define Q 16
int i, j, l, ll;
int ls, is;
double temp;
double s_a[P];
for (is = 0; is < m; is += Q){
for (ls = 0; ls < k; ls += P){
for (j = 0; j < n; j++) {
for (i = is; i < MIN(m, is+Q); i++) {
temp = 0.;
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++) /* Copying */
s_a[ll] = a[i + l * lda];
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++) {
temp += b[l + j * ldb] * s_a[ll];
}
c[i + j * ldc] += alpha * temp;
}
}
}
}
(b) 2nd step
Expand temporary stack matrix s_a to 2 dimention.
<< 54 MFlops, 82681 DTB misses>>
#define P 40
#define Q 16
int i, ii, j, l, ll;
int ls, is;
double temp;
double s_a[P][Q];
for (is = 0; is < m; is += Q){
for (ls = 0; ls < k; ls += P){
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++) { /* Copying */
for (i = is, ii=0; i < MIN(m, is+Q); i++, ii++)
s_a[ll][ii] = a[i + l * lda];
}
for (j = 0; j < n; j++) {
for (i = is, ii=0; i < MIN(m, is+Q); i++, ii++) {
temp = 0.;
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++) {
temp += b[l + j * ldb] * s_a[ll][ii];
}
c[i + j * ldc] += alpha * temp;
}
}
}
}
(c) 3rd step
Do same way for s_b(Matrix B).
<< 68 MFlops, 82670 DTB misses>>
#define P 40
#define Q 16
int i, ii, j, l, ll;
int ls, is;
double temp;
double s_a[P][Q], s_b[P];
for (is = 0; is < m; is += Q){
for (ls = 0; ls < k; ls += P){
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++) { /* Copying */
for (i = is, ii=0; i < MIN(m, is+Q); i++, ii++)
s_a[ll][ii] = a[i + l * lda];
}
for (j = 0; j < n; j++) {
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++) /* Copying */
s_b[ll] = b[l + j * ldb];
for (i = is, ii=0; i < MIN(m, is+Q); i++, ii++) {
temp = 0.;
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++) {
temp += s_b[ll] * s_a[ll][ii];
}
c[i + j * ldc] += alpha * temp;
}
}
}
}
(d) 4th step
I want to move "s_b copying routine" to outer loops. But it's
difficult to move, so apply Blocking for "j loop".
<< 66 MFlops, 78124 DTB misses>>
#define P 40
#define Q 16
int i, ii, j, l, ll;
int ls, is, js;
double temp;
double s_a[P][Q], s_b[P];
for (js = 0; js < n; js += R){
for (is = 0; is < m; is += Q){
for (ls = 0; ls < k; ls += P){
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++) { /* Copying */
for (i = is, ii=0; i < MIN(m, is+Q); i++, ii++)
s_a[ll][ii] = a[i + l * lda];
}
for (j = js; j < MIN(n, js+R); j++) {
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++) /* Copying */
s_b[ll] = b[l + j * ldb];
for (i = is, ii=0; i < MIN(m, is+Q); i++, ii++) {
temp = 0.;
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++) {
temp += s_b[ll] * s_a[ll][ii];
}
c[i + j * ldc] += alpha * temp;
}
}
}
}
}
(e) 5th step
Also, expand s_b matrix to 2 dimention.
The matrix sizes of s_a and s_b are pretty large, but 21164 has large
and no-wait 2nd cache, so I choose this algorithm.
<< 55 MFlops, 80007 DTB misses>>
#define P 40
#define Q 16
#define R 32
int i, ii, j, jj, l, ll;
int ls, is, js;
double temp;
double s_a[P][Q], s_b[R][P];
for (js = 0; js < n; js += R){
for (is = 0; is < m; is += Q){
for (ls = 0; ls < k; ls += P){
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++) { /* Copying */
for (i = is, ii=0; i < MIN(m, is+Q); i++, ii++)
s_a[ll][ii] = a[i + l * lda];
}
for (j = js, jj=0; j < MIN(n, js+R); j++, jj++) {
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++) /* Copying */
s_b[jj][ll] = b[l + j * ldb];
}
for (j = js, jj=0; j < MIN(n, js+R); j++, jj++) {
for (i = is, ii=0; i < MIN(m, is+Q); i++, ii++) {
temp = 0.;
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++) {
temp += s_b[jj][ll] * s_a[ll][ii];
}
c[i + j * ldc] += alpha * temp;
}
}
}
}
}
(f) 6th(final) step
To move "copying routine" to outer loops, I moved "ls loop" to most
outer loop, then moved "s_b copying routine".
<< 59 MFlops, 4470 DTB misses>>
~~~~ <- not typo.
#define P 40
#define Q 16
#define R 32
int i, ii, j, jj, l, ll;
int ls, is, js;
double temp;
double s_a[P][Q], s_b[R][P];
for (ls = 0; ls < k; ls += P){ /* Swapped */
for (js = 0; js < n; js += R){
for (j = js, jj=0; j < MIN(n, js+R); j++, jj++) { /* Copying & Moved */
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++)
s_b[jj][ll] = b[l + j * ldb];
}
for (is = 0; is < m; is += Q){
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++) { /* Copying */
for (i = is, ii=0; i < MIN(m, is+Q); i++, ii++)
s_a[ll][ii] = a[i + l * lda];
}
/* Now Start calculating ... */
for (j = js, jj=0; j < MIN(n, js+R); j++, jj++) {
for (i = is, ii=0; i < MIN(m, is+Q); i++, ii++) {
temp = 0.;
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++) {
temp += s_b[jj][ll] * s_a[ll][ii];
}
c[i + j * ldc] += alpha * temp;
}
}
}
}
}
Thus, DTB misses were considerably reduced and we can avoid cache
congruence problem.
Cache Congruence(MFlops)
-----------------------------------------------------
size | 510 | 511 | 512 | 513 | 514
--------+--------+--------+--------+--------+--------
(a) | 27.8 | 26.4 | 18.7 | 26.0 | 27.7
--------+--------+--------+--------+--------+--------
(b) | 54.1 | 54.2 | 53.6 | 54.4 | 54.1
--------+--------+--------+--------+--------+--------
(c) | 67.0 | 68.9 | 65.8 | 68.5 | 66.0
--------+--------+--------+--------+--------+--------
(d) | 65.6 | 66.0 | 65.2 | 64.9 | 65.4
--------+--------+--------+--------+--------+--------
(e) | 54.9 | 54.9 | 54.5 | 54.5 | 54.8
--------+--------+--------+--------+--------+--------
(f) | 59.4 | 59.3 | 59.1 | 59.0 | 59.0
-----------------------------------------------------
Continue to No.4
Basic optimization was finished. Next, I'll explain Alpha 21164
specific optimization.
Thanks,
goto@statabo.rim.or.jp
next prev parent reply other threads:[~1998-12-18 15:06 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
1998-11-30 14:19 how to optimize dgemm? No.1 Kazushige Goto
1998-12-01 13:31 ` Joachim Wesner
1998-12-09 14:14 ` how to optimize dgemm? No.2 Kazushige Goto
1998-12-10 3:43 ` naohiko shimizu
1998-12-18 15:06 ` Kazushige Goto [this message]
1998-12-20 14:43 ` how to optimize dgemm? No.4 Kazushige Goto
1998-12-21 12:23 ` how to optimize dgemm? No.5 Kazushige Goto
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox