* how to optimize dgemm? No.1
@ 1998-11-30 14:19 Kazushige Goto
1998-12-01 13:31 ` Joachim Wesner
1998-12-09 14:14 ` how to optimize dgemm? No.2 Kazushige Goto
0 siblings, 2 replies; 7+ messages in thread
From: Kazushige Goto @ 1998-11-30 14:19 UTC (permalink / raw)
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 3221 bytes --]
Recently, some people asked me how to optimize my dgemm routine.
I wrote some information in dgemm_k.S source code, but it's not
enough.
So, I decided to explain some opimization method of Alpha step by
step. Because it takes too long to explain.
First, I make a sample & simple benchmark program. Please try to type
"make" and benchmark show you actual speed with MFlops.
I explain only Non-transposed x Non-transposed routine. If you have
any questions, please tell me.
Originally, BLAS dgemm kernel routine is programmed as follows.
It's really pretty simple, but it takes too long(about 23MFlops,
600MHz 21164).
void dgemm_nn(int m, int n, int k, double alpha, double *a,
int lda, double *b, int ldb,
double *c, int ldc){
int i, j, l;
for (j = 0; j < n; j++) {
for (i = 0; i < m; i++) {
for (l = 0; l < k; l++) {
c[i + j * ldc] += alpha * b[l + j * ldb] * a[i + l * lda];
}
}
}
}
1. loop exchangeable
This dgemm_nn routine works fine even if any loop is exchanged.
<< 44MFLops >>
for (j = 0; j < n; j++) {
for (l = 0; l < k; l++) {
for (i = 0; i < m; i++) {
c[i + j * ldc] += alpha * b[l + j * ldb] * a[i + l * lda];
}
}
}
or
<< 40MFLops >>
for (l = 0; l < k; l++) {
for (j = 0; j < n; j++) {
for (i = 0; i < m; i++) {
c[i + j * ldc] += alpha * b[l + j * ldb] * a[i + l * lda];
}
}
}
or
....
So we can choose a loop to get best performance.
2. Inner dot product
A basic strategy of Alpha(also, all other RISC machines) is to
"calculate innder dot product routine". Because "store" operation
also causes data traffics.
It means, we can choose one of this.
for (j = 0; j < n; j++) {
for (i = 0; i < m; i++) {
for (l = 0; l < k; l++) {
c[i + j * ldc] += alpha * b[l + j * ldb] * a[i + l * lda];
}
}
}
or
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
for (l = 0; l < k; l++) {
c[i + j * ldc] += alpha * b[l + j * ldb] * a[i + l * lda];
}
}
}
c[i+j*ldc] is a constant value in most inner loop. So we can change
it to
<< 27MFLops >>
double temp;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
temp = 0.0;
for (l = 0; l < k; l++) {
temp += b[l + j * ldb] * a[i + l * lda];
}
c[i + j * ldc ] += alpha * temp;
}
}
or
<< 22MFLops >>
double temp;
for (j = 0; j < n; j++) {
for (i = 0; i < m; i++) {
temp = 0.0;
for (l = 0; l < k; l++) {
temp += b[l + j * ldb] * a[i + l * lda];
}
c[i + j * ldc ] += alpha * temp;
}
}
It's still slow, but it'll be much faster as optimizing dgemm_nn.
Continue to No.2
Thanks,
goto@statabo.rim.or.jp
Application/Octet-Stream attachment: gemm-1.tar.gz_
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: how to optimize dgemm? No.1
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
1 sibling, 0 replies; 7+ messages in thread
From: Joachim Wesner @ 1998-12-01 13:31 UTC (permalink / raw)
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 845 bytes --]
Kazushige Goto wrote:
< Kazushige Goto explaining about the benefits of loop restructuring and
the like >
If you can read German, some time ago there was an article on a very
related subject with examples, resp. on optimized programms for vector
computers, by a pretty unknown author (myself <g>) in the German
computer mag "mc".
In the moment, I don't have the Ref. handy, I will look it up, but I
think I can also send a hard-copy to interested people (if not too
many).
BTW, be warned to get confused. For certain reasons too complex to
discuss here (but its in the article), an "inner dot product" based
routine may *NOT* be THE best way on a typical vector computer !!
There is also a good, yet now somewhat older, book (also German) by
Schönauer on that subject (cited in my article, I will look up the Ref,
too).
Joachim
^ permalink raw reply [flat|nested] 7+ messages in thread
* how to optimize dgemm? No.2
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 ` Kazushige Goto
1998-12-10 3:43 ` naohiko shimizu
1998-12-18 15:06 ` how to optimize dgemm? No.3 Kazushige Goto
1 sibling, 2 replies; 7+ messages in thread
From: Kazushige Goto @ 1998-12-09 14:14 UTC (permalink / raw)
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 23446 bytes --]
Hi,
This is a second part document of DGEMM.
3. Addressing of C matirx.
It's easy to determine :-).
<< 27 MFLops >>
int i, j, l;
double temp;
for (j = 0; j < n; j++) {
for (i = 0; i < m; i++) {
temp = 0.;
for (l = 0; l < k; l++) {
temp += b[l + j * ldb] * a[i + l * lda];
}
c[i + j * ldc] += alpha * temp;
}
}
4. Blocking
To use cache effectively, we can(must?) use a algorithm which is
called "blocking". First, we apply most inner loop.
<< 48 MFlops>>
#define MIN(a,b) ((a)>(b)? (b):(a))
#define P 40
int i, j, l;
int ls;
double temp;
for (ls = 0; ls < k; ls+=P){ /* Blocking */
for (j = 0; j < n; j++) {
for (i = 0; i < m; i++) {
temp = 0.;
for (l = ls; l < MIN(k, ls+P); l++) {
temp += b[l + j * ldb] * a[i + l * lda];
}
c[i + j * ldc] += alpha * temp;
}
}
}
Then, apply second innter loop.
<< 44 MFlops>>
#define MIN(a,b) ((a)>(b)? (b):(a))
#define P 40
#define Q 16
int i, j, l;
int ls, is;
double temp;
for (is = 0; is < m; is += Q){ /* Blocking */
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; l < MIN(k, ls+P); l++) {
temp += b[l + j * ldb] * a[i + l * lda];
}
c[i + j * ldc] += alpha * temp;
}
}
}
}
}
At now, we can not determine P and Q.
4. Copying
There are two variations.
a. calculate directly.
b. fist copy matrices to stack flame, then calculate.
It seems that (a) is better than (b), because (b)'s overhead is pretty
heavy. But it may not correct on 21164(maybe it's same on other
architecture).
Direct calculation causes some problems -- cache congruence and DTB
misses.
When leading dimensions are just 2^n(512, 1024, 2048, ...) or some
particluar values such as 768, ..., it's much slower(about half)
than others. This is called cache congruence. For example,
-------------------------------------
Leading Dimension | MFlops
--------------------+----------------
510 | 43.46
511 | 40.48
512 | 21.75 <- much slower
513 | 40.84
514 | 43.55
-------------------------------------
When we calculate on large matrix(large leading dimension), we expect
or hope it runs same speed, but it tends to be slower as leading
dimension.
-------------------------------------
Leading Dimension | MFlops
--------------------+----------------
500 | 44.36
1000 | 43.27
1500 | 43.01
2000 | 42.71
... | it takes toooo long. I gave up.
-------------------------------------
I'm afraid that this table is hard to understand. So I show table
which run on optimized direct/copying dgemm routines(they took almost
same algorithm except for direct or copying).
optmized dgemm routine(cache congruence)
----------------------------------------------
Leading Dimension | direct | copying
--------------------+-------------------------
504 | 661.78 | 807.23
508 | 536.18 | 787.36
512 | 138.67(*)| 820.15
516 | 525.38 | 774.45
520 | 644.99 | 805.77
----------------------------------------------
optmized dgemm routine(DTB misses)
----------------------------------------------
Leading Dimension | direct | copying
--------------------+-------------------------
400 | 673.68 | 810.12
800 | 666.14 | 805.03
1200 | 664.36 | 817.79
1600 | 650.91 | 813.12
2000 | 645.57 | 821.86
2400 | 635.02 | 820.46
----------------------------------------------
What's difference between them? To answer it, I explain with a good
tool which is called "performance counter"(PMCTR). This counter, which
is in Alpha CPU, shows us various useful information(cycles, DTB
misses, load/store count...).
# To use PMCTR, we must modify milo(PAL code) and kernel, though.
These values contain a little bit errors, because PMCTR can not count
on specific process(USER means all of processes except for Kernel and
PAL).
# long long .... long time, to mesure it....... Huu....
direct(leading dimention = 1000)
I t e m < P A L > < O S > < U S E R>
Cycles :57550152( 3.2) 50328787( 2.8) 1701192831( 94.0)
Instructions :34082981( 1.1) 7208644( 0.2) 3171976954( 98.7)
nonissue cycles :28085767( 5.4) 12809484( 2.5) 476459896( 92.1)
split-issue cycles : 179652( 1.4) 517078( 4.0) 12103980( 94.6)
pipe-dry cycles : 8154991( 7.5) 8794179( 8.1) 92090944( 84.5)
replay trap : 1030132( 9.1) 841867( 7.5) 9420666( 83.4)
single-issue cycles : 6991917( 12.6) 2055396( 3.7) 46372009( 83.7)
dual-issue cycles :13540825( 3.6) 836121( 0.2) 366379019( 96.2)
triple-issue cycles : 0( 0.0) 2261( 0.0) 503074753(100.0)
quad-issue cycles : 0( 0.0) 3906( 0.0) 220827314(100.0)
flow-change insns : 6686147( 16.2) 431793( 1.0) 34211383( 82.8)
IntOps issued : 1360591( 0.6) 1199895( 0.5) 235511911( 98.9)
FPOps issued : 0( 0.0) 12( 0.0) 2105000001(100.0)
loads issued : 2276139( 0.3) 1096963( 0.1) 776251092( 99.6)
stores issued : 21963( 0.1) 334926( 1.6) 21000003( 98.3)
Icache issued :16814563( 1.6) 11358401( 1.1) 1033736604( 97.3)
Dcache accesses : 2298628( 0.3) 1731192( 0.2) 797250356( 99.5)
S cache access : 2024299( 0.4) 2264914( 0.5) 498771558( 99.1)
long(>15) stalls : 227432( 3.2) 216772( 3.0) 6669097( 93.8)
PC-mispredicts : 97820( 65.6) 51288( 34.4) 44( 0.0)
BR-mispredicts : 6( 0.0) 134071( 4.6) 2793377( 95.4)
Icache/RFB misses : 35227( 12.0) 220505( 75.3) 37023( 12.6)
ITB misses : 32(100.0) 0( 0.0) 0( 0.0)
Dcache LD misses : 1037252( 0.1) 1960608( 0.3) 733383534( 99.6)
DTB misses : 82316( 3.8) 499( 0.0) 2093747( 96.2)
LDs merged in MAF : 5771( 0.0) 146924( 0.1) 249981039( 99.9)
LDU replay traps : 1028635( 83.9) 197183( 16.1) 4( 0.0)
WB/MAF replay traps : 2502( 0.0) 758730( 7.1) 9985798( 92.9)
MB stall cycles : 29722( 2.4) 1227877( 97.6) 12( 0.0)
LDx_L instructions : 0( 0.0) 9359(100.0) 0( 0.0)
Scache misses : 608891( 4.5) 563866( 4.1) 12475116( 91.4)
copying(leading dimention = 1000)
I t e m < P A L > < O S > < U S E R>
Cycles :22630326( 1.6) 26782138( 1.9) 1389113959( 96.6)
Instructions :10041953( 0.3) 6613082( 0.2) 3064465670( 99.5)
nonissue cycles : 9589458( 3.8) 20890488( 8.2) 224256235( 88.0)
split-issue cycles : 37879( 0.4) 352991( 4.0) 8470085( 95.6)
pipe-dry cycles : 6624444( 6.2) 8928737( 8.3) 91679061( 85.5)
replay trap : 314146( 2.5) 612803( 5.0) 11434078( 92.5)
single-issue cycles : 2063888( 7.3) 1190912( 4.2) 25022547( 88.5)
dual-issue cycles : 3984511( 1.2) 569822( 0.2) 325932120( 98.6)
triple-issue cycles : 0( 0.0) 5822( 0.0) 468973424(100.0)
quad-issue cycles : 0( 0.0) 5640( 0.0) 245133385(100.0)
flow-change insns : 1978314( 5.2) 344252( 0.9) 35838539( 93.9)
IntOps issued : 345393( 0.3) 907142( 0.8) 110849734( 98.9)
FPOps issued : 0( 0.0) 6( 0.0) 2037152001(100.0)
loads issued : 662904( 0.1) 991766( 0.1) 861625203( 99.8)
stores issued : 17185( 0.1) 287554( 1.5) 19000003( 98.4)
Icache issued : 4771180( 0.5) 4682886( 0.5) 968471068( 99.0)
Dcache accesses : 680286( 0.1) 3865593( 0.4) 880625550( 99.5)
S cache access : 1486992( 0.4) 1205931( 0.3) 382070734( 99.3)
long(>15) stalls : 116345( 4.2) 180011( 6.6) 2448747( 89.2)
PC-mispredicts : 25189( 38.9) 39495( 61.0) 38( 0.1)
BR-mispredicts : 40( 0.0) 13353( 0.9) 1418903( 99.1)
Icache/RFB misses : 212017( 36.8) 175389( 30.5) 188426( 32.7)
ITB misses : 11(100.0) 0( 0.0) 0( 0.0)
Dcache LD misses : 352476( 0.0) 1580582( 0.2) 797500776( 99.8)
DTB misses : 12893( 2.0) 93( 0.0) 622252( 98.0)
LDs merged in MAF : 4664( 0.0) 44524( 0.0) 428926948(100.0)
LDU replay traps : 294586( 66.1) 151238( 33.9) 134( 0.0)
WB/MAF replay traps : 220( 0.0) 187368( 1.7) 10681438( 98.3)
MB stall cycles : 23600( 2.3) 987487( 97.7) 0( 0.0)
LDx_L instructions : 0( 0.0) 7598(100.0) 0( 0.0)
Scache misses : 414992( 3.0) 516696( 3.7) 12976391( 93.3)
These tables are pretty heavy, so you might confuse.
ITEM direct copying RATIO (direct/copying)
---------------------------------------------------------------------------
Cycles : 1701192831 1389113959 1.22
Instructions : 3171976954 3064465670 1.04
nonissue cycles : 476459896 224256235 2.12
split-issue cycles : 12103980 8470085 1.42
pipe-dry cycles : 92090944 91679061 1.00
replay trap : 9420666 11434078 0.82
single-issue cycles : 46372009 25022547 1.85
dual-issue cycles : 366379019 325932120 1.12
triple-issue cycles : 503074753 468973424 1.07
quad-issue cycles : 220827314 245133385 0.90
flow-change insns : 34211383 35838539 0.95
IntOps issued : 235511911 110849734 2.12
FPOps issued : 2105000001 2037152001 1.03
loads issued : 776251092 861625203 0.90
stores issued : 21000003 19000003 1.10
Icache issued : 1033736604 968471068 1.06
Dcache accesses : 797250356 880625550 0.90
S cache access : 498771558 382070734 1.30
long(>15) stalls : 6669097 2448747 2.72(*)
PC-mispredicts : 44 38 ----
BR-mispredicts : 2793377 1418903 1.97
Icache/RFB misses : 37023 188426 0.20
ITB misses : 0 0 ----
Dcache LD misses : 733383534 797500776 0.92
DTB misses : 2093747 622252 3.36(*)
LDs merged in MAF : 249981039 428926948 0.58(*)
LDU replay traps : 4 134 ----
WB/MAF replay traps : 9985798 10681438 0.93
MB stall cycles : 12 0 ----
LDx_L instructions : 0 0 ----
Scache misses : 12475116 12976391 0.96
Please take notice of "DTB misses", its ratio is 3.36. DTB is
filled by PAL code and it takes too long. I do not know how long it
takes(please refer to attached program). So I calculate. Thus,
it takes about 950 clocks(RPCC says about 860 clocks).
DTB miss operation
I t e m < P A L > < O S > < U S E R>
Cycles : 757( 77.9) 196( 20.2) 19( 2.0)
Instructions : 47( 92.2) 0( 0.0) 4( 7.8)
nonissue cycles : 460( 99.6) 0( 0.0) 2( 0.4)
split-issue cycles : 3(100.0) 0( 0.0) 0( 0.0)
pipe-dry cycles : 412( 96.9) 0( 0.0) 13( 3.1)
replay trap : 2(100.0) 0( 0.0) 0( 0.0)
single-issue cycles : 13( 76.5) 0( 0.0) 4( 23.5)
dual-issue cycles : 17(100.0) 0( 0.0) 0( 0.0)
triple-issue cycles : 0( ---) 0( ---) 0( ---)
quad-issue cycles : 0( ---) 0( ---) 0( ---)
flow-change insns : 8( 88.9) 0( 0.0) 1( 11.1)
IntOps issued : 16(100.0) 0( 0.0) 0( 0.0)
FPOps issued : 0( ---) 0( ---) 0( ---)
loads issued : 3( 75.0) 0( 0.0) 1( 25.0)
stores issued : 0( ---) 0( ---) 0( ---)
Icache issued : 23( 82.1) 0( 0.0) 5( 17.9)
Dcache accesses : 3( 75.0) 0( 0.0) 1( 25.0)
S cache access : 15( 83.3) 0( 0.0) 3( 16.7)
long(>15) stalls : 3(100.0) 0( 0.0) 0( 0.0)
PC-mispredicts : 1(100.0) 0( 0.0) 0( 0.0)
BR-mispredicts : 0( ---) 0( ---) 0( ---)
Icache/RFB misses : 2( 66.7) 0( 0.0) 1( 33.3)
ITB misses : 0( ---) 0( ---) 0( ---)
Dcache LD misses : 3( 75.0) 0( 0.0) 1( 25.0)
DTB misses : 1( 50.0) 0( 0.0) 1( 50.0)
LDs merged in MAF : 0( ---) 0( ---) 0( ---)
LDU replay traps : 2(100.0) 0( 0.0) 0( 0.0)
WB/MAF replay traps : 0( ---) 0( ---) 0( ---)
MB stall cycles : 0( ---) 0( ---) 0( ---)
LDx_L instructions : 0( ---) 0( ---) 0( ---)
Scache misses : 8( 88.9) 0( 0.0) 1( 11.1)
I also mesured DTB misses on ld(leading dimension)=2000 and compared
against ld=1000. The theoretical ratio is 8, but it's much worse on
direct calculation.
The ratio of DTB misses
------------------------------------------------------
Leading Dimension | 1000 | 2000 | ratio
--------------------+----------+-----------+----------
direct | 2093747 | 26788263 | 12.79
copying | 622252 | 5151404 | 8.27
------------------------------------------------------
Conclution:
We may(should?) take copying method to reduce DTB misses.
Extra Information.
Do you know how long it takes to move register from main memory?
(no DTB miss, only cache miss)
clocks of moving from memory
---------------------------------------------------
1st level | 2nd level | 3rd level | main memory
-------------+-----------+-----------+-------------
2 | 8 | 27 | 134
---------------------------------------------------
Continue to No.3
# It was pretty heavy.... tired.
Thanks,
goto@statabo.rim.or.jp
.set noreorder
.set noat
.data
.align 4
$message:
.ascii "%ld\n"
#define SIZE 1024*1024
.align 4
$dummy :
.space 8*SIZE
.quad 0
$dummy2 :
.text
.globl main
.ent main
main:
ldgp $29,0($27)
lda $30, -32($30)
stq $26, 0($30)
lda $6, $dummy
lda $5, SIZE
.align 4
$loop:
ldq $3, 0($6)
addq $6, 8, $6
subq $5, 1, $5
bgt $5, $loop
rpcc $0
.align 4
ldt $f0, 0($30)
fmov $f0, $f1
.align 4
rpcc $1
subl $1, $0, $17
lda $16, $message
jsr $26, printf
ldq $26, 0($30)
lda $30, 32($30)
clr $0
ret
.end main
I t e m < P A L > < O S > < U S E R>
Cycles :841694024( 5.7) 467923843( 3.2) 13384449433( 91.1)
Instructions :438882906( 1.7) 66633172( 0.3) 25375797655( 98.0)
nonissue cycles :452487461( 10.2) 329793362( 7.4) 3658785135( 82.4)
split-issue cycles : 2568830( 2.5) 4991705( 4.9) 94301523( 92.6)
pipe-dry cycles :122281048( 13.8) 107391420( 12.1) 655141542( 74.0)
replay trap :18180167( 22.9) 2293654( 2.9) 59066625( 74.3)
single-issue cycles :90036920( 18.8) 15711521( 3.3) 372783457( 77.9)
dual-issue cycles :174246340( 5.6) 2141935( 0.1) 2951392260( 94.4)
triple-issue cycles : 0( 0.0) 429217( 0.0) 4020433775(100.0)
quad-issue cycles : 0( 0.0) 3706962( 0.2) 1760249267( 99.8)
flow-change insns :85946754( 23.7) 2113938( 0.6) 274852064( 75.7)
IntOps issued :18807758( 1.0) 14351081( 0.7) 1884081309( 98.3)
FPOps issued : 0( 0.0) 98( 0.0) 16840000001(100.0)
loads issued :29386620( 0.5) 8023613( 0.1) 6210784239( 99.4)
stores issued : 175532( 0.1) 3490153( 2.0) 168001622( 97.9)
Icache issued :238531183( 2.8) 29699819( 0.4) 8204292556( 96.8)
Dcache accesses :29570262( 0.5) 13857156( 0.2) 6378006221( 99.3)
S cache access :27331269( 0.7) 13469663( 0.3) 3969119743( 99.0)
long(>15) stalls : 2941030( 5.6) 1411024( 2.7) 47736999( 91.6)
PC-mispredicts : 1348690( 71.4) 540882( 28.6) 370( 0.0)
BR-mispredicts : 1223( 0.0) 67035( 0.3) 22345543( 99.7)
Icache/RFB misses : 298327( 12.6) 1700645( 72.0) 361679( 15.3)
ITB misses : 260(100.0) 0( 0.0) 0( 0.0)
Dcache LD misses :18264354( 0.3) 8605617( 0.1) 5838806976( 99.5)
DTB misses : 1223620( 4.4) 849( 0.0) 26788263( 95.6)
LDs merged in MAF : 48748( 0.0) 1494676( 0.1) 1996543335( 99.9)
LDU replay traps :18140020( 91.0) 1790381( 9.0) 555( 0.0)
WB/MAF replay traps : 27309( 0.0) 44646( 0.1) 58216534( 99.9)
MB stall cycles : 267732( 2.6) 9979054( 97.4) 2292( 0.0)
LDx_L instructions : 0( 0.0) 87203(100.0) 0( 0.0)
Scache misses : 5959809( 5.7) 4462748( 4.3) 93337972( 90.0)
I t e m < P A L > < O S > < U S E R>
Cycles :229751423( 2.0) 336746468( 2.9) 11082456462( 95.1)
Instructions :83659478( 0.3) 60983836( 0.2) 24505910953( 99.4)
nonissue cycles :143801699( 6.8) 169852262( 8.1) 1792187898( 85.1)
split-issue cycles : 354696( 0.5) 4009482( 5.6) 67181945( 93.9)
pipe-dry cycles :58809729( 7.3) 36401150( 4.5) 709906515( 88.2)
replay trap : 3440808( 3.7) 3597469( 3.9) 84884366( 92.3)
single-issue cycles :17323087( 7.9) 8848198( 4.0) 192463502( 88.0)
dual-issue cycles :33323312( 1.3) 2784437( 0.1) 2607158586( 98.6)
triple-issue cycles : 0( 0.0) 42145( 0.0) 3751788586(100.0)
quad-issue cycles : 0( 0.0) 458158( 0.0) 1961113860(100.0)
flow-change insns :16514290( 5.4) 2597946( 0.9) 286371054( 93.7)
IntOps issued : 3270098( 0.4) 7265740( 0.8) 885462583( 98.8)
FPOps issued : 0( 0.0) 70( 0.0) 16297072009(100.0)
loads issued : 5527759( 0.1) 7154792( 0.1) 6889783639( 99.8)
stores issued : 137601( 0.1) 2138112( 1.4) 148001691( 98.5)
Icache issued :42560119( 0.5) 46981666( 0.6) 7724262543( 98.9)
Dcache accesses : 5665455( 0.1) 14945119( 0.2) 7037001444( 99.7)
S cache access :13620681( 0.4) 10677937( 0.3) 3052853894( 99.2)
long(>15) stalls : 1470558( 6.6) 1489637( 6.6) 19443736( 86.8)
PC-mispredicts : 239246( 43.1) 314949( 56.8) 326( 0.1)
BR-mispredicts : 1283( 0.0) 50717( 0.4) 11311660( 99.5)
Icache/RFB misses : 1700169( 36.7) 1272585( 27.4) 1664040( 35.9)
ITB misses : 249(100.0) 0( 0.0) 0( 0.0)
Dcache LD misses : 3687679( 0.1) 16178308( 0.3) 6375418876( 99.7)
DTB misses : 128544( 2.4) 4684( 0.1) 5151404( 97.5)
LDs merged in MAF : 37113( 0.0) 2670839( 0.1) 3430618214( 99.9)
LDU replay traps : 3286998( 66.4) 1665591( 33.6) 295( 0.0)
WB/MAF replay traps : 1172( 0.0) 1088719( 1.3) 80825378( 98.7)
MB stall cycles : 186524( 2.3) 7921648( 97.7) 42( 0.0)
LDx_L instructions : 0( 0.0) 82343(100.0) 0( 0.0)
Scache misses : 4663041( 4.2) 5114323( 4.6) 101125812( 91.2)
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: how to optimize dgemm? No.2
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 ` how to optimize dgemm? No.3 Kazushige Goto
1 sibling, 0 replies; 7+ messages in thread
From: naohiko shimizu @ 1998-12-10 3:43 UTC (permalink / raw)
In article <19981209231455P.goto@statabo.rim.or.jp>
goto@statabo.rim.or.jp writes:
>>
>> Conclution:
>> We may(should?) take copying method to reduce DTB misses.
>>
JUST NO!
I have a plan to overcome the problem, and one of my undergraduate
students is now involved in.
But he is new to C or FORTRAN or Linux or Alpha(actually to computer),
then I cannot say when he will finish his program.
When he will finish his work, we will not have to worry about the problem.
Naohiko Shimizu
Dept. Communication Engr./Univ. TOKAI.
1117 Kitakaname Hiratsuka 259-12 Japan
TEL.+81-463-58-1211(ext. 4084) FAX.+81-463-58-8320
<URL http://shimizu-lab.et.u-tokai.ac.jp/>
^ permalink raw reply [flat|nested] 7+ messages in thread
* how to optimize dgemm? No.3
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
1998-12-20 14:43 ` how to optimize dgemm? No.4 Kazushige Goto
1 sibling, 1 reply; 7+ messages in thread
From: Kazushige Goto @ 1998-12-18 15:06 UTC (permalink / raw)
[-- 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
^ permalink raw reply [flat|nested] 7+ messages in thread
* how to optimize dgemm? No.4
1998-12-18 15:06 ` how to optimize dgemm? No.3 Kazushige Goto
@ 1998-12-20 14:43 ` Kazushige Goto
1998-12-21 12:23 ` how to optimize dgemm? No.5 Kazushige Goto
0 siblings, 1 reply; 7+ messages in thread
From: Kazushige Goto @ 1998-12-20 14:43 UTC (permalink / raw)
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 5766 bytes --]
This is a 4th part document of DGEMM.
6. Alpha assembler instruction
Alpha is a load-store architecture, so instructions are pretty simple.
load(ldt) -> multiply(mult) -> add(addt) -> store(stt)
(simplify of matrix-matrix multiply)
(a) registers
Alpha has 32 integer($0, $1, ... , $31) and 32 floating point
registers($f0, $f1, ..., $f31). All of registers are available
architecturaly. But there are some limitations on Linux or Digital
UNIX.
Integer Registers
$0 - $8, $22 - $25, $28 : Free, $0 is a return value
$9 - $15 : need to be saved if you use
$16 - $21 : Free, but used as argument value
$26 : return address
$27 : entry address but you may use.
$29 : Global Pointer
$30 : Stack Pointer
$31 : Zero register.
Floating point Registers
$f0,$f1,$f10-$f15,$f22-$f30 : Free, $f0 is a return value
$f2 - $f9 : need to be saved if you use
$f16 - $f21 : Free, but used as argument value
$f31 : Zero register.
(b) load instruction
example
Correspond to C source
ldt $f0, 0($1) # $f0 = *($1)
ldt $f1, 8($1) # $f1 = *($1 + 1)
ldt $f2, 16($1) # $f2 = *($1 + 2)
This mean, $1 is a base address, and 0, 8, 16 are offset from $1, and
load to $f0, $f1, $f2 from memory. Two "ldt" instruction can issue in
one clock.
Therefore, this example takes 2 clocks, but we do not know how long
it takes until $f0 - $f2 are available.
data in 1st level cache : 2 clocks
data in 2nd level cache : 8 clocks
data in 3rd level cache : 27 clocks(21164A 600MHz, PC164LX)
data in main memory : 134 clocks(21164A 600MHz, PC164LX)
Page fault/DTB miss : ???
So, we must issue load instruction in advance(8 clocks or more) if
we want to use 2nd cache enough without stall.
(b) multiply and add instruction
21164 can issue mutliply and add instruction on one clock except for
resource conflicts.
/* 1 clock */
mult $f0, $f1, $f2 # $f2 = $f0 * $f1
addt $f3, $f4, $f5 # $f5 = $f3 + $f4
But it takes 4 clocks until $f2 and $f5 are available(latency). So,
we must put other useful instructions. For example,
/* 1 clock */
mult $f0, $f1, $f2 # $f2 = $f0 * $f1
addt $f3, $f4, $f5 # $f5 = $f3 + $f4
/* 1 clock */
mult $f6, $f7, $f8 # $f8 = $f6 * $f7
addt $f9, $f10, $f11 # $f11 = $f9 + $f10
/* 1 clock */
mult $f12, $f13, $f14 # $f14 = $f12 * $f13
addt $f15, $f16, $f17 # $f17 = $f15 + $f16
/* 1 clock */
mult $f18, $f19, $f20 # $f20 = $f18 * $f19
addt $f21, $f22, $f23 # $f23 = $f21 + $f22
$f2 and $f5 are available at last.
mult $f2, $f5, $f24 # $f24 = $f2 * $f5
There are some examples about mutiply and add instructions.
ex.1
/* 5 clocks, because $f2 is conflicted */
mult $f0, $f1, $f2 # $f2 = $f0 * $f1
addt $f2, $f4, $f5 # $f5 = $f2 + $f4
ex.2
/* 1 clock if $f0 and $f1 are available */
mult $f0, $f1, $f2 # $f2 = $f0 * $f1
addt $f0, $f1, $f3 # $f3 = $f0 + $f1
ex.3
/* 1 clock, I use a lot of this case in gemm routine */
mult $f0, $f1, $f2 # $f2 = $f0 * $f1
addt $f0, $f1, $f0 # $f0 = $f0 + $f1
ex.4
/* 2 clocks */
mult $f0, $f1, $f2 # $f2 = $f0 * $f1
mult $f0, $f1, $f3 # $f3 = $f0 * $f1
(c) store instruction
Store instrucion is almost same as load instruction, but can issue
only one instruction in one clock. 21164 can not issue load and store
instructions simultaneously.
(d) slot
21164 simultaneously issues instructions with a "slot" which consist
of 2-integerinstruction(includes load/store instructions) and
2-floating point operation unless any resource conflicts.
The combination of inter instruction is pretty flexible, but one of
floating point instruction is limited(see above).
ex.1
/* 1 clock */
mult $f0, $f1, $f2 # $f2 = $f0 * $f1
ldt $f0, 0($1)
addt $f3, $f4, $f5 # $f5 = $f3 + $f4
addq $1, 8, $1
ex.2
/* 1 clock */
mult $f0, $f1, $f2 # $f2 = $f0 * $f1
ldt $f0, 0($1)
addt $f3, $f4, $f5 # $f5 = $f3 + $f4
ldt $f3, 8($1)
ex.3
/* 1 clock */
mult $f0, $f1, $f2 # $f2 = $f0 * $f1
addt $f3, $f4, $f5 # $f5 = $f3 + $f4
ldt $f0, 0($1)
ldt $f3, 8($1)
ex.4
/* 3 clocks or more over */
ldt $f0, 0($1)
mult $f0, $f1, $f2 # $f2 = $f0 * $f1
addt $f3, $f4, $f5 # $f5 = $f3 + $f4
ldt $f3, 8($1)
ex.5
/* 2 clocks, because can not issue load and tore instrucions simultaneously */
mult $f0, $f1, $f2 # $f2 = $f0 * $f1
ldt $f0, 0($1)
addt $f3, $f4, $f5 # $f5 = $f3 + $f4
stt $f6, 8($1)
ex.6
/* 2 clocks, because can not issue 2-store instrucions simultaneously */
mult $f0, $f1, $f2 # $f2 = $f0 * $f1
stt $f4, 0($1)
addt $f3, $f4, $f5 # $f5 = $f3 + $f4
stt $f6, 8($1)
Continue to No. 5
Thanks,
goto@statabo.rim.or.jp
^ permalink raw reply [flat|nested] 7+ messages in thread
* how to optimize dgemm? No.5
1998-12-20 14:43 ` how to optimize dgemm? No.4 Kazushige Goto
@ 1998-12-21 12:23 ` Kazushige Goto
0 siblings, 0 replies; 7+ messages in thread
From: Kazushige Goto @ 1998-12-21 12:23 UTC (permalink / raw)
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 39034 bytes --]
This is a 5th part document of DGEMM.
I explain only "alignment value(multiply for 8)".
I'll attach complete source later.
7. Optimizing
(a) replace matrix array(s_a, s_b, c) with pointer
<< 118.9 MFlops >>
int i, ii, j, jj, l, ll;
int ls, is, js;
double temp;
#define LDA Q
#define LDB P
double s_a[P][LDA], s_b[R][LDB];
double *a_offset;
double *b_offset;
double *c_offset;
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++) {
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++) /* Copying & Moved */
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++) {
c_offset = c + is + j*ldc;
for (i = is, ii=0; i < MIN(m, is+Q); i++, ii++) {
temp = 0.;
a_offset = &s_a[0] [0] + ii;
b_offset = &s_b[jj][0];
for (l = MIN(k, ls+P) - ls; l > 0; l--) {
temp += *b_offset * *a_offset;
a_offset += LDA;
b_offset ++;
}
*c_offset += alpha * temp;
c_offset ++;
}
}
}
}
}
(b) separate load, mutiply and add instruction
<< 119.2 MFlops >>
int i, ii, j, jj, l, ll;
int ls, is, js;
#define LDA Q
#define LDB P
double s_a[P][LDA], s_b[R][LDB];
double *a_offset;
double *b_offset;
double *c_offset;
double atemp; /* for data loading of A */
double btemp; /* for data loading of B */
double ctemp; /* for summing of dot product */
double temp; /* temporary */
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++) {
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++) /* Copying & Moved */
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++) {
c_offset = c + is + j*ldc;
for (i = is, ii=0; i < MIN(m, is+Q); i++, ii++) {
ctemp = 0.;
a_offset = &s_a[0] [0] + ii;
b_offset = &s_b[jj][0];
for (l = MIN(k, ls+P) - ls; l > 0; l--) { /* just modified */
atemp = *a_offset; /* load */
btemp = *b_offset; /* load */
temp = atemp * btemp;/* multiply */
ctemp += temp; /* add */
a_offset += LDA;
b_offset ++;
}
*c_offset += alpha * ctemp;
c_offset ++;
}
}
}
}
}
(c) unrolling for "i loop"
<< 245.2 MFlops >> <- much faster than before!!
int i, ii, j, jj, l, ll;
int ls, is, js;
#define LDA Q
#define LDB P
double s_a[P][LDA], s_b[R][LDB];
double *a_offset;
double *b_offset;
double *c_offset;
double atemp1, atemp2, atemp3, atemp4; /* for data loading of A */
double btemp1; /* for data loading of B */
double ctemp1, ctemp2, ctemp3, ctemp4; /* for summing of dot product */
double temp1, temp2, temp3, temp4; /* temporary */
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++) {
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++) /* Copying & Moved */
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++) {
c_offset = c + is + j*ldc;
for (i = is, ii=0; i < MIN(m, is+Q); i += 4, ii += 4) {
a_offset = &s_a[0] [0] + ii;
b_offset = &s_b[jj][0];
ctemp1 = 0.;
ctemp2 = 0.;
ctemp3 = 0.;
ctemp4 = 0.;
for (l = MIN(k, ls+P) - ls; l > 0; l --) {
/* Data loading of A */
atemp1 = *(a_offset + 0);
atemp2 = *(a_offset + 1);
atemp3 = *(a_offset + 2);
atemp4 = *(a_offset + 3);
/* Data loading of B */
btemp1 = *b_offset;
/* calculation of temp1 to temp4 takes 4 clocks */
temp1 = atemp1 * btemp1;
temp2 = atemp2 * btemp1;
temp3 = atemp3 * btemp1;
temp4 = atemp4 * btemp1;
/* calculation of ctemp1 to ctemp4 takes 4 clocks */
/* total : 8 clocks exactly */
ctemp1 = ctemp1 + temp1;
ctemp2 = ctemp2 + temp2;
ctemp3 = ctemp3 + temp3;
ctemp4 = ctemp4 + temp4;
a_offset += LDA;
b_offset ++;
}
*(c_offset + 0) += alpha * ctemp1;
*(c_offset + 1) += alpha * ctemp2;
*(c_offset + 2) += alpha * ctemp3;
*(c_offset + 3) += alpha * ctemp4;
c_offset += 4;
}
}
}
}
}
(d) unrolling for "j loop"
<< 295.6 MFlops >>
int i, ii, j, jj, l, ll;
int ls, is, js;
#define LDA Q
#define LDB P
double s_a[P][LDA], s_b[R][LDB];
double *a_offset;
double *b_offset;
double *c_offset1, *c_offset2;
double atemp1, atemp2, atemp3, atemp4; /* for data loading of A */
double btemp1, btemp2; /* for data loading of B */
double ctemp1, ctemp2, ctemp3, ctemp4; /* for summing of dot product */
double ctemp5, ctemp6, ctemp7, ctemp8; /* for summing of dot product */
double temp1, temp2, temp3, temp4; /* temporary */
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++) {
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++) /* Copying & Moved */
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+=2, jj+=2) {
c_offset1 = c + is + j*ldc;
c_offset2 = c_offset1 + ldc;
for (i = is, ii=0; i < MIN(m, is+Q); i += 4, ii += 4) {
a_offset = &s_a[0] [0] + ii;
b_offset = &s_b[jj][0];
ctemp1 = 0.; ctemp2 = 0.; ctemp3 = 0.; ctemp4 = 0.;
ctemp5 = 0.; ctemp6 = 0.; ctemp7 = 0.; ctemp8 = 0.;
for (l = MIN(k, ls+P) - ls; l > 0; l --) {
/* Data loading of A */
atemp1 = *(a_offset + 0);
atemp2 = *(a_offset + 1);
atemp3 = *(a_offset + 2);
atemp4 = *(a_offset + 3);
/* Data loading of B */
btemp1 = *(b_offset + 0);
btemp2 = *(b_offset + LDB); /* LDB is a constant value */
temp1 = atemp1 * btemp1;
temp2 = atemp2 * btemp1;
temp3 = atemp3 * btemp1;
temp4 = atemp4 * btemp1;
ctemp1 = ctemp1 + temp1;
ctemp2 = ctemp2 + temp2;
ctemp3 = ctemp3 + temp3;
ctemp4 = ctemp4 + temp4;
temp1 = atemp1 * btemp2;
temp2 = atemp2 * btemp2;
temp3 = atemp3 * btemp2;
temp4 = atemp4 * btemp2;
ctemp5 = ctemp5 + temp1;
ctemp6 = ctemp6 + temp2;
ctemp7 = ctemp7 + temp3;
ctemp8 = ctemp8 + temp4;
a_offset += LDA;
b_offset ++;
}
*(c_offset1 + 0) += alpha * ctemp1;
*(c_offset1 + 1) += alpha * ctemp2;
*(c_offset1 + 2) += alpha * ctemp3;
*(c_offset1 + 3) += alpha * ctemp4;
*(c_offset2 + 0) += alpha * ctemp5;
*(c_offset2 + 1) += alpha * ctemp6;
*(c_offset2 + 2) += alpha * ctemp7;
*(c_offset2 + 3) += alpha * ctemp8;
c_offset1 += 4;
c_offset2 += 4;
}
}
}
}
}
(e) swapping calculation
The kernel calculation is
loop:
temp1 = atemp1 * btemp1;
temp2 = atemp2 * btemp1;
temp3 = atemp3 * btemp1;
temp4 = atemp4 * btemp1;
ctemp1 = ctemp1 + temp1;
ctemp2 = ctemp2 + temp2;
ctemp3 = ctemp3 + temp3;
ctemp4 = ctemp4 + temp4;
temp1 = atemp1 * btemp2;
temp2 = atemp2 * btemp2;
temp3 = atemp3 * btemp2;
temp4 = atemp4 * btemp2;
ctemp5 = ctemp5 + temp1;
ctemp6 = ctemp6 + temp2;
ctemp7 = ctemp7 + temp3;
ctemp8 = ctemp8 + temp4;
goto loop
If we optimize this calculation, it takes 12 clocks.
loop:
/* 4 clock(block A) */
temp1 = atemp1 * btemp1;
temp2 = atemp2 * btemp1;
temp3 = atemp3 * btemp1;
temp4 = atemp4 * btemp1;
/* 4 clock(block B) */
ctemp1 = ctemp1 + temp1;
temp1 = atemp1 * btemp2;
ctemp2 = ctemp2 + temp2;
temp2 = atemp2 * btemp2;
ctemp3 = ctemp3 + temp3;
temp3 = atemp3 * btemp2;
ctemp4 = ctemp4 + temp4;
temp4 = atemp4 * btemp2;
/* 4 clock(Block C) */
ctemp5 = ctemp5 + temp1;
ctemp6 = ctemp6 + temp2;
ctemp7 = ctemp7 + temp3;
ctemp8 = ctemp8 + temp4;
goto loop
So, we move "Block C" from bottom to top.
temp1 = 0.;
temp2 = 0.;
temp3 = 0.;
temp4 = 0.;
loop:
/* 4 clock(Block C) */
ctemp5 = ctemp5 + temp1;
ctemp6 = ctemp6 + temp2;
ctemp7 = ctemp7 + temp3;
ctemp8 = ctemp8 + temp4;
/* 4 clock(block A) */
temp1 = atemp1 * btemp1;
temp2 = atemp2 * btemp1;
temp3 = atemp3 * btemp1;
temp4 = atemp4 * btemp1;
/* 4 clock(block B) */
ctemp1 = ctemp1 + temp1;
temp1 = atemp1 * btemp2;
ctemp2 = ctemp2 + temp2;
temp2 = atemp2 * btemp2;
ctemp3 = ctemp3 + temp3;
temp3 = atemp3 * btemp2;
ctemp4 = ctemp4 + temp4;
temp4 = atemp4 * btemp2;
goto loop
/* 4 clock(Block C) */
ctemp5 = ctemp5 + temp1;
ctemp6 = ctemp6 + temp2;
ctemp7 = ctemp7 + temp3;
ctemp8 = ctemp8 + temp4;
Then this routine should be optmized as follows, and it runs 8 clocks.
temp1 = 0.;
temp2 = 0.;
temp3 = 0.;
temp4 = 0.;
loop:
/* 4 clock(block A) */
ctemp5 = ctemp5 + temp1;
temp1 = atemp1 * btemp1;
ctemp6 = ctemp6 + temp2;
temp2 = atemp2 * btemp1;
ctemp7 = ctemp7 + temp3;
temp3 = atemp3 * btemp1;
ctemp8 = ctemp8 + temp4;
temp4 = atemp4 * btemp1;
/* 4 clock(block B) */
ctemp1 = ctemp1 + temp1;
temp1 = atemp1 * btemp2;
ctemp2 = ctemp2 + temp2;
temp2 = atemp2 * btemp2;
ctemp3 = ctemp3 + temp3;
temp3 = atemp3 * btemp2;
ctemp4 = ctemp4 + temp4;
temp4 = atemp4 * btemp2;
goto loop
/* 4 clock(Block C) */
ctemp5 = ctemp5 + temp1;
ctemp6 = ctemp6 + temp2;
ctemp7 = ctemp7 + temp3;
ctemp8 = ctemp8 + temp4;
<<323.2 MFlops >>
int i, ii, j, jj, l, ll;
int ls, is, js;
#define LDA Q
#define LDB P
double s_a[P][LDA], s_b[R][LDB];
double *a_offset;
double *b_offset;
double *c_offset1, *c_offset2;
double atemp1, atemp2, atemp3, atemp4; /* for data loading of A */
double btemp1, btemp2; /* for data loading of B */
double ctemp1, ctemp2, ctemp3, ctemp4; /* for summing of dot product */
double ctemp5, ctemp6, ctemp7, ctemp8; /* for summing of dot product */
double temp1, temp2, temp3, temp4; /* temporary */
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++) {
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++) /* Copying & Moved */
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+=2, jj+=2) {
c_offset1 = c + is + j*ldc;
c_offset2 = c_offset1 + ldc;
for (i = is, ii=0; i < MIN(m, is+Q); i += 4, ii += 4) {
a_offset = &s_a[0] [0] + ii;
b_offset = &s_b[jj][0];
ctemp1 = 0.; ctemp2 = 0.; ctemp3 = 0.; ctemp4 = 0.;
ctemp5 = 0.; ctemp6 = 0.; ctemp7 = 0.; ctemp8 = 0.;
/* must be cleared */
temp1 = 0.; temp2 = 0.; temp3 = 0.; temp4 = 0.;
for (l = MIN(k, ls+P) - ls; l > 0; l --) {
/* Data loading of A */
atemp1 = *(a_offset + 0);
atemp2 = *(a_offset + 1);
atemp3 = *(a_offset + 2);
atemp4 = *(a_offset + 3);
/* Data loading of B */
btemp1 = *(b_offset + 0);
btemp2 = *(b_offset + LDB); /* LDB is a constant value */
ctemp5 = ctemp5 + temp1;
ctemp6 = ctemp6 + temp2;
ctemp7 = ctemp7 + temp3;
ctemp8 = ctemp8 + temp4;
temp1 = atemp1 * btemp1;
temp2 = atemp2 * btemp1;
temp3 = atemp3 * btemp1;
temp4 = atemp4 * btemp1;
ctemp1 = ctemp1 + temp1;
ctemp2 = ctemp2 + temp2;
ctemp3 = ctemp3 + temp3;
ctemp4 = ctemp4 + temp4;
temp1 = atemp1 * btemp2;
temp2 = atemp2 * btemp2;
temp3 = atemp3 * btemp2;
temp4 = atemp4 * btemp2;
a_offset += LDA;
b_offset ++;
}
ctemp5 = ctemp5 + temp1;
ctemp6 = ctemp6 + temp2;
ctemp7 = ctemp7 + temp3;
ctemp8 = ctemp8 + temp4;
*(c_offset1 + 0) += alpha * ctemp1;
*(c_offset1 + 1) += alpha * ctemp2;
*(c_offset1 + 2) += alpha * ctemp3;
*(c_offset1 + 3) += alpha * ctemp4;
*(c_offset2 + 0) += alpha * ctemp5;
*(c_offset2 + 1) += alpha * ctemp6;
*(c_offset2 + 2) += alpha * ctemp7;
*(c_offset2 + 3) += alpha * ctemp8;
c_offset1 += 4;
c_offset2 += 4;
}
}
}
}
}
(f) unrolling for "l loop"
<< 336.2 MFlops >>
int i, ii, j, jj, l, ll;
int ls, is, js;
#define LDA Q
#define LDB P
double s_a[P][LDA], s_b[R][LDB];
double *a_offset;
double *b_offset;
double *c_offset1, *c_offset2;
double atemp1, atemp2, atemp3, atemp4; /* for data loading of A */
double btemp1, btemp2; /* for data loading of B */
double ctemp1, ctemp2, ctemp3, ctemp4; /* for summing of dot product */
double ctemp5, ctemp6, ctemp7, ctemp8; /* for summing of dot product */
double temp1, temp2, temp3, temp4; /* temporary */
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++) {
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++) /* Copying & Moved */
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+=2, jj+=2) {
c_offset1 = c + is + j*ldc;
c_offset2 = c_offset1 + ldc;
for (i = is, ii=0; i < MIN(m, is+Q); i += 4, ii += 4) {
a_offset = &s_a[0] [0] + ii;
b_offset = &s_b[jj][0];
ctemp1 = 0.; ctemp2 = 0.; ctemp3 = 0.; ctemp4 = 0.;
ctemp5 = 0.; ctemp6 = 0.; ctemp7 = 0.; ctemp8 = 0.;
/* must be cleared */
temp1 = 0.; temp2 = 0.; temp3 = 0.; temp4 = 0.;
for (l = MIN(k, ls+P) - ls; l > 0; l -= 4) {
/* Data loading of A */
atemp1 = *(a_offset + 0);
atemp2 = *(a_offset + 1);
atemp3 = *(a_offset + 2);
atemp4 = *(a_offset + 3);
a_offset += LDA;
/* Data loading of B */
btemp1 = *(b_offset + 0);
btemp2 = *(b_offset + LDB); /* LDB is a constant value */
ctemp5 = ctemp5 + temp1;
ctemp6 = ctemp6 + temp2;
ctemp7 = ctemp7 + temp3;
ctemp8 = ctemp8 + temp4;
temp1 = atemp1 * btemp1;
temp2 = atemp2 * btemp1;
temp3 = atemp3 * btemp1;
temp4 = atemp4 * btemp1;
ctemp1 = ctemp1 + temp1;
ctemp2 = ctemp2 + temp2;
ctemp3 = ctemp3 + temp3;
ctemp4 = ctemp4 + temp4;
temp1 = atemp1 * btemp2;
temp2 = atemp2 * btemp2;
temp3 = atemp3 * btemp2;
temp4 = atemp4 * btemp2;
/* Data loading of A */
atemp1 = *(a_offset + 0);
atemp2 = *(a_offset + 1);
atemp3 = *(a_offset + 2);
atemp4 = *(a_offset + 3);
a_offset += LDA;
/* Data loading of B */
btemp1 = *(b_offset + 1);
btemp2 = *(b_offset + 1 + LDB); /* LDB is a constant value */
ctemp5 = ctemp5 + temp1;
ctemp6 = ctemp6 + temp2;
ctemp7 = ctemp7 + temp3;
ctemp8 = ctemp8 + temp4;
temp1 = atemp1 * btemp1;
temp2 = atemp2 * btemp1;
temp3 = atemp3 * btemp1;
temp4 = atemp4 * btemp1;
ctemp1 = ctemp1 + temp1;
ctemp2 = ctemp2 + temp2;
ctemp3 = ctemp3 + temp3;
ctemp4 = ctemp4 + temp4;
temp1 = atemp1 * btemp2;
temp2 = atemp2 * btemp2;
temp3 = atemp3 * btemp2;
temp4 = atemp4 * btemp2;
/* Data loading of A */
atemp1 = *(a_offset + 0);
atemp2 = *(a_offset + 1);
atemp3 = *(a_offset + 2);
atemp4 = *(a_offset + 3);
a_offset += LDA;
/* Data loading of B */
btemp1 = *(b_offset + 2);
btemp2 = *(b_offset + 2 + LDB); /* LDB is a constant value */
ctemp5 = ctemp5 + temp1;
ctemp6 = ctemp6 + temp2;
ctemp7 = ctemp7 + temp3;
ctemp8 = ctemp8 + temp4;
temp1 = atemp1 * btemp1;
temp2 = atemp2 * btemp1;
temp3 = atemp3 * btemp1;
temp4 = atemp4 * btemp1;
ctemp1 = ctemp1 + temp1;
ctemp2 = ctemp2 + temp2;
ctemp3 = ctemp3 + temp3;
ctemp4 = ctemp4 + temp4;
temp1 = atemp1 * btemp2;
temp2 = atemp2 * btemp2;
temp3 = atemp3 * btemp2;
temp4 = atemp4 * btemp2;
/* Data loading of A */
atemp1 = *(a_offset + 0);
atemp2 = *(a_offset + 1);
atemp3 = *(a_offset + 2);
atemp4 = *(a_offset + 3);
a_offset += LDA;
/* Data loading of B */
btemp1 = *(b_offset + 3);
btemp2 = *(b_offset + 3 + LDB); /* LDB is a constant value */
b_offset += 4;
ctemp5 = ctemp5 + temp1;
ctemp6 = ctemp6 + temp2;
ctemp7 = ctemp7 + temp3;
ctemp8 = ctemp8 + temp4;
temp1 = atemp1 * btemp1;
temp2 = atemp2 * btemp1;
temp3 = atemp3 * btemp1;
temp4 = atemp4 * btemp1;
ctemp1 = ctemp1 + temp1;
ctemp2 = ctemp2 + temp2;
ctemp3 = ctemp3 + temp3;
ctemp4 = ctemp4 + temp4;
temp1 = atemp1 * btemp2;
temp2 = atemp2 * btemp2;
temp3 = atemp3 * btemp2;
temp4 = atemp4 * btemp2;
}
ctemp5 = ctemp5 + temp1;
ctemp6 = ctemp6 + temp2;
ctemp7 = ctemp7 + temp3;
ctemp8 = ctemp8 + temp4;
*(c_offset1 + 0) += alpha * ctemp1;
*(c_offset1 + 1) += alpha * ctemp2;
*(c_offset1 + 2) += alpha * ctemp3;
*(c_offset1 + 3) += alpha * ctemp4;
*(c_offset2 + 0) += alpha * ctemp5;
*(c_offset2 + 1) += alpha * ctemp6;
*(c_offset2 + 2) += alpha * ctemp7;
*(c_offset2 + 3) += alpha * ctemp8;
c_offset1 += 4;
c_offset2 += 4;
}
}
}
}
}
(g) expanding register for loading(s_a and s_b, total 24 floating
point registers)
<< 349.2 MFlops >>
int i, ii, j, jj, l, ll;
int ls, is, js;
#define LDA Q
#define LDB P
double s_a[P][LDA], s_b[R][LDB];
double *a_offset;
double *b_offset;
double *c_offset1, *c_offset2;
double atemp1, atemp2, atemp3, atemp4; /* for data loading of A */
double atemp5, atemp6, atemp7, atemp8; /* for data loading of A */
double btemp1, btemp2, btemp3, btemp4; /* for data loading of B */
double ctemp1, ctemp2, ctemp3, ctemp4; /* for summing of dot product */
double ctemp5, ctemp6, ctemp7, ctemp8; /* for summing of dot product */
double temp1, temp2, temp3, temp4; /* temporary */
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++) {
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++) /* Copying & Moved */
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+=2, jj+=2) {
c_offset1 = c + is + j*ldc;
c_offset2 = c_offset1 + ldc;
for (i = is, ii=0; i < MIN(m, is+Q); i += 4, ii += 4) {
a_offset = &s_a[0] [0] + ii;
b_offset = &s_b[jj][0];
ctemp1 = 0.; ctemp2 = 0.; ctemp3 = 0.; ctemp4 = 0.;
ctemp5 = 0.; ctemp6 = 0.; ctemp7 = 0.; ctemp8 = 0.;
/* must be cleared */
temp1 = 0.; temp2 = 0.; temp3 = 0.; temp4 = 0.;
for (l = MIN(k, ls+P) - ls; l > 0; l -= 4) {
/* Data loading of A */
atemp1 = *(a_offset + 0);
atemp2 = *(a_offset + 1);
atemp3 = *(a_offset + 2);
atemp4 = *(a_offset + 3);
a_offset += LDA;
/* Data loading of B */
btemp1 = *(b_offset + 0);
btemp2 = *(b_offset + LDB); /* LDB is a constant value */
ctemp5 = ctemp5 + temp1;
ctemp6 = ctemp6 + temp2;
ctemp7 = ctemp7 + temp3;
ctemp8 = ctemp8 + temp4;
temp1 = atemp1 * btemp1;
temp2 = atemp2 * btemp1;
temp3 = atemp3 * btemp1;
temp4 = atemp4 * btemp1;
ctemp1 = ctemp1 + temp1;
ctemp2 = ctemp2 + temp2;
ctemp3 = ctemp3 + temp3;
ctemp4 = ctemp4 + temp4;
temp1 = atemp1 * btemp2;
temp2 = atemp2 * btemp2;
temp3 = atemp3 * btemp2;
temp4 = atemp4 * btemp2;
/* Data loading of A */
atemp5 = *(a_offset + 0);
atemp6 = *(a_offset + 1);
atemp7 = *(a_offset + 2);
atemp8 = *(a_offset + 3);
a_offset += LDA;
/* Data loading of B */
btemp3 = *(b_offset + 1);
btemp4 = *(b_offset + 1 + LDB); /* LDB is a constant value */
ctemp5 = ctemp5 + temp1;
ctemp6 = ctemp6 + temp2;
ctemp7 = ctemp7 + temp3;
ctemp8 = ctemp8 + temp4;
temp1 = atemp5 * btemp3;
temp2 = atemp6 * btemp3;
temp3 = atemp7 * btemp3;
temp4 = atemp8 * btemp3;
ctemp1 = ctemp1 + temp1;
ctemp2 = ctemp2 + temp2;
ctemp3 = ctemp3 + temp3;
ctemp4 = ctemp4 + temp4;
temp1 = atemp5 * btemp4;
temp2 = atemp6 * btemp4;
temp3 = atemp7 * btemp4;
temp4 = atemp8 * btemp4;
/* Data loading of A */
atemp1 = *(a_offset + 0);
atemp2 = *(a_offset + 1);
atemp3 = *(a_offset + 2);
atemp4 = *(a_offset + 3);
a_offset += LDA;
/* Data loading of B */
btemp1 = *(b_offset + 2);
btemp2 = *(b_offset + 2 + LDB); /* LDB is a constant value */
ctemp5 = ctemp5 + temp1;
ctemp6 = ctemp6 + temp2;
ctemp7 = ctemp7 + temp3;
ctemp8 = ctemp8 + temp4;
temp1 = atemp1 * btemp1;
temp2 = atemp2 * btemp1;
temp3 = atemp3 * btemp1;
temp4 = atemp4 * btemp1;
ctemp1 = ctemp1 + temp1;
ctemp2 = ctemp2 + temp2;
ctemp3 = ctemp3 + temp3;
ctemp4 = ctemp4 + temp4;
temp1 = atemp1 * btemp2;
temp2 = atemp2 * btemp2;
temp3 = atemp3 * btemp2;
temp4 = atemp4 * btemp2;
/* Data loading of A */
atemp5 = *(a_offset + 0);
atemp6 = *(a_offset + 1);
atemp7 = *(a_offset + 2);
atemp8 = *(a_offset + 3);
a_offset += LDA;
/* Data loading of B */
btemp3 = *(b_offset + 3);
btemp4 = *(b_offset + 3 + LDB); /* LDB is a constant value */
b_offset += 4;
ctemp5 = ctemp5 + temp1;
ctemp6 = ctemp6 + temp2;
ctemp7 = ctemp7 + temp3;
ctemp8 = ctemp8 + temp4;
temp1 = atemp5 * btemp3;
temp2 = atemp6 * btemp3;
temp3 = atemp7 * btemp3;
temp4 = atemp8 * btemp3;
ctemp1 = ctemp1 + temp1;
ctemp2 = ctemp2 + temp2;
ctemp3 = ctemp3 + temp3;
ctemp4 = ctemp4 + temp4;
temp1 = atemp5 * btemp4;
temp2 = atemp6 * btemp4;
temp3 = atemp7 * btemp4;
temp4 = atemp8 * btemp4;
}
ctemp5 = ctemp5 + temp1;
ctemp6 = ctemp6 + temp2;
ctemp7 = ctemp7 + temp3;
ctemp8 = ctemp8 + temp4;
*(c_offset1 + 0) += alpha * ctemp1;
*(c_offset1 + 1) += alpha * ctemp2;
*(c_offset1 + 2) += alpha * ctemp3;
*(c_offset1 + 3) += alpha * ctemp4;
*(c_offset2 + 0) += alpha * ctemp5;
*(c_offset2 + 1) += alpha * ctemp6;
*(c_offset2 + 2) += alpha * ctemp7;
*(c_offset2 + 3) += alpha * ctemp8;
c_offset1 += 4;
c_offset2 += 4;
}
}
}
}
}
Continue to No. 6
Thanks,
goto@statabo.rim.or.jp
void dgemm_nn(int m, int n, int k, double alpha, double *a,
int lda, double *b, int ldb,
double *c, int ldc){
#define MIN(a,b) ((a)>(b)? (b):(a))
#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++) {
for (l = ls, ll=0; l < MIN(k, ls+P); l++, ll++) /* Copying & Moved */
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;
}
}
}
}
}
}
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~1998-12-21 12:23 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` how to optimize dgemm? No.3 Kazushige Goto
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox