Actual source code: ex7.c
2: static char help[] = "Reads a PETSc matrix and vector from a file and solves a linear system.\n\
3: Tests inplace factorization for SeqBAIJ. Input parameters include\n\
4: -f0 <input_file> : first file to load (small system)\n\n";
6: /*T
7: Concepts: KSP^solving a linear system
8: Concepts: PetscLog^profiling multiple stages of code;
9: Processors: n
10: T*/
12: /*
13: Include "petscksp.h" so that we can use KSP solvers. Note that this file
14: automatically includes:
15: petsc.h - base PETSc routines petscvec.h - vectors
16: petscsys.h - system routines petscmat.h - matrices
17: petscis.h - index sets petscksp.h - Krylov subspace methods
18: petscviewer.h - viewers petscpc.h - preconditioners
19: */
20: #include petscksp.h
24: int main(int argc,char **args)
25: {
26: KSP ksp; /* linear solver context */
27: Mat A,B; /* matrix */
28: Vec x,b,u; /* approx solution, RHS, exact solution */
29: PetscViewer fd; /* viewer */
30: char file[2][PETSC_MAX_PATH_LEN]; /* input file name */
32: PetscInt its;
33: PetscTruth flg;
34: PetscReal norm;
35: PetscScalar zero = 0.0,none = -1.0;
37: PetscInitialize(&argc,&args,(char *)0,help);
39: /*
40: Determine files from which we read the two linear systems
41: (matrix and right-hand-side vector).
42: */
43: PetscOptionsGetString(PETSC_NULL,"-f0",file[0],PETSC_MAX_PATH_LEN-1,&flg);
44: if (!flg) SETERRQ(1,"Must indicate binary file with the -f0 option");
47: /*
48: Open binary file. Note that we use PETSC_FILE_RDONLY to indicate
49: reading from this file.
50: */
51: PetscViewerBinaryOpen(PETSC_COMM_WORLD,file[0],PETSC_FILE_RDONLY,&fd);
53: /*
54: Load the matrix and vector; then destroy the viewer.
55: */
56: MatLoad(fd,MATSEQBAIJ,&A);
57: MatConvert(A,MATSAME,MAT_INITIAL_MATRIX,&B);
58: VecLoad(fd,PETSC_NULL,&b);
59: PetscViewerDestroy(fd);
61: /*
62: If the loaded matrix is larger than the vector (due to being padded
63: to match the block size of the system), then create a new padded vector.
64: */
65: {
66: PetscInt m,n,j,mvec,start,end,idx;
67: Vec tmp;
68: PetscScalar *bold;
70: /* Create a new vector b by padding the old one */
71: MatGetLocalSize(A,&m,&n);
72: VecCreate(PETSC_COMM_WORLD,&tmp);
73: VecSetSizes(tmp,m,PETSC_DECIDE);
74: VecSetFromOptions(tmp);
75: VecGetOwnershipRange(b,&start,&end);
76: VecGetLocalSize(b,&mvec);
77: VecGetArray(b,&bold);
78: for (j=0; j<mvec; j++) {
79: idx = start+j;
80: VecSetValues(tmp,1,&idx,bold+j,INSERT_VALUES);
81: }
82: VecRestoreArray(b,&bold);
83: VecDestroy(b);
84: VecAssemblyBegin(tmp);
85: VecAssemblyEnd(tmp);
86: b = tmp;
87: }
88: VecDuplicate(b,&x);
89: VecDuplicate(b,&u);
90: VecSet(x,zero);
92: /*
93: Create linear solver; set operators; set runtime options.
94: */
95: KSPCreate(PETSC_COMM_WORLD,&ksp);
96: KSPSetOperators(ksp,A,B,DIFFERENT_NONZERO_PATTERN);
97: KSPSetFromOptions(ksp);
99: /*
100: Here we explicitly call KSPSetUp() and KSPSetUpOnBlocks() to
101: enable more precise profiling of setting up the preconditioner.
102: These calls are optional, since both will be called within
103: KSPSolve() if they haven't been called already.
104: */
105: KSPSetUp(ksp);
106: KSPSetUpOnBlocks(ksp);
107: KSPSolve(ksp,b,x);
109: /*
110: Check error, print output, free data structures.
111: This stage is not profiled separately.
112: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
114: /*
115: Check error
116: */
117: MatMult(A,x,u);
118: VecAXPY(u,none,b);
119: VecNorm(u,NORM_2,&norm);
120: KSPGetIterationNumber(ksp,&its);
121: PetscPrintf(PETSC_COMM_WORLD,"Number of iterations = %3D\n",its);
122: PetscPrintf(PETSC_COMM_WORLD,"Residual norm = %A\n",norm);
124: /*
125: Free work space. All PETSc objects should be destroyed when they
126: are no longer needed.
127: */
128: MatDestroy(A);
129: MatDestroy(B);
130: VecDestroy(b);
131: VecDestroy(u); VecDestroy(x);
132: KSPDestroy(ksp);
135: PetscFinalize();
136: return 0;
137: }