Actual source code: isutil.c
1: #include <petsctao.h>
2: #include <petsc/private/vecimpl.h>
3: #include <petsc/private/taoimpl.h>
4: #include <../src/tao/matrix/submatfree.h>
6: /*@
7: TaoVecGetSubVec - Gets a subvector using the `IS`
9: Input Parameters:
10: + vfull - the full matrix
11: . is - the index set for the subvector
12: . reduced_type - the method `Tao` is using for subsetting
13: - maskvalue - the value to set the unused vector elements to (for `TAO_SUBSET_MASK` or `TAO_SUBSET_MATRIXFREE`)
15: Output Parameter:
16: . vreduced - the subvector
18: Level: developer
20: Notes:
21: `maskvalue` should usually be `0.0`, unless a pointwise divide will be used.
23: .seealso: `TaoMatGetSubMat()`, `TaoSubsetType`
24: @*/
25: PetscErrorCode TaoVecGetSubVec(Vec vfull, IS is, TaoSubsetType reduced_type, PetscReal maskvalue, Vec *vreduced)
26: {
27: PetscInt nfull, nreduced, nreduced_local, rlow, rhigh, flow, fhigh;
28: PetscInt i, nlocal;
29: PetscReal *fv, *rv;
30: const PetscInt *s;
31: IS ident;
32: VecType vtype;
33: VecScatter scatter;
34: MPI_Comm comm;
36: PetscFunctionBegin;
40: PetscCall(VecGetSize(vfull, &nfull));
41: PetscCall(ISGetSize(is, &nreduced));
43: if (nreduced == nfull) {
44: PetscCall(VecDestroy(vreduced));
45: PetscCall(VecDuplicate(vfull, vreduced));
46: PetscCall(VecCopy(vfull, *vreduced));
47: } else {
48: switch (reduced_type) {
49: case TAO_SUBSET_SUBVEC:
50: PetscCall(VecGetType(vfull, &vtype));
51: PetscCall(VecGetOwnershipRange(vfull, &flow, &fhigh));
52: PetscCall(ISGetLocalSize(is, &nreduced_local));
53: PetscCall(PetscObjectGetComm((PetscObject)vfull, &comm));
54: if (*vreduced) PetscCall(VecDestroy(vreduced));
55: PetscCall(VecCreate(comm, vreduced));
56: PetscCall(VecSetType(*vreduced, vtype));
58: PetscCall(VecSetSizes(*vreduced, nreduced_local, nreduced));
59: PetscCall(VecGetOwnershipRange(*vreduced, &rlow, &rhigh));
60: PetscCall(ISCreateStride(comm, nreduced_local, rlow, 1, &ident));
61: PetscCall(VecScatterCreate(vfull, is, *vreduced, ident, &scatter));
62: PetscCall(VecScatterBegin(scatter, vfull, *vreduced, INSERT_VALUES, SCATTER_FORWARD));
63: PetscCall(VecScatterEnd(scatter, vfull, *vreduced, INSERT_VALUES, SCATTER_FORWARD));
64: PetscCall(VecScatterDestroy(&scatter));
65: PetscCall(ISDestroy(&ident));
66: break;
68: case TAO_SUBSET_MASK:
69: case TAO_SUBSET_MATRIXFREE:
70: /* vr[i] = vf[i] if i in is
71: vr[i] = 0 otherwise */
72: if (!*vreduced) PetscCall(VecDuplicate(vfull, vreduced));
74: PetscCall(VecSet(*vreduced, maskvalue));
75: PetscCall(ISGetLocalSize(is, &nlocal));
76: PetscCall(VecGetOwnershipRange(vfull, &flow, &fhigh));
77: PetscCall(VecGetArray(vfull, &fv));
78: PetscCall(VecGetArray(*vreduced, &rv));
79: PetscCall(ISGetIndices(is, &s));
80: PetscCheck(nlocal <= (fhigh - flow), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "IS local size %" PetscInt_FMT " > Vec local size %" PetscInt_FMT, nlocal, fhigh - flow);
81: for (i = 0; i < nlocal; ++i) rv[s[i] - flow] = fv[s[i] - flow];
82: PetscCall(ISRestoreIndices(is, &s));
83: PetscCall(VecRestoreArray(vfull, &fv));
84: PetscCall(VecRestoreArray(*vreduced, &rv));
85: break;
86: }
87: }
88: PetscFunctionReturn(PETSC_SUCCESS);
89: }
91: /*@
92: TaoMatGetSubMat - Gets a submatrix using the `IS`
94: Input Parameters:
95: + M - the full matrix (`n x n`)
96: . is - the index set for the submatrix (both row and column index sets need to be the same)
97: . v1 - work vector of dimension n, needed for `TAO_SUBSET_MASK` option
98: - subset_type - the method `Tao` is using for subsetting
100: Output Parameter:
101: . Msub - the submatrix
103: Level: developer
105: .seealso: `TaoVecGetSubVec()`, `TaoSubsetType`
106: @*/
107: PetscErrorCode TaoMatGetSubMat(Mat M, IS is, Vec v1, TaoSubsetType subset_type, Mat *Msub)
108: {
109: IS iscomp;
110: PetscBool flg = PETSC_TRUE;
112: PetscFunctionBegin;
115: PetscCall(MatDestroy(Msub));
116: switch (subset_type) {
117: case TAO_SUBSET_SUBVEC:
118: PetscCall(MatCreateSubMatrix(M, is, is, MAT_INITIAL_MATRIX, Msub));
119: break;
121: case TAO_SUBSET_MASK:
122: /* Get Reduced Hessian
123: Msub[i,j] = M[i,j] if i,j in Free_Local or i==j
124: Msub[i,j] = 0 if i!=j and i or j not in Free_Local
125: */
126: PetscObjectOptionsBegin((PetscObject)M);
127: PetscCall(PetscOptionsBool("-overwrite_hessian", "modify the existing hessian matrix when computing submatrices", "TaoSubsetType", flg, &flg, NULL));
128: PetscOptionsEnd();
129: if (flg) PetscCall(MatDuplicate(M, MAT_COPY_VALUES, Msub));
130: else {
131: /* Act on hessian directly (default) */
132: PetscCall(PetscObjectReference((PetscObject)M));
133: *Msub = M;
134: }
135: /* Save the diagonal to temporary vector */
136: PetscCall(MatGetDiagonal(*Msub, v1));
138: /* Zero out rows and columns */
139: PetscCall(ISComplementVec(is, v1, &iscomp));
141: /* Use v1 instead of 0 here because of PETSc bug */
142: PetscCall(MatZeroRowsColumnsIS(*Msub, iscomp, 1.0, v1, v1));
144: PetscCall(ISDestroy(&iscomp));
145: break;
146: case TAO_SUBSET_MATRIXFREE:
147: PetscCall(ISComplementVec(is, v1, &iscomp));
148: PetscCall(MatCreateSubMatrixFree(M, iscomp, iscomp, Msub));
149: PetscCall(ISDestroy(&iscomp));
150: break;
151: }
152: PetscFunctionReturn(PETSC_SUCCESS);
153: }
155: /*@
156: TaoEstimateActiveBounds - Generates index sets for variables at the lower and upper
157: bounds, as well as fixed variables where lower and upper bounds equal each other.
159: Input Parameters:
160: + X - solution vector
161: . XL - lower bound vector, may be `NULL`
162: . XU - upper bound vector, may be `NULL`
163: . G - unprojected gradient
164: . S - step direction with which the active bounds will be estimated
165: . W - work vector of type and size of `X`
166: - steplen - the step length at which the active bounds will be estimated (needs to be conservative)
168: Output Parameters:
169: + bound_tol - tolerance for the bound estimation
170: . active_lower - index set for active variables at the lower bound
171: . active_upper - index set for active variables at the upper bound
172: . active_fixed - index set for fixed variables
173: . active - index set for all active variables
174: - inactive - complementary index set for inactive variables
176: Level: developer
178: Notes:
179: This estimation is based on Bertsekas' method, with a built in diagonal scaling value of `1.0e-3`.
181: .seealso: `TAOBNCG`, `TAOBNTL`, `TAOBNTR`, `TaoBoundSolution()`
182: @*/
183: PetscErrorCode TaoEstimateActiveBounds(Vec X, Vec XL, Vec XU, Vec G, Vec S, Vec W, PetscReal steplen, PetscReal *bound_tol, IS *active_lower, IS *active_upper, IS *active_fixed, IS *active, IS *inactive)
184: {
185: PetscReal wnorm;
186: PetscReal zero = PetscPowReal(PETSC_MACHINE_EPSILON, 2.0 / 3.0);
187: PetscInt i, n_isl = 0, n_isu = 0, n_isf = 0, n_isa = 0, n_isi = 0;
188: PetscInt N_isl, N_isu, N_isf, N_isa, N_isi;
189: PetscInt n, low, high, nDiff;
190: PetscInt *isl = NULL, *isu = NULL, *isf = NULL, *isa = NULL, *isi = NULL;
191: const PetscScalar *xl = NULL, *xu = NULL, *x, *g;
192: MPI_Comm comm = PetscObjectComm((PetscObject)X);
194: PetscFunctionBegin;
202: if (XL) PetscCheckSameComm(X, 1, XL, 2);
203: if (XU) PetscCheckSameComm(X, 1, XU, 3);
204: PetscCheckSameComm(X, 1, G, 4);
205: PetscCheckSameComm(X, 1, S, 5);
206: PetscCheckSameComm(X, 1, W, 6);
207: if (XL) VecCheckSameSize(X, 1, XL, 2);
208: if (XU) VecCheckSameSize(X, 1, XU, 3);
209: VecCheckSameSize(X, 1, G, 4);
210: VecCheckSameSize(X, 1, S, 5);
211: VecCheckSameSize(X, 1, W, 6);
213: /* Update the tolerance for bound detection (this is based on Bertsekas' method) */
214: PetscCall(VecCopy(X, W));
215: PetscCall(VecAXPBY(W, steplen, 1.0, S));
216: PetscCall(TaoBoundSolution(W, XL, XU, 0.0, &nDiff, W));
217: PetscCall(VecAXPBY(W, 1.0, -1.0, X));
218: PetscCall(VecNorm(W, NORM_2, &wnorm));
219: *bound_tol = PetscMin(*bound_tol, wnorm);
221: /* Clear all index sets */
222: PetscCall(ISDestroy(active_lower));
223: PetscCall(ISDestroy(active_upper));
224: PetscCall(ISDestroy(active_fixed));
225: PetscCall(ISDestroy(active));
226: PetscCall(ISDestroy(inactive));
228: PetscCall(VecGetOwnershipRange(X, &low, &high));
229: PetscCall(VecGetLocalSize(X, &n));
230: if (!XL && !XU) {
231: PetscCall(ISCreateStride(comm, n, low, 1, inactive));
232: PetscFunctionReturn(PETSC_SUCCESS);
233: }
234: if (n > 0) {
235: PetscCall(VecGetArrayRead(X, &x));
236: if (XL) PetscCall(VecGetArrayRead(XL, &xl));
237: if (XU) PetscCall(VecGetArrayRead(XU, &xu));
238: PetscCall(VecGetArrayRead(G, &g));
240: /* Loop over variables and categorize the indexes */
241: PetscCall(PetscMalloc1(n, &isl));
242: PetscCall(PetscMalloc1(n, &isu));
243: PetscCall(PetscMalloc1(n, &isf));
244: PetscCall(PetscMalloc1(n, &isa));
245: PetscCall(PetscMalloc1(n, &isi));
246: for (i = 0; i < n; ++i) {
247: if (XL && XU && xl[i] == xu[i]) {
248: /* Fixed variables */
249: isf[n_isf] = low + i;
250: ++n_isf;
251: isa[n_isa] = low + i;
252: ++n_isa;
253: } else if (XL && xl[i] > PETSC_NINFINITY && x[i] <= xl[i] + *bound_tol && g[i] > zero) {
254: /* Lower bounded variables */
255: isl[n_isl] = low + i;
256: ++n_isl;
257: isa[n_isa] = low + i;
258: ++n_isa;
259: } else if (XU && xu[i] < PETSC_INFINITY && x[i] >= xu[i] - *bound_tol && g[i] < zero) {
260: /* Upper bounded variables */
261: isu[n_isu] = low + i;
262: ++n_isu;
263: isa[n_isa] = low + i;
264: ++n_isa;
265: } else {
266: /* Inactive variables */
267: isi[n_isi] = low + i;
268: ++n_isi;
269: }
270: }
272: PetscCall(VecRestoreArrayRead(X, &x));
273: if (XL) PetscCall(VecRestoreArrayRead(XL, &xl));
274: if (XU) PetscCall(VecRestoreArrayRead(XU, &xu));
275: PetscCall(VecRestoreArrayRead(G, &g));
276: }
278: /* Collect global sizes */
279: PetscCallMPI(MPIU_Allreduce(&n_isl, &N_isl, 1, MPIU_INT, MPI_SUM, comm));
280: PetscCallMPI(MPIU_Allreduce(&n_isu, &N_isu, 1, MPIU_INT, MPI_SUM, comm));
281: PetscCallMPI(MPIU_Allreduce(&n_isf, &N_isf, 1, MPIU_INT, MPI_SUM, comm));
282: PetscCallMPI(MPIU_Allreduce(&n_isa, &N_isa, 1, MPIU_INT, MPI_SUM, comm));
283: PetscCallMPI(MPIU_Allreduce(&n_isi, &N_isi, 1, MPIU_INT, MPI_SUM, comm));
285: /* Create index set for lower bounded variables */
286: if (N_isl > 0) {
287: PetscCall(ISCreateGeneral(comm, n_isl, isl, PETSC_OWN_POINTER, active_lower));
288: } else {
289: PetscCall(PetscFree(isl));
290: }
291: /* Create index set for upper bounded variables */
292: if (N_isu > 0) {
293: PetscCall(ISCreateGeneral(comm, n_isu, isu, PETSC_OWN_POINTER, active_upper));
294: } else {
295: PetscCall(PetscFree(isu));
296: }
297: /* Create index set for fixed variables */
298: if (N_isf > 0) {
299: PetscCall(ISCreateGeneral(comm, n_isf, isf, PETSC_OWN_POINTER, active_fixed));
300: } else {
301: PetscCall(PetscFree(isf));
302: }
303: /* Create index set for all actively bounded variables */
304: if (N_isa > 0) {
305: PetscCall(ISCreateGeneral(comm, n_isa, isa, PETSC_OWN_POINTER, active));
306: } else {
307: PetscCall(PetscFree(isa));
308: }
309: /* Create index set for all inactive variables */
310: if (N_isi > 0) {
311: PetscCall(ISCreateGeneral(comm, n_isi, isi, PETSC_OWN_POINTER, inactive));
312: } else {
313: PetscCall(PetscFree(isi));
314: }
315: PetscFunctionReturn(PETSC_SUCCESS);
316: }
318: /*@
319: TaoBoundStep - Ensures the correct zero or adjusted step direction values for active
320: variables.
322: Input Parameters:
323: + X - solution vector
324: . XL - lower bound vector
325: . XU - upper bound vector
326: . active_lower - index set for lower bounded active variables
327: . active_upper - index set for lower bounded active variables
328: . active_fixed - index set for fixed active variables
329: - scale - amplification factor for the step that needs to be taken on actively bounded variables
331: Output Parameter:
332: . S - step direction to be modified
334: Level: developer
336: .seealso: `TAOBNCG`, `TAOBNTL`, `TAOBNTR`, `TaoBoundSolution()`
337: @*/
338: PetscErrorCode TaoBoundStep(Vec X, Vec XL, Vec XU, IS active_lower, IS active_upper, IS active_fixed, PetscReal scale, Vec S)
339: {
340: Vec step_lower, step_upper, step_fixed;
341: Vec x_lower, x_upper;
342: Vec bound_lower, bound_upper;
344: PetscFunctionBegin;
345: /* Adjust step for variables at the estimated lower bound */
346: if (active_lower) {
347: PetscCall(VecGetSubVector(S, active_lower, &step_lower));
348: PetscCall(VecGetSubVector(X, active_lower, &x_lower));
349: PetscCall(VecGetSubVector(XL, active_lower, &bound_lower));
350: PetscCall(VecCopy(bound_lower, step_lower));
351: PetscCall(VecAXPY(step_lower, -1.0, x_lower));
352: PetscCall(VecScale(step_lower, scale));
353: PetscCall(VecRestoreSubVector(S, active_lower, &step_lower));
354: PetscCall(VecRestoreSubVector(X, active_lower, &x_lower));
355: PetscCall(VecRestoreSubVector(XL, active_lower, &bound_lower));
356: }
358: /* Adjust step for the variables at the estimated upper bound */
359: if (active_upper) {
360: PetscCall(VecGetSubVector(S, active_upper, &step_upper));
361: PetscCall(VecGetSubVector(X, active_upper, &x_upper));
362: PetscCall(VecGetSubVector(XU, active_upper, &bound_upper));
363: PetscCall(VecCopy(bound_upper, step_upper));
364: PetscCall(VecAXPY(step_upper, -1.0, x_upper));
365: PetscCall(VecScale(step_upper, scale));
366: PetscCall(VecRestoreSubVector(S, active_upper, &step_upper));
367: PetscCall(VecRestoreSubVector(X, active_upper, &x_upper));
368: PetscCall(VecRestoreSubVector(XU, active_upper, &bound_upper));
369: }
371: /* Zero out step for fixed variables */
372: if (active_fixed) {
373: PetscCall(VecGetSubVector(S, active_fixed, &step_fixed));
374: PetscCall(VecSet(step_fixed, 0.0));
375: PetscCall(VecRestoreSubVector(S, active_fixed, &step_fixed));
376: }
377: PetscFunctionReturn(PETSC_SUCCESS);
378: }
380: /*@
381: TaoBoundSolution - Ensures that the solution vector is snapped into the bounds within a given tolerance.
383: Collective
385: Input Parameters:
386: + X - solution vector
387: . XL - lower bound vector, may be `NULL`
388: . XU - upper bound vector, may be `NULL`
389: - bound_tol - absolute tolerance in enforcing the bound
391: Output Parameters:
392: + nDiff - total number of vector entries that have been bounded
393: - Xout - modified solution vector satisfying bounds to `bound_tol`
395: Level: developer
397: .seealso: `TAOBNCG`, `TAOBNTL`, `TAOBNTR`, `TaoBoundStep()`
398: @*/
399: PetscErrorCode TaoBoundSolution(Vec X, Vec XL, Vec XU, PetscReal bound_tol, PetscInt *nDiff, Vec Xout)
400: {
401: PetscInt i, n, low, high;
402: PetscScalar *xout;
403: const PetscScalar *x, *xl = NULL, *xu = NULL;
405: PetscFunctionBegin;
410: if (!XL && !XU) {
411: PetscCall(VecCopy(X, Xout));
412: *nDiff = 0;
413: PetscFunctionReturn(PETSC_SUCCESS);
414: }
415: if (XL) PetscCheckSameComm(X, 1, XL, 2);
416: if (XU) PetscCheckSameComm(X, 1, XU, 3);
417: PetscCheckSameComm(X, 1, Xout, 6);
418: if (XL) VecCheckSameSize(X, 1, XL, 2);
419: if (XU) VecCheckSameSize(X, 1, XU, 3);
420: VecCheckSameSize(X, 1, Xout, 6);
422: PetscCall(VecGetOwnershipRange(X, &low, &high));
423: PetscCall(VecGetLocalSize(X, &n));
424: *nDiff = 0;
425: if (n > 0) {
426: PetscCall(VecGetArrayRead(X, &x));
427: if (XL) PetscCall(VecGetArrayRead(XL, &xl));
428: if (XU) PetscCall(VecGetArrayRead(XU, &xu));
429: PetscCall(VecGetArray(Xout, &xout));
431: for (i = 0; i < n; ++i) {
432: if (XL && xl[i] > PETSC_NINFINITY && x[i] <= xl[i] + bound_tol) {
433: xout[i] = xl[i];
434: ++(*nDiff);
435: } else if (XU && xu[i] < PETSC_INFINITY && x[i] >= xu[i] - bound_tol) {
436: xout[i] = xu[i];
437: ++(*nDiff);
438: }
439: }
441: PetscCall(VecRestoreArrayRead(X, &x));
442: if (XL) PetscCall(VecRestoreArrayRead(XL, &xl));
443: if (XU) PetscCall(VecRestoreArrayRead(XU, &xu));
444: PetscCall(VecRestoreArray(Xout, &xout));
445: }
446: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, nDiff, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)X)));
447: PetscFunctionReturn(PETSC_SUCCESS);
448: }