Actual source code: ex41.c
1: static char help[] = "Reads a PETSc matrix and vector from a socket connection, solves a linear system and sends the result back.\n";
3: /*
4: Include "petscksp.h" so that we can use KSP solvers. Note that this file
5: automatically includes:
6: petscsys.h - base PETSc routines petscvec.h - vectors
7: petscmat.h - matrices
8: petscis.h - index sets petscksp.h - Krylov subspace methods
9: petscviewer.h - viewers petscpc.h - preconditioners
10: */
11: #include <petscksp.h>
13: int main(int argc, char **args)
14: {
15: KSP ksp; /* linear solver context */
16: Mat A; /* matrix */
17: Vec x, b; /* approx solution, RHS, exact solution */
18: PetscViewer fd; /* viewer */
20: PetscFunctionBeginUser;
21: PetscCall(PetscInitialize(&argc, &args, (char *)0, help));
22: fd = PETSC_VIEWER_SOCKET_WORLD;
24: PetscCall(VecCreate(PETSC_COMM_WORLD, &b));
25: PetscCall(VecLoad(b, fd));
26: PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
27: PetscCall(MatLoad(A, fd));
28: PetscCall(VecDuplicate(b, &x));
30: PetscCall(KSPCreate(PETSC_COMM_WORLD, &ksp));
31: PetscCall(KSPSetOperators(ksp, A, A));
32: PetscCall(KSPSetFromOptions(ksp));
33: PetscCall(KSPSetUp(ksp));
34: PetscCall(KSPSolve(ksp, b, x));
35: PetscCall(VecView(x, fd));
36: PetscCall(MatDestroy(&A));
37: PetscCall(VecDestroy(&b));
38: PetscCall(VecDestroy(&x));
39: PetscCall(KSPDestroy(&ksp));
41: PetscCall(PetscFinalize());
42: return 0;
43: }
45: /*TEST
47: build:
48: requires: defined(PETSC_USE_SOCKET_VIEWER)
50: test:
51: TODO: Need to figure out how to test examples that use sockets
53: TEST*/