Actual source code: ex12.c

      2: /* Program usage:  mpirun -np <procs> ex12 [-help] [all PETSc options] */

      4: static char help[] = "Solves a linear system in parallel with KSP.\n\
      5: Input parameters include:\n\
      6:   -m <mesh_x>       : number of mesh points in x-direction\n\
      7:   -n <mesh_n>       : number of mesh points in y-direction\n\n";

      9: /*T
     10:    Concepts: KSP^solving a system of linear equations
     11:    Concepts: KSP^Laplacian, 2d
     12:    Concepts: PC^registering preconditioners
     13:    Processors: n
     14: T*/

     16: /*
     17:    Demonstrates registering a new preconditioner (PC) type.

     19:    To register a PC type whose code is linked into the executable,
     20:    use PCRegister(). To register a PC type in a dynamic library use PCRegisterDynamic()

     22:    Also provide the prototype for your PCCreate_XXX() function. In
     23:    this example we use the PETSc implementation of the Jacobi method,
     24:    PCCreate_Jacobi() just as an example.

     26:    See the file src/ksp/pc/impls/jacobi/jacobi.c for details on how to
     27:    write a new PC component.

     29:    See the manual page PCRegisterDynamic() for details on how to register a method.
     30: */

     32: /*
     33:   Include "petscksp.h" so that we can use KSP solvers.  Note that this file
     34:   automatically includes:
     35:      petsc.h       - base PETSc routines   petscvec.h - vectors
     36:      petscsys.h    - system routines       petscmat.h - matrices
     37:      petscis.h     - index sets            petscksp.h - Krylov subspace methods
     38:      petscviewer.h - viewers               petscpc.h  - preconditioners
     39: */
     40:  #include petscksp.h

     43: EXTERN PetscErrorCode PCCreate_Jacobi(PC);

     48: int main(int argc,char **args)
     49: {
     50:   Vec            x,b,u;  /* approx solution, RHS, exact solution */
     51:   Mat            A;        /* linear system matrix */
     52:   KSP            ksp;     /* linear solver context */
     53:   PetscReal      norm;     /* norm of solution error */
     54:   PetscInt       i,j,I,J,Istart,Iend,m = 8,n = 7,its;
     56:   PetscScalar    v,one = 1.0,neg_one = -1.0;
     57:   PC             pc;      /* preconditioner context */

     59:   PetscInitialize(&argc,&args,(char *)0,help);
     60:   PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);
     61:   PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);

     63:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     64:          Compute the matrix and right-hand-side vector that define
     65:          the linear system, Ax = b.
     66:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     67:   /*
     68:      Create parallel matrix, specifying only its global dimensions.
     69:      When using MatCreate(), the matrix format can be specified at
     70:      runtime. Also, the parallel partitioning of the matrix is
     71:      determined by PETSc at runtime.
     72:   */
     73:   MatCreate(PETSC_COMM_WORLD,&A);
     74:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
     75:   MatSetFromOptions(A);

     77:   /*
     78:      Currently, all PETSc parallel matrix formats are partitioned by
     79:      contiguous chunks of rows across the processors.  Determine which
     80:      rows of the matrix are locally owned.
     81:   */
     82:   MatGetOwnershipRange(A,&Istart,&Iend);

     84:   /*
     85:      Set matrix elements for the 2-D, five-point stencil in parallel.
     86:       - Each processor needs to insert only elements that it owns
     87:         locally (but any non-local elements will be sent to the
     88:         appropriate processor during matrix assembly).
     89:       - Always specify global rows and columns of matrix entries.
     90:    */
     91:   for (I=Istart; I<Iend; I++) {
     92:     v = -1.0; i = I/n; j = I - i*n;
     93:     if (i>0)   {J = I - n; MatSetValues(A,1,&I,1,&J,&v,INSERT_VALUES);}
     94:     if (i<m-1) {J = I + n; MatSetValues(A,1,&I,1,&J,&v,INSERT_VALUES);}
     95:     if (j>0)   {J = I - 1; MatSetValues(A,1,&I,1,&J,&v,INSERT_VALUES);}
     96:     if (j<n-1) {J = I + 1; MatSetValues(A,1,&I,1,&J,&v,INSERT_VALUES);}
     97:     v = 4.0; MatSetValues(A,1,&I,1,&I,&v,INSERT_VALUES);
     98:   }

    100:   /*
    101:      Assemble matrix, using the 2-step process:
    102:        MatAssemblyBegin(), MatAssemblyEnd()
    103:      Computations can be done while messages are in transition
    104:      by placing code between these two statements.
    105:   */
    106:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
    107:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

    109:   /*
    110:      Create parallel vectors.
    111:       - When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
    112:       we specify only the vector's global
    113:         dimension; the parallel partitioning is determined at runtime.
    114:       - When solving a linear system, the vectors and matrices MUST
    115:         be partitioned accordingly.  PETSc automatically generates
    116:         appropriately partitioned matrices and vectors when MatCreate()
    117:         and VecCreate() are used with the same communicator.
    118:       - Note: We form 1 vector from scratch and then duplicate as needed.
    119:   */
    120:   VecCreate(PETSC_COMM_WORLD,&u);
    121:   VecSetSizes(u,PETSC_DECIDE,m*n);
    122:   VecSetFromOptions(u);
    123:   VecDuplicate(u,&b);
    124:   VecDuplicate(b,&x);

    126:   /*
    127:      Set exact solution; then compute right-hand-side vector.
    128:      Use an exact solution of a vector with all elements of 1.0;
    129:   */
    130:   VecSet(u,one);
    131:   MatMult(A,u,b);

    133:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    134:                 Create the linear solver and set various options
    135:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    137:   /*
    138:      Create linear solver context
    139:   */
    140:   KSPCreate(PETSC_COMM_WORLD,&ksp);

    142:   /*
    143:      Set operators. Here the matrix that defines the linear system
    144:      also serves as the preconditioning matrix.
    145:   */
    146:   KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);

    148:   /*
    149:        First register a new PC type with the command PCRegister()
    150:   */
    151:   PCRegister("ourjacobi",0,"PCCreate_Jacobi",PCCreate_Jacobi);
    152:
    153:   /*
    154:      Set the PC type to be the new method
    155:   */
    156:   KSPGetPC(ksp,&pc);
    157:   PCSetType(pc,"ourjacobi");

    159:   /*
    160:     Set runtime options, e.g.,
    161:         -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
    162:     These options will override those specified above as long as
    163:     KSPSetFromOptions() is called _after_ any other customization
    164:     routines.
    165:   */
    166:   KSPSetFromOptions(ksp);

    168:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    169:                       Solve the linear system
    170:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    172:   KSPSolve(ksp,b,x);

    174:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    175:                       Check solution and clean up
    176:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    178:   /*
    179:      Check the error
    180:   */
    181:   VecAXPY(x,neg_one,u);
    182:   VecNorm(x,NORM_2,&norm);
    183:   KSPGetIterationNumber(ksp,&its);
    184:   /* Scale the norm */
    185:   /*  norm *= sqrt(1.0/((m+1)*(n+1))); */

    187:   /*
    188:      Print convergence information.  PetscPrintf() produces a single
    189:      print statement from all processes that share a communicator.
    190:   */
    191:   PetscPrintf(PETSC_COMM_WORLD,"Norm of error %A iterations %D\n",norm,its);

    193:   /*
    194:      Free work space.  All PETSc objects should be destroyed when they
    195:      are no longer needed.
    196:   */
    197:   KSPDestroy(ksp);
    198:   VecDestroy(u);  VecDestroy(x);
    199:   VecDestroy(b);  MatDestroy(A);

    201:   /*
    202:      Always call PetscFinalize() before exiting a program.  This routine
    203:        - finalizes the PETSc libraries as well as MPI
    204:        - provides summary and diagnostic information if certain runtime
    205:          options are chosen (e.g., -log_summary).
    206:   */
    207:   PetscFinalize();
    208:   return 0;
    209: }