Actual source code: ex23.c
2: /* Program usage: mpirun ex23 [-help] [all PETSc options] */
4: static char help[] = "Solves a tridiagonal linear system.\n\n";
6: /*T
7: Concepts: KSP^basic parallel example;
8: Processors: n
9: T*/
11: /*
12: Include "petscksp.h" so that we can use KSP solvers. Note that this file
13: automatically includes:
14: petsc.h - base PETSc routines petscvec.h - vectors
15: petscsys.h - system routines petscmat.h - matrices
16: petscis.h - index sets petscksp.h - Krylov subspace methods
17: petscviewer.h - viewers petscpc.h - preconditioners
19: Note: The corresponding uniprocessor example is ex1.c
20: */
21: #include petscksp.h
25: int main(int argc,char **args)
26: {
27: Vec x, b, u; /* approx solution, RHS, exact solution */
28: Mat A; /* linear system matrix */
29: KSP ksp; /* linear solver context */
30: PC pc; /* preconditioner context */
31: PetscReal norm; /* norm of solution error */
33: PetscInt i,n = 10,col[3],its,rstart,rend,nlocal;
34: PetscScalar neg_one = -1.0,one = 1.0,value[3];
36: PetscInitialize(&argc,&args,(char *)0,help);
37: PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
39: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
40: Compute the matrix and right-hand-side vector that define
41: the linear system, Ax = b.
42: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
44: /*
45: Create vectors. Note that we form 1 vector from scratch and
46: then duplicate as needed. For this simple case let PETSc decide how
47: many elements of the vector are stored on each processor. The second
48: argument to VecSetSizes() below causes PETSc to decide.
49: */
50: VecCreate(PETSC_COMM_WORLD,&x);
51: VecSetSizes(x,PETSC_DECIDE,n);
52: VecSetFromOptions(x);
53: VecDuplicate(x,&b);
54: VecDuplicate(x,&u);
56: /* Identify the starting and ending mesh points on each
57: processor for the interior part of the mesh. We let PETSc decide
58: above. */
60: VecGetOwnershipRange(x,&rstart,&rend);
61: VecGetLocalSize(x,&nlocal);
63: /*
64: Create matrix. When using MatCreate(), the matrix format can
65: be specified at runtime.
67: Performance tuning note: For problems of substantial size,
68: preallocation of matrix memory is crucial for attaining good
69: performance. Since preallocation is not possible via the generic
70: matrix creation routine MatCreate(), we recommend for practical
71: problems instead to use the creation routine for a particular matrix
72: format, e.g.,
73: MatCreateMPIAIJ() - sequential AIJ (compressed sparse row)
74: MatCreateMPIBAIJ() - block AIJ
75: See the matrix chapter of the users manual for details.
77: We pass in nlocal as the "local" size of the matrix to force it
78: to have the same parallel layout as the vector created above.
79: */
80: MatCreate(PETSC_COMM_WORLD,&A);
81: MatSetSizes(A,nlocal,nlocal,n,n);
82: MatSetFromOptions(A);
84: /*
85: Assemble matrix.
87: The linear system is distributed across the processors by
88: chunks of contiguous rows, which correspond to contiguous
89: sections of the mesh on which the problem is discretized.
90: For matrix assembly, each processor contributes entries for
91: the part that it owns locally.
92: */
95: if (!rstart) {
96: rstart = 1;
97: i = 0; col[0] = 0; col[1] = 1; value[0] = 2.0; value[1] = -1.0;
98: MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
99: }
100: if (rend == n) {
101: rend = n-1;
102: i = n-1; col[0] = n-2; col[1] = n-1; value[0] = -1.0; value[1] = 2.0;
103: MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
104: }
106: /* Set entries corresponding to the mesh interior */
107: value[0] = -1.0; value[1] = 2.0; value[2] = -1.0;
108: for (i=rstart; i<rend; i++) {
109: col[0] = i-1; col[1] = i; col[2] = i+1;
110: MatSetValues(A,1,&i,3,col,value,INSERT_VALUES);
111: }
113: /* Assemble the matrix */
114: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
115: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
117: /*
118: Set exact solution; then compute right-hand-side vector.
119: */
120: VecSet(u,one);
121: MatMult(A,u,b);
123: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
124: Create the linear solver and set various options
125: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
126: /*
127: Create linear solver context
128: */
129: KSPCreate(PETSC_COMM_WORLD,&ksp);
131: /*
132: Set operators. Here the matrix that defines the linear system
133: also serves as the preconditioning matrix.
134: */
135: KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);
137: /*
138: Set linear solver defaults for this problem (optional).
139: - By extracting the KSP and PC contexts from the KSP context,
140: we can then directly call any KSP and PC routines to set
141: various options.
142: - The following four statements are optional; all of these
143: parameters could alternatively be specified at runtime via
144: KSPSetFromOptions();
145: */
146: KSPGetPC(ksp,&pc);
147: PCSetType(pc,PCJACOBI);
148: KSPSetTolerances(ksp,1.e-7,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);
150: /*
151: Set runtime options, e.g.,
152: -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
153: These options will override those specified above as long as
154: KSPSetFromOptions() is called _after_ any other customization
155: routines.
156: */
157: KSPSetFromOptions(ksp);
158:
159: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
160: Solve the linear system
161: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
162: /*
163: Solve linear system
164: */
165: KSPSolve(ksp,b,x);
167: /*
168: View solver info; we could instead use the option -ksp_view to
169: print this info to the screen at the conclusion of KSPSolve().
170: */
171: KSPView(ksp,PETSC_VIEWER_STDOUT_WORLD);
173: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
174: Check solution and clean up
175: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
176: /*
177: Check the error
178: */
179: VecAXPY(x,neg_one,u);
180: VecNorm(x,NORM_2,&norm);
181: KSPGetIterationNumber(ksp,&its);
182: PetscPrintf(PETSC_COMM_WORLD,"Norm of error %A, Iterations %D\n",norm,its);
183: /*
184: Free work space. All PETSc objects should be destroyed when they
185: are no longer needed.
186: */
187: VecDestroy(x); VecDestroy(u);
188: VecDestroy(b); MatDestroy(A);
189: KSPDestroy(ksp);
191: /*
192: Always call PetscFinalize() before exiting a program. This routine
193: - finalizes the PETSc libraries as well as MPI
194: - provides summary and diagnostic information if certain runtime
195: options are chosen (e.g., -log_summary).
196: */
197: PetscFinalize();
198: return 0;
199: }