function [score, time, niters] = harness3(n, whichstart)
% HARNESS3 : Test and validation harness for matlab cg solver
%
% [score, time, niters] = harness3(n, whichstart);
%
% Generate a sparse n-by-n matrix A and a dense n-by-1 vector b
% (default n=1000), run an external "cgsolve" routine to solve
% the system Ax = b for x by the conjugate gradient method
% (within a relative residual tolerance of 10^-6).
% Report timing, # of CG iterations, and correctness.
%
% "whichstart" (default 1) is an integer specifying
% which of several matrices and right-hand-sides to use.
%
% John R. Gilbert    12 Feb 2006

if nargin < 1
    n = 1000;
end;
if nargin < 2
    whichstart = 1;
end;

fprintf('Matlab test harness for Conjugate Gradient solver.\n');
fprintf('Number of processors = 1 (sequential Matlab code)\n');
fprintf('Matrix dimension = %d\n', n);
[A, b] = gendata3(n, whichstart);
tic;
[x, niters] = cgsolve(A, b);
time = toc;
fprintf('Elapsed time = %d\n', time);
fprintf('2-norm of solution = %d\n', norm(x));
fprintf('1-norm of solution = %d\n', norm(x,1));
fprintf('relative 2-norm of residual = %d\n', norm(A*x-b)/norm(b));
score = validate3(whichstart, x);
if score == 1
    fprintf('Answer is correct.\n');
elseif score == 0
    fprintf('Answer is not correct.\n');
end;