Actual source code: ex6.c
1: static char help[] = "Reads a PETSc matrix and vector from a file and solves a linear system.\n\
2: Input arguments are:\n\
3: -f <input_file> : file to load. For example see $PETSC_DIR/share/petsc/datafiles/matrices\n\n";
5: #include <petscksp.h>
6: #include <petsclog.h>
8: static PetscErrorCode KSPTestResidualMonitor(KSP ksp, PetscInt i, PetscReal r, PetscCtx ctx)
9: {
10: Vec *t, *v;
11: PetscReal err;
13: PetscFunctionBeginUser;
14: PetscCall(KSPCreateVecs(ksp, 2, &t, 2, &v));
15: PetscCall(KSPBuildResidualDefault(ksp, t[0], v[0], &v[0]));
16: PetscCall(KSPBuildResidual(ksp, t[1], v[1], &v[1]));
17: PetscCall(VecAXPY(v[1], -1.0, v[0]));
18: PetscCall(VecNorm(v[1], NORM_INFINITY, &err));
19: PetscCheck(err <= PETSC_SMALL, PetscObjectComm((PetscObject)ksp), PETSC_ERR_PLIB, "Inconsistent residual computed at step %" PetscInt_FMT ": %g (KSP %g)", i, (double)err, (double)r);
20: PetscCall(VecDestroyVecs(2, &t));
21: PetscCall(VecDestroyVecs(2, &v));
22: PetscFunctionReturn(PETSC_SUCCESS);
23: }
25: int main(int argc, char **args)
26: {
27: PetscInt its;
28: PetscLogStage stage1, stage2;
29: PetscReal norm;
30: Vec x, b, u;
31: Mat A;
32: char file[PETSC_MAX_PATH_LEN];
33: PetscViewer fd;
34: PetscBool table = PETSC_FALSE, flg, test_residual = PETSC_FALSE, b_in_f = PETSC_TRUE;
35: KSP ksp;
37: PetscFunctionBeginUser;
38: PetscCall(PetscInitialize(&argc, &args, NULL, help));
39: PetscCall(PetscOptionsGetBool(NULL, NULL, "-table", &table, NULL));
40: PetscCall(PetscOptionsGetBool(NULL, NULL, "-test_residual", &test_residual, NULL));
41: PetscCall(PetscOptionsGetBool(NULL, NULL, "-b_in_f", &b_in_f, NULL));
43: /* Read matrix and RHS */
44: PetscCall(PetscOptionsGetString(NULL, NULL, "-f", file, sizeof(file), &flg));
45: PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_USER_INPUT, "Must indicate binary file with the -f option");
46: PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, file, FILE_MODE_READ, &fd));
47: PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
48: PetscCall(MatLoad(A, fd));
49: if (b_in_f) {
50: PetscCall(VecCreate(PETSC_COMM_WORLD, &b));
51: PetscCall(VecLoad(b, fd));
52: } else {
53: PetscCall(MatCreateVecs(A, NULL, &b));
54: PetscCall(VecSetRandom(b, NULL));
55: }
56: PetscCall(PetscViewerDestroy(&fd));
58: /*
59: If the load matrix is larger than the vector, due to being padded
60: to match the blocksize then create a new padded vector
61: */
62: {
63: PetscInt m, n, j, mvec, start, end, indx;
64: Vec tmp;
65: PetscScalar *bold;
67: PetscCall(MatGetLocalSize(A, &m, &n));
68: PetscCall(VecCreate(PETSC_COMM_WORLD, &tmp));
69: PetscCall(VecSetSizes(tmp, m, PETSC_DECIDE));
70: PetscCall(VecSetFromOptions(tmp));
71: PetscCall(VecGetOwnershipRange(b, &start, &end));
72: PetscCall(VecGetLocalSize(b, &mvec));
73: PetscCall(VecGetArray(b, &bold));
74: for (j = 0; j < mvec; j++) {
75: indx = start + j;
76: PetscCall(VecSetValues(tmp, 1, &indx, bold + j, INSERT_VALUES));
77: }
78: PetscCall(VecRestoreArray(b, &bold));
79: PetscCall(VecDestroy(&b));
80: PetscCall(VecAssemblyBegin(tmp));
81: PetscCall(VecAssemblyEnd(tmp));
82: b = tmp;
83: }
84: PetscCall(VecDuplicate(b, &x));
85: PetscCall(VecDuplicate(b, &u));
87: PetscCall(PetscBarrier((PetscObject)A));
89: PetscCall(PetscLogStageRegister("mystage 1", &stage1));
90: PetscCall(PetscLogStagePush(stage1));
91: PetscCall(KSPCreate(PETSC_COMM_WORLD, &ksp));
92: PetscCall(KSPSetOperators(ksp, A, A));
93: PetscCall(KSPSetFromOptions(ksp));
94: if (test_residual) PetscCall(KSPMonitorSet(ksp, KSPTestResidualMonitor, NULL, NULL));
95: PetscCall(KSPSetUp(ksp));
96: PetscCall(KSPSetUpOnBlocks(ksp));
97: PetscCall(PetscLogStagePop());
98: PetscCall(PetscBarrier((PetscObject)A));
100: PetscCall(PetscLogStageRegister("mystage 2", &stage2));
101: PetscCall(PetscLogStagePush(stage2));
102: PetscCall(KSPSolve(ksp, b, x));
103: PetscCall(PetscLogStagePop());
105: /* Show result */
106: PetscCall(MatMult(A, x, u));
107: PetscCall(VecAXPY(u, -1.0, b));
108: PetscCall(VecNorm(u, NORM_2, &norm));
109: PetscCall(KSPGetIterationNumber(ksp, &its));
110: /* matrix PC KSP Options its residual */
111: if (table) {
112: char *matrixname, kspinfo[120];
113: PetscViewer viewer;
114: PetscCall(PetscViewerStringOpen(PETSC_COMM_WORLD, kspinfo, sizeof(kspinfo), &viewer));
115: PetscCall(KSPView(ksp, viewer));
116: PetscCall(PetscStrrchr(file, '/', &matrixname));
117: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "%-8.8s %3" PetscInt_FMT " %2.0e %s \n", matrixname, its, (double)norm, kspinfo));
118: PetscCall(PetscViewerDestroy(&viewer));
119: } else {
120: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Number of iterations = %3" PetscInt_FMT "\n", its));
121: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Residual norm = %g\n", (double)norm));
122: }
124: /* Cleanup */
125: PetscCall(KSPDestroy(&ksp));
126: PetscCall(VecDestroy(&x));
127: PetscCall(VecDestroy(&b));
128: PetscCall(VecDestroy(&u));
129: PetscCall(MatDestroy(&A));
130: PetscCall(PetscFinalize());
131: return 0;
132: }
134: /*TEST
136: test:
137: args: -ksp_type preonly -pc_type lu -options_left no -f ${DATAFILESPATH}/matrices/arco1
138: requires: datafilespath double !complex !defined(PETSC_USE_64BIT_INDICES)
140: test:
141: suffix: 2
142: args: -sub_pc_type ilu -options_left no -f ${DATAFILESPATH}/matrices/arco1 -ksp_gmres_restart 100 -ksp_orthogonalization_cgs_refinement_type refine_always -sub_ksp_type preonly -pc_type bjacobi -pc_bjacobi_blocks 8 -sub_pc_factor_in_place -ksp_monitor
143: requires: datafilespath double !complex !defined(PETSC_USE_64BIT_INDICES)
145: test:
146: suffix: 7
147: args: -ksp_orthogonalization_cgs_refinement_type refine_always -pc_type asm -pc_asm_blocks 6 -f ${DATAFILESPATH}/matrices/small -matload_block_size 6 -ksp_monitor
148: requires: datafilespath double !complex !defined(PETSC_USE_64BIT_INDICES)
150: test:
151: requires: double !complex !defined(PETSC_USE_64BIT_INDICES)
152: suffix: 3
153: filter: sed -e "s/CONVERGED_RTOL/CONVERGED_ATOL/g"
154: args: -f ${wPETSC_DIR}/share/petsc/datafiles/matrices/spd-real-int32-float64 -pc_type none -ksp_type {{cg groppcg pipecg pipecgrr pipelcg pipeprcg cgne nash stcg gltr fcg pipefcg gmres pipefgmres fgmres lgmres dgmres pgmres tcqmr bcgs ibcgs qmrcgs fbcgs fbcgsr bcgsl pipebcgs cgs tfqmr cr pipecr lsqr qcg bicg minres symmlq lcd gcr pipegcr cgls}} -ksp_max_it 20 -ksp_error_if_not_converged -ksp_converged_reason -test_residual
156: test:
157: requires: double !complex !defined(PETSC_USE_64BIT_INDICES)
158: suffix: 3_maxits
159: output_file: output/ex6_maxits.out
160: args: -f ${wPETSC_DIR}/share/petsc/datafiles/matrices/spd-real-int32-float64 -pc_type none -ksp_type {{chebyshev cg groppcg pipecg pipecgrr pipelcg pipeprcg cgne nash stcg gltr fcg pipefcg gmres pipefgmres fgmres lgmres dgmres pgmres tcqmr bcgs ibcgs qmrcgs fbcgs fbcgsr bcgsl pipebcgs cgs tfqmr cr pipecr qcg bicg minres symmlq lcd gcr pipegcr cgls richardson}} -ksp_max_it 4 -ksp_error_if_not_converged -ksp_converged_maxits -ksp_converged_reason -test_residual -ksp_norm_type none
162: testset:
163: requires: double !complex !defined(PETSC_USE_64BIT_INDICES)
164: output_file: output/ex6_skip.out
165: args: -f ${wPETSC_DIR}/share/petsc/datafiles/matrices/spd-real-int32-float64 -pc_type none -ksp_max_it 8 -ksp_error_if_not_converged -ksp_convergence_test skip -ksp_converged_reason -test_residual
166: #SYMMLQ converges in 4 iterations and then generate nans
167: test:
168: suffix: 3_skip
169: args: -ksp_type {{chebyshev cg groppcg pipecg pipecgrr pipelcg pipeprcg cgne nash stcg gltr fcg pipefcg gmres fgmres lgmres dgmres pgmres tcqmr bcgs ibcgs qmrcgs fbcgs fbcgsr bcgsl pipebcgs cgs tfqmr cr pipecr qcg bicg minres lcd gcr cgls richardson}}
170: #PIPEGCR generates nans on linux-knl
171: #PIPEFGMRES can have happy breakdown which is not handled well with no convergence test
172: test:
173: requires: !defined(PETSC_USE_AVX512_KERNELS)
174: suffix: 3_skip_pipegcr
175: args: -ksp_type pipegcr
176: test:
177: requires: hpddm
178: suffix: 3_skip_hpddm
179: args: -ksp_type hpddm -ksp_hpddm_type {{cg gmres bgmres bcg bfbcg gcrodr bgcrodr}}
181: test:
182: requires: double !complex !defined(PETSC_USE_64BIT_INDICES) hpddm
183: suffix: 3_hpddm
184: output_file: output/ex6_3.out
185: filter: sed -e "s/CONVERGED_RTOL/CONVERGED_ATOL/g"
186: args: -f ${wPETSC_DIR}/share/petsc/datafiles/matrices/spd-real-int32-float64 -pc_type none -ksp_type hpddm -ksp_hpddm_type {{cg gmres bgmres bcg bfbcg gcrodr bgcrodr}} -ksp_max_it 20 -ksp_error_if_not_converged -ksp_converged_reason -test_residual
188: # test CG shortcut for residual access
189: test:
190: suffix: 4
191: args: -ksp_converged_reason -ksp_max_it 20 -ksp_converged_maxits -ksp_type {{cg pipecg groppcg}} -ksp_norm_type {{preconditioned unpreconditioned natural}separate output} -pc_type {{bjacobi none}separate output} -f ${DATAFILESPATH}/matrices/poisson_2d13p -b_in_f 0 -test_residual
192: requires: datafilespath double !complex !defined(PETSC_USE_64BIT_INDICES)
194: TEST*/