Actual source code: cp.c
1: #include <petsc/private/pcimpl.h>
2: #include <../src/mat/impls/aij/seq/aij.h>
4: /*
5: Private context (data structure) for the CP preconditioner.
6: */
7: typedef struct {
8: PetscInt n, m;
9: Vec work;
10: PetscScalar *d; /* sum of squares of each column */
11: PetscScalar *a; /* non-zeros by column */
12: PetscInt *i, *j; /* offsets of nonzeros by column, non-zero indices by column */
13: } PC_CP;
15: static PetscErrorCode PCSetUp_CP(PC pc)
16: {
17: PC_CP *cp = (PC_CP *)pc->data;
18: PetscInt i, j, *colcnt;
19: PetscBool flg;
20: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)pc->pmat->data;
22: PetscFunctionBegin;
23: PetscCall(PetscObjectTypeCompare((PetscObject)pc->pmat, MATSEQAIJ, &flg));
24: PetscCheck(flg, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Currently only handles SeqAIJ matrices");
26: PetscCall(MatGetLocalSize(pc->pmat, &cp->m, &cp->n));
27: PetscCheck(cp->m == cp->n, PETSC_COMM_SELF, PETSC_ERR_SUP, "Currently only for square matrices");
29: if (!cp->work) PetscCall(MatCreateVecs(pc->pmat, &cp->work, NULL));
30: if (!cp->d) PetscCall(PetscMalloc1(cp->n, &cp->d));
31: if (cp->a && pc->flag != SAME_NONZERO_PATTERN) PetscCall(PetscFree3(cp->a, cp->i, cp->j));
33: /* convert to column format */
34: if (!cp->a) PetscCall(PetscMalloc3(aij->nz, &cp->a, cp->n + 1, &cp->i, aij->nz, &cp->j));
35: PetscCall(PetscCalloc1(cp->n, &colcnt));
37: for (i = 0; i < aij->nz; i++) colcnt[aij->j[i]]++;
38: cp->i[0] = 0;
39: for (i = 0; i < cp->n; i++) cp->i[i + 1] = cp->i[i] + colcnt[i];
40: PetscCall(PetscArrayzero(colcnt, cp->n));
41: for (i = 0; i < cp->m; i++) { /* over rows */
42: for (j = aij->i[i]; j < aij->i[i + 1]; j++) { /* over columns in row */
43: cp->j[cp->i[aij->j[j]] + colcnt[aij->j[j]]] = i;
44: cp->a[cp->i[aij->j[j]] + colcnt[aij->j[j]]++] = aij->a[j];
45: }
46: }
47: PetscCall(PetscFree(colcnt));
49: /* compute sum of squares of each column d[] */
50: for (i = 0; i < cp->n; i++) { /* over columns */
51: cp->d[i] = 0.;
52: for (j = cp->i[i]; j < cp->i[i + 1]; j++) cp->d[i] += cp->a[j] * cp->a[j]; /* over rows in column */
53: cp->d[i] = 1.0 / cp->d[i];
54: }
55: PetscFunctionReturn(PETSC_SUCCESS);
56: }
58: static PetscErrorCode PCApply_CP(PC pc, Vec bb, Vec xx)
59: {
60: PC_CP *cp = (PC_CP *)pc->data;
61: PetscScalar *b, *x, xt;
63: PetscFunctionBegin;
64: PetscCall(VecCopy(bb, cp->work));
65: PetscCall(VecGetArray(cp->work, &b));
66: PetscCall(VecGetArray(xx, &x));
68: for (PetscInt i = 0; i < cp->n; i++) { /* over columns */
69: xt = 0.;
70: for (PetscInt j = cp->i[i]; j < cp->i[i + 1]; j++) xt += cp->a[j] * b[cp->j[j]]; /* over rows in column */
71: xt *= cp->d[i];
72: x[i] = xt;
73: for (PetscInt j = cp->i[i]; j < cp->i[i + 1]; j++) b[cp->j[j]] -= xt * cp->a[j]; /* over rows in column updating b*/
74: }
75: for (PetscInt i = cp->n - 1; i > -1; i--) { /* over columns */
76: xt = 0.;
77: for (PetscInt j = cp->i[i]; j < cp->i[i + 1]; j++) xt += cp->a[j] * b[cp->j[j]]; /* over rows in column */
78: xt *= cp->d[i];
79: x[i] = xt;
80: for (PetscInt j = cp->i[i]; j < cp->i[i + 1]; j++) b[cp->j[j]] -= xt * cp->a[j]; /* over rows in column updating b*/
81: }
83: PetscCall(VecRestoreArray(cp->work, &b));
84: PetscCall(VecRestoreArray(xx, &x));
85: PetscFunctionReturn(PETSC_SUCCESS);
86: }
88: static PetscErrorCode PCReset_CP(PC pc)
89: {
90: PC_CP *cp = (PC_CP *)pc->data;
92: PetscFunctionBegin;
93: PetscCall(PetscFree(cp->d));
94: PetscCall(VecDestroy(&cp->work));
95: PetscCall(PetscFree3(cp->a, cp->i, cp->j));
96: PetscFunctionReturn(PETSC_SUCCESS);
97: }
99: static PetscErrorCode PCDestroy_CP(PC pc)
100: {
101: PC_CP *cp = (PC_CP *)pc->data;
103: PetscFunctionBegin;
104: PetscCall(PCReset_CP(pc));
105: PetscCall(PetscFree(cp->d));
106: PetscCall(PetscFree3(cp->a, cp->i, cp->j));
107: PetscCall(PetscFree(pc->data));
108: PetscFunctionReturn(PETSC_SUCCESS);
109: }
111: /*MC
112: PCCP - a "column-projection" preconditioner. Iteratively projects the current residual onto the one dimensional spaces
113: spanned by each of the columns of the matrix.
115: This is a terrible preconditioner and is not recommended, ever!
117: Loops over the entries of x computing dx_i (e_i is the unit vector in the ith direction) to
118: .vb
120: min || b - A(x + dx_i e_i ||_2
121: dx_i
123: That is, it changes a single entry of x to minimize the new residual norm.
124: Let A_i represent the ith column of A, then the minimization can be written as
126: min || r - (dx_i) A e_i ||_2
127: dx_i
128: or min || r - (dx_i) A_i ||_2
129: dx_i
131: take the derivative with respect to dx_i to obtain
132: dx_i = (A_i^T A_i)^(-1) A_i^T r
134: This is equivalent to using Gauss-Seidel on the normal equations
135: .ve
137: Notes:
138: This procedure can also be done with block columns or any groups of columns
139: but this is not coded.
141: These "projections" can be done simultaneously for all columns (similar to the Jacobi method)
142: or sequentially (similar to Gauss-Seidel/SOR). This is only coded for SOR type.
144: This is related to, but not the same as "row projection" methods.
146: This is currently coded only for `MATSEQAIJ` matrices
148: Level: intermediate
150: .seealso: [](ch_ksp), `PCCreate()`, `PCSetType()`, `PCType`, `PCJACOBI`, `PCSOR`
151: M*/
153: PETSC_EXTERN PetscErrorCode PCCreate_CP(PC pc)
154: {
155: PC_CP *cp;
157: PetscFunctionBegin;
158: PetscCall(PetscNew(&cp));
159: pc->data = (void *)cp;
161: pc->ops->apply = PCApply_CP;
162: pc->ops->applytranspose = PCApply_CP;
163: pc->ops->setup = PCSetUp_CP;
164: pc->ops->reset = PCReset_CP;
165: pc->ops->destroy = PCDestroy_CP;
166: PetscFunctionReturn(PETSC_SUCCESS);
167: }