Actual source code: ex27.c

petsc-master 2016-05-24
Report Typos and Errors
  2: static char help[] = "Reads a PETSc matrix and vector from a file and solves the normal equations.\n\n";
  3: /*T
  4:    Concepts: KSP^solving a linear system
  5:    Concepts: Normal equations
  6:    Processors: n
  7: T*/

  9: /*
 10:   Include "petscksp.h" so that we can use KSP solvers.  Note that this file
 11:   automatically includes:
 12:      petscsys.h       - base PETSc routines   petscvec.h - vectors
 13:      petscmat.h - matrices
 14:      petscis.h     - index sets            petscksp.h - Krylov subspace methods
 15:      petscviewer.h - viewers               petscpc.h  - preconditioners
 16: */
 17: #include <petscksp.h>


 22: int main(int argc,char **args)
 23: {
 24:   KSP            ksp;             /* linear solver context */
 25:   Mat            A,N;                /* matrix */
 26:   Vec            x,b,u,Ab;          /* approx solution, RHS, exact solution */
 27:   PetscViewer    fd;               /* viewer */
 28:   char           file[PETSC_MAX_PATH_LEN];     /* input file name */
 29:   PetscErrorCode ierr,ierrp;
 30:   PetscInt       its,n,m;
 31:   PetscReal      norm;

 33:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 34:   /*
 35:      Determine files from which we read the linear system
 36:      (matrix and right-hand-side vector).
 37:   */
 38:   PetscOptionsGetString(NULL,NULL,"-f",file,PETSC_MAX_PATH_LEN,NULL);

 40:   /* -----------------------------------------------------------
 41:                   Beginning of linear solver loop
 42:      ----------------------------------------------------------- */
 43:   /*
 44:      Loop through the linear solve 2 times.
 45:       - The intention here is to preload and solve a small system;
 46:         then load another (larger) system and solve it as well.
 47:         This process preloads the instructions with the smaller
 48:         system so that more accurate performance monitoring (via
 49:         -log_summary) can be done with the larger one (that actually
 50:         is the system of interest).
 51:   */
 52:   PetscPreLoadBegin(PETSC_FALSE,"Load system");

 54:   /* - - - - - - - - - - - New Stage - - - - - - - - - - - - -
 55:                          Load system
 56:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 58:   /*
 59:      Open binary file.  Note that we use FILE_MODE_READ to indicate
 60:      reading from this file.
 61:   */
 62:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);

 64:   /*
 65:      Load the matrix and vector; then destroy the viewer.
 66:   */
 67:   MatCreate(PETSC_COMM_WORLD,&A);
 68:   MatSetType(A,MATMPIAIJ);
 69:   MatLoad(A,fd);
 70:   VecCreate(PETSC_COMM_WORLD,&b);
 71:   PetscPushErrorHandler(PetscIgnoreErrorHandler,NULL);
 72:   ierrp = VecLoad(b,fd);
 73:   PetscPopErrorHandler();
 74:   MatGetLocalSize(A,&m,&n);
 75:   if (ierrp) {   /* if file contains no RHS, then use a vector of all ones */
 76:     PetscScalar one = 1.0;
 77:     VecCreate(PETSC_COMM_WORLD,&b);
 78:     VecSetSizes(b,m,PETSC_DECIDE);
 79:     VecSetFromOptions(b);
 80:     VecSet(b,one);
 81:   }
 82:   PetscViewerDestroy(&fd);


 85:   VecDuplicate(b,&u);
 86:   VecCreateMPI(PETSC_COMM_WORLD,n,PETSC_DECIDE,&x);
 87:   VecDuplicate(x,&Ab);
 88:   VecSet(x,0.0);

 90:   /* - - - - - - - - - - - New Stage - - - - - - - - - - - - -
 91:                     Setup solve for system
 92:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 94:   /*
 95:      Conclude profiling last stage; begin profiling next stage.
 96:   */
 97:   PetscPreLoadStage("KSPSetUp");

 99:   MatCreateNormal(A,&N);
100:   MatMultTranspose(A,b,Ab);

102:   /*
103:      Create linear solver; set operators; set runtime options.
104:   */
105:   KSPCreate(PETSC_COMM_WORLD,&ksp);

107:   KSPSetOperators(ksp,N,N);
108:   KSPSetFromOptions(ksp);

110:   /*
111:      Here we explicitly call KSPSetUp() and KSPSetUpOnBlocks() to
112:      enable more precise profiling of setting up the preconditioner.
113:      These calls are optional, since both will be called within
114:      KSPSolve() if they haven't been called already.
115:   */
116:   KSPSetUp(ksp);
117:   KSPSetUpOnBlocks(ksp);

119:   /*
120:                          Solve system
121:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

123:   /*
124:      Begin profiling next stage
125:   */
126:   PetscPreLoadStage("KSPSolve");

128:   /*
129:      Solve linear system
130:   */
131:   KSPSolve(ksp,Ab,x);

133:   /*
134:       Conclude profiling this stage
135:    */
136:   PetscPreLoadStage("Cleanup");

138:   /* - - - - - - - - - - - New Stage - - - - - - - - - - - - -
139:           Check error, print output, free data structures.
140:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

142:   /*
143:      Check error
144:   */
145:   MatMult(A,x,u);
146:   VecAXPY(u,-1.0,b);
147:   VecNorm(u,NORM_2,&norm);
148:   KSPGetIterationNumber(ksp,&its);
149:   PetscPrintf(PETSC_COMM_WORLD,"Number of iterations = %3D\n",its);
150:   PetscPrintf(PETSC_COMM_WORLD,"Residual norm %g\n",(double)norm);

152:   /*
153:      Free work space.  All PETSc objects should be destroyed when they
154:      are no longer needed.
155:   */
156:   MatDestroy(&A); VecDestroy(&b);
157:   MatDestroy(&N); VecDestroy(&Ab);
158:   VecDestroy(&u); VecDestroy(&x);
159:   KSPDestroy(&ksp);
160:   PetscPreLoadEnd();
161:   /* -----------------------------------------------------------
162:                       End of linear solver loop
163:      ----------------------------------------------------------- */

165:   PetscFinalize();
166:   return ierr;
167: }