Actual source code: ex17.c

  1: /*$Id: ex17.c,v 1.21 2001/08/07 21:30:54 bsmith Exp $*/

  3: /* Usage:  mpirun ex2 [-help] [all PETSc options] */

  5: static char help[] = "Solves a linear system in parallel with SLES.n
  6: Input parameters include:n
  7:   -view_exact_sol   : write exact solution vector to stdoutn
  8:   -m <mesh_x>       : number of mesh points in x-directionn
  9:   -n <mesh_n>       : number of mesh points in y-directionnn";

 11: /*T
 12:    Concepts: Laplacian, 2d
 13:    Processors: n
 14: T*/

 16: /* 
 17:   Include "petscsles.h" so that we can use SLES solvers.  Note that this file
 18:   automatically includes:
 19:      petsc.h       - base PETSc routines   petscvec.h - vectors
 20:      petscsys.h    - system routines       petscmat.h - matrices
 21:      petscis.h     - index sets            petscksp.h - Krylov subspace methods
 22:      petscviewer.h - viewers               petscpc.h  - preconditioners
 23: */
 24:  #include petscsles.h

 26: int main(int argc,char **args)
 27: {
 28:   Vec         x,b,u;  /* approx solution, RHS, exact solution */
 29:   Mat         A;        /* linear system matrix */
 30:   SLES        sles;     /* linear solver context */
 31:   PetscRandom rctx;     /* random number generator context */
 32:   PetscReal   norm;     /* norm of solution error */
 33:   int         i,I,Istart,Iend,ierr,m = 5,n = 5,its,*cols;
 34:   PetscScalar neg_one = -1.0,*ua;
 35:   PetscTruth  flg;

 37:   PetscInitialize(&argc,&args,(char *)0,help);
 38:   PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);
 39:   PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
 40:   PetscPrintf(PETSC_COMM_WORLD,"system size: m=%d, n=%dn",m,n);
 41:   if (m < n) SETERRQ(1,"Supports m >= n only!");

 43:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 44:          Compute the matrix and right-hand-side vector that define
 45:          the linear system, Ax = b.
 46:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 48:   /* 
 49:      Create parallel vectors.
 50:       - When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
 51:         we specify only the vector's global
 52:         dimension; the parallel partitioning is determined at runtime. 
 53:       - When solving a linear system, the vectors and matrices MUST
 54:         be partitioned accordingly.  PETSc automatically generates
 55:         appropriately partitioned matrices and vectors when MatCreate()
 56:         and VecCreate() are used with the same communicator. 
 57:       - Note: We form 1 vector from scratch and then duplicate as needed.
 58:   */
 59:   VecCreate(PETSC_COMM_WORLD,&u);
 60:   VecSetSizes(u,PETSC_DECIDE,n);
 61:   VecSetFromOptions(u);
 62:   VecDuplicate(u,&x);
 63:   VecCreate(PETSC_COMM_WORLD,&b);
 64:   VecSetSizes(b,PETSC_DECIDE,m);
 65:   VecSetFromOptions(b);

 67:   /* 
 68:      Set exact solution with random components.
 69:   */
 70:   PetscRandomCreate(PETSC_COMM_WORLD,RANDOM_DEFAULT,&rctx);
 71:   VecSetRandom(rctx,u);
 72:   VecAssemblyBegin(u);
 73:   VecAssemblyEnd(u);

 75:   /* 
 76:      Create parallel matrix, specifying only its global dimensions.
 77:      When using MatCreate(), the matrix format can be specified at
 78:      runtime. Also, the parallel partitioning of the matrix is
 79:      determined by PETSc at runtime.
 80:   */
 81:   MatCreate(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,m,n,&A);
 82:   MatSetFromOptions(A);

 84:   /* 
 85:      Currently, all PETSc parallel matrix formats are partitioned by
 86:      contiguous chunks of rows across the processors.  Determine which
 87:      rows of the matrix are locally owned. 
 88:   */
 89:   MatGetOwnershipRange(A,&Istart,&Iend);

 91:   /* 
 92:      Set matrix elements in parallel.
 93:       - Each processor needs to insert only elements that it owns
 94:         locally (but any non-local elements will be sent to the
 95:         appropriate processor during matrix assembly). 
 96:       - Always specify global rows and columns of matrix entries.
 97:    */
 98:   VecGetArray(u,&ua);
 99:   PetscMalloc(n*sizeof(int),&cols);
100:   for (i=0; i<n; i++) {
101:     cols[i] = i;
102:   }
103:   for (I=Istart; I<Iend; I++) {
104:     VecSetRandom(rctx,u);
105:     MatSetValues(A,1,&I,n,cols,ua,INSERT_VALUES);
106:   }

108:   /* 
109:      Assemble matrix, using the 2-step process:
110:        MatAssemblyBegin(), MatAssemblyEnd()
111:      Computations can be done while messages are in transition
112:      by placing code between these two statements.
113:   */
114:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
115:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

117:   /*
118:       Compute right-hand-side vector.
119:   */
120:   MatMult(A,u,b);

122:   /*
123:      View the exact solution vector if desired
124:   */
125:   PetscOptionsHasName(PETSC_NULL,"-view_exact_sol",&flg);
126:   if (flg) {VecView(u,PETSC_VIEWER_STDOUT_WORLD);}

128:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
129:                 Create the linear solver and set various options
130:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

132:   /* 
133:      Create linear solver context
134:   */
135:   SLESCreate(PETSC_COMM_WORLD,&sles);

137:   /* 
138:      Set operators. Here the matrix that defines the linear system
139:      also serves as the preconditioning matrix.
140:   */
141:   SLESSetOperators(sles,A,A,DIFFERENT_NONZERO_PATTERN);

143:   /* 
144:     Set runtime options, e.g.,
145:         -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
146:     These options will override those specified above as long as
147:     SLESSetFromOptions() is called _after_ any other customization
148:     routines.
149:   */
150:   SLESSetFromOptions(sles);

152:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
153:                       Solve the linear system
154:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

156:   SLESSolve(sles,b,x,&its);

158:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
159:                       Check solution and clean up
160:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

162:   /* 
163:      Check the error
164:   */
165:   VecAXPY(&neg_one,u,x);
166:   VecNorm(x,NORM_2,&norm);

168:   /*
169:      Print convergence information.  PetscPrintf() produces a single 
170:      print statement from all processes that share a communicator.
171:   */
172:   PetscPrintf(PETSC_COMM_WORLD,"Norm of error %A iterations %dn",norm,its);

174:   /* 
175:      Free work space.  All PETSc objects should be destroyed when they
176:      are no longer needed.
177:   */
178:   SLESDestroy(sles);
179:   VecDestroy(u);  VecDestroy(x);
180:   VecDestroy(b);  MatDestroy(A);
181:   PetscRandomDestroy(rctx);
182:   PetscFree(cols);

184:   /*
185:      Always call PetscFinalize() before exiting a program.  This routine
186:        - finalizes the PETSc libraries as well as MPI
187:        - provides summary and diagnostic information if certain runtime
188:          options are chosen (e.g., -log_summary). 
189:   */
190:   PetscFinalize();
191:   return 0;
192: }