* Cache, performance, and data structures used by simulations
@ 1995-03-01 2:19 Peter Mardahl
1995-03-01 23:30 ` Vivek Sadananda Pai
1995-03-02 18:03 ` John L. Henning
0 siblings, 2 replies; 8+ messages in thread
From: Peter Mardahl @ 1995-03-01 2:19 UTC (permalink / raw)
This is really a question.
We have been benchmarking a series of codes on different architectures.
The two of most interest to us are AlphaStation 200 4/233 and
HP 712/80. Both have 256k cache. I do not know the memory bandwidth
of the HP, the AlphaStation's is 132MB/s. No swapping took place.
We benchmarked two different codes on these systems, both scientific
simulation programs.
One of them gave a performance on each machine consistent with expectations
based on SPECfp. (i.e., The alphastation was as much faster as we'd expect
from SPECfp than the hp)
The other code performed very badly on the Alphastation, it ran slightly
SLOWER than the HP. (We expect the Alphastation to be 60% faster)
The two codes perform nearly identical calculations, however they use
different datastructures:
One uses a structure like this for a particle:
typedef struct {
double x,y;
double v1,v2,v3;
} particle;
particle *particles; /* array has dimension #particles */
The other one stores this information like this:
double *x, *y, *v1, *v2, *v3; /* each array has dimension #particles */
Each code updates each particle in sequence, and accesses all the fields.
We can't benchmark the two codes against each other, apples and oranges....
But the structure-based code gets just exactly the performance you'd expect
from SPECfp while the array-based code does not.
Our theory to explain this:
The HP has a much better memory bandwidth/CPU speed ratio than the DEC
The DEC can more efficiently cache the structure-based code than it can
the array-based code, keeping the cpu busier, so it performs as expected,
but
the dec can't cache the array-based code as well, so it performs badly
compared to the HP with its better memory-bandwidth.
What do you all think? Are we right? Do you have a better theory?
It's important that we know, because we may switch the array-based
code over to structures if we can get a 30-60% speedup out of it.
Please Cc: me any followups.
Regards,
Peter Mardahl
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Cache, performance, and data structures used by simulations
1995-03-01 2:19 Cache, performance, and data structures used by simulations Peter Mardahl
@ 1995-03-01 23:30 ` Vivek Sadananda Pai
1995-03-02 23:05 ` Nick Maclaren
1995-03-02 18:03 ` John L. Henning
1 sibling, 1 reply; 8+ messages in thread
From: Vivek Sadananda Pai @ 1995-03-01 23:30 UTC (permalink / raw)
In article <3j0ljg$in7@agate.berkeley.edu>, peterm@tonks.EECS.Berkeley.EDU (Peter Mardahl) writes:
|>
|> This is really a question.
|>
|> We have been benchmarking a series of codes on different architectures.
|> The two of most interest to us are AlphaStation 200 4/233 and
|> HP 712/80. Both have 256k cache. I do not know the memory bandwidth
|> of the HP, the AlphaStation's is 132MB/s. No swapping took place.
[...]
My theory:
The Alpha uses 8k direct-mapped first level caches to do its
work. I don't know what the HP uses.
In the case of the structure-based code, everything you want
for a given element is pulled in at the same time. In the
case of the array-based code, bad things can happen. For
example, assume that all of your arrays start on page boundaries.
In this case, if you want to access the same element location
from all the arrays, they'll map to the same line of the first
level cache, potentially causing lots and lots of cache
conflicts.
In this case, the larger (and more highly associative)
second-level cache gets used to supply the data. So, it's
probably not an issue of memory bandwidth, but secondary-to-primary
cache bandwidth. It might also involve the main memory bandwidth,
but without a lot more information, I can't be sure (not that
I could, even with a lot more information).
|> What do you all think? Are we right? Do you have a better theory?
|> It's important that we know, because we may switch the array-based
|> code over to structures if we can get a 30-60% speedup out of it.
If your code uses the same element location from many arrays at
the same time (or close to each other), you're probably better off
switching to structures. The exact speedup you'll see depends on
a number of factors including #cache conflicts, memory bandwidth,
reuse, etc.
|> Please Cc: me any followups.
I tried, but something seems to be poorly configured at your
end. I got the following mailer-daemon error:
554 MX list for tonks.eecs.berkeley.edu. points back to langmuir.EECS.Berkeley.EDU
554 <peterm@tonks.eecs.berkeley.edu>... Local configuration error
Hope this helps,
-Vivek
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Cache, performance, and data structures used by simulations
1995-03-01 2:19 Cache, performance, and data structures used by simulations Peter Mardahl
1995-03-01 23:30 ` Vivek Sadananda Pai
@ 1995-03-02 18:03 ` John L. Henning
1995-03-03 16:52 ` Greg Lindahl
1 sibling, 1 reply; 8+ messages in thread
From: John L. Henning @ 1995-03-02 18:03 UTC (permalink / raw)
In article <3j0ljg$in7@agate.berkeley.edu>, peterm@tonks.EECS.Berkeley.EDU
(Peter Mardahl) writes that performance is consistent with expectations (when
comparing an AlphaStation 200 4/233 to an HP 712/80) if he uses a structure,
but not with an array. The structure was:
>typedef struct {
> double x,y;
> double v1,v2,v3;
>} particle;
>
>particle *particles; /* array has dimension #particles */
And the array was:
>double *x, *y, *v1, *v2, *v3; /* each array has dimension #particles */
>
Mr. Mardahl hypothesized cache effects and/or memory bandwidth as sources of
the difference, and asked for comments.
First, a guess about the memory bandwidth. I suspect that it is not the
primary issue in this case, for two reasons: 1) I don't think HP has claimed
that memory bandwidth is a strong suit for the 712/80, but if I'm wrong please
correct me. 2) More main memory bandwidth can certainly help some benchmarks,
but it often seems like the bigger benefit comes from understanding cache
effects and dealing with them. That is, the bigger bang-for-the-buck often
comes from making the best USE of whatever bandwidth you have.
Therefore, when you mention structures versus arrays I am reminded that I
recently heard this very recommendation from a compiler developer who
specializes in Alpha performance. His point was that almost all cache-based
systems (not just Alpha) will tend to benefit from structures instead of
arrays because you are more likely to use a full cache line when it is brought
in.
Recall that caches typically bring in more than just the item you have
requested; for example, if the cache linesize is 32 bytes and you are
processing 8 byte items, you get 4 items loaded every time a cache line is
refilled. So if your program code is likely to use the surrounding items, you
make good use of your memory bandwidth; and if it is likely to ignore them
then you've wasted some bandwidth. Structures can be very helpful.
The other point about multiple arrays is that they can cause cache thrashing.
RISC systems have many fewer cache locations than main memory locations, and
use various strategies to map main memory locations to cache locations (with
varying speeds and expenses). Performance will suffer if you touch multiple
arrays simultaneously, and the locations of interest in different arrays (for
a given iteration) map to the same location in a cache.
You may have stumbled across an array size that fits better in one system's
cache than in another system's cache, or that maps better in one than in
another. To find out exactly what is happening would require specific,
detailed investigation.
But the point about structures tending to be more cache-friendly than arrays
is, I believe, generally applicable and a Good Performance Practice for
RISC-based systems.
/John Henning
CSD Performance Group
Digital Equipment Corporation
henning@i4get.enet.dec.com
Speaking for myself, not Digital
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Cache, performance, and data structures used by simulations
1995-03-01 23:30 ` Vivek Sadananda Pai
@ 1995-03-02 23:05 ` Nick Maclaren
1995-03-04 19:01 ` John L. Henning
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Nick Maclaren @ 1995-03-02 23:05 UTC (permalink / raw)
In article <3j301q$lue@larry.rice.edu>,
Vivek Sadananda Pai <vivek@noel.cs.rice.edu> wrote:
>In article <3j0ljg$in7@agate.berkeley.edu>, peterm@tonks.EECS.Berkeley.EDU (Peter Mardahl) writes:
>
>|> What do you all think? Are we right? Do you have a better theory?
>|> It's important that we know, because we may switch the array-based
>|> code over to structures if we can get a 30-60% speedup out of it.
>
>If your code uses the same element location from many arrays at
>the same time (or close to each other), you're probably better off
>switching to structures. The exact speedup you'll see depends on
>a number of factors including #cache conflicts, memory bandwidth,
>reuse, etc.
Um, yes, but there is another solution: make each array slightly
bigger than it needs to be, so that each starts on a different
cache slot. This is done automatically (!) by some run-time systems,
where they keep chaining information on allocated blocks, and means
that it is the very devil to compare cache-dependent programs on
different systems.
I don't know any system that uses hashing to access cache slots,
but it was certainly proposed some decades ago. Nowadays it would
be quite cheap to do, and it would really confuse such benchmarking!
Nick Maclaren,
University of Cambridge Computer Laboratory,
New Museums Site, Pembroke Street, Cambridge CB2 3QG, England.
Email: nmm1@cam.ac.uk
Tel.: +44 1223 334761 Fax: +44 1223 334679
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Cache, performance, and data structures used by simulations
1995-03-02 18:03 ` John L. Henning
@ 1995-03-03 16:52 ` Greg Lindahl
0 siblings, 0 replies; 8+ messages in thread
From: Greg Lindahl @ 1995-03-03 16:52 UTC (permalink / raw)
In article <3j54js$79i@peavax.eng.pko.dec.com>,
John L. Henning <henning@i4get.enet.dec.com> wrote:
>Therefore, when you mention structures versus arrays I am reminded that I
>recently heard this very recommendation from a compiler developer who
>specializes in Alpha performance. His point was that almost all cache-based
>systems (not just Alpha) will tend to benefit from structures instead of
>arrays because you are more likely to use a full cache line when it is brought
>in.
As usual, any generalization of this sort is suspect. IF your program
happens to use all the information in the structure at once, then
locality could be improved relative to several arrays. IF your program
instead uses one element from each structure in an array of
structures, locality could be worse than having several arrays.
Instead of making up rules of thumb that can be wrong, it's better to
teach programmers about the real issues.
>The other point about multiple arrays is that they can cause cache thrashing.
>RISC systems have many fewer cache locations than main memory
>locations,
The same is true about non-RISC systems as well. Smart programmers
avoid power-of-two array sizes; smart compilers and linkers can do
much of the work as well, although putting arrays in malloc()ed
storage often leads to problems because malloc() likes to align things
on pages.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Cache, performance, and data structures used by simulations
1995-03-02 23:05 ` Nick Maclaren
@ 1995-03-04 19:01 ` John L. Henning
1995-03-06 20:08 ` Vivek Sadananda Pai
1995-03-13 18:10 ` Paul Repacholi
2 siblings, 0 replies; 8+ messages in thread
From: John L. Henning @ 1995-03-04 19:01 UTC (permalink / raw)
In article <3j5j0b$b8n@lyra.csx.cam.ac.uk>, nmm1@cus.cam.ac.uk (Nick Maclaren)
writes...
>Um, yes, but there is another solution: make each array slightly
>bigger than it needs to be, so that each starts on a different
>cache slot.
This is a good solution when you have reason to believe that you are
stomping on the same cache lines with two or more different data
structures. But there doesn't seem to be any particular evidence of
that in this case. (I finally got through to the original poster Peter
Mardahl by e-mail, and asked about the size of the arrays.)
I still think the structures are the right way to go. But it was
suggested that I ought to clarify that structures are particularly
likely to be helpful when you are doing *random* accesses. For
sequential accesses, they are much less likely to make any difference.
/John Henning
CSD Performance Group
Digital Equipment Corporation
henning@i4get.enet.dec.com
Speaking for myself, not Digital
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Cache, performance, and data structures used by simulations
1995-03-02 23:05 ` Nick Maclaren
1995-03-04 19:01 ` John L. Henning
@ 1995-03-06 20:08 ` Vivek Sadananda Pai
1995-03-13 18:10 ` Paul Repacholi
2 siblings, 0 replies; 8+ messages in thread
From: Vivek Sadananda Pai @ 1995-03-06 20:08 UTC (permalink / raw)
In article <3j5j0b$b8n@lyra.csx.cam.ac.uk>, nmm1@cus.cam.ac.uk (Nick Maclaren) writes:
|> In article <3j301q$lue@larry.rice.edu>,
|> Vivek Sadananda Pai <vivek@noel.cs.rice.edu> wrote:
[ in response to a question about a performance hit on an Alpha ]
|> >If your code uses the same element location from many arrays at
|> >the same time (or close to each other), you're probably better off
|> >switching to structures. The exact speedup you'll see depends on
|> >a number of factors including #cache conflicts, memory bandwidth,
|> >reuse, etc.
|>
|> Um, yes, but there is another solution: make each array slightly
|> bigger than it needs to be, so that each starts on a different
|> cache slot. This is done automatically (!) by some run-time systems,
|> where they keep chaining information on allocated blocks, and means
|> that it is the very devil to compare cache-dependent programs on
|> different systems.
My apologies - I should have been clearer in that last paragraph. I
was assuming that the page-alignment was not an issue that could be
controlled. Yes, you're correct that if you have control over where
the arrays can be placed, you can get around this issue. However, in
the areas that I'm working on, almost everything gets page-aligned,
so I was unnecessarily confining my response.
As another poster pointed out, many malloc() implementations will
page-align memory requests, especially those for large blocks (such
as arrays). If you face this situation, and you don't want to re-write
your code, you could essentially do a sleazy hack as follows:
instead of
array = (element *) malloc(bigsize)
do
arrayTemp = (element *) malloc(bigsize+OFFSET*sizeof(element))
array = &arrayTemp[OFFSET];
If you do this for all of your (conflicting) arrays, and vary OFFSET
for each one, you can work it such that element X of each array falls
into a different cache line. Of course, you need to do this for all
of your arrays that conflict, but this may be a more palatable choice
than doing a large re-write.
-Vivek
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Cache, performance, and data structures used by simulations
1995-03-02 23:05 ` Nick Maclaren
1995-03-04 19:01 ` John L. Henning
1995-03-06 20:08 ` Vivek Sadananda Pai
@ 1995-03-13 18:10 ` Paul Repacholi
2 siblings, 0 replies; 8+ messages in thread
From: Paul Repacholi @ 1995-03-13 18:10 UTC (permalink / raw)
In article <3j5j0b$b8n@lyra.csx.cam.ac.uk> nmm1@cus.cam.ac.uk (Nick Maclaren) writes:
> I don't know any system that uses hashing to access cache slots,
> but it was certainly proposed some decades ago. Nowadays it would
> be quite cheap to do, and it would really confuse such benchmarking!
The KL-10s did this. Also some of the more interesting S/360/370
cpus.
Also, carefull use of apdding at the beginning of the array can
do wonders if you are doing A[i]+B[i]->C[i] type things.
--
~Paul
prep@yarrow.wt.com.au +61 (09) 257-1001
prep@yarrow.wt.uwa.edu.au ( old ) 1 Crescent Rd,
Kalamunda,
West Aust 6076
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~1995-03-13 18:10 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1995-03-01 2:19 Cache, performance, and data structures used by simulations Peter Mardahl
1995-03-01 23:30 ` Vivek Sadananda Pai
1995-03-02 23:05 ` Nick Maclaren
1995-03-04 19:01 ` John L. Henning
1995-03-06 20:08 ` Vivek Sadananda Pai
1995-03-13 18:10 ` Paul Repacholi
1995-03-02 18:03 ` John L. Henning
1995-03-03 16:52 ` Greg Lindahl
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox