Actual source code: ex40.c
2: static char help[] = "Solves a linear system in parallel with KSP.\n\
3: Input parameters include:\n\
4: -random_exact_sol : use a random exact solution vector\n\
5: -view_exact_sol : write exact solution vector to stdout\n\
6: -m <mesh_x> : number of mesh points in x-direction\n\
7: -n <mesh_y> : number of mesh points in y-direction\n\n";
9: /*
10: Include "petscksp.h" so that we can use KSP solvers. Note that this file
11: automatically includes:
12: petscsys.h - base PETSc routines petscvec.h - vectors
13: petscmat.h - matrices
14: petscis.h - index sets petscksp.h - Krylov subspace methods
15: petscviewer.h - viewers petscpc.h - preconditioners
16: */
17: #include <petscksp.h>
19: int main(int argc,char **args)
20: {
21: Vec x,b,u; /* approx solution, RHS, exact solution */
22: Mat A; /* linear system matrix */
23: KSP ksp; /* linear solver context */
24: PetscRandom rctx; /* random number generator context */
25: PetscReal norm; /* norm of solution error */
26: PetscInt i,j,Ii,J,m = 8,n = 7,its;
28: PetscBool flg = PETSC_FALSE;
29: PetscScalar v;
30: PetscMPIInt rank;
32: PetscInitialize(&argc,&args,(char*)0,help);
33: PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
34: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
35: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
36: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
37: Compute the matrix and right-hand-side vector that define
38: the linear system, Ax = b.
39: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
40: /*
41: Create parallel matrix, specifying only its global dimensions.
42: When using MatCreate(), the matrix format can be specified at
43: runtime. Also, the parallel partitioning of the matrix is
44: determined by PETSc at runtime.
46: Performance tuning note: For problems of substantial size,
47: preallocation of matrix memory is crucial for attaining good
48: performance. See the matrix chapter of the users manual for details.
49: */
50: MatCreate(PETSC_COMM_WORLD,&A);
51: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
52: MatSetType(A,MATELEMENTAL);
53: MatSetFromOptions(A);
54: MatSetUp(A);
55: if (rank==0) {
56: PetscInt M,N;
57: MatGetSize(A,&M,&N);
58: for (Ii=0; Ii<M; Ii++) {
59: v = -1.0; i = Ii/n; j = Ii - i*n;
60: if (i>0) {J = Ii - n; MatSetValues(A,1,&Ii,1,&J,&v,ADD_VALUES);}
61: if (i<m-1) {J = Ii + n; MatSetValues(A,1,&Ii,1,&J,&v,ADD_VALUES);}
62: if (j>0) {J = Ii - 1; MatSetValues(A,1,&Ii,1,&J,&v,ADD_VALUES);}
63: if (j<n-1) {J = Ii + 1; MatSetValues(A,1,&Ii,1,&J,&v,ADD_VALUES);}
64: v = 4.0; MatSetValues(A,1,&Ii,1,&Ii,&v,ADD_VALUES);
65: }
66: }
67: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
68: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
70: /* MatSetOption(A,MAT_SYMMETRIC,PETSC_TRUE); */
72: /*
73: Create parallel vectors.
74: - We form 1 vector from scratch and then duplicate as needed.
75: - When using VecCreate(), VecSetSizes and VecSetFromOptions()
76: in this example, we specify only the
77: vector's global dimension; the parallel partitioning is determined
78: at runtime.
79: - When solving a linear system, the vectors and matrices MUST
80: be partitioned accordingly. PETSc automatically generates
81: appropriately partitioned matrices and vectors when MatCreate()
82: and VecCreate() are used with the same communicator.
83: - The user can alternatively specify the local vector and matrix
84: dimensions when more sophisticated partitioning is needed
85: (replacing the PETSC_DECIDE argument in the VecSetSizes() statement
86: below).
87: */
88: VecCreate(PETSC_COMM_WORLD,&u);
89: VecSetSizes(u,PETSC_DECIDE,m*n);
90: VecSetFromOptions(u);
91: VecDuplicate(u,&b);
92: VecDuplicate(b,&x);
94: /*
95: Set exact solution; then compute right-hand-side vector.
96: By default we use an exact solution of a vector with all
97: elements of 1.0; Alternatively, using the runtime option
98: -random_sol forms a solution vector with random components.
99: */
100: PetscOptionsGetBool(NULL,NULL,"-random_exact_sol",&flg,NULL);
101: if (flg) {
102: PetscRandomCreate(PETSC_COMM_WORLD,&rctx);
103: PetscRandomSetFromOptions(rctx);
104: VecSetRandom(u,rctx);
105: PetscRandomDestroy(&rctx);
106: } else {
107: VecSet(u,1.0);
108: }
109: MatMult(A,u,b);
111: /*
112: View the exact solution vector if desired
113: */
114: flg = PETSC_FALSE;
115: PetscOptionsGetBool(NULL,NULL,"-view_exact_sol",&flg,NULL);
116: if (flg) VecView(u,PETSC_VIEWER_STDOUT_WORLD);
118: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
119: Create the linear solver and set various options
120: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
122: /*
123: Create linear solver context
124: */
125: KSPCreate(PETSC_COMM_WORLD,&ksp);
127: /*
128: Set operators. Here the matrix that defines the linear system
129: also serves as the preconditioning matrix.
130: */
131: KSPSetOperators(ksp,A,A);
133: /*
134: Set linear solver defaults for this problem (optional).
135: - By extracting the KSP and PC contexts from the KSP context,
136: we can then directly call any KSP and PC routines to set
137: various options.
138: - The following two statements are optional; all of these
139: parameters could alternatively be specified at runtime via
140: KSPSetFromOptions(). All of these defaults can be
141: overridden at runtime, as indicated below.
142: */
143: KSPSetTolerances(ksp,1.e-2/((m+1)*(n+1)),PETSC_DEFAULT,PETSC_DEFAULT,
144: PETSC_DEFAULT);
146: /*
147: Set runtime options, e.g.,
148: -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
149: These options will override those specified above as long as
150: KSPSetFromOptions() is called _after_ any other customization
151: routines.
152: */
153: KSPSetFromOptions(ksp);
155: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
156: Solve the linear system
157: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
159: KSPSolve(ksp,b,x);
161: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
162: Check solution and clean up
163: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
165: /*
166: Check the error
167: */
168: VecAXPY(x,-1.0,u);
169: VecNorm(x,NORM_2,&norm);
170: KSPGetIterationNumber(ksp,&its);
172: /*
173: Print convergence information. PetscPrintf() produces a single
174: print statement from all processes that share a communicator.
175: An alternative is PetscFPrintf(), which prints to a file.
176: */
177: PetscPrintf(PETSC_COMM_WORLD,"Norm of error %g iterations %D\n",(double)norm,its);
179: /*
180: Free work space. All PETSc objects should be destroyed when they
181: are no longer needed.
182: */
183: KSPDestroy(&ksp);
184: VecDestroy(&u)); PetscCall(VecDestroy(&x);
185: VecDestroy(&b)); PetscCall(MatDestroy(&A);
187: /*
188: Always call PetscFinalize() before exiting a program. This routine
189: - finalizes the PETSc libraries as well as MPI
190: - provides summary and diagnostic information if certain runtime
191: options are chosen (e.g., -log_view).
192: */
193: PetscFinalize();
194: return 0;
195: }
197: /*TEST
199: test:
200: nsize: 6
201: args: -pc_type none
202: requires: elemental
204: test:
205: suffix: 2
206: nsize: 6
207: args: -pc_type lu -pc_factor_mat_solver_type elemental
208: requires: elemental
210: TEST*/