Actual source code: ex21.c
1: static char help[] = "Solves a RBF kernel matrix with KSP and PCH2OPUS.\n\n";
3: #include <petscksp.h>
5: typedef struct {
6: PetscReal sigma;
7: PetscReal *l;
8: PetscReal lambda;
9: } RBFCtx;
11: static PetscScalar RBF(PetscInt sdim, PetscReal x[], PetscReal y[], PetscCtx ctx)
12: {
13: RBFCtx *rbfctx = (RBFCtx *)ctx;
14: PetscReal diff = 0.0;
15: PetscReal s = rbfctx->sigma;
16: PetscReal *l = rbfctx->l;
17: PetscReal lambda = rbfctx->lambda;
19: for (PetscInt d = 0; d < sdim; d++) diff += (x[d] - y[d]) * (x[d] - y[d]) / (l[d] * l[d]);
20: return s * s * PetscExpReal(-0.5 * diff) + (diff != 0.0 ? 0.0 : lambda);
21: }
23: int main(int argc, char **args)
24: {
25: Vec x, b, u, d;
26: Mat A, Ae = NULL, Ad = NULL;
27: KSP ksp;
28: PetscRandom r;
29: PC pc;
30: PetscReal norm, *coords, eta, scale = 0.5;
31: PetscInt basisord, leafsize, sdim, n, its, i;
32: PetscMPIInt size;
33: RBFCtx fctx;
35: PetscFunctionBeginUser;
36: PetscCall(PetscInitialize(&argc, &args, NULL, help));
37: PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
38: PetscCheck(size == 1, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "This is a uniprocessor example only!");
39: PetscCall(PetscRandomCreate(PETSC_COMM_WORLD, &r));
40: PetscCall(PetscRandomSetFromOptions(r));
42: sdim = 2;
43: PetscCall(PetscOptionsGetInt(NULL, NULL, "-sdim", &sdim, NULL));
44: n = 32;
45: PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
46: eta = 0.6;
47: PetscCall(PetscOptionsGetReal(NULL, NULL, "-eta", &eta, NULL));
48: leafsize = 32;
49: PetscCall(PetscOptionsGetInt(NULL, NULL, "-leafsize", &leafsize, NULL));
50: basisord = 8;
51: PetscCall(PetscOptionsGetInt(NULL, NULL, "-basisord", &basisord, NULL));
53: /* Create random points */
54: PetscCall(PetscMalloc1(sdim * n, &coords));
55: PetscCall(PetscRandomGetValuesReal(r, sdim * n, coords));
57: fctx.lambda = 0.01;
58: PetscCall(PetscOptionsGetReal(NULL, NULL, "-lambda", &fctx.lambda, NULL));
59: PetscCall(PetscRandomGetValueReal(r, &fctx.sigma));
60: PetscCall(PetscOptionsGetReal(NULL, NULL, "-sigma", &fctx.sigma, NULL));
61: PetscCall(PetscMalloc1(sdim, &fctx.l));
62: PetscCall(PetscRandomGetValuesReal(r, sdim, fctx.l));
63: PetscCall(PetscOptionsGetRealArray(NULL, NULL, "-l", fctx.l, (i = sdim, &i), NULL));
64: PetscCall(PetscOptionsGetReal(NULL, NULL, "-scale", &scale, NULL));
66: /* Populate dense matrix for comparisons */
67: {
68: PetscCall(MatCreateDense(PETSC_COMM_WORLD, n, n, PETSC_DECIDE, PETSC_DECIDE, NULL, &Ad));
69: for (PetscInt i = 0; i < n; i++) {
70: for (PetscInt j = 0; j < n; j++) PetscCall(MatSetValue(Ad, i, j, RBF(sdim, coords + i * sdim, coords + j * sdim, &fctx), INSERT_VALUES));
71: }
72: PetscCall(MatAssemblyBegin(Ad, MAT_FINAL_ASSEMBLY));
73: PetscCall(MatAssemblyEnd(Ad, MAT_FINAL_ASSEMBLY));
74: }
76: /* Create and assemble the matrix */
77: PetscCall(MatCreateH2OpusFromKernel(PETSC_COMM_WORLD, n, n, PETSC_DECIDE, PETSC_DECIDE, sdim, coords, PETSC_FALSE, RBF, &fctx, eta, leafsize, basisord, &A));
78: PetscCall(MatSetOption(A, MAT_SYMMETRIC, PETSC_TRUE));
79: PetscCall(MatSetOption(A, MAT_SYMMETRY_ETERNAL, PETSC_TRUE));
80: PetscCall(PetscObjectSetName((PetscObject)A, "RBF"));
81: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
82: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
83: PetscCall(MatViewFromOptions(A, NULL, "-rbf_view"));
85: PetscCall(MatCreateVecs(A, &x, &b));
86: PetscCall(VecDuplicate(x, &u));
87: PetscCall(VecDuplicate(x, &d));
89: {
90: PetscReal norm;
91: PetscCall(MatComputeOperator(A, MATDENSE, &Ae));
92: PetscCall(MatAXPY(Ae, -1.0, Ad, SAME_NONZERO_PATTERN));
93: PetscCall(MatGetDiagonal(Ae, d));
94: PetscCall(MatViewFromOptions(Ae, NULL, "-A_view"));
95: PetscCall(MatViewFromOptions(Ae, NULL, "-D_view"));
96: PetscCall(MatNorm(Ae, NORM_FROBENIUS, &norm));
97: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Approx err %g\n", norm));
98: PetscCall(VecNorm(d, NORM_2, &norm));
99: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Approx err (diag) %g\n", norm));
100: PetscCall(MatDestroy(&Ae));
101: }
103: PetscCall(VecSet(u, 1.0));
104: PetscCall(MatMult(Ad, u, b));
105: PetscCall(MatViewFromOptions(Ad, NULL, "-Ad_view"));
106: PetscCall(KSPCreate(PETSC_COMM_WORLD, &ksp));
107: PetscCall(KSPSetOperators(ksp, Ad, A));
108: PetscCall(KSPGetPC(ksp, &pc));
109: PetscCall(PCSetType(pc, PCH2OPUS));
110: PetscCall(KSPSetFromOptions(ksp));
111: /* we can also pass the points coordinates
112: In this case it is not needed, since the preconditioning
113: matrix is of type H2OPUS */
114: PetscCall(PCSetCoordinates(pc, sdim, n, coords));
116: PetscCall(KSPSolve(ksp, b, x));
117: PetscCall(VecAXPY(x, -1.0, u));
118: PetscCall(VecNorm(x, NORM_2, &norm));
119: PetscCall(KSPGetIterationNumber(ksp, &its));
120: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Norm of error %g, Iterations %" PetscInt_FMT "\n", (double)norm, its));
122: /* change lambda and reassemble */
123: PetscCall(VecSet(x, (scale - 1.) * fctx.lambda));
124: PetscCall(MatDiagonalSet(Ad, x, ADD_VALUES));
125: fctx.lambda *= scale;
126: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
127: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
128: {
129: PetscReal norm;
130: PetscCall(MatComputeOperator(A, MATDENSE, &Ae));
131: PetscCall(MatAXPY(Ae, -1.0, Ad, SAME_NONZERO_PATTERN));
132: PetscCall(MatGetDiagonal(Ae, d));
133: PetscCall(MatViewFromOptions(Ae, NULL, "-A_view"));
134: PetscCall(MatViewFromOptions(Ae, NULL, "-D_view"));
135: PetscCall(MatNorm(Ae, NORM_FROBENIUS, &norm));
136: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Approx err %g\n", norm));
137: PetscCall(VecNorm(d, NORM_2, &norm));
138: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Approx err (diag) %g\n", norm));
139: PetscCall(MatDestroy(&Ae));
140: }
141: PetscCall(KSPSetOperators(ksp, Ad, A));
142: PetscCall(MatMult(Ad, u, b));
143: PetscCall(KSPSolve(ksp, b, x));
144: PetscCall(MatMult(Ad, x, u));
145: PetscCall(VecAXPY(u, -1.0, b));
146: PetscCall(VecNorm(u, NORM_2, &norm));
147: PetscCall(KSPGetIterationNumber(ksp, &its));
148: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Residual norm error %g, Iterations %" PetscInt_FMT "\n", (double)norm, its));
150: PetscCall(PetscFree(coords));
151: PetscCall(PetscFree(fctx.l));
152: PetscCall(PetscRandomDestroy(&r));
153: PetscCall(VecDestroy(&x));
154: PetscCall(VecDestroy(&u));
155: PetscCall(VecDestroy(&d));
156: PetscCall(VecDestroy(&b));
157: PetscCall(MatDestroy(&A));
158: PetscCall(MatDestroy(&Ad));
159: PetscCall(KSPDestroy(&ksp));
160: PetscCall(PetscFinalize());
161: return 0;
162: }
164: /*TEST
166: build:
167: requires: h2opus
169: test:
170: requires: h2opus !single
171: suffix: 1
172: args: -ksp_error_if_not_converged -pc_h2opus_monitor
174: test:
175: requires: h2opus !single
176: suffix: 1_ns
177: output_file: output/ex21_1.out
178: args: -ksp_error_if_not_converged -pc_h2opus_monitor -pc_h2opus_hyperorder 2
180: test:
181: requires: h2opus !single
182: suffix: 2
183: args: -ksp_error_if_not_converged -pc_h2opus_monitor -pc_h2opus_hyperorder 4
185: TEST*/