Actual source code: symbadbrdn.c
1: #include <../src/ksp/ksp/utils/lmvm/symbrdn/symbrdn.h>
2: #include <../src/ksp/ksp/utils/lmvm/diagbrdn/diagbrdn.h>
4: /*------------------------------------------------------------*/
6: static PetscErrorCode MatSolve_LMVMSymBadBrdn(Mat B, Vec F, Vec dX)
7: {
8: Mat_LMVM *lmvm = (Mat_LMVM*)B->data;
9: Mat_SymBrdn *lsb = (Mat_SymBrdn*)lmvm->ctx;
10: PetscErrorCode ierr;
11: PetscInt i, j;
12: PetscScalar yjtqi, sjtyi, wtyi, ytx, stf, wtf, ytq;
15: /* Efficient shortcuts for pure BFGS and pure DFP configurations */
16: if (lsb->phi == 0.0) {
17: MatSolve_LMVMBFGS(B, F, dX);
18: return(0);
19: }
20: if (lsb->phi == 1.0) {
21: MatSolve_LMVMDFP(B, F, dX);
22: return(0);
23: }
25: VecCheckSameSize(F, 2, dX, 3);
26: VecCheckMatCompatible(B, dX, 3, F, 2);
28: if (lsb->needQ) {
29: /* Start the loop for (Q[k] = (B_k)^{-1} * Y[k]) */
30: for (i = 0; i <= lmvm->k; ++i) {
31: MatSymBrdnApplyJ0Inv(B, lmvm->Y[i], lsb->Q[i]);
32: for (j = 0; j <= i-1; ++j) {
33: /* Compute the necessary dot products */
34: VecDotBegin(lmvm->Y[j], lsb->Q[i], &yjtqi);
35: VecDotBegin(lmvm->S[j], lmvm->Y[i], &sjtyi);
36: VecDotEnd(lmvm->Y[j], lsb->Q[i], &yjtqi);
37: VecDotEnd(lmvm->S[j], lmvm->Y[i], &sjtyi);
38: /* Compute the pure DFP component of the inverse application*/
39: VecAXPBYPCZ(lsb->Q[i], -PetscRealPart(yjtqi)/lsb->ytq[j], PetscRealPart(sjtyi)/lsb->yts[j], 1.0, lsb->Q[j], lmvm->S[j]);
40: /* Tack on the convexly scaled extras to the inverse application*/
41: if (lsb->psi[j] > 0.0) {
42: VecAXPBYPCZ(lsb->work, 1.0/lsb->yts[j], -1.0/lsb->ytq[j], 0.0, lmvm->S[j], lsb->Q[j]);
43: VecDot(lsb->work, lmvm->Y[i], &wtyi);
44: VecAXPY(lsb->Q[i], lsb->phi*lsb->ytq[j]*PetscRealPart(wtyi), lsb->work);
45: }
46: }
47: VecDot(lmvm->Y[i], lsb->Q[i], &ytq);
48: lsb->ytq[i] = PetscRealPart(ytq);
49: }
50: lsb->needQ = PETSC_FALSE;
51: }
53: /* Start the outer iterations for ((B^{-1}) * dX) */
54: MatSymBrdnApplyJ0Inv(B, F, dX);
55: for (i = 0; i <= lmvm->k; ++i) {
56: /* Compute the necessary dot products -- store yTs and yTp for inner iterations later */
57: VecDotBegin(lmvm->Y[i], dX, &ytx);
58: VecDotBegin(lmvm->S[i], F, &stf);
59: VecDotEnd(lmvm->Y[i], dX, &ytx);
60: VecDotEnd(lmvm->S[i], F, &stf);
61: /* Compute the pure DFP component */
62: VecAXPBYPCZ(dX, -PetscRealPart(ytx)/lsb->ytq[i], PetscRealPart(stf)/lsb->yts[i], 1.0, lsb->Q[i], lmvm->S[i]);
63: /* Tack on the convexly scaled extras */
64: VecAXPBYPCZ(lsb->work, 1.0/lsb->yts[i], -1.0/lsb->ytq[i], 0.0, lmvm->S[i], lsb->Q[i]);
65: VecDot(lsb->work, F, &wtf);
66: VecAXPY(dX, lsb->phi*lsb->ytq[i]*PetscRealPart(wtf), lsb->work);
67: }
69: return(0);
70: }
72: /*------------------------------------------------------------*/
74: static PetscErrorCode MatMult_LMVMSymBadBrdn(Mat B, Vec X, Vec Z)
75: {
76: Mat_LMVM *lmvm = (Mat_LMVM*)B->data;
77: Mat_SymBrdn *lsb = (Mat_SymBrdn*)lmvm->ctx;
78: PetscErrorCode ierr;
79: PetscInt i, j;
80: PetscReal numer;
81: PetscScalar sjtpi, sjtyi, yjtsi, yjtqi, wtsi, wtyi, stz, ytx, ytq, wtx, stp;
84: /* Efficient shortcuts for pure BFGS and pure DFP configurations */
85: if (lsb->phi == 0.0) {
86: MatMult_LMVMBFGS(B, X, Z);
87: return(0);
88: }
89: if (lsb->phi == 1.0) {
90: MatMult_LMVMDFP(B, X, Z);
91: return(0);
92: }
94: VecCheckSameSize(X, 2, Z, 3);
95: VecCheckMatCompatible(B, X, 2, Z, 3);
97: if (lsb->needQ) {
98: /* Start the loop for (Q[k] = (B_k)^{-1} * Y[k]) */
99: for (i = 0; i <= lmvm->k; ++i) {
100: MatSymBrdnApplyJ0Inv(B, lmvm->Y[i], lsb->Q[i]);
101: for (j = 0; j <= i-1; ++j) {
102: /* Compute the necessary dot products */
103: VecDotBegin(lmvm->Y[j], lsb->Q[i], &yjtqi);
104: VecDotBegin(lmvm->S[j], lmvm->Y[i], &sjtyi);
105: VecDotEnd(lmvm->Y[j], lsb->Q[i], &yjtqi);
106: VecDotEnd(lmvm->S[j], lmvm->Y[i], &sjtyi);
107: /* Compute the pure DFP component of the inverse application*/
108: VecAXPBYPCZ(lsb->Q[i], -PetscRealPart(yjtqi)/lsb->ytq[j], PetscRealPart(sjtyi)/lsb->yts[j], 1.0, lsb->Q[j], lmvm->S[j]);
109: /* Tack on the convexly scaled extras to the inverse application*/
110: if (lsb->psi[j] > 0.0) {
111: VecAXPBYPCZ(lsb->work, 1.0/lsb->yts[j], -1.0/lsb->ytq[j], 0.0, lmvm->S[j], lsb->Q[j]);
112: VecDot(lsb->work, lmvm->Y[i], &wtyi);
113: VecAXPY(lsb->Q[i], lsb->phi*lsb->ytq[j]*PetscRealPart(wtyi), lsb->work);
114: }
115: }
116: VecDot(lmvm->Y[i], lsb->Q[i], &ytq);
117: lsb->ytq[i] = PetscRealPart(ytq);
118: }
119: lsb->needQ = PETSC_FALSE;
120: }
121: if (lsb->needP) {
122: /* Start the loop for (P[k] = (B_k) * S[k]) */
123: for (i = 0; i <= lmvm->k; ++i) {
124: MatSymBrdnApplyJ0Fwd(B, lmvm->S[i], lsb->P[i]);
125: for (j = 0; j <= i-1; ++j) {
126: /* Compute the necessary dot products */
127: VecDotBegin(lmvm->S[j], lsb->P[i], &sjtpi);
128: VecDotBegin(lmvm->Y[j], lmvm->S[i], &yjtsi);
129: VecDotEnd(lmvm->S[j], lsb->P[i], &sjtpi);
130: VecDotEnd(lmvm->Y[j], lmvm->S[i], &yjtsi);
131: /* Compute the pure BFGS component of the forward product */
132: VecAXPBYPCZ(lsb->P[i], -PetscRealPart(sjtpi)/lsb->stp[j], PetscRealPart(yjtsi)/lsb->yts[j], 1.0, lsb->P[j], lmvm->Y[j]);
133: /* Tack on the convexly scaled extras to the forward product */
134: if (lsb->phi > 0.0) {
135: VecAXPBYPCZ(lsb->work, 1.0/lsb->yts[j], -1.0/lsb->stp[j], 0.0, lmvm->Y[j], lsb->P[j]);
136: VecDot(lsb->work, lmvm->S[i], &wtsi);
137: VecAXPY(lsb->P[i], lsb->psi[j]*lsb->stp[j]*PetscRealPart(wtsi), lsb->work);
138: }
139: }
140: VecDot(lmvm->S[i], lsb->P[i], &stp);
141: lsb->stp[i] = PetscRealPart(stp);
142: if (lsb->phi == 1.0) {
143: lsb->psi[i] = 0.0;
144: } else if (lsb->phi == 0.0) {
145: lsb->psi[i] = 1.0;
146: } else {
147: numer = (1.0 - lsb->phi)*lsb->yts[i]*lsb->yts[i];
148: lsb->psi[i] = numer / (numer + (lsb->phi*lsb->ytq[i]*lsb->stp[i]));
149: }
150: }
151: lsb->needP = PETSC_FALSE;
152: }
154: /* Start the outer iterations for (B * X) */
155: MatSymBrdnApplyJ0Fwd(B, X, Z);
156: for (i = 0; i <= lmvm->k; ++i) {
157: /* Compute the necessary dot products */
158: VecDotBegin(lmvm->S[i], Z, &stz);
159: VecDotBegin(lmvm->Y[i], X, &ytx);
160: VecDotEnd(lmvm->S[i], Z, &stz);
161: VecDotEnd(lmvm->Y[i], X, &ytx);
162: /* Compute the pure BFGS component */
163: VecAXPBYPCZ(Z, -PetscRealPart(stz)/lsb->stp[i], PetscRealPart(ytx)/lsb->yts[i], 1.0, lsb->P[i], lmvm->Y[i]);
164: /* Tack on the convexly scaled extras */
165: VecAXPBYPCZ(lsb->work, 1.0/lsb->yts[i], -1.0/lsb->stp[i], 0.0, lmvm->Y[i], lsb->P[i]);
166: VecDot(lsb->work, X, &wtx);
167: VecAXPY(Z, lsb->psi[i]*lsb->stp[i]*PetscRealPart(wtx), lsb->work);
168: }
169: return(0);
170: }
172: /*------------------------------------------------------------*/
174: static PetscErrorCode MatSetFromOptions_LMVMSymBadBrdn(PetscOptionItems *PetscOptionsObject, Mat B)
175: {
176: Mat_LMVM *lmvm = (Mat_LMVM*)B->data;
177: Mat_SymBrdn *lsb = (Mat_SymBrdn*)lmvm->ctx;
178: Mat_LMVM *dbase;
179: Mat_DiagBrdn *dctx;
180: PetscErrorCode ierr;
183: MatSetFromOptions_LMVMSymBrdn(PetscOptionsObject, B);
184: if (lsb->scale_type == MAT_LMVM_SYMBROYDEN_SCALE_DIAGONAL) {
185: dbase = (Mat_LMVM*)lsb->D->data;
186: dctx = (Mat_DiagBrdn*)dbase->ctx;
187: dctx->forward = PETSC_FALSE;
188: }
189: return(0);
190: }
192: /*------------------------------------------------------------*/
194: PetscErrorCode MatCreate_LMVMSymBadBrdn(Mat B)
195: {
196: Mat_LMVM *lmvm;
197: PetscErrorCode ierr;
200: MatCreate_LMVMSymBrdn(B);
201: PetscObjectChangeTypeName((PetscObject)B, MATLMVMSYMBADBROYDEN);
202: B->ops->setfromoptions = MatSetFromOptions_LMVMSymBadBrdn;
203: B->ops->solve = MatSolve_LMVMSymBadBrdn;
205: lmvm = (Mat_LMVM*)B->data;
206: lmvm->ops->mult = MatMult_LMVMSymBadBrdn;
207: return(0);
208: }
210: /*------------------------------------------------------------*/
212: /*@
213: MatCreateLMVMSymBadBroyden - Creates a limited-memory Symmetric "Bad" Broyden-type matrix used
214: for approximating Jacobians. L-SymBadBrdn is a convex combination of L-DFP and
215: L-BFGS such that `^{-1} = (1 - phi)*BFGS^{-1} + phi*DFP^{-1}. The combination factor
216: phi is restricted to the range [0, 1], where the L-SymBadBrdn matrix is guaranteed
217: to be symmetric positive-definite. Note that this combination is on the inverses and not
218: on the forwards. For forward convex combinations, use the L-SymBrdn matrix.
220: The provided local and global sizes must match the solution and function vectors
221: used with MatLMVMUpdate() and MatSolve(). The resulting L-SymBrdn matrix will have
222: storage vectors allocated with VecCreateSeq() in serial and VecCreateMPI() in
223: parallel. To use the L-SymBrdn matrix with other vector types, the matrix must be
224: created using MatCreate() and MatSetType(), followed by MatLMVMAllocate().
225: This ensures that the internal storage and work vectors are duplicated from the
226: correct type of vector.
228: Collective
230: Input Parameters:
231: + comm - MPI communicator, set to PETSC_COMM_SELF
232: . n - number of local rows for storage vectors
233: - N - global size of the storage vectors
235: Output Parameter:
236: . B - the matrix
238: It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions()
239: paradigm instead of this routine directly.
241: Options Database Keys:
242: + -mat_lmvm_num_vecs - maximum number of correction vectors (i.e.: updates) stored
243: . -mat_lmvm_phi - (developer) convex ratio between BFGS and DFP components of the update
244: . -mat_lmvm_scale_type - (developer) type of scaling applied to J0 (none, scalar, diagonal)
245: . -mat_lmvm_theta - (developer) convex ratio between BFGS and DFP components of the diagonal J0 scaling
246: . -mat_lmvm_rho - (developer) update limiter for the J0 scaling
247: . -mat_lmvm_alpha - (developer) coefficient factor for the quadratic subproblem in J0 scaling
248: . -mat_lmvm_beta - (developer) exponential factor for the diagonal J0 scaling
249: - -mat_lmvm_sigma_hist - (developer) number of past updates to use in J0 scaling
251: Level: intermediate
253: .seealso: MatCreate(), MATLMVM, MATLMVMSYMBROYDEN, MatCreateLMVMDFP(), MatCreateLMVMSR1(),
254: MatCreateLMVMBFGS(), MatCreateLMVMBrdn(), MatCreateLMVMBadBrdn()
255: @*/
256: PetscErrorCode MatCreateLMVMSymBadBroyden(MPI_Comm comm, PetscInt n, PetscInt N, Mat *B)
257: {
258: PetscErrorCode ierr;
261: MatCreate(comm, B);
262: MatSetSizes(*B, n, n, N, N);
263: MatSetType(*B, MATLMVMSYMBADBROYDEN);
264: MatSetUp(*B);
265: return(0);
266: }