Usenet comp.os.linux.alpha (1997-2013)
 help / color / mirror / Atom feed
From: Alex Rhomberg <rhomberg@ife.ee.ethz.ch>
Subject: optimized (or optimizable) C programming?
Date: 1999/10/04	[thread overview]
Message-ID: <37F8AC50.13AA8F7D@ife.ee.ethz.ch> (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));
}



             reply	other threads:[~1999-10-04  4:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-10-04  4:00 Alex Rhomberg [this message]
1999-10-04  4:00 ` optimized (or optimizable) C programming? 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
read-only archive

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