Actual source code: ex42.c
2: static char help[] = "Solves a linear system in parallel with MINRES. Modified from ../tutorials/ex2.c \n\n";
4: #include <petscksp.h>
6: int main(int argc, char **args)
7: {
8: Vec x, b; /* approx solution, RHS */
9: Mat A; /* linear system matrix */
10: KSP ksp; /* linear solver context */
11: PetscInt Ii, Istart, Iend, m = 11;
12: PetscScalar v;
14: PetscFunctionBeginUser;
15: PetscCall(PetscInitialize(&argc, &args, (char *)0, help));
16: PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL));
18: /* Create parallel diagonal matrix */
19: PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
20: PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, m, m));
21: PetscCall(MatSetFromOptions(A));
22: PetscCall(MatMPIAIJSetPreallocation(A, 1, NULL, 1, NULL));
23: PetscCall(MatSeqAIJSetPreallocation(A, 1, NULL));
24: PetscCall(MatSetUp(A));
25: PetscCall(MatGetOwnershipRange(A, &Istart, &Iend));
27: for (Ii = Istart; Ii < Iend; Ii++) {
28: v = (PetscReal)Ii + 1;
29: PetscCall(MatSetValues(A, 1, &Ii, 1, &Ii, &v, INSERT_VALUES));
30: }
31: /* Make A sigular */
32: Ii = m - 1; /* last diagonal entry */
33: v = 0.0;
34: PetscCall(MatSetValues(A, 1, &Ii, 1, &Ii, &v, INSERT_VALUES));
35: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
36: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
38: PetscCall(MatCreateVecs(A, &x, &b));
39: PetscCall(VecSet(x, 1.0));
40: PetscCall(MatMult(A, x, b));
41: PetscCall(VecSet(x, 0.0));
43: /* Create linear solver context */
44: PetscCall(KSPCreate(PETSC_COMM_WORLD, &ksp));
45: PetscCall(KSPSetOperators(ksp, A, A));
46: PetscCall(KSPSetFromOptions(ksp));
47: PetscCall(KSPSolve(ksp, b, x));
48: PetscCall(KSPSolve(ksp, b, x));
50: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
51: Check solution and clean up
52: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
53: PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD));
55: /* Free work space. */
56: PetscCall(KSPDestroy(&ksp));
57: PetscCall(VecDestroy(&x));
58: PetscCall(VecDestroy(&b));
59: PetscCall(MatDestroy(&A));
61: PetscCall(PetscFinalize());
62: return 0;
63: }
65: /*TEST
67: test:
68: args: -ksp_type minres -pc_type none -ksp_converged_reason
70: test:
71: suffix: 2
72: nsize: 3
73: args: -ksp_type minres -pc_type none -ksp_converged_reason
75: TEST*/