Actual source code: ex41.c


  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: /*
  5:   Include "petscksp.h" so that we can use KSP solvers.  Note that this file
  6:   automatically includes:
  7:      petscsys.h       - base PETSc routines   petscvec.h - vectors
  8:      petscmat.h - matrices
  9:      petscis.h     - index sets            petscksp.h - Krylov subspace methods
 10:      petscviewer.h - viewers               petscpc.h  - preconditioners
 11: */
 12: #include <petscksp.h>

 14: int main(int argc, char **args)
 15: {
 16:   KSP         ksp;  /* linear solver context */
 17:   Mat         A;    /* matrix */
 18:   Vec         x, b; /* approx solution, RHS, exact solution */
 19:   PetscViewer fd;   /* viewer */

 21:   PetscFunctionBeginUser;
 22:   PetscCall(PetscInitialize(&argc, &args, (char *)0, help));
 23:   fd = PETSC_VIEWER_SOCKET_WORLD;

 25:   PetscCall(VecCreate(PETSC_COMM_WORLD, &b));
 26:   PetscCall(VecLoad(b, fd));
 27:   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
 28:   PetscCall(MatLoad(A, fd));
 29:   PetscCall(VecDuplicate(b, &x));

 31:   PetscCall(KSPCreate(PETSC_COMM_WORLD, &ksp));
 32:   PetscCall(KSPSetOperators(ksp, A, A));
 33:   PetscCall(KSPSetFromOptions(ksp));
 34:   PetscCall(KSPSetUp(ksp));
 35:   PetscCall(KSPSolve(ksp, b, x));
 36:   PetscCall(VecView(x, fd));
 37:   PetscCall(MatDestroy(&A));
 38:   PetscCall(VecDestroy(&b));
 39:   PetscCall(VecDestroy(&x));
 40:   PetscCall(KSPDestroy(&ksp));

 42:   PetscCall(PetscFinalize());
 43:   return 0;
 44: }

 46: /*TEST

 48:      build:
 49:        requires: defined(PETSC_USE_SOCKET_VIEWER)

 51:      test:
 52:        TODO: Need to figure out how to test examples that use sockets

 54: TEST*/