Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 15th, 2012  |  syntax: None  |  size: 10.61 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. PETSc solving linear system with ksp guide
  2. /* Program usage:  mpiexec -n <procs> ex2 [-help] [all PETSc options] */
  3.  
  4. static char help[] = "Solves a linear system in parallel with KSP.n
  5. Input parameters include:n
  6.   -random_exact_sol : use a random exact solution vectorn
  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";
  10.  
  11. /*T
  12.    Concepts: KSP^basic parallel example;
  13.    Concepts: KSP^Laplacian, 2d
  14.    Concepts: Laplacian, 2d
  15.    Processors: n
  16. T*/
  17.  
  18. /*
  19.   Include "petscksp.h" so that we can use KSP solvers.  Note that this file
  20.   automatically includes:
  21.      petscsys.h       - base PETSc routines   petscvec.h - vectors
  22.      petscmat.h - matrices
  23.      petscis.h     - index sets            petscksp.h - Krylov subspace methods
  24.      petscviewer.h - viewers               petscpc.h  - preconditioners
  25. */
  26. #include <C:PETSCincludepetscksp.h>
  27.  
  28. #undef __FUNCT__
  29. #define __FUNCT__ "main"
  30. int main(int argc,char **args)
  31. {
  32.   Vec            x,b,u;  /* approx solution, RHS, exact solution */
  33.   Mat            A;        /* linear system matrix */
  34.   KSP            ksp;     /* linear solver context */
  35.   PetscRandom    rctx;     /* random number generator context */
  36.   PetscReal      norm;     /* norm of solution error */
  37.   PetscInt       i,j,Ii,J,Istart,Iend,m = 8,n = 7,its;
  38.   PetscErrorCode ierr;
  39.   PetscBool      flg = PETSC_FALSE;
  40.   PetscScalar    v;
  41. #if defined(PETSC_USE_LOG)
  42.   PetscLogStage  stage;
  43. #endif
  44.  
  45.   PetscInitialize(&argc,&args,(char *)0,help);
  46.   ierr = PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);CHKERRQ(ierr);
  47.   ierr = PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);CHKERRQ(ierr);
  48.   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  49.          Compute the matrix and right-hand-side vector that define
  50.          the linear system, Ax = b.
  51.      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  52.   /*
  53.      Create parallel matrix, specifying only its global dimensions.
  54.      When using MatCreate(), the matrix format can be specified at
  55.      runtime. Also, the parallel partitioning of the matrix is
  56.      determined by PETSc at runtime.
  57.  
  58.      Performance tuning note:  For problems of substantial size,
  59.      preallocation of matrix memory is crucial for attaining good
  60.      performance. See the matrix chapter of the users manual for details.
  61.   */
  62.   ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
  63.   ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);CHKERRQ(ierr);
  64.   ierr = MatSetFromOptions(A);CHKERRQ(ierr);
  65.   ierr = MatMPIAIJSetPreallocation(A,5,PETSC_NULL,5,PETSC_NULL);CHKERRQ(ierr);
  66.   ierr = MatSeqAIJSetPreallocation(A,5,PETSC_NULL);CHKERRQ(ierr);
  67.  
  68.   /*
  69.      Currently, all PETSc parallel matrix formats are partitioned by
  70.      contiguous chunks of rows across the processors.  Determine which
  71.      rows of the matrix are locally owned.
  72.   */
  73.   ierr = MatGetOwnershipRange(A,&Istart,&Iend);CHKERRQ(ierr);
  74.  
  75.   /*
  76.      Set matrix elements for the 2-D, five-point stencil in parallel.
  77.       - Each processor needs to insert only elements that it owns
  78.         locally (but any non-local elements will be sent to the
  79.         appropriate processor during matrix assembly).
  80.       - Always specify global rows and columns of matrix entries.
  81.  
  82.      Note: this uses the less common natural ordering that orders first
  83.      all the unknowns for x = h then for x = 2h etc; Hence you see J = Ii +- n
  84.      instead of J = I +- m as you might expect. The more standard ordering
  85.      would first do all variables for y = h, then y = 2h etc.
  86.  
  87.    */
  88.   ierr = PetscLogStageRegister("Assembly", &stage);CHKERRQ(ierr);
  89.   ierr = PetscLogStagePush(stage);CHKERRQ(ierr);
  90.   for (Ii=Istart; Ii<Iend; Ii++) {
  91.     v = -1.0; i = Ii/n; j = Ii - i*n;  
  92.     if (i>0)   {J = Ii - n; ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);}
  93.     if (i<m-1) {J = Ii + n; ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);}
  94.     if (j>0)   {J = Ii - 1; ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);}
  95.     if (j<n-1) {J = Ii + 1; ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);}
  96.     v = 4.0; ierr = MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES);CHKERRQ(ierr);
  97.   }
  98.  
  99.   /*
  100.      Assemble matrix, using the 2-step process:
  101.        MatAssemblyBegin(), MatAssemblyEnd()
  102.      Computations can be done while messages are in transition
  103.      by placing code between these two statements.
  104.   */
  105.   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
  106.   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
  107.   ierr = PetscLogStagePop();CHKERRQ(ierr);
  108.  
  109.   /* A is symmetric. Set symmetric flag to enable ICC/Cholesky preconditioner */
  110.   ierr = MatSetOption(A,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
  111.  
  112.   /*
  113.      Create parallel vectors.
  114.       - We form 1 vector from scratch and then duplicate as needed.
  115.       - When using VecCreate(), VecSetSizes and VecSetFromOptions()
  116.         in this example, we specify only the
  117.         vector's global dimension; the parallel partitioning is determined
  118.         at runtime.
  119.       - When solving a linear system, the vectors and matrices MUST
  120.         be partitioned accordingly.  PETSc automatically generates
  121.         appropriately partitioned matrices and vectors when MatCreate()
  122.         and VecCreate() are used with the same communicator.  
  123.       - The user can alternatively specify the local vector and matrix
  124.         dimensions when more sophisticated partitioning is needed
  125.         (replacing the PETSC_DECIDE argument in the VecSetSizes() statement
  126.         below).
  127.   */
  128.   ierr = VecCreate(PETSC_COMM_WORLD,&u);CHKERRQ(ierr);
  129.   ierr = VecSetSizes(u,PETSC_DECIDE,m*n);CHKERRQ(ierr);
  130.   ierr = VecSetFromOptions(u);CHKERRQ(ierr);
  131.   ierr = VecDuplicate(u,&b);CHKERRQ(ierr);
  132.   ierr = VecDuplicate(b,&x);CHKERRQ(ierr);
  133.  
  134.   /*
  135.      Set exact solution; then compute right-hand-side vector.
  136.      By default we use an exact solution of a vector with all
  137.      elements of 1.0;  Alternatively, using the runtime option
  138.      -random_sol forms a solution vector with random components.
  139.   */
  140.   ierr = PetscOptionsGetBool(PETSC_NULL,"-random_exact_sol",&flg,PETSC_NULL);CHKERRQ(ierr);
  141.   if (flg) {
  142.     ierr = PetscRandomCreate(PETSC_COMM_WORLD,&rctx);CHKERRQ(ierr);
  143.     ierr = PetscRandomSetFromOptions(rctx);CHKERRQ(ierr);
  144.     ierr = VecSetRandom(u,rctx);CHKERRQ(ierr);
  145.     ierr = PetscRandomDestroy(&rctx);CHKERRQ(ierr);
  146.   } else {
  147.     ierr = VecSet(u,1.0);CHKERRQ(ierr);
  148.   }
  149.   ierr = MatMult(A,u,b);CHKERRQ(ierr);
  150.  
  151.   /*
  152.      View the exact solution vector if desired
  153.   */
  154.   flg  = PETSC_FALSE;
  155.   ierr = PetscOptionsGetBool(PETSC_NULL,"-view_exact_sol",&flg,PETSC_NULL);CHKERRQ(ierr);
  156.   if (flg) {ierr = VecView(u,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);}
  157.  
  158.   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  159.                 Create the linear solver and set various options
  160.      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  161.  
  162.   /*
  163.      Create linear solver context
  164.   */
  165.   ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr);
  166.  
  167.   /*
  168.      Set operators. Here the matrix that defines the linear system
  169.      also serves as the preconditioning matrix.
  170.   */
  171.   ierr = KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);
  172.  
  173.   /*
  174.      Set linear solver defaults for this problem (optional).
  175.      - By extracting the KSP and PC contexts from the KSP context,
  176.        we can then directly call any KSP and PC routines to set
  177.        various options.
  178.      - The following two statements are optional; all of these
  179.        parameters could alternatively be specified at runtime via
  180.        KSPSetFromOptions().  All of these defaults can be
  181.        overridden at runtime, as indicated below.
  182.   */
  183.   ierr = KSPSetTolerances(ksp,1.e-2/((m+1)*(n+1)),1.e-50,PETSC_DEFAULT,
  184.                           PETSC_DEFAULT);CHKERRQ(ierr);
  185.  
  186.   /*
  187.     Set runtime options, e.g.,
  188.         -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
  189.     These options will override those specified above as long as
  190.     KSPSetFromOptions() is called _after_ any other customization
  191.     routines.
  192.   */
  193.   ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
  194.  
  195.   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  196.                       Solve the linear system
  197.      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  198.  
  199.   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
  200.  
  201.   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  202.                       Check solution and clean up
  203.      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  204.  
  205.   /*
  206.      Check the error
  207.   */
  208.   ierr = VecAXPY(x,-1.0,u);CHKERRQ(ierr);
  209.   ierr = VecNorm(x,NORM_2,&norm);CHKERRQ(ierr);
  210.   ierr = KSPGetIterationNumber(ksp,&its);CHKERRQ(ierr);
  211.   /* Scale the norm */
  212.   /*  norm *= sqrt(1.0/((m+1)*(n+1))); */
  213.  
  214.   /*
  215.      Print convergence information.  PetscPrintf() produces a single
  216.      print statement from all processes that share a communicator.
  217.      An alternative is PetscFPrintf(), which prints to a file.
  218.   */
  219.   ierr = PetscPrintf(PETSC_COMM_WORLD,"Norm of error %A iterations %Dn",
  220.                      norm,its);CHKERRQ(ierr);
  221.  
  222.   /*
  223.      Free work space.  All PETSc objects should be destroyed when they
  224.      are no longer needed.
  225.   */
  226.   ierr = KSPDestroy(&ksp);CHKERRQ(ierr);
  227.   ierr = VecDestroy(&u);CHKERRQ(ierr);  ierr = VecDestroy(&x);CHKERRQ(ierr);
  228.   ierr = VecDestroy(&b);CHKERRQ(ierr);  ierr = MatDestroy(&A);CHKERRQ(ierr);
  229.  
  230.   /*
  231.      Always call PetscFinalize() before exiting a program.  This routine
  232.        - finalizes the PETSc libraries as well as MPI
  233.        - provides summary and diagnostic information if certain runtime
  234.          options are chosen (e.g., -log_summary).
  235.   */
  236.   ierr = PetscFinalize();
  237.   return 0;
  238. }
  239.        
  240. ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
  241.        
  242. ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);CHKERRQ(ierr);
  243.        
  244. ierr = MatSetFromOptions(A);CHKERRQ(ierr);
  245.        
  246. ierr = MatMPIAIJSetPreallocation(A,5,PETSC_NULL,5,PETSC_NULL);CHKERRQ(ierr);
  247.   ierr = MatSeqAIJSetPreallocation(A,5,PETSC_NULL);CHKERRQ(ierr);
  248.        
  249. ierr = MatGetOwnershipRange(A,&Istart,&Iend);CHKERRQ(ierr);
  250.        
  251. for (Ii=Istart; Ii<Iend; Ii++) {
  252.     v = -1.0; i = Ii/n; j = Ii - i*n;
  253.        
  254. J = Ii - n; ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);
  255.     J = Ii + n; ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);
  256.     J = Ii - 1; ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);
  257.     J = Ii + 1; ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);
  258.  
  259.     v = 4.0; ierr = MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES);CHKERRQ(ierr);
  260.   }
  261.        
  262. ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
  263.   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);