Actual source code: ex42.c
petsc-3.4.2 2013-07-02

      2: static char help[] = "Solves a linear system in parallel with MINRES. Modified from ../tutorials/ex2.c \n\n";

      4: #include <petscksp.h>

      8: int main(int argc,char **args)
      9: {
     10:   Vec            x,b;      /* approx solution, RHS */
     11:   Mat            A;        /* linear system matrix */
     12:   KSP            ksp;      /* linear solver context */
     13:   PetscInt       Ii,Istart,Iend,m = 11;
     15:   PetscScalar    v;

     17:   PetscInitialize(&argc,&args,(char*)0,help);
     18:   PetscOptionsGetInt(NULL,"-m",&m,NULL);

     20:   /* Create parallel diagonal matrix */
     21:   MatCreate(PETSC_COMM_WORLD,&A);
     22:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m,m);
     23:   MatSetFromOptions(A);
     24:   MatMPIAIJSetPreallocation(A,1,NULL,1,NULL);
     25:   MatSeqAIJSetPreallocation(A,1,NULL);
     26:   MatSetUp(A);
     27:   MatGetOwnershipRange(A,&Istart,&Iend);

     29:   for (Ii=Istart; Ii<Iend; Ii++) {
     30:     v = (PetscReal)Ii+1;
     31:     MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES);
     32:   }
     33:   /* Make A sigular */
     34:   Ii = m - 1; /* last diagonal entry */
     35:   v  = 0.0;
     36:   MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES);
     37:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
     38:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

     40:   /* A is symmetric. Set symmetric flag to enable KSP_type = minres */
     41:   MatSetOption(A,MAT_SYMMETRIC,PETSC_TRUE);

     43:   VecCreate(PETSC_COMM_WORLD,&b);
     44:   VecSetSizes(b,PETSC_DECIDE,m);
     45:   VecSetFromOptions(b);
     46:   VecDuplicate(b,&x);
     47:   VecSet(b,1.0);
     48:   VecSet(x,0.0);

     50:   /* Create linear solver context */
     51:   KSPCreate(PETSC_COMM_WORLD,&ksp);
     52:   KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);
     53:   KSPSetFromOptions(ksp);
     54:   KSPSolve(ksp,b,x);

     56:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     57:                       Check solution and clean up
     58:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     59:   VecView(x,PETSC_VIEWER_STDOUT_WORLD);

     61:   /* Free work space. */
     62:   KSPDestroy(&ksp);
     63:   VecDestroy(&x);
     64:   VecDestroy(&b);
     65:   MatDestroy(&A);

     67:   PetscFinalize();
     68:   return 0;
     69: }