import java.io.*; public class Driver { /* Driver routine -- test out the code on a sample problem */ public static void driver(int n, int single maxiter, Sparse aMat, Preconditioner pMat) { try { double [1d] single [1d] b = new double [0:Ti.numProcs()-1][1d]; double [1d] single [1d] x = new double [0:Ti.numProcs()-1][1d]; double [1d] rhist = new double [0:maxiter-1]; int myCount = PVector.numPer(n); int myLow = PVector.lowPer(n); b.exchange(new double [myLow:myLow+myCount-1]); x.exchange(new double [myLow:myLow+myCount-1]); double single rtol = 1e-3; int i, retval; DataOutputStream rhistOutFile; DataOutputStream xOutFile; if (Ti.thisProc() == 0) { try { rhistOutFile = new DataOutputStream(new FileOutputStream("rhist.out")); xOutFile = new DataOutputStream(new FileOutputStream("x.out")); } catch (IOException e) { System.out.println("Unable to open output files."); System.exit(1); } } b[Ti.thisProc()].set(1); retval = CG.precondCG(aMat, pMat, b, x, rtol, n, rhist, maxiter); if (Ti.thisProc() == 0) { for (int p = 0; p < Ti.numProcs(); p++) { for (i = x[p].domain().min()[1]; i <= x[p].domain().max()[1]; i++) { xOutFile.writeBytes(Double.toString(x[p][i]) + "\n"); } } } int lastIter; if (Ti.thisProc() == 0) { if (retval < 0) { lastIter = maxiter; System.out.println("Iteration failed to converge!\n"); } else { lastIter = retval; System.out.println("Converged after " + retval + " iterations."); } for (i = 0; i < lastIter; ++i) { rhistOutFile.writeBytes(Double.toString(rhist[i])+"\n"); } rhistOutFile.close(); xOutFile.close(); } } catch (IOException e) { System.out.println("Unable to open or use output files: " + e); return; } } public static single void main(String [] args) { int single maxIter = 500; int n; Sparse aMat; Preconditioner pMat; MatMarket single aMM; MatMarket aMMOnZero; if (Ti.thisProc() == 0) { if (args.length <= 0 || Character.isDigit(args[0].charAt(0))) { if (args.length <= 0) { n = 10; } else { try { n = Integer.parseInt(args[0]); } catch (Exception e) { System.out.println("Unable to parse argument, using default."); n = 10; } } System.out.println("Using 1-d Poisson on a " + n + " point mesh.\n"); aMat = null; // new Poisson1D(n) if parallelized pMat = null; // put preconditioner here if one exists. } else { try { aMMOnZero = MMIO.CSRLoad(args[0]); n = aMMOnZero.n; } catch (IOException e) { System.out.println(e); System.exit(1); } pMat = null; System.out.println("Using problem " + args[0]); } } aMM = broadcast aMMOnZero from 0; if (aMM.n == 0) { // no matrix was created, so give up return; } n = broadcast n from 0; aMat = new CSR(broadcast aMM.entries from 0, aMM.n); driver(n, maxIter, aMat, new IdentityPrecond()); /* Add this code to run with preconditioner if (Ti.thisProc() == 0) { System.out.println("Using block Jacobi Preconditioner\n"); } driver(n, maxIter, aMat, pMat); */ } }