home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!gatech!hubcap!mjs
- From: mjs@hubcap.clemson.edu (M. J. Saltzman)
- Subject: Re: Reasons for using C vs. Fortran or vic
- Message-ID: <1992Nov15.224010.13609@hubcap.clemson.edu>
- Organization: Clemson University, Clemson SC
- References: <1992Nov12.205823.13951@u.washington.edu> <1992Nov13.162451.13049@zia.aoc.nrao.edu> <1992Nov15.201555.20678@thunder.mcrcim.mcgill.edu>
- Date: Sun, 15 Nov 1992 22:40:10 GMT
- Lines: 51
-
- In article <1992Nov15.201555.20678@thunder.mcrcim.mcgill.edu> mouse@thunder.mcrcim.mcgill.edu (der Mouse) writes:
- >In article <1992Nov13.162451.13049@zia.aoc.nrao.edu>, cflatter@nrao.edu (Chris Flatters) writes:
- >
- >> Storing arrays by column or by row doesn't make any difference
- >> whatsoever to the speed of matrix operations: it is merely a notation
- >> change.
- >
- >This is true only in the abstract. I have experienced (not just heard
- >about) multiple orders of magnitude speed differences, because going
- >through arrays "the wrong way" had catastrohpically bad VM behavior:
- >essentially, every access involved a page fault. Doing it "the right
- >way" stepped through VM in a nice sequential way, with minimal paging.
-
- Note that you do have to adjust the details of the access pattern to
- match the storage patter, in order to not suffer performance penalties
- because of lack of locality. For example, if you are storing matrices
- in row-major order, you should perform matrix-vector multiplication as
- a sequence of inner products of the vector with the rows of the
- matrix:
-
- /* Compute y = y + Ax, accessing A in row-major order */
-
- for i = 1 to m do
- for j = 1 to n do
- y[i] = y[i] + A[i][j] * x[j]
-
- If your matrices are stored in column-major order, you should
- perform matrix-vector multiplication by summing scalar multiples of
- the columns of the matrix:
-
- /* Compute y = y + Ax, accessing A in column-major order */
-
- for j = 1 to n do
- for i = 1 to m do
- y[i] = y[i] + A[i][j] * x[j]
-
- Thus, there is a "right way" for either storage pattern, but they are
- different.
-
- Now, if you are doing matrix-matrix multiplication for matrices stored
- in column-major order, the "right way" is a natural extension of the
- second algorithm above (i.e., C = AB is computed by multiplying A by
- successive columns of B). If your matrices are in row-major order,
- the interpretation of the appropriate sequential-access algorithm is
- not so straightforward. I conjecture that this is why Fortran
- designers chose column-major storage for matrices. Once that's done,
- the extension to more than two dimensions is pretty much dictated.
- --
- Matthew Saltzman
- Clemson University Math Sciences
- mjs@clemson.edu
-