home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!europa.asd.contel.com!darwin.sura.net!wupost!uwm.edu!ogicse!news.u.washington.edu!hardy.u.washington.edu!rons
- From: rons@hardy.u.washington.edu (Ronald Schoenberg)
- Newsgroups: comp.lang.c
- Subject: Re: Reasons for using C vs. Fortran or vice/versa
- Message-ID: <1992Nov13.060701.6917@u.washington.edu>
- Date: 13 Nov 92 06:07:01 GMT
- Article-I.D.: u.1992Nov13.060701.6917
- References: <1992Nov12.135901.15191@ccd.harris.com> <1992Nov12.205823.13951@u.washington.edu> <gay.721614205@sfu.ca>
- Sender: news@u.washington.edu (USENET News System)
- Distribution: usa
- Organization: University of Washington, Seattle
- Lines: 81
-
- 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.
-
- As I'm sure you well know, if you were to actually use the two
- dimensional arrays in C (I avoid it myself), the generated
- assembly code would contain the extra operations because data in
- two-dimensional C arrays are row echelon. C programmers have
- dispensed with one additional operation by using the zero offset.
- But for really efficient numerical work that is not enough.
-
- Actually I program in C++ using the M++ matrix class library.
- M++ is based on the BLAS (Basic Linear Algebra Subroutines) which are
- most efficient when data are stored column echelon, and therefore
- M++ stores its data column echelon. But I don't have
- to think very much about how the data are stored because that is all
- taken care of for me behind the scenes (viva la C++).
-
- I apologize for any faux pas in the code below - I am a converted
- Fortran number-cruncher.
-
-
- #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];
- }
- }
- }
-