Usenet comp.os.linux.alpha (1997-2013)
 help / color / mirror / Atom feed
* optimized (or optimizable) C programming?
@ 1999-10-04  4:00 Alex Rhomberg
  1999-10-04  4:00 ` Greg Lindahl
  1999-10-05  4:00 ` Robert Harley
  0 siblings, 2 replies; 5+ messages in thread
From: Alex Rhomberg @ 1999-10-04  4:00 UTC (permalink / raw)


Hi

I'm trying to optimize some c program. It's FE computation, many matrix
operations with about 3*3 to 3*8 matrices.

I played around with some bit of code with a matrix multiplication.
code is at the end.
I use the compaq ccc compiler

First is the short approach: three nested for loops. 
for i
  for j
    for k
      f[i][j] += a[i][k]*b[k][j]
Gives me about 8% peak perf (80 MFLOPS).

Second is some reordering and manual unrolling
Gives me the same as the above (with -unroll 100)

For the third try, I use intermediate vars for ALL elements. Makes the
code slightly bigger. 
Suddenly, I get 406 MFLOPS. (With complete unrolling only 364???)

Third try, after removing all intermediates on the RHS, the compiler
still sees no LHS array accesses and optimizes (even better).
The maximum I got this way is 422 MFLOPS, which is acceptable

Last test says, I get the same if I locally alloc the LHS vector and
memcpy it to the target when I'm done multiplying

**********************
QUESTION SECTION
Is there a way I can tell the compiler that RHS arrays don't change for
some part of the code? Pragma or something?

Is there a documentation of optimizing pragmas for the compaq ccc?

Is writing optimized C code witchcraft and sorcery?

Can somebody suggest a faster way?

Why does it get slower with more unrolling?

- Alex
***********************
CODE SECTION

FIRST: (unoptimized, 80 MFLOPS)
void test1 (double fmat[9], const double x[24], const double dhdX0[24])
{
  int i, j, k;
  for (i = 0;i < 3;i++) 
    for (j = 0;j < 3;j++) {
      fmat[i+3*j] = x[3*0+i]*dhdX0[j+3*0];
      for (k = 1;k < 8;k++) 
	fmat[i+3*j] += x[3*k+i]*dhdX0[j+3*k];
    }
}

SECOND: All temps (409 MFLOPS)
void test2 (double *fmat_lin, const double *x, const double *dhdX0_lin)
{
  int k;
  double x0, x1, x2, d0, d1, d2;
  double f00, f01, f02, f10, f11, f12, f20, f21, f22;

  x0 = x[0]; x1 = x[1]; x2=x[2];
  d0=dhdX0_lin[0]; d1=dhdX0_lin[1];d2=dhdX0_lin[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;
  for (k = 1;k < 8;k++) {
    x0 = x[3*k]; x1 = x[3*k+1]; x2 = x[3*k+2];
    d0=dhdX0_lin[3*k]; d1=dhdX0_lin[3*k+1];d2=dhdX0_lin[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;
  }
  fmat_lin[3*0+0] = f00;  fmat_lin[3*1+0] = f01;
  fmat_lin[3*2+0] = f02;  fmat_lin[3*0+1] = f10;
  fmat_lin[3*1+1] = f11;  fmat_lin[3*2+1] = f12;
  fmat_lin[3*0+2] = f20;  fmat_lin[3*1+2] = f21;
  fmat_lin[3*2+2] = f22;
}

THIRD: local lvalues (427 MFLOPS)
void test3 (double *fmat_lahm, const double *x, const double *dhdX0_lin)
{
  int k;
  double fmat_lin[9];

  fmat_lin[0+3*0] = x[0]*dhdX0_lin[0];
  fmat_lin[0+3*1] = x[0]*dhdX0_lin[1];
  fmat_lin[0+3*2] = x[0]*dhdX0_lin[2];
  fmat_lin[1+3*0] = x[1]*dhdX0_lin[0];
  fmat_lin[1+3*1] = x[1]*dhdX0_lin[1];
  fmat_lin[1+3*2] = x[1]*dhdX0_lin[2];
  fmat_lin[2+3*0] = x[2]*dhdX0_lin[0];
  fmat_lin[2+3*1] = x[2]*dhdX0_lin[1];
  fmat_lin[2+3*2] = x[2]*dhdX0_lin[2];
  for (k = 1;k < 8;k++) {
    fmat_lin[0+3*0] += x[3*k+0]*dhdX0_lin[3*k+0];
    fmat_lin[0+3*1] += x[3*k+0]*dhdX0_lin[3*k+1];
    fmat_lin[0+3*2] += x[3*k+0]*dhdX0_lin[3*k+2];
    fmat_lin[1+3*0] += x[3*k+1]*dhdX0_lin[3*k+0];
    fmat_lin[1+3*1] += x[3*k+1]*dhdX0_lin[3*k+1];
    fmat_lin[1+3*2] += x[3*k+1]*dhdX0_lin[3*k+2];
    fmat_lin[2+3*0] += x[3*k+2]*dhdX0_lin[3*k+0];
    fmat_lin[2+3*1] += x[3*k+2]*dhdX0_lin[3*k+1];
    fmat_lin[2+3*2] += x[3*k+2]*dhdX0_lin[3*k+2];
  }

  memcpy(fmat_lahm, fmat_lin, 9*sizeof(double));
}



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: optimized (or optimizable) C programming?
  1999-10-04  4:00 optimized (or optimizable) C programming? Alex Rhomberg
@ 1999-10-04  4:00 ` Greg Lindahl
  1999-10-05  4:00 ` Robert Harley
  1 sibling, 0 replies; 5+ messages in thread
From: Greg Lindahl @ 1999-10-04  4:00 UTC (permalink / raw)


Alex Rhomberg <rhomberg@ife.ee.ethz.ch> writes:

> Is there a way I can tell the compiler that RHS arrays don't change for
> some part of the code? Pragma or something?

Some C compilers have a #pragma noalias, which makes them behave more
like Fortran compilers. I don't know what ccc has.

> Is writing optimized C code witchcraft and sorcery?

Well, you could always switch to fortran ;-)

-- g



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: optimized (or optimizable) C programming?
  1999-10-04  4:00 optimized (or optimizable) C programming? Alex Rhomberg
  1999-10-04  4:00 ` Greg Lindahl
@ 1999-10-05  4:00 ` Robert Harley
  1999-10-05  4:00   ` Alex Rhomberg
  1 sibling, 1 reply; 5+ messages in thread
From: Robert Harley @ 1999-10-05  4:00 UTC (permalink / raw)



Alex Rhomberg <rhomberg@ife.ee.ethz.ch> writes:
> void test2 (double *fmat_lin, const double *x, const double *dhdX0_lin)

If you know that fmat_lin doesn't overlap with the others you can mark
it with  __restrict in ccc and in the latest gcc.


#if defined(__DECC) || defined(__GNUC__) && (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ >= 95)
#define RESTRICT __restrict
#else
#define RESTRICT
#endif


void test2
  ( double * RESTRICT fmat_lin, const double *x, const double *dhdX0_lin ) {

  ...;

} /* end function test2 */


Rob.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: optimized (or optimizable) C programming?
  1999-10-05  4:00 ` Robert Harley
@ 1999-10-05  4:00   ` Alex Rhomberg
  1999-10-05  4:00     ` ccc code wierdness Alex Rhomberg
  0 siblings, 1 reply; 5+ messages in thread
From: Alex Rhomberg @ 1999-10-05  4:00 UTC (permalink / raw)


Robert Harley wrote:
> 
> Alex Rhomberg <rhomberg@ife.ee.ethz.ch> writes:
> > void test2 (double *fmat_lin, const double *x, const double *dhdX0_lin)
> 
> If you know that fmat_lin doesn't overlap with the others you can mark
> it with  __restrict in ccc and in the latest gcc.

> #define RESTRICT __restrict

> void test2
>   ( double * RESTRICT fmat_lin, const double *x, const double *dhdX0_lin ) {

This doesn't seem to make a difference:

=cut thistest.c

void scatter( double * __restrict dforce, double * df_el, int *nodeI)
{
  int thisnode;
  int jE_node;
  for (jE_node = 0; jE_node < 8; jE_node++) /* scatter internal force*/
  {
    thisnode = nodeI[jE_node]*3;/*will be replaced by pointers*/
    dforce[thisnode+0] -= df_el[jE_node*3+0];
    dforce[thisnode+1] -= df_el[jE_node*3+1];
    dforce[thisnode+2] -= df_el[jE_node*3+2];
  }
}

=cut commands

ccc -fast -std1 -c thistest.c -o ttt.o # both -std1 and __restrict
should tell the                    
                                       # compiler that inputs don't
overlap
objdump -rd ttt.o | more

=cut part of output, annotated
  9c:   00 00 88 8e     ldt     $f20,0(t7)
  a0:   d0 ff b1 8e     ldt     $f21,-48(a1)    
  a4:   34 14 95 5a     subt    $f20,$f21,$f20   #first wait for $f21
  a8:   00 00 88 9e     stt     $f20,0(t7)       #wait for  fadd latency
  ac:   08 00 c8 8e     ldt     $f22,8(t7)
  b0:   d8 ff f1 8e     ldt     $f23,-40(a1)
  b4:   36 14 d7 5a     subt    $f22,$f23,$f22   #sleep(2)
  b8:   08 00 c8 9e     stt     $f22,8(t7)       #drink_coffee

=cut comment
It gets better if I restrict ALL the parameters

- Alex



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: ccc code wierdness
  1999-10-05  4:00   ` Alex Rhomberg
@ 1999-10-05  4:00     ` Alex Rhomberg
  0 siblings, 0 replies; 5+ messages in thread
From: Alex Rhomberg @ 1999-10-05  4:00 UTC (permalink / raw)


Alex Rhomberg wrote:

> void scatter( double * __restrict dforce, 
>               double * __restrict df_el, int * __restrictnodeI)
> {
>   int thisnode;
>   int jE_node;
>   for (jE_node = 0; jE_node < 8; jE_node++) /* scatter internal force*/
>   {
>     thisnode = nodeI[jE_node]*3;/*will be replaced by pointers*/
>     dforce[thisnode+0] -= df_el[jE_node*3+0];
>     dforce[thisnode+1] -= df_el[jE_node*3+1];
>     dforce[thisnode+2] -= df_el[jE_node*3+2];
>   }
> }
> 
> =cut part of output, annotated
>   9c:   00 00 88 8e     ldt     $f20,0(t7)
>   a0:   d0 ff b1 8e     ldt     $f21,-48(a1)
>   a4:   34 14 95 5a     subt    $f20,$f21,$f20   #first wait for $f21
>   a8:   00 00 88 9e     stt     $f20,0(t7)       #wait for  fadd latency
>   ac:   08 00 c8 8e     ldt     $f22,8(t7)
>   b0:   d8 ff f1 8e     ldt     $f23,-40(a1)
>   b4:   36 14 d7 5a     subt    $f22,$f23,$f22   #sleep(2)
>   b8:   08 00 c8 9e     stt     $f22,8(t7)       #drink_coffee
> 

=cut more comments
if I compile the top code snippet in its own file, I get better code. If
I include it in a second file, I get the same kind of code I
posted(unoptimized)
strange...
Any hints?

- Alex



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~1999-10-05  4:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1999-10-04  4:00 optimized (or optimizable) C programming? Alex Rhomberg
1999-10-04  4:00 ` Greg Lindahl
1999-10-05  4:00 ` Robert Harley
1999-10-05  4:00   ` Alex Rhomberg
1999-10-05  4:00     ` ccc code wierdness Alex Rhomberg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox