alphalinux.org high-perf list (1998-2001)
 help / color / mirror / Atom feed
From: Kazushige Goto <goto@statabo.rim.or.jp>
Subject: how to optimize dgemm? No.1
Date: Mon, 30 Nov 1998 23:19:31 +0900	[thread overview]
Message-ID: <19981130231931B.goto@statabo.rim.or.jp> (raw)

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 3221 bytes --]

Recently, some people asked me how to optimize my dgemm routine.

I wrote some information in dgemm_k.S source code, but it's not

enough.

So, I decided to explain some opimization method of Alpha step by

step. Because it takes too long to explain.

First, I make a sample & simple benchmark program.  Please try to type 

"make" and benchmark show you actual speed with MFlops.

I explain only Non-transposed x Non-transposed routine.  If you have

any questions, please tell me.

Originally, BLAS dgemm kernel routine is programmed as follows.

It's really pretty simple, but it takes too long(about 23MFlops,

600MHz 21164).

void dgemm_nn(int m, int n, int k, double alpha, double *a,

              int lda, double *b, int ldb,

              double *c, int ldc){

  int i, j, l;

  for (j = 0; j < n; j++) {

    for (i = 0; i < m; i++) {

      for (l = 0; l < k; l++) {

          c[i + j * ldc] += alpha * b[l + j * ldb] * a[i + l * lda];

        }

    }

  }

}

1. loop exchangeable

This dgemm_nn routine works fine even if any loop is exchanged.

 << 44MFLops >>

  for (j = 0; j < n; j++) {

    for (l = 0; l < k; l++) {

      for (i = 0; i < m; i++) {

          c[i + j * ldc] += alpha * b[l + j * ldb] * a[i + l * lda];

        }

    }

  }

or

 << 40MFLops >>

  for (l = 0; l < k; l++) {

    for (j = 0; j < n; j++) {

      for (i = 0; i < m; i++) {

          c[i + j * ldc] += alpha * b[l + j * ldb] * a[i + l * lda];

        }

    }

  }

or

....

So we can choose a loop to get best performance.

2. Inner dot product

A basic strategy of Alpha(also, all other RISC machines) is to

"calculate innder dot product routine".  Because "store" operation

also causes data traffics.

It means,  we can choose one of this.

  for (j = 0; j < n; j++) {

    for (i = 0; i < m; i++) {

      for (l = 0; l < k; l++) {

          c[i + j * ldc] += alpha * b[l + j * ldb] * a[i + l * lda];

        }

    }

  }

or

  for (i = 0; i < m; i++) {

    for (j = 0; j < n; j++) {

      for (l = 0; l < k; l++) {

          c[i + j * ldc] += alpha * b[l + j * ldb] * a[i + l * lda];

        }

    }

  }

c[i+j*ldc] is a constant value in most inner loop.  So we can change

it to 

 << 27MFLops >>

  double temp;

  for (i = 0; i < m; i++) {

    for (j = 0; j < n; j++) {

      temp = 0.0;

      for (l = 0; l < k; l++) {

          temp += b[l + j * ldb] * a[i + l * lda];

        }

      c[i + j * ldc ] += alpha * temp;

    }

  }

or

 << 22MFLops >>

  double temp;

  for (j = 0; j < n; j++) {

    for (i = 0; i < m; i++) {

      temp = 0.0;

      for (l = 0; l < k; l++) {

          temp += b[l + j * ldb] * a[i + l * lda];

        }

      c[i + j * ldc ] += alpha * temp;

    }

  }

It's still slow, but it'll be much faster as optimizing dgemm_nn.

Continue to No.2

Thanks,

  goto@statabo.rim.or.jp

Application/Octet-Stream attachment: gemm-1.tar.gz_

             reply	other threads:[~1998-11-30 14:19 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-11-30 14:19 Kazushige Goto [this message]
1998-12-01 13:31 ` how to optimize dgemm? No.1 Joachim Wesner
1998-12-09 14:14 ` how to optimize dgemm? No.2 Kazushige Goto
1998-12-10  3:43   ` naohiko shimizu
1998-12-18 15:06   ` how to optimize dgemm? No.3 Kazushige Goto
1998-12-20 14:43     ` how to optimize dgemm? No.4 Kazushige Goto
1998-12-21 12:23       ` how to optimize dgemm? No.5 Kazushige Goto
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