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: PetscInitialize(&argc,&args,(char*)0,help);
15: PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
17: /* Create parallel diagonal matrix */
18: MatCreate(PETSC_COMM_WORLD,&A);
19: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m,m);
20: MatSetFromOptions(A);
21: MatMPIAIJSetPreallocation(A,1,NULL,1,NULL);
22: MatSeqAIJSetPreallocation(A,1,NULL);
23: MatSetUp(A);
24: MatGetOwnershipRange(A,&Istart,&Iend);
26: for (Ii=Istart; Ii<Iend; Ii++) {
27: v = (PetscReal)Ii+1;
28: MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES);
29: }
30: /* Make A sigular */
31: Ii = m - 1; /* last diagonal entry */
32: v = 0.0;
33: MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES);
34: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
35: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
37: /* A is symmetric. Set symmetric flag to enable KSP_type = minres */
38: MatSetOption(A,MAT_SYMMETRIC,PETSC_TRUE);
40: VecCreate(PETSC_COMM_WORLD,&b);
41: VecSetSizes(b,PETSC_DECIDE,m);
42: VecSetFromOptions(b);
43: VecDuplicate(b,&x);
44: VecSet(x,1.0);
45: MatMult(A,x,b);
46: VecSet(x,0.0);
48: /* Create linear solver context */
49: KSPCreate(PETSC_COMM_WORLD,&ksp);
50: KSPSetOperators(ksp,A,A);
51: KSPSetFromOptions(ksp);
52: KSPSolve(ksp,b,x);
54: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
55: Check solution and clean up
56: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
57: VecView(x,PETSC_VIEWER_STDOUT_WORLD);
59: /* Free work space. */
60: KSPDestroy(&ksp);
61: VecDestroy(&x);
62: VecDestroy(&b);
63: MatDestroy(&A);
65: PetscFinalize();
66: return 0;
67: }
69: /*TEST
71: test:
72: args: -ksp_type minres -pc_type none -ksp_converged_reason
74: test:
75: suffix: 2
76: nsize: 3
77: args: -ksp_type minres -pc_type none -ksp_converged_reason
79: TEST*/