Usenet comp.sys.dec (1990-2013)
 help / color / mirror / Atom feed
From: Lennart Bengtsson <tfylb@fy.no.chalmers.spam.se>
Subject: Re: Fast matrix multiply and benchmark
Date: 1998/12/21	[thread overview]
Message-ID: <367D9D1E.32A9@fy.no.chalmers.spam.se> (raw)
In-Reply-To: 75ca18$ss8$1@news01cc.so-net.ne.jp

Hi,

Dr. Naohiko Shimizu recently proposed a way to organize the computation
of matrix products so that the number of TLB missed was minimized.
TLB misses may occur, for instance, when accessing the elements of
a large matrix row by row, and the number of columns are greater than
the available number of TLB entries. This is the case when calculating
a matrix product using dot products:

c(i,j) = sum_k a(i,k)*b(k,j)   (inner loop over index k)

Accessing elements row-by-row also has other problems, like cache
trashing (cache collisions between elements in the same row.)

For computations with a high degree of data reuse, like BLAS 3, I would
like to point out that data copying techniques can be very useful for 
making better use of the cache and TLB. In the matrix multiply case, the
idea is to copy blocks of A to a work array that fits in the cache, and
at the same time transpose it. Then the inner loop above accesses both A
and B elements column-wise. Compared with Dr. Shimizu's approach, this
way of organizing the loops avoids store instructions in the inner loop.
For processors with write-through L1 caches like SUN's ultrasparc,
reducing the number of store instructions is often a good idea.

Most processors have at least two levels of cache (IBM's power 2 chips
and HP's processors are notable exceptions). These benefit from a
two-level cache blocking strategy: A is blocked of L2 cache and B is
blocked for L1 cache. The matrix multiply code below illustrates this
point. The performance of the code is excellent: for a 1000*1000 matrix
multiply I have measured 320 Mflops (80 % of peak) on a O2000 server
(195 MHz R10000, 4 MB L2 cache) and 304 Mflops (76 % of peak) on a SUN
ultra-2 workstation (200 MHz ultrasparc-I, 1 MB L2 cache.)

In order to raise the flops/cycle ratio above one, outer loop unrolling,
controlled by the directives below, is employed. It is straightforward
(but tedious) to unroll the loops manually, in case the compiler cannot
do it automatically (most don't). The goal of the unrolling technique
(also called register blocking) is to calculate eight elements of the C
matrix at the same time in the inner loop. Then the number of loads
during one iteration is 6 (four from atemp and two from btemp), while
the number of floating point ops is 16. It is not necessary to unroll
the *inner* loop manually, because it will be pipelined by the compiler
(most commercial fortran compilers will do that at a sufficiently high
level of optimization.)


c 'N','N' version of dgemm, alpha=beta=1 assumed for simplicity
c Lennart Bengtsson, Chalmers
c Covered by the GNU public license: free to use for non-commercial
c purposes

      subroutine dgemm_nn(m,n,k,a,b,c)
      integer m,n,k
      real*8 a(m,k),b(k,n),c(m,n)

      integer kb,ib,jb
c Cache blocking parameters
c atemp should fit in L2 cache
c btemp + four columnms of atemp should fit in L1 cache
      parameter(kb=80,ib=1500,jb=16)
      real*8 atemp(kb,ib), btemp(kb,jb), c1
      integer i1,j1,k1,i2,j2,nbi,nbj,nbk

      do k1 = 1, k, kb
         nbk = min(kb,k-k1+1)
         do i1 = 1, m, ib
            nbi = min(ib,m-i1+1)
c Transpose block of A
            do k2 = 1, nbk
               do i = 1, nbi
                  atemp(k2,i) = a(i+i1-1,k2+k1-1)
               enddo
            enddo
            do j1 = 1, n, jb
               nbj = min(jb, n-j1+1)
c Copy block of B to L1 cache
               do j2 = 1, nbj
                  do k2 = 1, nbk
                     btemp(k2,j2) = b(k2+k1-1,j2+j1-1)
                  enddo
               enddo
C*$* UNROLL (4)
               do i2 = 1, nbi
C*$* UNROLL (2)
                  do j2 = 1, nbj
                     c1 = c(i1+i2-1,j2+j1-1)
c dot product
                     do k2 = 1, nbk
                        c1 = c1 + atemp(k2,i2)*btemp(k2,j2)
                     enddo
                     c(i1+i2-1,j2+j1-1) = c1
                  enddo
               enddo
            enddo
         enddo
      enddo
      end



Lennart Bengtsson
Department of Applied Physics
Chalmers University of Technology
Sweden

(remove .no.spam from e-mail address above)


pshimizu@fa2.so-net.ne.jp wrote:
> 
> Hi, I would like to announce that my DGEMM code (matrix multiply
> subroutine compatible with non-transposed version of BLAS3) with
> benchmarking facility is released on my WEB site.
> Alpha community already has a assembler DGEMM code, but my code
> is designed for generic architecture(though it will use upto 26
> fp registers in the inner loop).
> I hope that when running on the out-of-order machines it will be
> as fast as the assembler counterpart.
> 
> I got 202MFLOPS on HP D series PA8000(180MHz?) machine without
> parameter tuning.
> 
> This routine coded by C and callable from FORTRAN.
> 
> And it has a BENCHMARCK main routine to check the performance of
> 1000 by 1000 matrix multiply on the target machine.
> If the program is compiled with -DMAIN by C compiler you will get a
> benchmark code.
> To reduce TLB miss rate, my DGEMM code uses a little more memory
> traffic, then a system with smart memory subsystem will mark higher
> performance.
> 
> The code is designed to run faster on the cache/TLB based machines.
> Some parameter should be developed to extract best performance on a
> specific machine.
> I myself have only Alpha(21064A) machine, and I assume other
> parameters with some technical documents.
> 
> See the following announcemnt document and the source for more
> information of my DGEMM code.
> URL:
> http://shimizu-lab.et.u-tokai.ac.jp/pgm/gemm/index.html
> 
> Some benchmarking results with my and my university's machines
> are also on the WEB:
> 
> http://shimizu-lab.et.u-tokai.ac.jp/pgm/gemm/bench.html
> 
> ------------------------- announcement ---------------------------
> Fast DGEMM routine with Blocking. Last updated date : 98.12.18
> 
> Contact information:
> 
> Dr. Naohiko Shimizu
> School of Engr. Tokai Univ.
> 1117 Kitakaname Hiratsuka
> Kanagawa 259-1292 Japan
> email: nshimizu@et.u-tokai.ac.jp
> URL: http://shimizu-lab.et.u-tokai.ac.jp
> 
(truncated)



  parent reply	other threads:[~1998-12-21  5:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-12-18  5:00 Fast matrix multiply and benchmark pshimizu
1998-12-18  5:00 ` Dann Corbit
1998-12-21  5:00 ` Lennart Bengtsson [this message]
1998-12-21  5:00   ` Bernd Paysan
1998-12-26  5:00   ` pshimizu
1998-12-28  5:00     ` Andy Glew
1998-12-29  5:00       ` pshimizu
1998-12-29  5:00         ` Andy Glew
1998-12-28  5:00     ` Andy Glew
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