alphalinux.org high-perf list (1998-2001)
 help / color / mirror / Atom feed
From: Kazushige Goto <goto@statabo.rim.or.jp>
Subject: how to optimize dgemm? No.2
Date: Wed, 09 Dec 1998 23:14:55 +0900	[thread overview]
Message-ID: <19981209231455P.goto@statabo.rim.or.jp> (raw)
In-Reply-To: <19981130231931B.goto@statabo.rim.or.jp>

[-- 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)

  parent reply	other threads:[~1998-12-09 14:14 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 ` Kazushige Goto [this message]
1998-12-10  3:43   ` how to optimize dgemm? No.2 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
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