From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <199606132210.PAA12033@koala.azstarnet.com> Date: Thu, 13 Jun 1996 15:10:50 -0700 From: David Mosberger-Tang Subject: Re: performance >>>>> On Fri, 14 Jun 1996 07:24:16 +0930 (CST), Berndt Josef Wulf said: Berndt> Which program is this and where can I find the source code Berndt> for it? The source for test3 is appended below. Note that gcc is not doing a good job at optimizing the code due to conservative alias analysis (that's my best guess, at least). If you want to get good performance on an Alpha, change the subroutines to do the following: (a) load all values from memory into temporary variables (b) do all the operations (c) store the results back into memory This is a way of telling gcc that there really is no aliasing going on. I did this for routine svmva() and its execution time dropped from 1.07 down to 0.56 seconds according to gprof. Unfortunately, I managed to destroy my optimized source, but it's really straight forward (if error-prone) to apply the above changes. It's funny, the existing code looks very much like it has been optimized for an x86 (only one or two temporary values are used in the unrolled loops). Another caveat: the file second.c uses times() which is known to be broken on some (older?) versions of the Linux/Alpha libc. It would be safer to use gettimeofday(). --david <--test3.c--><--test3.c--><--test3.c--><--test3.c--> #include #include #include "second.h" void report (int nx, int lx, float op, float tm) { float mf = -1.; if (tm > 0.) mf = op / tm; printf ("%10d%10d%10.2f%10.2f\n", nx, lx, tm, mf); } extern void svm (float, float *, float *, int); extern void svmva (float, float *, float *, float *, int); extern void svmsa (float, float *, float, float *, int); extern void vvm (float *, float *, float *, int); extern void vva (float *, float *, float *, int); extern float vsum (float *, int); extern float vdot (float *, float *, int); void main (int argc, char *argv[]) { #define LOOP 2000000 #define NM 7 #define NNX 500 int OP, i, j, k, nx, nnx, lx, mmx; float a = .1, d, *x, *y, op, tm; double *px, *py, *pz; long str, LX, NX[NM] = {500, 1000, 5000, 10000, 50000, 100000, 500000}; if (argc < 2) { printf ("Usage : %s loop-multiplier [stride]\n", argv[0]); exit (1); } sscanf (argv[1], "%ld", &LX); str = NNX; OP = 10; if (argc > 2) sscanf (argv[2], "%ld", &str); if (str <= 0) str = NX[NM]; LX *= LOOP; printf (" Size Loops Time MFlops\n"); for (j = 0; j < NM; j++) { nx = NX[j]; lx = LX / nx; x = (float *) malloc (nx * sizeof (float)); y = (float *) malloc (nx * sizeof (float)); if (x == (float *) 0) { puts ("Cannot allocate memory for x[]\n"); exit (-1); } if (y == (float *) 0) { puts ("Cannot allocate memory for y[]\n"); exit (-1); } for (i = 0; i < nx; i++) { x[i] = 1.; y[i] = 0.; } op = (1.e-3 * (OP * nx)) * (1.e-3 * lx); d = 0.; tm = second (); nnx = (nx < str ? nx : str); for (k = 0; k < lx; k++) for (i = 0; i < nx; i += nnx) { mmx = nx - i; mmx = (mmx < nnx ? mmx : nnx); svm (a, &x[i], &y[i], mmx); svmva (a, &x[i], &y[i], &y[i], mmx); svmsa (a, &x[i], a, &y[i], mmx); vvm (&x[i], &y[i], &y[i], mmx); vva (&x[i], &y[i], &y[i], mmx); d += vsum (&x[i], mmx); d -= vdot (&x[i], &x[i], mmx); } tm = second () - tm; report (nx, lx, op, tm); free (x); free (y); } } void svm (float a, float *x, float *y, int n) { int i, m; float t1, t2; m = n & ~7L; for (i = 0; i < m; i += 8) { t1 = a * x[i + 0]; t2 = a * x[i + 1]; y[i + 0] = t1; t1 = a * x[i + 2]; y[i + 1] = t2; t2 = a * x[i + 3]; y[i + 2] = t1; t1 = a * x[i + 4]; y[i + 3] = t2; t2 = a * x[i + 5]; y[i + 4] = t1; t1 = a * x[i + 6]; y[i + 5] = t2; t2 = a * x[i + 7]; y[i + 6] = t1; y[i + 7] = t2; } for (i = m; i < n; i++) y[i] = a * x[i]; return; } void svmva (float a, float *x, float *y, float *z, int n) { int i, m; float t1, t2; m = n & ~7L; for (i = 0; i < m; i += 8) { t1 = a * x[i + 0]; t2 = a * x[i + 1]; t1 += y[i + 0]; t2 += y[i + 1]; z[i + 0] = t1; t1 = a * x[i + 2]; z[i + 1] = t2; t1 += y[i + 2]; t2 = a * x[i + 3]; z[i + 2] = t1; t2 += y[i + 3]; t1 = a * x[i + 4]; z[i + 3] = t2; t1 += y[i + 4]; t2 = a * x[i + 5]; z[i + 4] = t1; t2 += y[i + 5]; t1 = a * x[i + 6]; z[i + 5] = t2; t2 = a * x[i + 7]; t1 += y[i + 6]; t2 += y[i + 7]; z[i + 6] = t1; z[i + 7] = t2; } for (i = m; i < n; i++) z[i] = y[i] + a * x[i]; return; } void svmsa (float a, float *x, float b, float *y, int n) { int i, m; float t1, t2; m = n & ~7L; for (i = 0; i < m; i += 8) { t1 = a * x[i + 0]; t2 = a * x[i + 1]; t1 += b; t2 += b; y[i + 0] = t1; t1 = a * x[i + 2]; y[i + 1] = t2; t1 += b; t2 = a * x[i + 3]; y[i + 2] = t1; t2 += b; t1 = a * x[i + 4]; y[i + 3] = t2; t1 += b; t2 = a * x[i + 5]; y[i + 4] = t1; t2 += b; t1 = a * x[i + 6]; y[i + 5] = t2; t2 = a * x[i + 7]; t1 += b; t2 += b; y[i + 6] = t1; y[i + 7] = t2; } for (i = m; i < n; i++) y[i] = a * x[i] + b; return; } void vvm (float *x, float *y, float *z, int n) { int i, m; float t1, t2; m = n & ~7L; for (i = 0; i < m; i += 8) { t1 = x[i + 0] * y[i + 0]; t2 = x[i + 1] * y[i + 1]; z[i + 0] = t1; t1 = x[i + 2] * y[i + 2]; z[i + 1] = t2; t2 = x[i + 3] * y[i + 3]; z[i + 2] = t1; t1 = x[i + 4] * y[i + 4]; z[i + 3] = t2; t2 = x[i + 5] * y[i + 5]; z[i + 4] = t1; t1 = x[i + 6] * y[i + 6]; z[i + 5] = t2; t2 = x[i + 7] * y[i + 7]; z[i + 6] = t1; z[i + 7] = t2; } for (i = m; i < n; i++) z[i] = x[i] * y[i]; return; } void vva (float *x, float *y, float *z, int n) { int i, m; float t1, t2; m = n & ~7L; for (i = 0; i < m; i += 8) { t1 = x[i + 0] + y[i + 0]; t2 = x[i + 1] + y[i + 1]; z[i + 0] = t1; t1 = x[i + 2] + y[i + 2]; z[i + 1] = t2; t2 = x[i + 3] + y[i + 3]; z[i + 2] = t1; t1 = x[i + 4] + y[i + 4]; z[i + 3] = t2; t2 = x[i + 5] + y[i + 5]; z[i + 4] = t1; t1 = x[i + 6] + y[i + 6]; z[i + 5] = t2; t2 = x[i + 7] + y[i + 7]; z[i + 6] = t1; z[i + 7] = t2; } for (i = m; i < n; i++) z[i] = x[i] + y[i]; return; } float vsum (float *x, int n) { int i, m; float vs, t1, t2, t3, t4; m = n & ~7L; t1 = t2 = 0.f; for (i = 0; i < m; i += 8) { t1 += x[i + 0]; t2 += x[i + 1]; t1 += x[i + 4]; t2 += x[i + 5]; t1 += x[i + 2]; t2 += x[i + 3]; t1 += x[i + 6]; t2 += x[i + 7]; } vs = t1 + t2; for (i = m; i < n; i++) vs += x[i]; return vs; } float vdot (float *x, float *y, int n) { int i, m; float vs, t1, t2, s1, s2; m = n & ~7L; s1 = s2 = t1 = t2 = 0.f; for (i = 0; i < m; i += 8) { t1 = x[i + 0] * y[i + 0]; s2 += t2; t2 = x[i + 1] * y[i + 1]; s1 += t1; t1 = x[i + 2] * y[i + 2]; s2 += t2; t2 = x[i + 3] * y[i + 3]; s1 += t1; t1 = x[i + 4] * y[i + 4]; s2 += t2; t2 = x[i + 5] * y[i + 5]; s1 += t1; t1 = x[i + 6] * y[i + 6]; s2 += t2; t2 = x[i + 7] * y[i + 7]; s1 += t1; } vs = s1 + s2; for (i = m; i < n; i++) vs += x[i] * y[i]; return vs; } <--second.c--><--second.c--><--second.c--><--second.c--><--second.c--> #include #include #include #include "second.h" double second () { struct tms buffer; times(&buffer); return buffer.tms_utime / (double) sysconf(_SC_CLK_TCK); }