From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111c93,64f8df62a7534aa X-Google-Attributes: gid111c93,public From: Alex Rhomberg Subject: Re: fast array calculation in C Date: 1999/10/18 Message-ID: <380AE3CA.DE154537@ife.ee.ethz.ch>#1/1 X-Deja-AN: 537426145 Content-Transfer-Encoding: 7bit References: <3809E64D.85BDCAFC@ife.ee.ethz.ch> <7ud6gc$2o94@news2.newsguy.com> To: Greg Lindahl Content-Type: text/plain; charset=us-ascii X-Trace: 18 Oct 1999 11:09:31 +0100, beethoven.ee.ethz.ch Organization: Electronics Lab, ETH Zurich, Switzerland MIME-Version: 1.0 Newsgroups: comp.os.linux.alpha [cc mailed and posted] Greg Lindahl wrote: > > Alex Rhomberg 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