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.
[A, b, startrow, nrows, ncols, nonzeros, x0] =
getAb(fname_A, fname_b, fname_xinit, fname_solution)
reads the files with the given names. startrow is the
starting row number for the block row
that a process is assigned. In general that would be rank*n/p + 1.
For a serial version, startrow = 1.
Notice that the row numbers are indexed starting with 1, not 0.
If you have trouble with returning memory allocated in a
subprogram, add a task input to getAB:
task = 1: only read the first line: nrows, ncols, nnz, startrow
[allocate the arrays in the main program here]
task = 2: read the full data
but you should not need to do that.
[maxits, tol] = setCGparameters()
[d, r0, rho] = InitializeCG(A, b, x0, nrow, ncols, nnz, startrow)This carries out iteration 0, before the while loop that does most of the iteration.
[x, pltret] = cg(A, b, x0, maxits, tol, nrow, ncols, nnz, startrow)implements the main loop.
OK = checksolution(A, b, x)
OK = writesolutionfile(x)
The Matlab code is not broken down that way (it was never intended to be parallel). The files are
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.
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.