* Re: Fast matrix multiply and benchmark
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
1 sibling, 0 replies; 9+ messages in thread
From: Dann Corbit @ 1998-12-18 5:00 UTC (permalink / raw)
Gateway 2000 GP6-300 with 128 megs ram. Microsoft VC++ 6.0 compiler
Pentium II 300 Mhz results:
m:1000 n:1000 k:1000
Shimizu's DGEMM : 80.857 MFLOPS(24.735 sec)
m:1000 n:1000 k:1000
Shimizu's DGEMM : 80.564 MFLOPS(24.825 sec)
m:1000 n:1000 k:1000
Shimizu's DGEMM : 81.281 MFLOPS(24.606 sec)
There is a C and Fortran MATRIX inversion benchmark written by
DaveGemini@aol.com that you may be interested in.
--
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ ftp: ftp://rtfm.mit.edu, C-FAQ Book: ISBN 0-201-84519-9
Try "C Programming: A Modern Approach" ISBN 0-393-96945-2
Want Software? Algorithms? Pubs? http://www.infoseek.com
^ permalink raw reply [flat|nested] 9+ messages in thread
* Fast matrix multiply and benchmark
@ 1998-12-18 5:00 pshimizu
1998-12-18 5:00 ` Dann Corbit
1998-12-21 5:00 ` Lennart Bengtsson
0 siblings, 2 replies; 9+ messages in thread
From: pshimizu @ 1998-12-18 5:00 UTC (permalink / raw)
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
You can call this subroutine from C or FORTRAN.
Only non-transposed routine is implemented.
This program is not alpha specific. You can compile and run
for many other architectures.
Some benchmarking results with my and my university's machines
are on the WEB:
http://shimizu-lab.et.u-tokai.ac.jp/pgm/gemm/bench.html
C:
void dgemms_nn(int m, int n, int k,
double alpha, double *a, int lda, double *b, int ldb,
double beta, double *c, int ldc);
*CATION* C user should know that the matrices is assumed to be stored
in the FORTRAN order(Row Major).
FORTRAN:
call dgemms_nn(m,n,k,alpha,a,lda,b,ldb,beta,c,ldc)
For benchmarking I placed main C routine. Use -DMAIN option to
compile for benchmaking.
Compile options:
-DMAIN : compile for benchmarking
-Dev6 : compile for 21264
-Dev4 : compile for 21064(A)
-DRPCC : use RPCC instruction to measure the time
The core routine of DGEMM is matrices multiply. There are known
way to use blocking algorithm for this problem. Usually they are
intended only to the cache optimization. But for a large problem it is
required to optimize the usage of translation buffer. Every matrix
element which is adjucent by one column will require an entry on the
translation buffer. Then buffer shortage will easily be occured on
most RISC processors. This program is to demonstrate the blocking
algorithm to reduce both the buffer miss and cache miss on a certain
processor.
Conventional method:
Now we are going to solve a problem : C = aAB + bC.
A=[A11 A12 A13 ... ; A21 A22 A23 ...; ... ; As1 As2 ...]
B=[B11 B12 B13 ... ; B21 B22 B23 ...; ... ; Bt1 Bt2 ...]
C=[C11 C12 C13 ... ; C21 C22 C23 ...; ... ; Cs1 Cs2 ...]
Cij = bCij + a*sum(Ail*Blj for l=1 to t)
More square the Aij will result in less memory load traffic. And
usually above algorithm is just implemented to reduce store traffic.
My method:
In fortran, the row direction is continuous on memory. Then if we
calculate Ail*Blj for i=1 to n, the translation miss will be reduced.
Of course this requires more temporaly result on Cij which induce to
occure more store traffic. For many implementation of RISC machines,
the second or third cache is organized as store-in cache. Then once
store is required it must first read a cache block from memoy. And the
store traffic is almost as heavy as the double of the same amount load
traffic. Since the store occurence of that computation completely
depends on the number of column of Ail, we can control the traffic.
The traffic by Blj can be almost ignored, because once Blj is loaded
into the cache, many of Ail*Blj can be executed with the cached data.
But the load traffic of Ail will be another major traffic for this
problem. And we should keep the portion of Ail in cache as large as
possible. But if the store traffic and the load traffic will both be
affordable for a specific machine, we have a chance to induce the
maximum performance.
To fit in the translation buffer entry, the program should use
blocking on Ail. The total number of the row times the sizeof double
in a block should be fit in the pagesize of the processor. And the
total number of the column of Ail, Blj, Cij should not be exceed the
number of translation buffer entries.
The number of column of the Ail is arbitrary. The more will decrease
the store traffic and increase the load traffic, the less will increase
the store traffic but decrease the load traffic. Then there is some
optimal point for a specific machine.
The Ail will cause load traffic from the number of column of the Ail on
every the number of column of Ail by the number of column of Blj
submatrix multiplication. The Cij will cause store traffic on the
every row of the Cij.
The parameter depends on the processor and memory system
configuration, and you can tune for your system.
But if the number of the translation buffer is small and load/store
throughput is also small, the result will be hopeless. You should
think to replace your machine.
If the Ail is aligned to the page size, it is easy to treat. But for
general we cannot expect so. I assume that the translation buffer
entries used by Ail will be doubled effectively. To be hold the
all required page in the translation buffer.
Of course this not ideal solution. You may want to align the A
to the page boundary and set LDA to the factor of the pagesize.
The algorithm will be following:
do ii=1 to k, ail_clmn_size
do jj=1 to m, ail_maj_blk_size
do kk=1 to n, blj_clmn_size
do ll=jj to jj+ail_maj_blk_size
ctmp_ll_kk += A_ll_ii*B_ii_kk
enddo
enddo
enddo
enddo
For AlphaPC164LX parameters:
DTB 64 entries
Page size 8KB
load throughput almost 700MB/S
store throughput almost 400MB/S
Then I think the following number is affordable.
1. The number of column of Ail is 20
2. The number of column of Blj,Cij is 12
3. The blocking factor of the Ail row is 512 (just fit into the half page)
You may have a good result for 21264 with the parameter of:
1. The number of column of Ail is 40
2. The number of column of Blj,Cij is 24
3. The blocking factor of the Ail row is 512 (just fit into the half page)
History:
98.12.18: Revised boundary condition of the unrollings again.
Matrix B prefetch is designed not to hurt the load performance.
98.12.17(2): Revised boundary condition of the unrollings.
98.12.17: Revised not to use integer multiply for pointer traverse.
Force to set Ail_max to the factor of 8 to reduce the followup loop.
If you still need -DNOALGN may or may not help you.
98.12.16: Revised the handling of alpha and beta parameters.
98.12.15: Revised to do more execution in the inner loop.
First version available :98.12.12
ToDo:
1: Inner block may need to be coded by inline assembler for higher
performance.
2: Processor/System specific Ail_max, Blj_max should be developped to
extract best performance.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Fast matrix multiply and benchmark
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
1998-12-21 5:00 ` Bernd Paysan
1998-12-26 5:00 ` pshimizu
1 sibling, 2 replies; 9+ messages in thread
From: Lennart Bengtsson @ 1998-12-21 5:00 UTC (permalink / raw)
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)
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Fast matrix multiply and benchmark
1998-12-21 5:00 ` Lennart Bengtsson
@ 1998-12-21 5:00 ` Bernd Paysan
1998-12-26 5:00 ` pshimizu
1 sibling, 0 replies; 9+ messages in thread
From: Bernd Paysan @ 1998-12-21 5:00 UTC (permalink / raw)
In article <367D9D1E.32A9@fy.no.chalmers.spam.se>,
Lennart Bengtsson <tfylb@fy.no.chalmers.spam.se> wrote:
> 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)
I thought blocking of matrix products is pretty standard and straight-forward.
You can get quite close to peak performance even with low memory bandwidth. I
got to more than 30 MFlops on a 50 MHz PA-RISC (50 MFlops peak).
I organized the dot product so that I compute a 5x5 field in the inner loop.
This fits nicely into a typical 31-element register set: five temporaries for
row, one for column (or vice versa, the single temporary at best is the one
without unit stride), and 25 destination registers. You then have 25 macs for
10 cache loads.
The outer loop then blocks the matrix into sub-fields that fit into the L1
cache or the limit of TLB mapable part, whatever is smaller. The machine I
used then had no L2 cache, but the next outer block would have either half
the size of the TLB area or the L2 cache, whatever is smaller or the L2 area
if the TLB area already is covered by the L1 loop. The next outer block
should trade between RAM and swap space, if necessary.
A 128 element TLB can keep at ease one 100x100 matrix, and the two 5x100
stripes for second source and result. You have to compute one million macs
for 240k memory throughput.
--
Bernd Paysan
"Late answers are wrong answers!"
http://www.jwdt.com/~paysan/
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Fast matrix multiply and benchmark
1998-12-21 5:00 ` Lennart Bengtsson
1998-12-21 5:00 ` Bernd Paysan
@ 1998-12-26 5:00 ` pshimizu
1998-12-28 5:00 ` Andy Glew
1998-12-28 5:00 ` Andy Glew
1 sibling, 2 replies; 9+ messages in thread
From: pshimizu @ 1998-12-26 5:00 UTC (permalink / raw)
Hi,
I know the copying trick, but I think the shortage of TLB entiries
is caused by the poor architect who only reffers to the SPECmark.
Not only this program, but many real world programs will suffer
from the shortage. And then I intended not to use the copying trick.
I will post a benchmark program which will test the system's TLB
missed load performance in a separate article.
I hope many architects will decide to have more TLB entries in thier
chip.
From my experience to design some mainframes, it is not so difficult
to have a large set of TLB entiries. If one use the set associative
method.
Thank,
Naohiko Shimizu.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Fast matrix multiply and benchmark
1998-12-26 5:00 ` pshimizu
@ 1998-12-28 5:00 ` Andy Glew
1998-12-29 5:00 ` pshimizu
1998-12-28 5:00 ` Andy Glew
1 sibling, 1 reply; 9+ messages in thread
From: Andy Glew @ 1998-12-28 5:00 UTC (permalink / raw)
> From my experience to design some mainframes, it is not so difficult
> to have a large set of TLB entiries. If one use the set associative
> method.
Amen.
Although it should also be noted that, for matrix multiply,
the TLB misses can be done out-of-order, and could be made
to overlap with each other.
The high cost of TLB misses is in large part due to the "stop the world"
implementation. P6 allowed TLB misses to proceed without blocking
independent instructions. Pipelining TLB misses is an obvious next step.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Fast matrix multiply and benchmark
1998-12-26 5:00 ` pshimizu
1998-12-28 5:00 ` Andy Glew
@ 1998-12-28 5:00 ` Andy Glew
1 sibling, 0 replies; 9+ messages in thread
From: Andy Glew @ 1998-12-28 5:00 UTC (permalink / raw)
To: nshimizu, Andy Glew
In a post I just sent off I mentioned the possibility of
non-blocking and pipelined TLB miss handling.
Such handling could be done by a suitable hardware
state machine page table walker - or by a separate thread.
It may be worth emphasizing that non-blocking and pipelined
TLB miss handling may be most useful hiding the latency
of L1 TLB misses that hit in an L2 TLB --- thus avoiding
the extremely large latencies that might be associated with
walking page tables in memory.
And, by the way... I hope nobody throws up the "TLB misses
must use uncached memory accesses" red herring. That has been
proven wrong long back.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Fast matrix multiply and benchmark
1998-12-29 5:00 ` pshimizu
@ 1998-12-29 5:00 ` Andy Glew
0 siblings, 0 replies; 9+ messages in thread
From: Andy Glew @ 1998-12-29 5:00 UTC (permalink / raw)
>I agree. Nowadays almost all processor implement out of order
>execution, and it must be easy to make the LOAD as speculative.
>Then we can use non-blocking and/or pipelining to access TLB misses.
>Of course without a large L2 TLB it would be useless.
Well, it did prove somewhat useful even without an L2 TLB...
i.e. where a TLB miss actually walks page tables in memory.
I just mentioned the possibility of a true L2 TLB, because it might
reduce the latencies to a more hideable level.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Fast matrix multiply and benchmark
1998-12-28 5:00 ` Andy Glew
@ 1998-12-29 5:00 ` pshimizu
1998-12-29 5:00 ` Andy Glew
0 siblings, 1 reply; 9+ messages in thread
From: pshimizu @ 1998-12-29 5:00 UTC (permalink / raw)
In article <3688385E.C72A980B@cs.wisc.edu>
glew@cs.wisc.edu writes:
>>
>> Although it should also be noted that, for matrix multiply,
>> the TLB misses can be done out-of-order, and could be made
>> to overlap with each other.
>>
>> The high cost of TLB misses is in large part due to the "stop the world"
>> implementation. P6 allowed TLB misses to proceed without blocking
>> independent instructions. Pipelining TLB misses is an obvious next step.
>>
I agree. Nowadays almost all processor implement out of order
execution, and it must be easy to make the LOAD as speculative.
Then we can use non-blocking and/or pipelining to access TLB misses.
Of course without a large L2 TLB it would be useless.
I am thinking to implement L2 TLB for Linux/Alpha palcode, but
many alpha implementation does not allow non-blocking TLB misses
and it will help little for large problems.
Naohiko Shimizu,
Dept. Communication Engr. Tokai University
Email: nshimizu@et.u-tokai.ac.jp
http://shimizu-lab.et.u-tokai.ac.jp/
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~1998-12-29 5:00 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox