* fast array calculation in C
@ 1999-10-07 4:00 Alex Rhomberg
1999-10-09 4:00 ` Greg Lindahl
0 siblings, 1 reply; 18+ messages in thread
From: Alex Rhomberg @ 1999-10-07 4:00 UTC (permalink / raw)
Hi
I'm trying to do fast arrays/matrixes/vectors in C
Not easy
I've found that looking at the produces assembler is important.
In my case, the Compaq ccc compiler produces nearly three times faster
code than gcc. It gets dependencies much better.
I've found that the biggest difference in code efficience comes if the
compiler thinks that lvalue and rvalue accesses could be the same
address or not. By feeding the produced assembler into a short Perl
script, I was able to identify ugly code, optimize it and shave 30% of
the execution time of a 700 line function by optimizing 4 lines.
I'll post the perl-script for those who are interested.
It finds the lines where the target of a ldt/addt/mult/subt function
appears in the following line, which means pipeline stalls:
- Alex
**************Listing of find_stalls ***************
#!/usr/bin/perl -wlp
# usage:
#objdump -d myobject.o | find_stalls|more
#then search for UGLY!
use strict;
use vars qw/$target/;
BEGIN {$target = "alotta garbaga"}
chomp;
$_ .= "\t\t#UGLY!!" if /\Q$target,/;
$target = "allotta garbaga";
$target = $1 if /ldt\s+(\$f\d+),/; #target is first arg of ldt
$target = $1 if /(?:mult|subt|addt)\s+.*(\$f\d+)/;
#target is last arg of mlt, subt and add
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: fast array calculation in C
1999-10-07 4:00 fast array calculation in C Alex Rhomberg
@ 1999-10-09 4:00 ` Greg Lindahl
1999-10-11 4:00 ` Alex Rhomberg
0 siblings, 1 reply; 18+ messages in thread
From: Greg Lindahl @ 1999-10-09 4:00 UTC (permalink / raw)
Alex Rhomberg <rhomberg@ife.ee.ethz.ch> writes:
> I've found that looking at the produces assembler is important.
> In my case, the Compaq ccc compiler produces nearly three times faster
> code than gcc. It gets dependencies much better.
OK.
> I've found that the biggest difference in code efficience comes if the
> compiler thinks that lvalue and rvalue accesses could be the same
> address or not.
This is usually known as the "aliasing problem" -- the compiler isn't
sure if a given pointer store is the same as a different pointer load,
so it doesn't re-use values in variables.
> By feeding the produced assembler into a short Perl
> script, I was able to identify ugly code, optimize it and shave 30% of
> the execution time of a 700 line function by optimizing 4 lines.
>
> I'll post the perl-script for those who are interested.
> It finds the lines where the target of a ldt/addt/mult/subt function
> appears in the following line, which means pipeline stalls:
.. but this is a different problem, an instruction scheduling
problem. What I am saying, mostly, is that I obviously don't
understand what you are saying.
In addition, is this savings in gcc or in ccc?
Are you using a 21264 or a 21164? The 21264 has enough out-of-order
execution stuff that the above problem doesn't always cause a stall.
If you are interested in this from a computer-science research side,
you can look at the Atlas and FFTW projects, which do a more
complicated version of what you have done.
-- g
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: fast array calculation in C
1999-10-09 4:00 ` Greg Lindahl
@ 1999-10-11 4:00 ` Alex Rhomberg
1999-10-11 4:00 ` Greg Lindahl
0 siblings, 1 reply; 18+ messages in thread
From: Alex Rhomberg @ 1999-10-11 4:00 UTC (permalink / raw)
Greg Lindahl wrote:
> > I've found that the biggest difference in code efficience comes if the
> > compiler thinks that lvalue and rvalue accesses could be the same
> > address or not.
>
> This is usually known as the "aliasing problem" -- the compiler isn't
> sure if a given pointer store is the same as a different pointer load,
> so it doesn't re-use values in variables.
exactly. The next part follows from this. As the compiler doesn't dare
reordering loads and stores, it will produce bad code.
> > By feeding the produced assembler into a short Perl
> > script, I was able to identify ugly code, optimize it and shave 30% of
> > the execution time of a 700 line function by optimizing 4 lines.
> >
> > I'll post the perl-script for those who are interested.
> > It finds the lines where the target of a ldt/addt/mult/subt function
> > appears in the following line, which means pipeline stalls:
>
> .. but this is a different problem, an instruction scheduling
> problem. What I am saying, mostly, is that I obviously don't
> understand what you are saying.
Just enter some test code and look at the produced assembler.
E.g. a vector addition:
If the compiler is unsure about aliasing, it will produce stalling code:
ldt $f1, 8(a1)
ldt $f2, 16(a2)
addt $f1,$f2,$f1 #STALL, wait for $f2
stt $f1 #STALL, wait for $f1
If the compiler knows there is no such problem (e.g. by using
temporaries or local arrays), it can reorder the instructions and
produce much much faster code.
The scheduling problem follows from the aliasing prob.
> In addition, is this savings in gcc or in ccc?
The savings are in ccc. After I entered all hints to remove aliasing
problems, the ccc produces no more "ugly" code, gcc still does. ccc's
code is about three times as fast.
> Are you using a 21264 or a 21164? The 21264 has enough out-of-order
> execution stuff that the above problem doesn't always cause a stall.
The stuff we are talking about are stores and loads. Does the 21264
analyze those to see that they don't access the same location so they
can be reordered? Still, I would expect no more than a factor of 2
improvement due to ooo execution. The code is still bad. E.g. for matric
multiplication (of small matrices), the good code also uses register
much better and contains only a fraction of the loads and stores.
I'm using EV56, I'm buying EV67
> If you are interested in this from a computer-science research side,
> you can look at the Atlas and FFTW projects, which do a more
> complicated version of what you have done.
I was interested to improve my times of 20us per Finite Element. I'm now
running at 2.8us per FE with 345 MFLOPS, which is more than a third of
peak. That's OK for me. There is not much room for improvement (except
increasing clock frequency)
- Alex
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: fast array calculation in C
1999-10-11 4:00 ` Alex Rhomberg
@ 1999-10-11 4:00 ` Greg Lindahl
1999-10-11 4:00 ` memore0
1999-10-12 4:00 ` Alex Rhomberg
0 siblings, 2 replies; 18+ messages in thread
From: Greg Lindahl @ 1999-10-11 4:00 UTC (permalink / raw)
Alex Rhomberg <rhomberg@ife.ee.ethz.ch> writes:
> exactly. The next part follows from this. As the compiler doesn't dare
> reordering loads and stores, it will produce bad code.
OIC. This makes good sense. Fortunately, my customers use Fortran. :-)
> The stuff we are talking about are stores and loads. Does the 21264
> analyze those to see that they don't access the same location so they
> can be reordered?
No. I think the Merced is the first processor to do that.
> I was interested to improve my times of 20us per Finite Element. I'm now
> running at 2.8us per FE with 345 MFLOPS, which is more than a third of
> peak. That's OK for me. There is not much room for improvement (except
> increasing clock frequency)
That's actually really good on an EV56. Most people did not get above
250 MFLOPS for C or Fortran. I have generally seen a 2x improvement
with an EV6 at the same clock, but I suspect you will get worse than
that since your code is already pretty good.
-- g
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: fast array calculation in C
1999-10-11 4:00 ` Greg Lindahl
@ 1999-10-11 4:00 ` memore0
1999-10-11 4:00 ` Greg Lindahl
1999-10-12 4:00 ` Alex Rhomberg
1999-10-12 4:00 ` Alex Rhomberg
1 sibling, 2 replies; 18+ messages in thread
From: memore0 @ 1999-10-11 4:00 UTC (permalink / raw)
On 11 Oct 1999 16:20:19 GMT, lindahl@pbm.com (Greg Lindahl) wrote:
>Alex Rhomberg <rhomberg@ife.ee.ethz.ch> writes:
>
>> I was interested to improve my times of 20us per Finite Element. I'm now
>> running at 2.8us per FE with 345 MFLOPS, which is more than a third of
>> peak. That's OK for me. There is not much room for improvement (except
>> increasing clock frequency)
>
>That's actually really good on an EV56. Most people did not get above
>250 MFLOPS for C or Fortran.
Just to stick my nose in here...
Remember this is hand optimized assembler. Getting a 5-10x
performance boost over c by handiwork is not that rare. We
do it frequently in the embedded world. First write your
stuff in C, debug it for functionality, then optimize as
needed. Sometimes a light touch on a few lines of code are
all you need to get a BIG boost, even in the real-time data
moving world(integer mostly).
As far as the net perf of the proc, I haven't the faintest
how 345MF rates since I run a lowly 233MHz axppci ;-)
> I have generally seen a 2x improvement
>with an EV6 at the same clock, but I suspect you will get worse than
>that since your code is already pretty good.
It does read like he knows what he's doing...
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: fast array calculation in C
1999-10-11 4:00 ` memore0
@ 1999-10-11 4:00 ` Greg Lindahl
1999-10-12 4:00 ` Alex Rhomberg
1 sibling, 0 replies; 18+ messages in thread
From: Greg Lindahl @ 1999-10-11 4:00 UTC (permalink / raw)
memore0@yahoo.com writes:
> Just to stick my nose in here...
> Remember this is hand optimized assembler.
He said it was C. He rearranged the C to get the compiler to generate
better code. That's what both Atlas and FFTW do.
-- g
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: fast array calculation in C
1999-10-12 4:00 ` memore0
@ 1999-10-12 4:00 ` Alex Rhomberg
0 siblings, 0 replies; 18+ messages in thread
From: Alex Rhomberg @ 1999-10-12 4:00 UTC (permalink / raw)
memore0@yahoo.com wrote:
>
> On Tue, 12 Oct 1999 09:34:20 +0200, Alex Rhomberg
> <rhomberg@ife.ee.ethz.ch> wrote:
> >No, this is optimized C. I just looked at the assembler to find the
> >parts where the compiler produces bad code and write those differently,
> >but still in C.
>
> In response to you and Greg, "oops".
>
> My first thought after that was to wonder why?
>
> I guess that my practice has pretty much been that if I
> look at the assembly, I change it there and save it as
> such. Obviously this has maintenance implications, but
> for embedded stuff, it's usually "write once, run forever".
There are some differences. To change the assembler efficiently (meaning
I don't just reorder a bunch of instructions but try to eliminate loads
and stores) I'd have to look at a bigger picture (in Assembler). I
optimized by finding bad code and changing that in C so the compiler
makes better code. This takes not too much time, and I still have one
portable code. To do it in Assembler, I'd hve to keep in mind all the
issues about pipeline depth and superscalar instruction (which the
compiler knows about). You normally have a lot less of those in embedded
programming.
I'm not afraid of doing Assembler for e.g. a DSP, but for a superscalar
RISC, I'd only use it for some core things (like the BLAS or math
library)
- Alex
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: fast array calculation in C
1999-10-12 4:00 ` Alex Rhomberg
@ 1999-10-12 4:00 ` Eberhard Burr
1999-10-13 4:00 ` Alex Rhomberg
1999-10-12 4:00 ` memore0
1 sibling, 1 reply; 18+ messages in thread
From: Eberhard Burr @ 1999-10-12 4:00 UTC (permalink / raw)
Alex Rhomberg <rhomberg@ife.ee.ethz.ch> writes:
> No, this is optimized C. I just looked at the assembler to find the
> parts where the compiler produces bad code and write those differently,
> but still in C.
Would you care posting an example? I am not a computer scientist but
I'm always interested in speedy calculations ;-) I guess from looking
at an example of what you did, I can learn much more than from
following the discussion about aliasing (honestly, I don't even know
what that is).
kind Regards,
--
Eberhard Burr check http://www.uni-karlsruhe.de/~Eberhard.Burr/publickey.asc
for PGP Key -- #include <stddisc.h> -- electric cookie follows
There's something different about us -- different from people of Europe,
Africa, Asia ... a deep and abiding belief in the Easter Bunny.
-- G. Gordon Liddy
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: fast array calculation in C
1999-10-11 4:00 ` Greg Lindahl
1999-10-11 4:00 ` memore0
@ 1999-10-12 4:00 ` Alex Rhomberg
1 sibling, 0 replies; 18+ messages in thread
From: Alex Rhomberg @ 1999-10-12 4:00 UTC (permalink / raw)
Greg Lindahl wrote:
>
> > The stuff we are talking about are stores and loads. Does the 21264
> > analyze those to see that they don't access the same location so they
> > can be reordered?
>
> No. I think the Merced is the first processor to do that.
And I thought, Merced won't do anything out of order, the compiler will
have to prepare everything (I see now how good that is done :-)
I suspect that he will only have a shortcut (from pending write directly
to newly issued read)
> > I was interested to improve my times of 20us per Finite Element. I'm now
> > running at 2.8us per FE with 345 MFLOPS, which is more than a third of
> > peak. That's OK for me. There is not much room for improvement (except
> > increasing clock frequency)
>
> That's actually really good on an EV56. Most people did not get above
> 250 MFLOPS for C or Fortran. I have generally seen a 2x improvement
> with an EV6 at the same clock, but I suspect you will get worse than
> that since your code is already pretty good.
I should get almost linear improvements with clock frequency. And then
there will be other stuff to worry about, when I connect 24 EV67 @ 667
MHz. Pieces of code that I cannot optimize so easily and will therefore
be glad if they get reordered.
Besides I'm almost at my target. Three years ago, I gazed into my
crystal ball and said "those Finite Elements will take 2.5 us each",
which was rather
limitless optimism. With slower calculations our project would not have
been doable.
I'm almost there, and with the faster processor I expect to probably be
even better (and then lose a lot of time doing communication and
collision detection...)
- Alex
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: fast array calculation in C
1999-10-11 4:00 ` memore0
1999-10-11 4:00 ` Greg Lindahl
@ 1999-10-12 4:00 ` Alex Rhomberg
1999-10-12 4:00 ` Eberhard Burr
1999-10-12 4:00 ` memore0
1 sibling, 2 replies; 18+ messages in thread
From: Alex Rhomberg @ 1999-10-12 4:00 UTC (permalink / raw)
memore0@yahoo.com wrote:
>
> On 11 Oct 1999 16:20:19 GMT, lindahl@pbm.com (Greg Lindahl) wrote:
> >That's actually really good on an EV56. Most people did not get above
> >250 MFLOPS for C or Fortran.
>
> Just to stick my nose in here...
> Remember this is hand optimized assembler. Getting a 5-10x
> performance boost over c by handiwork is not that rare.
No, this is optimized C. I just looked at the assembler to find the
parts where the compiler produces bad code and write those differently,
but still in C.
You can get quite nice results with the compaq C compiler
- Alex
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: fast array calculation in C
1999-10-12 4:00 ` Alex Rhomberg
1999-10-12 4:00 ` Eberhard Burr
@ 1999-10-12 4:00 ` memore0
1999-10-12 4:00 ` Alex Rhomberg
1 sibling, 1 reply; 18+ messages in thread
From: memore0 @ 1999-10-12 4:00 UTC (permalink / raw)
On Tue, 12 Oct 1999 09:34:20 +0200, Alex Rhomberg
<rhomberg@ife.ee.ethz.ch> wrote:
>memore0@yahoo.com wrote:
>>
>> On 11 Oct 1999 16:20:19 GMT, lindahl@pbm.com (Greg Lindahl) wrote:
>> >That's actually really good on an EV56. Most people did not get above
>> >250 MFLOPS for C or Fortran.
>>
>> Just to stick my nose in here...
>> Remember this is hand optimized assembler. Getting a 5-10x
>> performance boost over c by handiwork is not that rare.
>
>No, this is optimized C. I just looked at the assembler to find the
>parts where the compiler produces bad code and write those differently,
>but still in C.
In response to you and Greg, "oops".
My first thought after that was to wonder why?
I guess that my practice has pretty much been that if I
look at the assembly, I change it there and save it as
such. Obviously this has maintenance implications, but
for embedded stuff, it's usually "write once, run forever".
>You can get quite nice results with the compaq C compiler
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: fast array calculation in C
1999-10-12 4:00 ` Eberhard Burr
@ 1999-10-13 4:00 ` Alex Rhomberg
1999-10-13 4:00 ` Robert Harley
0 siblings, 1 reply; 18+ messages in thread
From: Alex Rhomberg @ 1999-10-13 4:00 UTC (permalink / raw)
Eberhard Burr wrote:
>
> Alex Rhomberg <rhomberg@ife.ee.ethz.ch> writes:
>
> Would you care posting an example? I am not a computer scientist but
> I'm always interested in speedy calculations ;-) I guess from looking
> at an example of what you did, I can learn much more than from
> following the discussion about aliasing (honestly, I don't even know
> what that is).
Sure
here is the code I posted to the alpha-c mailing list (for compaq ccc):
#define FAST or not and look at the speed differences.
I used the perl script at the bottom to find bad lines in the assembler
code
- Alex
===========snip example code follows
void subroutine(double *fmat_lin, const double __restrict x,
const double __restrict dhdX0_loc)
{
#ifdef FAST
double fmat_li[9];
#else
double *fmat_li = fmat_lin;
#endif
fmat_li[0+3*0] = x[0]*dhdX0_loc[0];
fmat_li[0+3*1] = x[0]*dhdX0_loc[1];
fmat_li[0+3*2] = x[0]*dhdX0_loc[2];
fmat_li[1+3*0] = x[1]*dhdX0_loc[0];
fmat_li[1+3*1] = x[1]*dhdX0_loc[1];
fmat_li[1+3*2] = x[1]*dhdX0_loc[2];
fmat_li[2+3*0] = x[2]*dhdX0_loc[0];
fmat_li[2+3*1] = x[2]*dhdX0_loc[1];
fmat_li[2+3*2] = x[2]*dhdX0_loc[2];
for (k = 1;k < 8;k++) {
fmat_li[0+3*0] += x[3*k+0]*dhdX0_loc[3*k+0];
fmat_li[0+3*1] += x[3*k+0]*dhdX0_loc[3*k+1];
fmat_li[0+3*2] += x[3*k+0]*dhdX0_loc[3*k+2];
fmat_li[1+3*0] += x[3*k+1]*dhdX0_loc[3*k+0];
fmat_li[1+3*1] += x[3*k+1]*dhdX0_loc[3*k+1];
fmat_li[1+3*2] += x[3*k+1]*dhdX0_loc[3*k+2];
fmat_li[2+3*0] += x[3*k+2]*dhdX0_loc[3*k+0];
fmat_li[2+3*1] += x[3*k+2]*dhdX0_loc[3*k+1];
fmat_li[2+3*2] += x[3*k+2]*dhdX0_loc[3*k+2];
}
#ifdef FAST
memcpy(fmat_lin, fmat_li, sizeof(double)*9);
#endif
}
============== perl code follows
#!/usr/bin/perl -wp
# usage if this script is named find_stalls
#objdump -d myobject.o | find_stalls | more
#then search for UGLY!
use strict;
use vars qw/$t/;
$t && /\Q$t,/ && s/$/\t\t#UGLY!!/; #append UGLY if target is seen
$t = (/(?:mult|subt|addt|ldt).*(\$f\d+)/)[0];
#new target is last floating register arg
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: fast array calculation in C
1999-10-13 4:00 ` Alex Rhomberg
@ 1999-10-13 4:00 ` Robert Harley
0 siblings, 0 replies; 18+ messages in thread
From: Robert Harley @ 1999-10-13 4:00 UTC (permalink / raw)
Alex Rhomberg <rhomberg@ife.ee.ethz.ch> writes:
> Sure
>
> here is the code I posted to the alpha-c mailing list (for compaq ccc):
How does one join that list?
Here's how I would write your code snippet if it was speed-critical.
I would compile with gcc 2.95 and Compaq's cc, with and without
-DUNROLL and compare the run-times.
------------------------------------------------------------------------------
/* Try with and without -DUNROLL - Rob. */
static void subroutine
( double * __restrict fmat
, const double * __restrict x, const double * __restrict d
) {
int k;
double d0,d1,d2, f00,f01,f02,f10,f11,f12,f20,f21,f22, x0,x1,x2;
x0 = x[0]; x1 = x[1]; x2 = x[2];
d0 = d[0]; d1 = d[1]; d2 = d[2];
f00 = x0*d0; f01 = x0*d1; f02 = x0*d2;
f10 = x1*d0; f11 = x1*d1; f12 = x1*d2;
f20 = x2*d0; f21 = x2*d1; f22 = x2*d2;
#ifndef UNROLL
for (k = 1; k < 8; ++k) {
x0 = x[3*k+0]; x1 = x[3*k+1]; x2 = x[3*k+2];
d0 = d[3*k+0]; d1 = d[3*k+1]; d2 = d[3*k+2];
f00 += x0*d0; f01 += x0*d1; f02 += x0*d2;
f10 += x1*d0; f11 += x1*d1; f12 += x1*d2;
f20 += x2*d0; f21 += x2*d1; f22 += x2*d2;
}
#else
#define STEP(k) \
x0 = x[3*k+0]; x1 = x[3*k+1]; x2 = x[3*k+2]; \
d0 = d[3*k+0]; d1 = d[3*k+1]; d2 = d[3*k+2]; \
f00 += x0*d0; f01 += x0*d1; f02 += x0*d2; \
f10 += x1*d0; f11 += x1*d1; f12 += x1*d2; \
f20 += x2*d0; f21 += x2*d1; f22 += x2*d2 /* ; */
STEP(1);
STEP(2);
STEP(3);
STEP(4);
STEP(5);
STEP(6);
STEP(7);
#undef STEP
#endif
fmat[0+3*0] = f00; fmat[0+3*1] = f01; fmat[0+3*2] = f02;
fmat[1+3*0] = f10; fmat[1+3*1] = f11; fmat[1+3*2] = f12;
fmat[2+3*0] = f20; fmat[2+3*1] = f21; fmat[2+3*2] = f22;
} /* end function subroutine */
------------------------------------------------------------------------------
Rob.
PS: Untested. If your computer blows up it's not my fault etc etc.
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: fast array calculation in C
[not found] ` <7ud6gc$2o94@news2.newsguy.com>
@ 1999-10-18 4:00 ` Emil Briggs
1999-10-18 4:00 ` Greg Lindahl
1999-10-18 4:00 ` Alex Rhomberg
1 sibling, 1 reply; 18+ messages in thread
From: Emil Briggs @ 1999-10-18 4:00 UTC (permalink / raw)
Greg Lindahl wrote:
>
> Alex Rhomberg <rhomberg@ife.ee.ethz.ch> writes:
>
> > > The memcpy() call at the end, on the other hand, is expensive.
> >
> > Did you check that?
> > It isn't (expensive). The compiler inlines it and then does some more
> > optimizations.
> > I get >400 MFLOPS (40% peak) with the memcpy
>
> Compilers are clever with memcopy, but I've yet to see one that could
> figure out at compile time "hey, the byte count is divisible by 8 and
> the buffers are aligned..." That's why a good compiler will usually do
> a better job of optimizing a copy loop.
>
The Cray vector compilers were smart enough to do wholesale replacment
of computational loops with calls to had coded assembly language
routines. Even up to the level of BLAS2 calls. I was quite impressed
when I looked at the assembly language output of the compiler and
realized what it was doing.
Regards
Emil
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: fast array calculation in C
[not found] ` <7ud6gc$2o94@news2.newsguy.com>
1999-10-18 4:00 ` Emil Briggs
@ 1999-10-18 4:00 ` Alex Rhomberg
1999-10-18 4:00 ` Greg Lindahl
1 sibling, 1 reply; 18+ messages in thread
From: Alex Rhomberg @ 1999-10-18 4:00 UTC (permalink / raw)
To: Greg Lindahl
[cc mailed and posted]
Greg Lindahl wrote:
>
> Alex Rhomberg <rhomberg@ife.ee.ethz.ch> writes:
>
> > > The memcpy() call at the end, on the other hand, is expensive.
> >
> > Did you check that?
> > It isn't (expensive). The compiler inlines it and then does some more
> > optimizations.
> > I get >400 MFLOPS (40% peak) with the memcpy
>
> Compilers are clever with memcopy, but I've yet to see one that could
> figure out at compile time "hey, the byte count is divisible by 8 and
> the buffers are aligned..." That's why a good compiler will usually do
> a better job of optimizing a copy loop.
>
> As for your "proof" that it isn't expensive, you didn't prove your
> point. Many computational kernels get more than 40% peak. You're
> certainly doing well, but you don't know yet if you're as good as you
> can get.
I didn't say "proof". But I can show you an example (which you can test
yourself):
Seems here is the memcpy you were looking for :-)
The memcopy gets compiled to:
d8: 00 00 b0 9d stt $f13,0(a0) #pretty optimized IMHO
dc: 08 00 10 9e stt $f16,8(a0)
e0: 10 00 30 9c stt $f1,16(a0)
e4: 18 00 d0 9d stt $f14,24(a0)
e8: 20 00 30 9e stt $f17,32(a0)
ec: 28 00 50 9d stt $f10,40(a0)
f0: 30 00 10 9c stt $f0,48(a0)
f4: 38 00 90 9d stt $f12,56(a0)
f8: 40 00 70 9d stt $f11,64(a0)
Compile the following with ccc -O4 -DFAST and check the assembler.
void subroutine(double *res, const double *x,
const double *y)
{
int k;
#ifdef FAST
double res_loc[9];
#else
double *res_loc = res;
#endif
res_loc[0+3*0] = x[0]*y[0];
res_loc[0+3*1] = x[0]*y[1];
res_loc[0+3*2] = x[0]*y[2];
res_loc[1+3*0] = x[1]*y[0];
res_loc[1+3*1] = x[1]*y[1];
res_loc[1+3*2] = x[1]*y[2];
res_loc[2+3*0] = x[2]*y[0];
res_loc[2+3*1] = x[2]*y[1];
res_loc[2+3*2] = x[2]*y[2];
for (k = 1;k < 8;k++) {
res_loc[0+3*0] += x[3*k+0]*y[3*k+0];
res_loc[0+3*1] += x[3*k+0]*y[3*k+1];
res_loc[0+3*2] += x[3*k+0]*y[3*k+2];
res_loc[1+3*0] += x[3*k+1]*y[3*k+0];
res_loc[1+3*1] += x[3*k+1]*y[3*k+1];
res_loc[1+3*2] += x[3*k+1]*y[3*k+2];
res_loc[2+3*0] += x[3*k+2]*y[3*k+0];
res_loc[2+3*1] += x[3*k+2]*y[3*k+1];
res_loc[2+3*2] += x[3*k+2]*y[3*k+2];
}
#ifdef FAST
memcpy(res, res_loc, sizeof(double)*9);
#endif
}
- Alex
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: fast array calculation in C
1999-10-18 4:00 ` Alex Rhomberg
@ 1999-10-18 4:00 ` Greg Lindahl
1999-10-18 4:00 ` Alex Rhomberg
0 siblings, 1 reply; 18+ messages in thread
From: Greg Lindahl @ 1999-10-18 4:00 UTC (permalink / raw)
Alex Rhomberg <rhomberg@ife.ee.ethz.ch> writes:
> I didn't say "proof". But I can show you an example (which you can test
> yourself):
That's an amazing proof. I've never seen a compiler that did that before.
I don't believe gcc does this.
-- g
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: fast array calculation in C
1999-10-18 4:00 ` Emil Briggs
@ 1999-10-18 4:00 ` Greg Lindahl
0 siblings, 0 replies; 18+ messages in thread
From: Greg Lindahl @ 1999-10-18 4:00 UTC (permalink / raw)
Emil Briggs <briggs@tick.physics.ncsu.edu> writes:
> The Cray vector compilers were smart enough to do wholesale replacment
> of computational loops with calls to had coded assembly language
> routines. Even up to the level of BLAS2 calls. I was quite impressed
> when I looked at the assembly language output of the compiler and
> realized what it was doing.
That's much easier than replacing memcpy with an optimal routine.
IBMs RS6000 compiler also does this.
-- g
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: fast array calculation in C
1999-10-18 4:00 ` Greg Lindahl
@ 1999-10-18 4:00 ` Alex Rhomberg
0 siblings, 0 replies; 18+ messages in thread
From: Alex Rhomberg @ 1999-10-18 4:00 UTC (permalink / raw)
Greg Lindahl wrote:
>
> Alex Rhomberg <rhomberg@ife.ee.ethz.ch> writes:
>
> > I didn't say "proof". But I can show you an example (which you can test
> > yourself):
>
> That's an amazing proof. I've never seen a compiler that did that before.
> I don't believe gcc does this.
It's pretty cool...
some comparisons:
for o in dies.o*; do wc -c $o;
objdump -d $o|~/perl/ugly | grep UGLY | wc -l;
objdump -d $o|grep jsr; done
1376 dies.o.ccc
0
2320 dies.o.gcc
20
23c: 00 40 5b 6b jsr ra,(t12),240 <subroutine+0x240>
gcc code is
- bigger
- uglier
- slower
- doesn't inline memcpy
ccc code is
- cool
- fast
- cool
- small
- Alex
^ permalink raw reply [flat|nested] 18+ messages in thread
end of thread, other threads:[~1999-10-18 4:00 UTC | newest]
Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1999-10-07 4:00 fast array calculation in C Alex Rhomberg
1999-10-09 4:00 ` Greg Lindahl
1999-10-11 4:00 ` Alex Rhomberg
1999-10-11 4:00 ` Greg Lindahl
1999-10-11 4:00 ` memore0
1999-10-11 4:00 ` Greg Lindahl
1999-10-12 4:00 ` Alex Rhomberg
1999-10-12 4:00 ` Eberhard Burr
1999-10-13 4:00 ` Alex Rhomberg
1999-10-13 4:00 ` Robert Harley
1999-10-12 4:00 ` memore0
1999-10-12 4:00 ` Alex Rhomberg
1999-10-12 4:00 ` Alex Rhomberg
[not found] <3809E64D.85BDCAFC@ife.ee.ethz.ch>
[not found] ` <7ud6gc$2o94@news2.newsguy.com>
1999-10-18 4:00 ` Emil Briggs
1999-10-18 4:00 ` Greg Lindahl
1999-10-18 4:00 ` Alex Rhomberg
1999-10-18 4:00 ` Greg Lindahl
1999-10-18 4:00 ` Alex Rhomberg
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox