Actual source code: ex16.c


  2: /* Usage:  mpiexec ex16 [-help] [all PETSc options] */

  4: static char help[] = "Solves a sequence of linear systems with different right-hand-side vectors.\n\
  5: Input parameters include:\n\
  6:   -ntimes <ntimes>  : number of linear systems to solve\n\
  7:   -view_exact_sol   : write exact solution vector to stdout\n\
  8:   -m <mesh_x>       : number of mesh points in x-direction\n\
  9:   -n <mesh_y>       : number of mesh points in y-direction\n\n";

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

 21: int main(int argc,char **args)
 22: {
 23:   Vec            x,b,u;  /* approx solution, RHS, exact solution */
 24:   Mat            A;        /* linear system matrix */
 25:   KSP            ksp;     /* linear solver context */
 26:   PetscReal      norm;     /* norm of solution error */
 27:   PetscInt       ntimes,i,j,k,Ii,J,Istart,Iend;
 28:   PetscInt       m   = 8,n = 7,its;
 29:   PetscBool      flg = PETSC_FALSE;
 30:   PetscScalar    v,one = 1.0,rhs;

 32:   PetscInitialize(&argc,&args,(char*)0,help);
 33:   PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
 34:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);

 36:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 37:          Compute the matrix for use in solving a series of
 38:          linear systems of the form, A x_i = b_i, for i=1,2,...
 39:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 40:   /*
 41:      Create parallel matrix, specifying only its global dimensions.
 42:      When using MatCreate(), the matrix format can be specified at
 43:      runtime. Also, the parallel partitioning of the matrix is
 44:      determined by PETSc at runtime.
 45:   */
 46:   MatCreate(PETSC_COMM_WORLD,&A);
 47:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
 48:   MatSetFromOptions(A);
 49:   MatSetUp(A);

 51:   /*
 52:      Currently, all PETSc parallel matrix formats are partitioned by
 53:      contiguous chunks of rows across the processors.  Determine which
 54:      rows of the matrix are locally owned.
 55:   */
 56:   MatGetOwnershipRange(A,&Istart,&Iend);

 58:   /*
 59:      Set matrix elements for the 2-D, five-point stencil in parallel.
 60:       - Each processor needs to insert only elements that it owns
 61:         locally (but any non-local elements will be sent to the
 62:         appropriate processor during matrix assembly).
 63:       - Always specify global rows and columns of matrix entries.
 64:    */
 65:   for (Ii=Istart; Ii<Iend; Ii++) {
 66:     v = -1.0; i = Ii/n; j = Ii - i*n;
 67:     if (i>0)   {J = Ii - n; MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);}
 68:     if (i<m-1) {J = Ii + n; MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);}
 69:     if (j>0)   {J = Ii - 1; MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);}
 70:     if (j<n-1) {J = Ii + 1; MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);}
 71:     v = 4.0; MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES);
 72:   }

 74:   /*
 75:      Assemble matrix, using the 2-step process:
 76:        MatAssemblyBegin(), MatAssemblyEnd()
 77:      Computations can be done while messages are in transition
 78:      by placing code between these two statements.
 79:   */
 80:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 81:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 83:   /*
 84:      Create parallel vectors.
 85:       - When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
 86:         we specify only the vector's global
 87:         dimension; the parallel partitioning is determined at runtime.
 88:       - When solving a linear system, the vectors and matrices MUST
 89:         be partitioned accordingly.  PETSc automatically generates
 90:         appropriately partitioned matrices and vectors when MatCreate()
 91:         and VecCreate() are used with the same communicator.
 92:       - Note: We form 1 vector from scratch and then duplicate as needed.
 93:   */
 94:   VecCreate(PETSC_COMM_WORLD,&u);
 95:   VecSetSizes(u,PETSC_DECIDE,m*n);
 96:   VecSetFromOptions(u);
 97:   VecDuplicate(u,&b);
 98:   VecDuplicate(b,&x);

100:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
101:                 Create the linear solver and set various options
102:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

104:   /*
105:      Create linear solver context
106:   */
107:   KSPCreate(PETSC_COMM_WORLD,&ksp);

109:   /*
110:      Set operators. Here the matrix that defines the linear system
111:      also serves as the preconditioning matrix.
112:   */
113:   KSPSetOperators(ksp,A,A);

115:   /*
116:     Set runtime options, e.g.,
117:         -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
118:     These options will override those specified above as long as
119:     KSPSetFromOptions() is called _after_ any other customization
120:     routines.
121:   */
122:   KSPSetFromOptions(ksp);

124:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
125:        Solve several linear systems of the form  A x_i = b_i
126:        I.e., we retain the same matrix (A) for all systems, but
127:        change the right-hand-side vector (b_i) at each step.

129:        In this case, we simply call KSPSolve() multiple times.  The
130:        preconditioner setup operations (e.g., factorization for ILU)
131:        be done during the first call to KSPSolve() only; such operations
132:        will NOT be repeated for successive solves.
133:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

135:   ntimes = 2;
136:   PetscOptionsGetInt(NULL,NULL,"-ntimes",&ntimes,NULL);
137:   for (k=1; k<ntimes+1; k++) {

139:     /*
140:        Set exact solution; then compute right-hand-side vector.  We use
141:        an exact solution of a vector with all elements equal to 1.0*k.
142:     */
143:     rhs  = one * (PetscReal)k;
144:     VecSet(u,rhs);
145:     MatMult(A,u,b);

147:     /*
148:        View the exact solution vector if desired
149:     */
150:     PetscOptionsGetBool(NULL,NULL,"-view_exact_sol",&flg,NULL);
151:     if (flg) VecView(u,PETSC_VIEWER_STDOUT_WORLD);

153:     KSPSolve(ksp,b,x);

155:     /*
156:        Check the error
157:     */
158:     VecAXPY(x,-1.0,u);
159:     VecNorm(x,NORM_2,&norm);
160:     KSPGetIterationNumber(ksp,&its);
161:     /*
162:        Print convergence information.  PetscPrintf() produces a single
163:        print statement from all processes that share a communicator.
164:     */
165:     PetscPrintf(PETSC_COMM_WORLD,"Norm of error %g System %D: iterations %D\n",(double)norm,k,its);
166:   }

168:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
169:                       Clean up
170:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
171:   /*
172:      Free work space.  All PETSc objects should be destroyed when they
173:      are no longer needed.
174:   */
175:   KSPDestroy(&ksp);
176:   VecDestroy(&u));  PetscCall(VecDestroy(&x);
177:   VecDestroy(&b));  PetscCall(MatDestroy(&A);

179:   /*
180:      Always call PetscFinalize() before exiting a program.  This routine
181:        - finalizes the PETSc libraries as well as MPI
182:        - provides summary and diagnostic information if certain runtime
183:          options are chosen (e.g., -log_view).
184:   */
185:   PetscFinalize();
186:   return 0;
187: }

189: /*TEST

191:    test:
192:       nsize: 2
193:       args: -ntimes 4 -ksp_gmres_cgs_refinement_type refine_always

195: TEST*/