Debian Codegraph @

packages/rol/adapters/tpetra/src/function/ROL_TpetraBoundConstraint.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 /** \file
11 \brief Contains definitions for Tpetra::MultiVector bound constraints.
12 \author Created by G. von Winckel
13 */
14
15 #ifndef ROL_TPETRABOUNDCONSTRAINT_HPP
16 #define ROL_TPETRABOUNDCONSTRAINT_HPP
17
18 #include "Kokkos_Core.hpp"
19 #include "ROL_TpetraMultiVector.hpp"
20 #include "ROL_BoundConstraint.hpp"
21
22
23 namespace ROL {
24
25 namespace KokkosStructs { // Parallel for and reduce functions
26
27 //----------------------------------------------------------------------
28 //
29 // Find the minimum u_i-l_i
30 template<class Real, class V>
31 struct MinGap {
32 typedef typename V::execution_space execution_space;
33 V L_; // Lower bounds
34 V U_; // Upper bounds
35
36 MinGap(const V& L, const V& U) : L_(L), U_(U) {}
37
38 KOKKOS_INLINE_FUNCTION
39 void operator() (const int i, Real &min) const {
40 const int M = L_.extent(1);
41 for(int j=0;j<M;++j) {
42 Real gap = U_(i,j)-L_(i,j);
43 if(gap<min) {
44 min = gap;
45 }
46 }
47 }
48
49 KOKKOS_INLINE_FUNCTION
50 void init(Real &min) const {
51 min = U_(0,0)-L_(0,0);
52 }
53
54 KOKKOS_INLINE_FUNCTION
55 void join(Real &globalMin,
56 const Real &localMin) const {
57 if(localMin<globalMin) {
58 globalMin = localMin;
59 }
60 }
61 }; // End struct MinGap
62
63 //----------------------------------------------------------------------
64 //
65 // Determine if every l_i<=x_i<=u_i
66 template<class Real, class V>
67 struct Feasible {
68 typedef typename V::execution_space execution_space;
69 V X_; // Optimization variable
70 V L_; // Lower bounds
71 V U_; // Upper bounds
72
73 Feasible(const V& X, const V& L, const V& U) : X_(X), L_(L), U_(U) {}
74
75 KOKKOS_INLINE_FUNCTION
76 void operator() (const int i, int &feasible) const {
77 const int M = L_.extent(1);
78 for(int j=0;j<M;++j) {
79 if( (X_(i,j)<L_(i,j)) || (X_(i,j)>U_(i,j)) ) {
80 feasible = 0;
81 }
82 }
83 }
84
85 KOKKOS_INLINE_FUNCTION
86 void init(int &feasible) const {
87 feasible = 1;
88 }
89
90 KOKKOS_INLINE_FUNCTION
91 void join(int &globalFeasible,
92 const int &localFeasible) const {
93 globalFeasible *= localFeasible;
94 }
95
96 }; // End struct Feasible
97
98 //----------------------------------------------------------------------
99 //
100 // Project x onto the bounds
101 template<class Real, class V, class CV>
102 struct Project {
103 typedef typename V::execution_space execution_space;
104 V X_; // Optimization variable
105 CV L_; // Lower bounds
106 CV U_; // Upper bounds
107
108 Project(V& X, const CV& L, const CV& U) : X_(X), L_(L), U_(U) {}
109
110 KOKKOS_INLINE_FUNCTION
111 void operator() (const int i) const {
112 const int M = L_.extent(1);
113 for(int j=0;j<M;++j) {
114 if( X_(i,j)<L_(i,j) ) {
115 X_(i,j) = L_(i,j);
116 }
117 else if( X_(i,j)>U_(i,j) ) {
118 X_(i,j) = U_(i,j);
119 }
120 }
121 }
122 }; // End struct Project
123
124 //----------------------------------------------------------------------
125 //
126 // Set variables to zero if they correspond to the lower active set
127 template<class Real, class V, class CV>
128 struct PruneLowerActive {
129 typedef typename V::execution_space execution_space;
130 V Y_; // Variable to be pruned
131 CV X_; // Optimization variable
132 CV L_; // Lower bounds
133 Real eps_;
134
135 PruneLowerActive(V &Y, const CV &X, const CV &L, Real eps) :
136 Y_(Y), X_(X), L_(L), eps_(eps) {}
137
138 KOKKOS_INLINE_FUNCTION
139 void operator() (const int i) const {
140 const Real zero(0);
141 const int M = L_.extent(1);
142 for(int j=0;j<M;++j) {
143 if(X_(i,j)<=L_(i,j)+eps_) {
144 Y_(i,j) = zero;
145 }
146 }
147 }
148 }; // End struct PruneLowerActive
149
150 //----------------------------------------------------------------------
151 //
152 // Set variables to zero if they correspond to the upper active set
153 template<class Real, class V, class CV>
154 struct PruneUpperActive {
155 typedef typename V::execution_space execution_space;
156 V Y_; // Variable to be pruned
157 CV X_; // Optimization variable
158 CV U_; // Upper bounds
159 Real eps_;
160
161 PruneUpperActive(V &Y, const CV &X, const CV &U, Real eps) :
162 Y_(Y), X_(X), U_(U), eps_(eps) {}
163
164 KOKKOS_INLINE_FUNCTION
165 void operator() (const int i) const {
166 const Real zero(0);
167 const int M = U_.extent(1);
168 for(int j=0;j<M;++j) {
169 if(X_(i,j)>=U_(i,j)-eps_) {
170 Y_(i,j) = zero;
171 }
172 }
173 }
174 }; // End struct PruneUpperActive
175
176 //----------------------------------------------------------------------
177 //
178 // Set variables to zero if they correspond to the active set
179 template<class Real, class V, class CV>
180 struct PruneActive {
181 typedef typename V::execution_space execution_space;
182 V Y_; // Variable to be pruned
183 CV X_; // Optimization variable
184 CV L_; // Lower bounds
185 CV U_; // Upper bounds
186 Real eps_;
187
188 PruneActive(V &Y, const CV &X, const CV &L, const CV &U, Real eps) :
189 Y_(Y), X_(X), L_(L), U_(U), eps_(eps) {}
190
191 KOKKOS_INLINE_FUNCTION
192 void operator() (const int i) const {
193 const Real zero(0);
194 const int M = L_.extent(1);
195 for(int j=0;j<M;++j) {
196 if(X_(i,j)<=L_(i,j)+eps_) {
197 Y_(i,j) = zero;
198 }
199 else if(X_(i,j)>=U_(i,j)-eps_) {
200 Y_(i,j) = zero;
201 }
202 }
203 }
204 }; // End struct PruneActive
205
206 //----------------------------------------------------------------------
207 //
208 // Set variables to zero if they correspond to the lower active set and grad is positive
209 template<class Real, class V, class CV>
210 struct GradPruneLowerActive {
211 typedef typename V::execution_space execution_space;
212 V Y_; // Variable to be pruned
213 CV G_; // Gradient
214 CV X_; // Optimization variable
215 CV L_; // Lower bounds
216
217 Real xeps_, geps_;
218
219 GradPruneLowerActive(V &Y, const CV &G, const CV &X, const CV &L,Real xeps, Real geps) :
220 Y_(Y), G_(G), X_(X), L_(L), xeps_(xeps), geps_(geps) {}
221
222 KOKKOS_INLINE_FUNCTION
223 void operator() (const int i) const {
224 const Real zero(0);
225 const int M = L_.extent(1);
226 for(int j=0;j<M;++j) {
227 if( (X_(i,j)<=L_(i,j)+xeps_) && (G_(i,j)>geps_) ) {
228 Y_(i,j) = zero;
229 }
230 }
231 }
232 }; // End struct GradPruneLowerActive
233
234 //----------------------------------------------------------------------
235 //
236 // Set variables to zero if they correspond to the upper active set and grad is negative
237 template<class Real, class V, class CV>
238 struct GradPruneUpperActive {
239 typedef typename V::execution_space execution_space;
240 V Y_; // Variable to be pruned
241 CV G_; // Gradient
242 CV X_; // Optimization variable
243 CV U_; // Upper bounds
244
245 Real xeps_, geps_;
246
247 GradPruneUpperActive(V &Y, const CV &G, const CV &X, const CV &U, Real xeps, Real geps) :
248 Y_(Y), G_(G), X_(X), U_(U), xeps_(xeps), geps_(geps) {}
249
250 KOKKOS_INLINE_FUNCTION
251 void operator() (const int i) const {
252 const Real zero(0);
253 const int M = U_.extent(1);
254 for(int j=0;j<M;++j) {
255 if( (X_(i,j)>=U_(i,j)-xeps_) && (G_(i,j)<-geps_) ) {
256 Y_(i,j) = zero;
257 }
258 }
259 }
260 }; // End struct GradPruneUpperActive
261
262 //----------------------------------------------------------------------
263 //
264 // Set variables to zero if they correspond to the active set
265 template<class Real, class V, class CV>
266 struct GradPruneActive {
267 typedef typename V::execution_space execution_space;
268 V Y_; // Variable to be pruned
269 CV G_; // Gradient
270 CV X_; // Optimization variable
271 CV L_; // Lower bounds
272 CV U_; // Upper bounds
273 Real xeps_, geps_;
274
275 GradPruneActive(V &Y, const CV &G, const CV &X, const CV &L, const CV &U, Real xeps, Real geps) :
276 Y_(Y), G_(G), X_(X), L_(L), U_(U), xeps_(xeps), geps_(geps) {}
277
278 KOKKOS_INLINE_FUNCTION
279 void operator() (const int i) const {
280 const Real zero(0);
281 const int M = L_.extent(1);
282 for(int j=0;j<M;++j) {
283 if(( X_(i,j)<=L_(i,j)+xeps_) && (G_(i,j)>geps_)) {
284 Y_(i,j) = zero;
285 }
286 else if(( X_(i,j)>=U_(i,j)-xeps_) && (G_(i,j)<-geps_) ) {
287 Y_(i,j) = zero;
288 }
289 }
290 }
291 }; // End struct GradPruneActive
292
293 } // End namespace KokkosStructs
294
295
296 //--------------------------------------------------------------------------
297
298
299 template <class Real, class LO, class GO, class Node>
300 class TpetraBoundConstraint : public BoundConstraint<Real> {
301
302
303 typedef Tpetra::MultiVector<Real,LO,GO,Node> MV;
304 typedef Tpetra::MultiVector<const Real,LO,GO,Node> CMV;
305 typedef ROL::Ptr<MV> MVP;
306 typedef TpetraMultiVector<Real,LO,GO,Node> TMV;
307 typedef ROL::Ptr<TMV> TMVP;
308 typedef typename MV::dual_view_type::t_dev ViewType;
309 typedef typename CMV::dual_view_type::t_dev ConstViewType;
310
311 private:
312 int gblDim_;
313 int lclDim_;
314 ConstViewType l_; // Kokkos view of Lower bounds
315 ConstViewType u_; // Kokkos view of Upper bounds
316 Real min_diff_;
317 Real scale_;
318 ROL::Ptr<const Teuchos::Comm<int> > comm_;
319
320 ROL::Ptr<const MV> getVector( const ROL::Vector<Real>& x ) {
321 return dynamic_cast<const TMV&>(x).getVector();
322 }
323
324 ROL::Ptr<MV> getVector( ROL::Vector<Real>& x ) {
325 return dynamic_cast<TMV&>(x).getVector();
326 }
327
328 public:
329
330 TpetraBoundConstraint(MVP lp, MVP up, Real scale = 1.0) :
331 gblDim_(lp->getGlobalLength()),
332 lclDim_(lp->getLocalLength()),
333 l_(lp->getLocalViewDevice(Tpetra::Access::ReadOnly)),
334 u_(up->getLocalViewDevice(Tpetra::Access::ReadOnly)),
335 scale_(scale),
336 comm_(lp->getMap()->getComm()) {
337
338 KokkosStructs::MinGap<Real,ConstViewType> findmin(l_,u_);
339 Real lclMinGap = 0;
340
341 // Reduce for this MPI process
342 Kokkos::parallel_reduce(lclDim_,findmin,lclMinGap);
343
344 Real gblMinGap;
345
346 // Reduce over MPI processes
347 Teuchos::reduceAll<int,Real>(*comm_,Teuchos::REDUCE_MIN,lclMinGap,Teuchos::outArg(gblMinGap));
348
349 min_diff_ = 0.5*gblMinGap;
350 }
351
352
353 bool isFeasible( const Vector<Real> &x ) {
354 auto xp = getVector(x);
355
356 int lclFeasible = 1;
357
358 ConstViewType x_lcl = xp->getLocalViewDevice(Tpetra::Access::ReadOnly);
359
360 KokkosStructs::Feasible<Real,ConstViewType> check(x_lcl, l_, u_);
361
362 Kokkos::parallel_reduce(lclDim_,check,lclFeasible);
363
364 Real gblFeasible;
365
366 Teuchos::reduceAll<int,Real>(*comm_,Teuchos::REDUCE_MIN,lclFeasible,Teuchos::outArg(gblFeasible));
367
368 return gblFeasible == 1 ? true : false;
369 }
370
371 void project( Vector<Real> &x ) {
372
373 auto xp = getVector(x);
374
375 ViewType x_lcl = xp->getLocalViewDevice(Tpetra::Access::ReadWrite);
376
377 KokkosStructs::Project<Real,ViewType,ConstViewType> proj(x_lcl,l_,u_);
378
379 Kokkos::parallel_for(lclDim_,proj);
380 }
381
382
383 void pruneLowerActive(Vector<Real> &v, const Vector<Real> &x, Real eps = Real(0)) {
384 auto xp = getVector(x);
385 auto vp = getVector(v);
386
387 Real epsn = std::min(scale_*eps,this->min_diff_);
388
389 ConstViewType x_lcl = xp->getLocalViewDevice(Tpetra::Access::ReadOnly);
390 ViewType v_lcl = vp->getLocalViewDevice(Tpetra::Access::ReadWrite);
391
392 KokkosStructs::PruneLowerActive<Real,ViewType,ConstViewType> prune(v_lcl,x_lcl,l_,epsn);
393
394 Kokkos::parallel_for(lclDim_,prune);
395 }
396
397 void pruneUpperActive(Vector<Real> &v, const Vector<Real> &x, Real eps = Real(0)) {
398 auto xp = getVector(x);
399 auto vp = getVector(v);
400
401 Real epsn = std::min(scale_*eps,this->min_diff_);
402
403 ConstViewType x_lcl = xp->getLocalViewDevice(Tpetra::Access::ReadOnly);
404 ViewType v_lcl = vp->getLocalViewDevice(Tpetra::Access::ReadWrite);
405
406 KokkosStructs::PruneUpperActive<Real,ViewType,ConstViewType> prune(v_lcl,x_lcl,u_,epsn);
407
408 Kokkos::parallel_for(lclDim_,prune);
409 }
410
411 void pruneActive(Vector<Real> &v, const Vector<Real> &x, Real eps = Real(0)) {
412 auto xp = getVector(x);
413 auto vp = getVector(v);
414
415 Real epsn = std::min(scale_*eps,this->min_diff_);
416
417 ConstViewType x_lcl = xp->getLocalViewDevice(Tpetra::Access::ReadOnly);
418 ViewType v_lcl = vp->getLocalViewDevice(Tpetra::Access::ReadWrite);
419
420 KokkosStructs::PruneActive<Real,ViewType,ConstViewType> prune(v_lcl,x_lcl,l_,u_,epsn);
421
422 Kokkos::parallel_for(lclDim_,prune);
423 }
424
425 void pruneLowerActive(Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps = Real(0), Real geps = Real(0)) {
426 auto xp = getVector(x);
427 auto gp = getVector(g);
428 auto vp = getVector(v);
429 Real epsn = std::min(scale_*xeps,this->min_diff_);
430
431 ConstViewType x_lcl = xp->getLocalViewDevice(Tpetra::Access::ReadOnly);
432 ConstViewType g_lcl = gp->getLocalViewDevice(Tpetra::Access::ReadOnly);
433 ViewType v_lcl = vp->getLocalViewDevice(Tpetra::Access::ReadWrite);
434
435 KokkosStructs::GradPruneLowerActive<Real,ViewType,ConstViewType> prune(v_lcl,g_lcl,x_lcl,l_,epsn,geps);
436
437 Kokkos::parallel_for(lclDim_,prune);
438 }
439
440 void pruneUpperActive(Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps = Real(0), Real geps = Real(0)) {
441 auto xp = getVector(x);
442 auto gp = getVector(g);
443 auto vp = getVector(v);
444 Real epsn = std::min(scale_*xeps,this->min_diff_);
445
446 ConstViewType x_lcl = xp->getLocalViewDevice(Tpetra::Access::ReadOnly);
447 ConstViewType g_lcl = gp->getLocalViewDevice(Tpetra::Access::ReadOnly);
448 ViewType v_lcl = vp->getLocalViewDevice(Tpetra::Access::ReadWrite);
449
450 KokkosStructs::GradPruneUpperActive<Real,ViewType,ConstViewType> prune(v_lcl,g_lcl,x_lcl,u_,epsn,geps);
451
452 Kokkos::parallel_for(lclDim_,prune);
453 }
454
455 void pruneActive(Vector<Real> &v, const Vector<Real> &g, const Vector<Real> &x, Real xeps = Real(0), Real geps = Real(0)) {
456 auto xp = getVector(x);
457 auto gp = getVector(g);
458 auto vp = getVector(v);
459 Real epsn = std::min(scale_*xeps,this->min_diff_);
460
461 ConstViewType x_lcl = xp->getLocalViewDevice(Tpetra::Access::ReadOnly);
462 ConstViewType g_lcl = gp->getLocalViewDevice(Tpetra::Access::ReadOnly);
463 ViewType v_lcl = vp->getLocalViewDevice(Tpetra::Access::ReadWrite);
464
465 KokkosStructs::GradPruneActive<Real,ViewType,ConstViewType> prune(v_lcl,g_lcl,x_lcl,l_,u_,epsn,geps);
466
467 Kokkos::parallel_for(lclDim_,prune);
468 }
469 }; // End class TpetraBoundConstraint
470
471 } // End ROL Namespace
472
473 #endif