Actual source code: ex7.c
petsc-3.8.4 2018-03-24
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: petscsys.h - base PETSc routines petscvec.h - vectors
16: 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>
22: int main(int argc,char **args)
23: {
24: KSP ksp; /* linear solver context */
25: Mat A,B; /* matrix */
26: Vec x,b,u; /* approx solution, RHS, exact solution */
27: PetscViewer fd; /* viewer */
28: char file[2][PETSC_MAX_PATH_LEN]; /* input file name */
30: PetscInt its;
31: PetscBool flg;
32: PetscReal norm;
34: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
35: /*
36: Determine files from which we read the two linear systems
37: (matrix and right-hand-side vector).
38: */
39: PetscOptionsGetString(NULL,NULL,"-f0",file[0],PETSC_MAX_PATH_LEN,&flg);
40: if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f0 option");
43: /*
44: Open binary file. Note that we use FILE_MODE_READ to indicate
45: reading from this file.
46: */
47: PetscViewerBinaryOpen(PETSC_COMM_WORLD,file[0],FILE_MODE_READ,&fd);
49: /*
50: Load the matrix and vector; then destroy the viewer.
51: */
52: MatCreate(PETSC_COMM_WORLD,&A);
53: MatSetType(A,MATSEQBAIJ);
54: MatLoad(A,fd);
55: MatConvert(A,MATSAME,MAT_INITIAL_MATRIX,&B);
56: VecCreate(PETSC_COMM_WORLD,&b);
57: VecLoad(b,fd);
58: PetscViewerDestroy(&fd);
60: /*
61: If the loaded matrix is larger than the vector (due to being padded
62: to match the block size of the system), then create a new padded vector.
63: */
64: {
65: PetscInt m,n,j,mvec,start,end,idx;
66: Vec tmp;
67: PetscScalar *bold;
69: /* Create a new vector b by padding the old one */
70: MatGetLocalSize(A,&m,&n);
71: VecCreate(PETSC_COMM_WORLD,&tmp);
72: VecSetSizes(tmp,m,PETSC_DECIDE);
73: VecSetFromOptions(tmp);
74: VecGetOwnershipRange(b,&start,&end);
75: VecGetLocalSize(b,&mvec);
76: VecGetArray(b,&bold);
77: for (j=0; j<mvec; j++) {
78: idx = start+j;
79: VecSetValues(tmp,1,&idx,bold+j,INSERT_VALUES);
80: }
81: VecRestoreArray(b,&bold);
82: VecDestroy(&b);
83: VecAssemblyBegin(tmp);
84: VecAssemblyEnd(tmp);
85: b = tmp;
86: }
87: VecDuplicate(b,&x);
88: VecDuplicate(b,&u);
89: VecSet(x,0.0);
91: /*
92: Create linear solver; set operators; set runtime options.
93: */
94: KSPCreate(PETSC_COMM_WORLD,&ksp);
95: KSPSetOperators(ksp,A,B);
96: KSPSetFromOptions(ksp);
98: /*
99: Here we explicitly call KSPSetUp() and KSPSetUpOnBlocks() to
100: enable more precise profiling of setting up the preconditioner.
101: These calls are optional, since both will be called within
102: KSPSolve() if they haven't been called already.
103: */
104: KSPSetUp(ksp);
105: KSPSetUpOnBlocks(ksp);
106: KSPSolve(ksp,b,x);
108: /*
109: Check error, print output, free data structures.
110: This stage is not profiled separately.
111: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
113: /*
114: Check error
115: */
116: MatMult(A,x,u);
117: VecAXPY(u,-1.0,b);
118: VecNorm(u,NORM_2,&norm);
119: KSPGetIterationNumber(ksp,&its);
120: PetscPrintf(PETSC_COMM_WORLD,"Number of iterations = %3D\n",its);
121: PetscPrintf(PETSC_COMM_WORLD,"Residual norm = %g\n",(double)norm);
123: /*
124: Free work space. All PETSc objects should be destroyed when they
125: are no longer needed.
126: */
127: MatDestroy(&A);
128: MatDestroy(&B);
129: VecDestroy(&b);
130: VecDestroy(&u); VecDestroy(&x);
131: KSPDestroy(&ksp);
134: PetscFinalize();
135: return ierr;
136: }