ROL
ROL_AtomVector.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 #ifndef ROL_ATOMVECTOR_H
45 #define ROL_ATOMVECTOR_H
46 
47 #include "ROL_StdVector.hpp"
48 #include "ROL_BatchManager.hpp"
49 
55 namespace ROL {
56 
57 template <class Real>
59 
60 template <class Real>
62 
63 template <class Real>
64 class AtomVector : public StdVector<Real> {
65  typedef typename std::vector<Real>::size_type uint;
66 private:
67  const uint numMySamples_;
68  const uint dimension_;
69 
70 public:
71  AtomVector(const Teuchos::RCP<std::vector<Real> > &vec,
72  const int numMySamples, const int dimension)
73  : StdVector<Real>(vec), numMySamples_(numMySamples), dimension_(dimension) {}
74 
75  Teuchos::RCP<const std::vector<Real> > getAtom(const int i) const {
76  std::vector<Real> pt(dimension_,0.);
77  if ( i >= 0 && i < (int)numMySamples_ ) {
78  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
79  for (uint j = 0; j < dimension_; j++) {
80  pt[j] = yval[i*dimension_ + j];
81  }
82  }
83  else {
84  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
85  ">>> ERROR (ROL::AtomVector): index out of bounds in getAtom!");
86  }
87  return Teuchos::rcp(new std::vector<Real>(pt));
88  }
89 
90  void setAtom(const int i, const std::vector<Real> &pt) {
91  if ( i >= 0 && i < (int)numMySamples_ ) {
92  std::vector<Real> &yval = *(StdVector<Real>::getVector());
93  for (uint j = 0; j < dimension_; j++) {
94  yval[i*dimension_ + j] = pt[j];
95  }
96  }
97  else {
98  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
99  ">>> ERROR (ROL::AtomVector): index out of bounds in setAtom!");
100  }
101  }
102 
103  int getNumMyAtoms(void) const {
104  return (int)numMySamples_;
105  }
106 
107  int getDimension(void) const {
108  return (int)dimension_;
109  }
110 };
111 
112 template<class Real>
113 class PrimalAtomVector : public AtomVector<Real> {
114  typedef typename std::vector<Real>::size_type uint;
115 private:
116  const uint numMySamples_;
117  const uint dimension_;
118  const Teuchos::RCP<std::vector<Real> > scale_;
119  const Teuchos::RCP<BatchManager<Real> > bman_;
120 
121  mutable Teuchos::RCP<DualAtomVector<Real> > dual_vec_;
122 
123 public:
124  PrimalAtomVector(const Teuchos::RCP<std::vector<Real> > &vec,
125  const int numMySamples,
126  const int dimension,
127  const Teuchos::RCP<std::vector<Real> > &scale,
128  const Teuchos::RCP<BatchManager<Real> > &bman)
129  : AtomVector<Real>(vec,numMySamples,dimension),
130  numMySamples_((uint)numMySamples), dimension_((uint)dimension),
131  scale_(scale), bman_(bman) {}
132 
133  Real dot(const Vector<Real> &x) const {
134  const PrimalAtomVector<Real> &ex = Teuchos::dyn_cast<const PrimalAtomVector>(x);
135  const std::vector<Real> &xval = *(ex.getVector());
136  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
137  uint index = 0;
138  Real val = 0;
139  for (uint i = 0; i < numMySamples_; i++) {
140  for (uint j = 0; j < dimension_; j++) {
141  index = i*dimension_ + j;
142  val += xval[index] * (*scale_)[index] * yval[index];
143  }
144  }
145  // Global sum
146  Real sum_val = 0;
147  bman_->sumAll(&val,&sum_val,1);
148  return sum_val;
149  }
150 
151  Teuchos::RCP<Vector<Real> > clone(void) const {
152  return Teuchos::rcp(new PrimalAtomVector(
153  Teuchos::rcp(new std::vector<Real>(numMySamples_*dimension_)),
154  numMySamples_,dimension_,scale_,bman_));
155  }
156 
157  const Vector<Real> & dual(void) const {
158  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
159  uint index = 0;
160  std::vector<Real> tmp(yval);
161  for (uint i = 0; i < numMySamples_; i++) {
162  for (uint j = 0; j < dimension_; j++) {
163  index = i*dimension_ + j;
164  tmp[index] *= (*scale_)[index];
165  }
166  }
167  dual_vec_ = Teuchos::rcp(new DualAtomVector<Real>(
168  Teuchos::rcp(new std::vector<Real>(tmp)),
169  numMySamples_,dimension_,scale_,bman_));
170  return *dual_vec_;
171  }
172 
173  int dimension(void) const {
174  Real dim = (Real)StdVector<Real>::dimension();
175  Real sum = 0.;
176  bman_->sumAll(&dim,&sum,1);
177  return (int)sum;
178  }
179 
180  Real reduce(const Elementwise::ReductionOp<Real> &r) const {
181  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
182  uint index = 0;
183  Real result = r.initialValue();
184  for (uint i = 0; i < numMySamples_; i++) {
185  for (uint j = 0; j < dimension_; j++) {
186  index = i*dimension_ + j;
187  r.reduce(yval[index],result);
188  }
189  }
190  // Global sum
191  Real sum = 0.;
192  bman_->reduceAll(&result,&sum,r);
193  return sum;
194  }
195 };
196 
197 template<class Real>
198 class DualAtomVector : public AtomVector<Real> {
199  typedef typename std::vector<Real>::size_type uint;
200 private:
201  const uint numMySamples_;
202  const uint dimension_;
203  const Teuchos::RCP<std::vector<Real> > scale_;
204  const Teuchos::RCP<BatchManager<Real> > bman_;
205 
206  mutable Teuchos::RCP<PrimalAtomVector<Real> > dual_vec_;
207 
208 public:
209  DualAtomVector(const Teuchos::RCP<std::vector<Real> > &vec,
210  const int numMySamples,
211  const int dimension,
212  const Teuchos::RCP<std::vector<Real> > &scale,
213  const Teuchos::RCP<BatchManager<Real> > &bman)
214  : AtomVector<Real>(vec,numMySamples,dimension),
215  numMySamples_((uint)numMySamples), dimension_((uint)dimension),
216  scale_(scale), bman_(bman) {}
217 
218  Real dot(const Vector<Real> &x) const {
219  const DualAtomVector<Real> &ex = Teuchos::dyn_cast<const DualAtomVector>(x);
220  const std::vector<Real> &xval = *(ex.getVector());
221  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
222  uint index = 0;
223  Real val = 0;
224  for (uint i = 0; i < numMySamples_; i++) {
225  for (uint j = 0; j < dimension_; j++) {
226  index = i*dimension_ + j;
227  val += xval[index] * yval[index] / (*scale_)[index];
228  }
229  }
230  // Global sum
231  Real sum_val = 0;
232  bman_->sumAll(&val,&sum_val,1);
233  return sum_val;
234  }
235 
236  Teuchos::RCP<Vector<Real> > clone(void) const {
237  return Teuchos::rcp(new DualAtomVector(
238  Teuchos::rcp(new std::vector<Real>(numMySamples_*dimension_)),
239  numMySamples_,dimension_,scale_,bman_));
240  }
241 
242  const Vector<Real> & dual(void) const {
243  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
244  uint index = 0;
245  std::vector<Real> tmp(yval);
246  for (uint i = 0; i < numMySamples_; i++) {
247  for (uint j = 0; j < dimension_; j++) {
248  index = i*dimension_ + j;
249  tmp[index] /= (*scale_)[index];
250  }
251  }
252  dual_vec_ = Teuchos::rcp(new PrimalAtomVector<Real>(
253  Teuchos::rcp(new std::vector<Real>(tmp)),
254  numMySamples_,dimension_,scale_,bman_));
255  return *dual_vec_;
256  }
257 
258  int dimension(void) const {
259  Real dim = (Real)StdVector<Real>::dimension();
260  Real sum = 0.;
261  bman_->sumAll(&dim,&sum,1);
262  return (int)sum;
263  }
264 
265  Real reduce(const Elementwise::ReductionOp<Real> &r) const {
266  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
267  uint index = 0;
268  Real result = r.initialValue();
269  for (uint i = 0; i < numMySamples_; i++) {
270  for (uint j = 0; j < dimension_; j++) {
271  index = i*dimension_ + j;
272  r.reduce(yval[index],result);
273  }
274  }
275  // Global sum
276  Real sum = 0.;
277  bman_->reduceAll(&result,&sum,r);
278  return sum;
279  }
280 };
281 
282 } // namespace ROL
283 
284 #endif
const Teuchos::RCP< BatchManager< Real > > bman_
Teuchos::RCP< Vector< Real > > clone(void) const
Clone to make a new (uninitialized) vector.
std::vector< Real >::size_type uint
void scale(const Real alpha)
Teuchos::RCP< const std::vector< Element > > getVector() const
Provides the std::vector implementation of the ROL::Vector interface.
int dimension(void) const
Return dimension of the vector space.
int getDimension(void) const
void setAtom(const int i, const std::vector< Real > &pt)
Real dot(const Vector< Real > &x) const
Compute where .
DualAtomVector(const Teuchos::RCP< std::vector< Real > > &vec, const int numMySamples, const int dimension, const Teuchos::RCP< std::vector< Real > > &scale, const Teuchos::RCP< BatchManager< Real > > &bman)
Real reduce(const Elementwise::ReductionOp< Real > &r) const
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
int dimension(void) const
Return dimension of the vector space.
Teuchos::RCP< DualAtomVector< Real > > dual_vec_
Provides the std::vector implementation of the ROL::Vector interface.
const Vector< Real > & dual(void) const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
AtomVector(const Teuchos::RCP< std::vector< Real > > &vec, const int numMySamples, const int dimension)
const uint dimension_
const Teuchos::RCP< std::vector< Real > > scale_
Teuchos::RCP< const std::vector< Real > > getAtom(const int i) const
const uint numMySamples_
std::vector< Real >::size_type uint
const Vector< Real > & dual(void) const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Real dot(const Vector< Real > &x) const
Compute where .
int getNumMyAtoms(void) const
Real reduce(const Elementwise::ReductionOp< Real > &r) const
PrimalAtomVector(const Teuchos::RCP< std::vector< Real > > &vec, const int numMySamples, const int dimension, const Teuchos::RCP< std::vector< Real > > &scale, const Teuchos::RCP< BatchManager< Real > > &bman)
const Teuchos::RCP< BatchManager< Real > > bman_
Teuchos::RCP< Vector< Real > > clone(void) const
Clone to make a new (uninitialized) vector.
std::vector< Real >::size_type uint
const Teuchos::RCP< std::vector< Real > > scale_
Teuchos::RCP< PrimalAtomVector< Real > > dual_vec_