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