* Optimal use of the L2 cache @ 2001-06-15 20:48 Tobias Oed 2001-06-15 22:22 ` Hoff Hoffman 0 siblings, 1 reply; 6+ messages in thread From: Tobias Oed @ 2001-06-15 20:48 UTC (permalink / raw) Hi all, after reading some of the posts here i'm affraid this is not the best newsgroup to ask this question, but for lack of anything better let me try here. If you know of a better place, please let me know. I have a piece of code that does a large amount of floating point computations. In order to get it to run faster, I build a lookup table of ~4000 floating point values. This brought some improovment but it's still very slow. I access the elements of the table in a more or less random fashion and am affraid that I get a terrible cache performance. After scanning thourgh the assembly (my last assembly dates back to the M68k), it apears that the rest of the computation is done entierly in registers, with just another array of 96 doubles in memory. If I am not mistaken, my EV67 has 64k of L2 data cache and everything should fit in there if it were not for aliasing. How could I convince the processor to keep my arrays in that cache. Would adding dummy values in the middle of the array help? How big should the chunks of used values be and how many dummy ones should I insert between them? Thanks for your input, Tobias. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Optimal use of the L2 cache 2001-06-15 20:48 Optimal use of the L2 cache Tobias Oed @ 2001-06-15 22:22 ` Hoff Hoffman 2001-06-15 23:23 ` Tobias Oed 0 siblings, 1 reply; 6+ messages in thread From: Hoff Hoffman @ 2001-06-15 22:22 UTC (permalink / raw) In article <3B2A74B6.5F18718A@physics.odu.edu>, Tobias Oed <tobias@physics.odu.edu> writes: Operating system, version, and platform? Compiler and version? Compiler command(s) used? Example of the code involved? :I have a piece of code that does a large amount of :floating point computations. In order to get it to :run faster, I build a lookup table of ~4000 floating :point values... You will want the floating point values naturally aligned. :I access the elements of the table in a more or less random fashion :and am affraid that I get a terrible cache performance. Yes, you could be thrashing the cache. Other considerations include the floating point format and the data alignment... :If I am not mistaken, my EV67 has 64k of L2 data cache... Which system do you have? :How could I convince the processor to keep my arrays in that cache. I'd normally expect it to happen, unless something else is tossing the pages out of cache -- eg: a lack of locality, excessive paging. :Would adding dummy values in the middle of the array help? That would not be my first choice. :How big should the chunks of used values be and :how many dummy ones should I insert between them? You say you access the data randomly, and your comments imply some sort of hashing... ---------------------------- #include <rtfaq.h> ----------------------------- For additional, please see the OpenVMS FAQ -- www.openvms.compaq.com --------------------------- pure personal opinion --------------------------- Hoff (Stephen) Hoffman OpenVMS Engineering hoffman#xdelta.zko.dec.com ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Optimal use of the L2 cache 2001-06-15 22:22 ` Hoff Hoffman @ 2001-06-15 23:23 ` Tobias Oed 2001-06-16 1:23 ` Hoff Hoffman 2001-06-16 17:42 ` Paul Hughett 0 siblings, 2 replies; 6+ messages in thread From: Tobias Oed @ 2001-06-15 23:23 UTC (permalink / raw) Hoff Hoffman wrote: > > In article <3B2A74B6.5F18718A@physics.odu.edu>, Tobias Oed <tobias@physics.odu.edu> writes: > > Operating system, version, and platform? Linux version 2.4.3-pre1 (root@gamow) (gcc version 2.95.2 19991024 (release)) #1 SMP Sun Mar 4 18:12:44 EST 2001 cpu : Alpha cpu model : EV67 cpu variation : 7 cpu revision : 0 cpu serial number : system type : Tsunami system variation : DP264 system revision : 0 system serial number : cycle frequency [Hz] : 666666666 timer frequency [Hz] : 1024.00 page size [bytes] : 8192 phys. address bits : 44 max. addr. space # : 255 BogoMIPS : 1330.04 kernel unaligned acc : 558589956 (pc=fffffc00004cab50,va=fffffc000056ec49) user unaligned acc : 63590241 (pc=2000000eb80,va=2000027f2cc) platform string : AlphaPC 264DP 666 MHz cpus detected : 2 cpus active : 2 cpu active mask : 0000000000000003 I have two processors and use both of them to do my computation using a message passing mechanism. This is of no concern here since what I'm talking about is running on a single processor. The message passing is in a much lower level of looping. > Compiler and version? Compaq C V6.2-504 on Linux 2.4.3-pre1 alpha Compiler Driver V6.2-504 (Linux) cc Driver Installed as ccc-6.2.9.504-2 SuSE Linux 7.0 (Alpha) VERSION = 7.0 Using /usr/lib/gcc-lib/alpha-suse-linux/2.95.2 (2). > Compiler command(s) used? CFLAGS=-fast -unroll 16 -std1 -msg_enable level4 > Example of the code involved? quick_table is my lookup table, iso_ff is the array in which I sum the result. There are six functions of the form: void add_born(double mN,/* as well as mPi,ks,kv,s,sp,sp,tp,tm */ double quick_table[4000], double iso_ff[16][6][2] ){ double redeno,imdeno,deno,recontri,imcontri; double iso_coefs[6],contri[16]; redeno=(0); imdeno=(-quick_table[30]); deno=(quick_table[867])/(redeno*redeno+imdeno*imdeno); redeno*=deno; imdeno*=deno; iso_coefs[0]=(quick_table[20]); iso_coefs[1]=(-ks); iso_coefs[2]=(0); iso_coefs[3]=(0); iso_coefs[4]=(quick_table[18]); iso_coefs[5]=(-kv); contri[0]=(quick_table[917]*quick_table[1826]); contri[1]=(-(quick_table[16]*quick_table[1663])); contri[2]=(0); contri[3]=(0); contri[4]=(0); contri[5]=(0); contri[6]=(0); contri[7]=(0); contri[8]=(tm*quick_table[1826]); contri[9]=(quick_table[14]*quick_table[1746]); contri[10]=(0); contri[11]=(quick_table[1826]); contri[12]=(mN*quick_table[4281]); contri[13]=(quick_table[4076]/2.); contri[14]=(-(quick_table[1449]*quick_table[1826])); contri[15]=(-(mN*quick_table[1663])); for(i=0;i<N_FORMFACTORS;i++){ recontri=redeno*contri[i]; imcontri=imdeno*contri[i]; for(j=0;j<N_ISOSPIN;j++){ iso_ff[i][j][RE]+=recontri*iso_coefs[j]; iso_ff[i][j][IM]+=imcontri*iso_coefs[j]; } } (This loop gets fully unrolled). This is repeated ~300 times with differnt assignement to redeno, imdeno, the iso_coeof and the contri and the same loop. > > :I have a piece of code that does a large amount of > :floating point computations. In order to get it to > :run faster, I build a lookup table of ~4000 floating > :point values... > > You will want the floating point values naturally aligned. I belive I can trust the compaq compiler to ensure that for me, can't I? > :I access the elements of the table in a more or less random fashion > :and am affraid that I get a terrible cache performance. > > Yes, you could be thrashing the cache. Other considerations > include the floating point format and the data alignment... Assuming that the data alignment is ok, how could I change the floating point format? Would using float instead of double get some speed? Also, would VAX instead of IEEE floating point representation make a difference? > :If I am not mistaken, my EV67 has 64k of L2 data cache... > > Which system do you have? > > :How could I convince the processor to keep my arrays in that cache. > > I'd normally expect it to happen, unless something else is tossing > the pages out of cache -- eg: a lack of locality, excessive paging. The problem is that my lookup table doesn't fit in a single page. 4000*8=32k a page is 8k. (correct??) > :Would adding dummy values in the middle of the array help? > > That would not be my first choice. > > :How big should the chunks of used values be and > :how many dummy ones should I insert between them? > > You say you access the data randomly, and your comments imply > some sort of hashing... I generated the code using mathematica. I also used mathematica to find the elements in my lookup table. That's how I know which element to use. I hope this clarifies my problem a bit... cheers, Tobias. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Optimal use of the L2 cache 2001-06-15 23:23 ` Tobias Oed @ 2001-06-16 1:23 ` Hoff Hoffman 2001-06-16 17:42 ` Paul Hughett 1 sibling, 0 replies; 6+ messages in thread From: Hoff Hoffman @ 2001-06-16 1:23 UTC (permalink / raw) In article <3B2A98FB.9C766616@physics.odu.edu>, Tobias Oed <tobias@physics.odu.edu> writes: Yes, the current generation of Alpha systems have an eight kilobyte page size. Your table will fit onto a handful of hardware pages, but I didn't specifically check the math -- one obvious question involves everything else that is competing for the cache... Can you pass a pointer to the array and not the array? Try compiling -- and I don't know the Linux switches -- with an EV6 (or EV56) as the architecture target, and with either generic or EV6/EV67 as the optimization target. I don't know the details of the argument passing on Linux, but on OpenVMS you will want to keep to six arguments or less for heavily-called functions. More than six arguments means the stack and the registers get used. I'd tend to use IEEE native floating point format, S or T. Compaq C V6.4A is the current version, donno if that's on Linux. V6.2 does have ability to generate byte-word and similar, but there have been a number of changes since that version. With the versions of Compaq C on OpenVMS, the compiler will keep the data naturally aligned unless requested -- either explicitly or implicitly -- otherwise. I would check the generated code to ensure that the compiler has spoted and has re-ordered some of the quick_table memory accesses to try to reduce the potential for thrashing, and I'd look to load in some of the references (eg: [1826] looks popular) explicitly if the compiler is not spotting this. ---------------------------- #include <rtfaq.h> ----------------------------- For additional, please see the OpenVMS FAQ -- www.openvms.compaq.com --------------------------- pure personal opinion --------------------------- Hoff (Stephen) Hoffman OpenVMS Engineering hoffman#xdelta.zko.dec.com ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Optimal use of the L2 cache 2001-06-15 23:23 ` Tobias Oed 2001-06-16 1:23 ` Hoff Hoffman @ 2001-06-16 17:42 ` Paul Hughett 2001-06-16 20:50 ` Tobias Oed 1 sibling, 1 reply; 6+ messages in thread From: Paul Hughett @ 2001-06-16 17:42 UTC (permalink / raw) Tobias Oed <tobias@physics.odu.edu> wrote: : for(i=0;i<N_FORMFACTORS;i++){ : recontri=redeno*contri[i]; : imcontri=imdeno*contri[i]; : for(j=0;j<N_ISOSPIN;j++){ : iso_ff[i][j][RE]+=recontri*iso_coefs[j]; : iso_ff[i][j][IM]+=imcontri*iso_coefs[j]; : } : } Not related to your cache question, but I see a few other improvements that you could make: (1) About half of your contri[i] values are set to zero just above this code. Add a test to avoid doing the inner loop at all when this is true. (2) Similarly, many of iso_coefs[j] values are zero. Unroll the N_ISOSPIN loop by hand and omit those calculations. (3) Look at the assembly code to see if your compiler optimizes iso_ff[i][j][IM] into something reasonable. If not, convert iso_ff into a single-dimensional array and do the address calculations by hand. Paul Hughett ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Optimal use of the L2 cache 2001-06-16 17:42 ` Paul Hughett @ 2001-06-16 20:50 ` Tobias Oed 0 siblings, 0 replies; 6+ messages in thread From: Tobias Oed @ 2001-06-16 20:50 UTC (permalink / raw) Paul Hughett wrote: > > Tobias Oed <tobias@physics.odu.edu> wrote: > > : for(i=0;i<N_FORMFACTORS;i++){ > : recontri=redeno*contri[i]; > : imcontri=imdeno*contri[i]; > : for(j=0;j<N_ISOSPIN;j++){ > : iso_ff[i][j][RE]+=recontri*iso_coefs[j]; > : iso_ff[i][j][IM]+=imcontri*iso_coefs[j]; > : } > : } > > Not related to your cache question, but I see a few other improvements > that you could make: > > (1) About half of your contri[i] values are set to zero just above this > code. Add a test to avoid doing the inner loop at all when this is true. There are 9 non zero ones. > (2) Similarly, many of iso_coefs[j] values are zero. Unroll the > N_ISOSPIN loop by hand and omit those calculations. There are 4 non zero ones. Since redeno is zero as well, out of the 16*6*2=192 iso_ff only 9*4=36 will have a contribution. The assembly shows exactly 36 stt $fxx,yy($18) (as well as 36 ldt $fxx,yy($18). So I think that the compiler was smart enough to do all that for me. > (3) Look at the assembly code to see if your compiler optimizes > iso_ff[i][j][IM] into something reasonable. If not, convert iso_ff into > a single-dimensional array and do the address calculations by hand. > > Paul Hughett Thanks for the idea, Tobias. ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2001-06-16 20:50 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2001-06-15 20:48 Optimal use of the L2 cache Tobias Oed 2001-06-15 22:22 ` Hoff Hoffman 2001-06-15 23:23 ` Tobias Oed 2001-06-16 1:23 ` Hoff Hoffman 2001-06-16 17:42 ` Paul Hughett 2001-06-16 20:50 ` Tobias Oed
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox