Actual source code: ex41.c

petsc-dev 2014-08-28
Report Typos and Errors
  2: static char help[] = "Reads a PETSc matrix and vector from a socket connection,  solves a linear system and sends the result back.\n";

  4: /*T
  5:    Concepts: KSP^solving a linear system
  6:    Processors: n
  7: T*/

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

 21: int main(int argc,char **args)
 22: {
 23:   KSP            ksp;             /* linear solver context */
 24:   Mat            A;            /* matrix */
 25:   Vec            x,b;          /* approx solution, RHS, exact solution */
 26:   PetscViewer    fd;               /* viewer */

 29:   PetscInitialize(&argc,&args,(char*)0,help);
 30:   fd = PETSC_VIEWER_SOCKET_WORLD;

 32:   VecCreate(PETSC_COMM_WORLD,&b);
 33:   VecLoad(b,fd);
 34:   MatCreate(PETSC_COMM_WORLD,&A);
 35:   MatLoad(A,fd);
 36:   VecDuplicate(b,&x);

 38:   KSPCreate(PETSC_COMM_WORLD,&ksp);
 39:   KSPSetOperators(ksp,A,A);
 40:   KSPSetFromOptions(ksp);
 41:   KSPSetUp(ksp);
 42:   KSPSolve(ksp,b,x);
 43:   VecView(x,fd);
 44:   MatDestroy(&A);
 45:   VecDestroy(&b);
 46:   VecDestroy(&x);
 47:   KSPDestroy(&ksp);

 49:   PetscFinalize();
 50:   return 0;
 51: }