home *** CD-ROM | disk | FTP | other *** search
- From: gajdos@hpavla.lf.hp.com (Larry Gajdos)
- Date: Thu, 19 Nov 1992 15:13:40 GMT
- Subject: Re: Reasons for using C vs. Fortran or vice/versa
- Message-ID: <9130057@hpavla.lf.hp.com>
- Organization: Hewlett-Packard Little Falls Site
- Path: sparky!uunet!cs.utexas.edu!sdd.hp.com!apollo.hp.com!cupnews0.cup.hp.com!nsa.hp.com!hpscit.sc.hp.com!scd.hp.com!hpscdm!hplextra!hpcss01!hpwala!hpavla!gajdos
- Newsgroups: comp.lang.c
- References: <1992Nov12.135901.15191@ccd.harris.com>
- Lines: 87
-
- In comp.lang.c, rons@hardy.u.washington.edu (Ronald Schoenberg) writes:
-
- > In article <gay.721614205@sfu.ca> gay@selkirk.sfu.ca (Ian D. Gay) writes:
- > >rons@hardy.u.washington.edu (Ronald Schoenberg) writes:
- > >
- > >> Fortran arrays are column echelon for a
- > >>reason, and C arrays are row echelon slowing down matrix operations
- > >>relative to Fortran. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- > >
- > >Maybe I'm missing something, but I don't see why this should be so.
- > >Could you explain more fully?
- > >
- > >
- > >>Ron Schoenberg
-
- > The most important matrix operation in numerical work is the gaxpy
- > ("generalized a times x plus y"). Numerical routines are written to
- > be rich in gaxpies (see Matrix Computations by Golub and Van Loan for
- > details). In the source code below I have written two gaxpies, one
- > for data stored column echelon and a second for the same data stored
- > row echelon. The only difference between the two is in the
- > calculation of the index of the element of the two dimensional array
- > used in the multiplication. You will note that the index in the
- > column echelon storage is a simple increment, whereas in the row
- > echelon storage two operations in addition to the assignment are
- > required. I know very little about assembly code, but I believe that an
- > assembly version of the column echelon gaxpy is especially efficient,
- > while the assembly code for row echelon would involve a number of extra
- > operations.
-
- A simple rearrangment of the row echelon calculation is shown below. This
- seems to show that there is no difference! Or am I missing something?
-
- > #include <stdio.h>
-
- > void main()
- > {
- > float a_col[] = { 1,5,9,13,2,6,10,14,3,7,11,15,4,8,12,16 };
- > float a_row[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
- > float x[] = { 1,2,3,4 };
- > float z[4];
-
- > int m = 4, n = 4;
- > int i, j, k;
-
- > /*
- > ** column echelon gaxpy
- > */
- > for (i = 0; i < m; i++) {
- > z[i] = 0;
- > k = i;
- > for (j = 0; j < n; j++) {
- > z[i] += a_col[k] * x[j];
- > k += n;
- > }
- > }
-
- > /*
- > ** row echelon gaxpy
- > */
- > for (i = 0; i < m; i++) {
- > z[i] = 0;
- > for (j = 0; j < n; j++) {
- > k = i * n + j;
- > z[i] += a_row[k] * x[j];
- > }
- > }
-
- /***** Redo the above row echelon gaxpy: *****/
-
- k = 0;
- for (i = 0; i < m; i++) {
- z[i] = 0;
- /***** k will be n*i on entry to loop *****/
- for (j = 0; j < n; j++) {
- /***** k will be n*i + j on each pass thru loop *****/
- z[i] += a_row[k] * x[j];
- k++;
- }
- }
-
- > }
-
-
- If anything, the code will be faster for the modfied row echelon calculation.
-
- Larry Gajdos
-