Conjugate Gradient Method

First write a serial vesion of CG that works correctly. Because we know a priori that a parallel version will be needed, here's how I would break down the code into functions/subroutines. This uses Matlab syntax, which allows multiple return arguments from function. If you use C, you'll have to modify things to use addresses.


The Matlab code is not broken down that way (it was never intended to be parallel). The files are

Three other files included are

New Matlab file additions

  1. 08 Apr 2009: have put an additional tarball with setup methods This creates a directory parsetup, with one new file: SetupParallel.m. This does not run the CG algorithm, all it does is create symmetric positive definite systems and write them out to files with the naming convention coeffmatrix001, coeffmatrix002, ..., coeffmatrix***. Those files you can then ship over to odin via scp, and put one in /tmp on each node you have an allocation for. That is a temporary file location and gets cleaned up periodically, but is guaranteed to be on each node's local hard drive, not on a network mounted filesystem. Then your MPI code can each read in the data it needs from /tmp/coeffmatrix*** in parallel. The rhs vector is also partitioned, but the initial x is not - still, every process needs a copy of the full x vector.

  2. 08 Apr 2009: Update to writearray.m: SetupParallel.m calls a version of writearray.m that is not the same as the original one listed above. This one takes a third (optional) argument to allow specifying where the row numbering will start when arrays are being written out. You can use this in place of the older one, since this rowoffset argument is an optional one. But the older one will not work if called from SetupParallel.m.

  3. Added, 09 Apr 2009: A version of the Matlab linear system generator that uses sparse matrices. This is one that you can use to generate large systems - it took me less than 40 seconds to create the files for a 60001 x 60001 matrix partitioned into 64 block rows. Storage for that problem size is about 7.4 Mbytes. The systems are stored in COO format: a header line with nrow, ncols, nnz, and startrow number, followed by all the nonzero entries, one per line. Each line has a row number, a column number, and the value. All of this is 1-based indexing.

    Beware one thing: some of the same names have been set up for the dense and sparse versions, because the Matlab code is nearly identical in either case (and cg.m required zero changes, since in Matlab w = A*d works correctly for dense or sparse matrices A). Notice that the Matlab codes are only for creating test cases for a parallel code to work on - Matlab is not parallel itself. The utilities in some of the files below may help you in debugging, but you do not have to implement anything but solver, and a driver that reads in the partitioned systems and writes out the solution and residual history.

    1. The entire tarball is in parsetup2.tar which creates a subdirectory called parsetup2.
    2. SetupParallel.m has only minor differences from the previous one, but does have some efficiency considerations in addition to using sparse matrices.
    3. setA.m similarly barely differs from the previous version. Do a diff (or better yet, an xxdiff) on the files to see what changed.
    4. writearraysparse.m required only four line changes from the original writearray.m. To keep both available, the new parsetup2 directory has the dense matrix version in writearraydense.m, with a link to the actual one to use.

  4. Added, 09 Apr 2009: to go along with the sparse matrix generator and partitioner, a set of Matlab codes that read in, assemble the matrix A from the partitioned files, and then runs CG is tar'd up in assemble.tar. It contains

    1. runcg.m which calls
    2. readsystem.m that in turn uses
    3. getarray.m and
    4. getparameters.m

    You do not need to use the codes in the "assemble" directory - they are just there to help with any debugging you need to do.


More Example Files

A PDE discretizer in Matlab has been added for creating sparse matrices. However, you need to edit it to produce the separate files coeffmat.001, coeffmat.002, ...


  • Edited, Thu 09 Apr 2009, 03:07 PM to point out addenda.
  • Edited, 2009-Apr-21 18:22:40 to add a serial CG tar ball.


  • Started 06 Apr 2009
  • Modified 07 Apr 2009, to have links to all files.
  • Modified: Tue 07 Apr 2009, 05:12 PM to include files that only write out the system and don't call cg.m
  • Modified: Wed 08 Apr 2009, 04:54 PM to put SystemSetup.m into tar file, and rename it SetupParallel.m.
  • Modified: 09 Apr 2009 to add sparse matrix generators.
  • Modified: Tue 21 Apr 2009, 06:27 PM to include a directory with a BLAS-linking makefile.
  • Modified: Tue 21 Apr 2009, 06:37 PM
  • Last Modified: Wed 29 Apr 2009, 04:43 PM