Skip to content

Commit 5b75a70

Browse files
committed
added api for lumped weight; some methods renaming
1 parent 7e20b4e commit 5b75a70

File tree

5 files changed

+80
-53
lines changed

5 files changed

+80
-53
lines changed

src/Drivers/Dense/NlpDenseConsEx1.hpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ class DiscretizedFunction : public hiop::hiopVectorPar
112112
class DenseConsEx1 : public hiop::hiopInterfaceDenseConstraints
113113
{
114114
public:
115-
DenseConsEx1(int n_mesh_elem, double mesh_ratio=1.0, bool use_weighted_vars=false)
115+
DenseConsEx1(int n_mesh_elem, double mesh_ratio=1.0, int type_weighted_vars=0)
116116
: n_vars(n_mesh_elem),
117117
comm(MPI_COMM_WORLD),
118-
use_weighted_space_(use_weighted_vars),
118+
type_weighted_space_((hiopInterfaceBase::WeightedSpaceType)type_weighted_vars),
119119
solver_(nullptr)
120120
{
121121
// create the members
@@ -259,7 +259,7 @@ class DenseConsEx1 : public hiop::hiopInterfaceDenseConstraints
259259
double alpha_pr,
260260
int ls_trials);
261261

262-
bool applyM(const size_type& n, const double* x_in, double* y_out)
262+
bool apply_M(const size_type& n, const double* x_in, double* y_out)
263263
{
264264
x->copyFrom(x_in);
265265
_mesh->applyM(*x);
@@ -270,7 +270,7 @@ class DenseConsEx1 : public hiop::hiopInterfaceDenseConstraints
270270
/**
271271
* Computes action y of the inner product weight matrix H on a vector x, namely y=H*x
272272
*/
273-
virtual bool applyH(const size_type& n, const double* x_in, double* y_out)
273+
virtual bool apply_H(const size_type& n, const double* x_in, double* y_out)
274274
{
275275
x->copyFrom(x_in);
276276
_mesh->applyM(*x);
@@ -281,7 +281,7 @@ class DenseConsEx1 : public hiop::hiopInterfaceDenseConstraints
281281
/**
282282
* Computes action y of the inverse of H on a vector x, namely y=H^{-1}*x
283283
*/
284-
virtual bool applyHinv(const size_type& n, const double* x_in, double* y_out)
284+
virtual bool apply_Hinv(const size_type& n, const double* x_in, double* y_out)
285285
{
286286
x->copyFrom(x_in);
287287
_mesh->applyMinv(*x);
@@ -290,17 +290,20 @@ class DenseConsEx1 : public hiop::hiopInterfaceDenseConstraints
290290
}
291291

292292
/**
293-
* Enables the use of weighted inner products via @applyM, @applyH, and @applyHinv
293+
* Enables the use of weighted inner products via @apply_M, @apply_H, and @apply_Hinv
294294
*/
295-
virtual bool useWeightedVectorSpace()
295+
virtual hiopInterfaceBase::WeightedSpaceType get_weighted_space_type()
296296
{
297-
return use_weighted_space_;
297+
// return
298+
// - hiopInterfaceBase::Euclidean for no weighted space
299+
// - hiopInterfaceBase::HilbertLumped for space weighted by lumped mass matrix
300+
return type_weighted_space_;
298301
}
299302

300303
private:
301304
int n_vars;
302305
MPI_Comm comm;
303-
bool use_weighted_space_;
306+
hiopInterfaceBase::WeightedSpaceType type_weighted_space_;
304307
Ex1Meshing1D* _mesh;
305308

306309
int n_local;

src/Interface/hiopInterface.hpp

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161

6262
namespace hiop
6363
{
64-
/** Solver status codes. */
64+
// Solver status codes
6565
enum hiopSolveStatus
6666
{
6767
//(partial) success
@@ -142,6 +142,24 @@ class hiopInterfaceBase
142142
hiopQuadratic,
143143
hiopNonlinear
144144
};
145+
/**
146+
* Type of weighting to be used internally for computing norm and inner products, for determining
147+
* representations of dual bound variables, and initial quasi-Newton Hessian approximations.
148+
*/
149+
enum WeightedSpaceType
150+
{
151+
// no weighted space.
152+
Euclidean = 0,
153+
154+
// lumps mass matrix and uses lumped matrix everywhere
155+
HilbertLumped,
156+
157+
/**
158+
* Experimental: lumps mass matrix and uses for dual bound representers and initial Hessian
159+
* approximations, however, uses H and H-inverse norms in termination criteria
160+
*/
161+
Hilbert
162+
};
145163

146164
public:
147165
hiopInterfaceBase() {};
@@ -288,17 +306,17 @@ class hiopInterfaceBase
288306
* mesh independent performance of the algorithm. Currently M is lumped into a diagonal
289307
* and used internally for the abovementioned dual variables.
290308
*
291-
* @note If the variables are not coming from discretizations (specified by a false
292-
* value returned by @useWeightedVectorSpace), the methods should return false. HiOp
293-
* will use internally M=I. Otherwise, should return true or false depending on whether
294-
* the user code succesfully applied M.
309+
* @note If the variables are not coming from discretizations (specified by
310+
* a hiopInterfaceBase::Euclidean return value from @get_weighted_space_type), the method is
311+
* not called and HiOp will use internally M=I. Otherwise, should true or false depending
312+
* on whether the user code succesfully applied M.
295313
*
296314
* @param[in] n the (global) number of variables
297315
* @param[in] x the array to which mass action is applied
298316
* @param[out] y the result of apply
299317
* @return see note above.
300318
*/
301-
virtual bool applyM(const size_type& n, const double* x, double* y)
319+
virtual bool apply_M(const size_type& n, const double* x, double* y)
302320
{
303321
return true;
304322
}
@@ -316,10 +334,9 @@ class hiopInterfaceBase
316334
* interior-point method for PDE-constrained optimization using finite element
317335
* discretizations, Optimiz. Meth. and Software, Vol. 38, 2023.
318336
*
319-
* @note If the variables are not coming from discretizations (specified by a false
320-
* value returned by @useWeightedVectorSpace), the methods should return false. HiOp
321-
* will use internally H=I. Otherwise, should return true or false depending on whether
322-
* the user code succesfully applied H.
337+
* @note The method is called only when hiopInterfaceBase::Hilbert is returned by
338+
* @get_weighted_space_type. In this case, it should return true or false depending on
339+
* whether the user code succesfully applied H.
323340
*
324341
* @note Currently HiOp only uses H for computing the inner products and norms (including
325342
* for vector representations of duals discretizations). The lumped mass matrix M is used
@@ -331,39 +348,46 @@ class hiopInterfaceBase
331348
* @param[out] y the result of apply
332349
* @return see note above
333350
*/
334-
virtual bool applyH(const size_type& n, const double* x, double* y)
351+
virtual bool apply_H(const size_type& n, const double* x, double* y)
335352
{
336353
return true;
337354
}
338355

339356
/**
340357
* Computes action y of the inverse of H on a vector x, namely y=H^{-1}*x
341358
*
342-
* See @applyH for a discussion of H and additional notes. The inverse of H plays an
359+
* See @apply_H for a discussion of H and additional notes. The inverse of H plays an
343360
* important role in computing the "dual" norms and, in turn, to ensure mesh
344-
* independent behavior of the IPM solver(s). Also see notes from @applyH.
361+
* independent behavior of the IPM solver(s). Also see notes from @apply_H.
345362
*
346363
* @param[in] n the (global) number of variables
347364
* @param[in] x the array to which the inverse is applied
348365
* @param[out] y the result of apply
349-
* @return see return of @applyH
366+
* @return see return of @apply_H
350367
*/
351-
virtual bool applyHinv(const size_type& n, const double* x, double* y)
368+
virtual bool apply_Hinv(const size_type& n, const double* x, double* y)
352369
{
353370
return true;
354371
}
355372

356373
/**
357-
* Enables the use of weighted inner products via @applyM, @applyH, and @applyHinv
374+
* Enables the use of weighted inner products via @apply_M, @apply_H, and @apply_Hinv
375+
*
376+
* See @WeightedSpaceType for the return values of this function and their meaning. If the
377+
* return value is
378+
* - hiopInterfaceBase::Euclidean: @apply_M, @apply_H, and @apply_Hinv need not be overriden by user.
379+
* If provided, they are not called.
380+
* - hiopInterfaceBase::HilbertLumped: only @apply_M is called. @apply_H and @apply_Hinv are not.
381+
* - hiopInterfaceBase::Hilbert: @apply_M, @apply_H, and @apply_Hinv are called.
358382
*
359-
* See also notes for @applyM, @applyH, and @applyHinv.
383+
* See also notes for @apply_M, @apply_H, and @apply_Hinv.
360384
*
361-
* @return true if enabled, false to default to Euclidean space with <u,v>=u^T*v
385+
* @return WeightedSpaceType (see @WeightedSpaceType and notes above
362386
*/
363-
virtual bool useWeightedVectorSpace()
387+
virtual WeightedSpaceType get_weighted_space_type()
364388
{
365389
//the default impl. instructs HiOp to use Euclidean/l^2 variables (H=I) internally
366-
return false;
390+
return Euclidean;
367391
}
368392

369393
/**

src/Optimization/VectorSpace.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ VectorSpace::VectorSpace(hiopNlpFormulation* nlp)
6363
{
6464
assert(nlp);
6565
M_lump_ = nullptr;
66-
if(nlp->useWeightedVectorSpace()) {
66+
if(nlp->get_weighted_space_type()) {
6767
vec_n_ = nlp_->alloc_primal_vec();
6868
vec_n2_ = nlp_->alloc_primal_vec();
6969
} else {
@@ -81,7 +81,7 @@ VectorSpace::~VectorSpace()
8181

8282
//bool VectorSpace::apply_M(const hiopVector& x, hiopVector& y) const
8383
//{
84-
// if(nlp_->useWeightedVectorSpace()) {
84+
// if(nlp_->get_weighted_space_type()) {
8585
// return nlp_->eval_M(x, y);
8686
// } else {
8787
// y.copyFrom(x);
@@ -100,7 +100,7 @@ bool VectorSpace::apply_M_lumped(const hiopVector& x, hiopVector& y) const
100100
// Computes H primal norm
101101
double VectorSpace::norm_H_primal(const hiopVector& x) const
102102
{
103-
if(nlp_->useWeightedVectorSpace()) {
103+
if(nlp_->get_weighted_space_type()) {
104104
nlp_->eval_H(x, *vec_n_);
105105
auto dp = x.dotProductWith(*vec_n_);
106106
return ::std::sqrt(dp);
@@ -111,7 +111,7 @@ double VectorSpace::norm_H_primal(const hiopVector& x) const
111111
// Computes H dual norm
112112
double VectorSpace::norm_H_dual(const hiopVector& x) const
113113
{
114-
if(nlp_->useWeightedVectorSpace()) {
114+
if(nlp_->get_weighted_space_type()) {
115115
nlp_->eval_H_inv(x, *vec_n_);
116116
auto dp = x.dotProductWith(*vec_n_);
117117
return ::std::sqrt(dp);
@@ -122,7 +122,7 @@ double VectorSpace::norm_H_dual(const hiopVector& x) const
122122

123123
double VectorSpace::norm_stationarity(const hiopVector& x) const
124124
{
125-
if(nlp_->useWeightedVectorSpace()) {
125+
if(nlp_->get_weighted_space_type()) {
126126
vec_n_->copyFrom(x);
127127
vec_n_->componentDiv(*M_lumped());
128128
return vec_n_->infnorm();
@@ -134,7 +134,7 @@ double VectorSpace::norm_stationarity(const hiopVector& x) const
134134
// Compute norm one weighted by M, i.e., 1^T*M*|x|
135135
double VectorSpace::norm_M_one(const hiopVector& x) const
136136
{
137-
if(nlp_->useWeightedVectorSpace()) {
137+
if(nlp_->get_weighted_space_type()) {
138138
//use vec_n2_ since vec_n_ may be changed in M_lumped_
139139
vec_n2_->copyFrom(x);
140140
vec_n2_->component_abs();
@@ -147,7 +147,7 @@ double VectorSpace::norm_M_one(const hiopVector& x) const
147147

148148
double VectorSpace::norm_complementarity(const hiopVector& x) const
149149
{
150-
if(nlp_->useWeightedVectorSpace()) {
150+
if(nlp_->get_weighted_space_type()) {
151151
// since both x (slacks) and the bound duals are in the same (primal) space, inf norm "is
152152
// mesh independent".
153153
return x.infnorm();
@@ -159,7 +159,7 @@ double VectorSpace::norm_complementarity(const hiopVector& x) const
159159
// Computes the "volume" of the space, 1^T M*1
160160
double VectorSpace::volume() const
161161
{
162-
if(nlp_->useWeightedVectorSpace()) {
162+
if(nlp_->get_weighted_space_type()) {
163163
double vol_total = nlp_->m_ineq_low() + nlp_->m_ineq_upp();
164164
if(nlp_->n_low() > 0 || nlp_->n_upp() > 0) {
165165
//compute ||1||_M
@@ -184,7 +184,7 @@ const hiopVector* VectorSpace::M_lumped() const
184184
{
185185
if(M_lump_ == nullptr) {
186186
M_lump_ = nlp_->alloc_primal_vec();
187-
if(nlp_->useWeightedVectorSpace()) {
187+
if(nlp_->get_weighted_space_type()) {
188188
vec_n_->setToConstant(1.);
189189
nlp_->eval_M(*vec_n_, *M_lump_);
190190
} else {
@@ -197,7 +197,7 @@ const hiopVector* VectorSpace::M_lumped() const
197197
void VectorSpace::
198198
add_linear_damping_term(const hiopVector& ixl, const hiopVector& ixu, const double& ct, hiopVector& x) const
199199
{
200-
if(nlp_->useWeightedVectorSpace()) {
200+
if(nlp_->get_weighted_space_type()) {
201201
vec_n_->copyFrom(ixl);
202202
vec_n_->axpy(-1.0, ixu);
203203
vec_n_->componentMult(*M_lumped());
@@ -209,7 +209,7 @@ add_linear_damping_term(const hiopVector& ixl, const hiopVector& ixu, const doub
209209

210210
double VectorSpace::log_barrier_eval_local(const hiopVector& x, const hiopVector& ix) const
211211
{
212-
if(nlp_->useWeightedVectorSpace()) {
212+
if(nlp_->get_weighted_space_type()) {
213213
return x.logBarrierWeighted_local(ix, *M_lumped());
214214
} else {
215215
return x.logBarrier_local(ix);
@@ -219,7 +219,7 @@ double VectorSpace::log_barrier_eval_local(const hiopVector& x, const hiopVector
219219
// Adds (to `gradx`) the gradient of the weighted log, namely gradx = gradx - mu * M_lumped * ix/s
220220
void VectorSpace::log_barrier_grad_add(const double& mu, const hiopVector& s, const hiopVector& ix, hiopVector& gradx) const
221221
{
222-
if(nlp_->useWeightedVectorSpace()) {
222+
if(nlp_->get_weighted_space_type()) {
223223
vec_n_->copyFrom(ix);
224224
vec_n_->componentDiv(s);
225225
vec_n_->componentMult(*M_lumped());
@@ -235,7 +235,7 @@ double VectorSpace::linear_damping_term_local(const hiopVector& s,
235235
const double& mu,
236236
const double& kappa_d) const
237237
{
238-
if(nlp_->useWeightedVectorSpace()) {
238+
if(nlp_->get_weighted_space_type()) {
239239
vec_n_->copyFrom(s);
240240
vec_n_->componentMult(*M_lumped());
241241
return vec_n_->linearDampingTerm_local(ixl, ixr, mu, kappa_d);

src/Optimization/hiopNlpFormulation.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -779,9 +779,9 @@ bool hiopNlpFormulation::eval_M(const hiopVector& x, hiopVector& y)
779779
"weighted vector spaces not supported when fixed variables are removed");
780780

781781
runStats.tm_eval_M_apply.start();
782-
bool bret = interface_base.applyM(nlp_transformations_.n_pre(),
783-
x_full->local_data_const(),
784-
y_full->local_data());
782+
bool bret = interface_base.apply_M(nlp_transformations_.n_pre(),
783+
x_full->local_data_const(),
784+
y_full->local_data());
785785
runStats.tm_eval_M_apply.stop();
786786
runStats.n_eval_M_apply++;
787787

@@ -802,9 +802,9 @@ bool hiopNlpFormulation::eval_H(const hiopVector& x, hiopVector& y)
802802
"weighted vector spaces not supported when fixed variables are removed");
803803

804804
runStats.tm_eval_H_apply.start();
805-
bool bret = interface_base.applyH(nlp_transformations_.n_pre(),
806-
x_full->local_data_const(),
807-
y_full->local_data());
805+
bool bret = interface_base.apply_H(nlp_transformations_.n_pre(),
806+
x_full->local_data_const(),
807+
y_full->local_data());
808808
runStats.tm_eval_H_apply.stop();
809809
runStats.n_eval_H_apply++;
810810

@@ -823,9 +823,9 @@ bool hiopNlpFormulation::eval_H_inv(const hiopVector& x, hiopVector& y)
823823
"weighted vector spaces not supported when fixed variables are removed");
824824

825825
runStats.tm_eval_Hinv_apply.start();
826-
bool bret = interface_base.applyHinv(nlp_transformations_.n_pre(),
827-
x_full->local_data_const(),
828-
y_full->local_data());
826+
bool bret = interface_base.apply_Hinv(nlp_transformations_.n_pre(),
827+
x_full->local_data_const(),
828+
y_full->local_data());
829829
runStats.tm_eval_Hinv_apply.stop();
830830
runStats.n_eval_Hinv_apply++;
831831

src/Optimization/hiopNlpFormulation.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ class hiopNlpFormulation
118118
virtual bool eval_Jac_d(hiopVector& x, bool new_x, hiopMatrix& Jac_d) = 0;
119119
virtual bool eval_Jac_c_d(hiopVector& x, bool new_x, hiopMatrix& Jac_c, hiopMatrix& Jac_d);
120120

121-
inline bool useWeightedVectorSpace()
121+
inline bool get_weighted_space_type()
122122
{
123-
return interface_base.useWeightedVectorSpace();
123+
return interface_base.get_weighted_space_type();
124124
}
125125
/* Wrapper over user-defined M apply that also applies the NLP transformations. */
126126
bool eval_M(const hiopVector& x, hiopVector& y);

0 commit comments

Comments
 (0)