ROL
ROL_SimulatedVector.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 #include "ROL_Vector.hpp"
45 #include "ROL_BatchManager.hpp"
46 
47 #ifndef ROL_SIMULATED_VECTOR_H
48 #define ROL_SIMULATED_VECTOR_H
49 
59 namespace ROL {
60 
61 template<class Real>
62 class SimulatedVector : public Vector<Real> {
63 
64  typedef Vector<Real> V;
65  typedef Teuchos::RCP<V> RCPV;
66  typedef Teuchos::RCP<BatchManager<Real> > RCPBM;
68 
69 private:
70  const std::vector<RCPV> vecs_;
71  Teuchos::RCP<BatchManager<Real> > bman_;
72  mutable std::vector<RCPV> dual_vecs_;
73  mutable Teuchos::RCP<PV> dual_pvec_;
74 public:
75 
76  typedef typename std::vector<PV>::size_type size_type;
77 
78  SimulatedVector( const std::vector<RCPV> &vecs, const RCPBM &bman ) : vecs_(vecs), bman_(bman) {
79  for( size_type i=0; i<vecs_.size(); ++i ) {
80  dual_vecs_.push_back((vecs_[i]->dual()).clone());
81  }
82  }
83 
84  void set( const V &x ) {
85  using Teuchos::dyn_cast;
86  const PV &xs = dyn_cast<const PV>(dyn_cast<const V>(x));
87 
88  TEUCHOS_TEST_FOR_EXCEPTION( numVectors() != xs.numVectors(),
89  std::invalid_argument,
90  "Error: Vectors must have the same number of subvectors." );
91 
92  for( size_type i=0; i<vecs_.size(); ++i ) {
93  vecs_[i]->set(*xs.get(i));
94  }
95  }
96 
97  void plus( const V &x ) {
98  using Teuchos::dyn_cast;
99  const PV &xs = dyn_cast<const PV>(dyn_cast<const V>(x));
100 
101  TEUCHOS_TEST_FOR_EXCEPTION( numVectors() != xs.numVectors(),
102  std::invalid_argument,
103  "Error: Vectors must have the same number of subvectors." );
104 
105  for( size_type i=0; i<vecs_.size(); ++i ) {
106  vecs_[i]->plus(*xs.get(i));
107  }
108  }
109 
110  void scale( const Real alpha ) {
111  for( size_type i=0; i<vecs_.size(); ++i ) {
112  vecs_[i]->scale(alpha);
113  }
114  }
115 
116  void axpy( const Real alpha, const V &x ) {
117  using Teuchos::dyn_cast;
118  const PV &xs = dyn_cast<const PV>(x);
119 
120  TEUCHOS_TEST_FOR_EXCEPTION( numVectors() != xs.numVectors(),
121  std::invalid_argument,
122  "Error: Vectors must have the same number of subvectors." );
123 
124  for( size_type i=0; i<vecs_.size(); ++i ) {
125  vecs_[i]->axpy(alpha,*xs.get(i));
126  }
127  }
128 
129  Real dot( const V &x ) const {
130  using Teuchos::dyn_cast;
131  const PV &xs = dyn_cast<const PV>(x);
132 
133  TEUCHOS_TEST_FOR_EXCEPTION( numVectors() != xs.numVectors(),
134  std::invalid_argument,
135  "Error: Vectors must have the same number of subvectors." );
136 
137  Real locresult = 0;
138  Real result = 0;
139  for( size_type i=0; i<vecs_.size(); ++i ) {
140  locresult += vecs_[i]->dot(*xs.get(i));
141  }
142 
143  bman_->sumAll(&locresult, &result, 1);
144 
145  return result;
146  }
147 
148  Real norm() const {
149  Real locresult = 0;
150  Real result = 0;
151  for( size_type i=0; i<vecs_.size(); ++i ) {
152  locresult += std::pow(vecs_[i]->norm(),2);
153  }
154 
155  bman_->sumAll(&locresult, &result, 1);
156 
157  return std::sqrt(result);
158  }
159 
160  RCPV clone() const {
161  using Teuchos::RCP;
162  using Teuchos::rcp;
163 
164  std::vector<RCPV> clonevec;
165  for( size_type i=0; i<vecs_.size(); ++i ) {
166  clonevec.push_back(vecs_[i]->clone());
167  }
168  return rcp( new PV(clonevec, bman_) );
169  }
170 
171  const V& dual(void) const {
172  using Teuchos::rcp;
173 
174  for( size_type i=0; i<vecs_.size(); ++i ) {
175  dual_vecs_[i]->set(vecs_[i]->dual());
176  }
177  dual_pvec_ = rcp( new PV( dual_vecs_, bman_ ) );
178  return *dual_pvec_;
179  }
180 
181  RCPV basis( const int i ) const { // this must be fixed for distributed batching
182 
183  TEUCHOS_TEST_FOR_EXCEPTION( i >= dimension() || i<0,
184  std::invalid_argument,
185  "Error: Basis index must be between 0 and vector dimension." );
186 
187  using Teuchos::RCP;
188  using Teuchos::rcp;
189  using Teuchos::dyn_cast;
190 
191  RCPV bvec = clone();
192 
193  // Downcast
194  PV &eb = dyn_cast<PV>(*bvec);
195 
196  int begin = 0;
197  int end = 0;
198 
199  // Iterate over subvectors
200  for( size_type j=0; j<vecs_.size(); ++j ) {
201 
202  end += vecs_[j]->dimension();
203 
204  if( begin<= i && i<end ) {
205  eb.set(j, *(vecs_[j]->basis(i-begin)) );
206  }
207  else {
208  eb.zero(j);
209  }
210 
211  begin = end;
212 
213  }
214  return bvec;
215  }
216 
217  int dimension() const { // this must be fixed for distributed batching
218  int total_dim = 0;
219  for( size_type j=0; j<vecs_.size(); ++j ) {
220  total_dim += vecs_[j]->dimension();
221  }
222  return total_dim;
223  }
224 
225  void zero() {
226  for( size_type j=0; j<vecs_.size(); ++j ) {
227  vecs_[j]->zero();
228  }
229  }
230 
231  // Apply the same unary function to each subvector
232  void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
233  for( size_type i=0; i<vecs_.size(); ++i ) {
234  vecs_[i]->applyUnary(f);
235  }
236  }
237 
238  // Apply the same binary function to each pair of subvectors in this vector and x
239  void applyBinary( const Elementwise::BinaryFunction<Real> &f, const V &x ) {
240  const PV &xs = Teuchos::dyn_cast<const PV>(x);
241 
242  for( size_type i=0; i<vecs_.size(); ++i ) {
243  vecs_[i]->applyBinary(f,*xs.get(i));
244  }
245  }
246 
247  Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
248  Real result = r.initialValue();
249 
250  for( size_type i=0; i<vecs_.size(); ++i ) {
251  r.reduce(vecs_[i]->reduce(r),result);
252  }
253  return result;
254  }
255 
256  // Methods that do not exist in the base class
257 
258  // In distributed batching mode, these are understood to take local indices.
259 
260  Teuchos::RCP<const Vector<Real> > get(size_type i) const {
261  return vecs_[i];
262  }
263 
264  Teuchos::RCP<Vector<Real> > get(size_type i) {
265  return vecs_[i];
266  }
267 
268  void set(size_type i, const V &x) {
269  vecs_[i]->set(x);
270  }
271 
272  void zero(size_type i) {
273  vecs_[i]->zero();
274  }
275 
277  return vecs_.size();
278  }
279 
280 };
281 
282 // Helper methods
283 template<class Real>
284 Teuchos::RCP<Vector<Real> > CreateSimulatedVector( const Teuchos::RCP<Vector<Real> > &a, const Teuchos::RCP<BatchManager<Real> > &bman ) {
285  using Teuchos::RCP;
286  using Teuchos::rcp;
287  typedef RCP<Vector<Real> > RCPV;
288  typedef SimulatedVector<Real> PV;
289 
290  RCPV temp[] = {a};
291  return rcp( new PV( std::vector<RCPV>(temp, temp+1), bman ) );
292 }
293 
294 template<class Real>
295 Teuchos::RCP<const Vector<Real> > CreateSimulatedVector( const Teuchos::RCP<const Vector<Real> > &a, const Teuchos::RCP<BatchManager<Real> > &bman ) {
296  using Teuchos::RCP;
297  using Teuchos::rcp;
298  typedef RCP<const Vector<Real> > RCPV;
299  typedef const SimulatedVector<Real> PV;
300 
301  RCPV temp[] = {a};
302  return rcp( new PV( std::vector<RCPV>(temp, temp+1), bman ) );
303 }
304 
305 template<class Real>
306 Teuchos::RCP<Vector<Real> > CreateSimulatedVector( const Teuchos::RCP<Vector<Real> > &a,
307  const Teuchos::RCP<Vector<Real> > &b,
308  const Teuchos::RCP<BatchManager<Real> > &bman ) {
309  using Teuchos::RCP;
310  using Teuchos::rcp;
311  typedef RCP<Vector<Real> > RCPV;
312  typedef SimulatedVector<Real> PV;
313 
314  RCPV temp[] = {a,b};
315  return rcp( new PV( std::vector<RCPV>(temp, temp+2), bman ) );
316 }
317 
318 template<class Real>
319 Teuchos::RCP<const Vector<Real> > CreateSimulatedVector( const Teuchos::RCP<const Vector<Real> > &a,
320  const Teuchos::RCP<const Vector<Real> > &b,
321  const Teuchos::RCP<BatchManager<Real> > &bman ) {
322  using Teuchos::RCP;
323  using Teuchos::rcp;
324  typedef RCP<const Vector<Real> > RCPV;
325  typedef const SimulatedVector<Real> PV;
326 
327  RCPV temp[] = {a,b};
328  return rcp( new PV( std::vector<RCPV>(temp, temp+2), bman ) );
329 }
330 
331 template<class Real>
332 Teuchos::RCP<Vector<Real> > CreateSimulatedVector( const Teuchos::RCP<Vector<Real> > &a,
333  const Teuchos::RCP<Vector<Real> > &b,
334  const Teuchos::RCP<Vector<Real> > &c,
335  const Teuchos::RCP<BatchManager<Real> > &bman ) {
336  using Teuchos::RCP;
337  using Teuchos::rcp;
338  typedef RCP<Vector<Real> > RCPV;
339  typedef SimulatedVector<Real> PV;
340 
341  RCPV temp[] = {a,b,c};
342  return rcp( new PV( std::vector<RCPV>(temp, temp+3), bman ) );
343 }
344 
345 template<class Real>
346 Teuchos::RCP<const Vector<Real> > CreateSimulatedVector( const Teuchos::RCP<const Vector<Real> > &a,
347  const Teuchos::RCP<const Vector<Real> > &b,
348  const Teuchos::RCP<const Vector<Real> > &c,
349  const Teuchos::RCP<BatchManager<Real> > &bman ) {
350  using Teuchos::RCP;
351  using Teuchos::rcp;
352  typedef RCP<const Vector<Real> > RCPV;
353  typedef const SimulatedVector<Real> PV;
354 
355  RCPV temp[] = {a,b,c};
356  return rcp( new PV( std::vector<RCPV>(temp, temp+3), bman ) );
357 }
358 
359 template<class Real>
360 Teuchos::RCP<Vector<Real> > CreateSimulatedVector( const Teuchos::RCP<Vector<Real> > &a,
361  const Teuchos::RCP<Vector<Real> > &b,
362  const Teuchos::RCP<Vector<Real> > &c,
363  const Teuchos::RCP<Vector<Real> > &d,
364  const Teuchos::RCP<BatchManager<Real> > &bman ) {
365  using Teuchos::RCP;
366  using Teuchos::rcp;
367  typedef RCP<Vector<Real> > RCPV;
368  typedef SimulatedVector<Real> PV;
369 
370  RCPV temp[] = {a,b,c,d};
371  return rcp( new PV( std::vector<RCPV>(temp, temp+4), bman ) );
372 }
373 
374 template<class Real>
375 Teuchos::RCP<const Vector<Real> > CreateSimulatedVector( const Teuchos::RCP<const Vector<Real> > &a,
376  const Teuchos::RCP<const Vector<Real> > &b,
377  const Teuchos::RCP<const Vector<Real> > &c,
378  const Teuchos::RCP<const Vector<Real> > &d,
379  const Teuchos::RCP<BatchManager<Real> > &bman ) {
380  using Teuchos::RCP;
381  using Teuchos::rcp;
382  typedef RCP<const Vector<Real> > RCPV;
383  typedef const SimulatedVector<Real> PV;
384 
385  RCPV temp[] = {a,b,c,d};
386  return rcp( new PV( std::vector<RCPV>(temp, temp+4), bman ) );
387 }
388 
389 } // namespace ROL
390 
391 #endif // ROL_SIMULATED_VECTOR_H
392 
void set(const V &x)
Set where .
std::vector< PV >::size_type size_type
size_type numVectors() const
std::vector< RCPV > dual_vecs_
Teuchos::RCP< Vector< Real > > CreateSimulatedVector(const Teuchos::RCP< Vector< Real > > &a, const Teuchos::RCP< BatchManager< Real > > &bman)
void applyBinary(const Elementwise::BinaryFunction< Real > &f, const V &x)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Real norm() const
Returns where .
Defines the linear algebra of a vector space on a generic partitioned vector where the individual vec...
const V & dual(void) const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
SimulatedVector(const std::vector< RCPV > &vecs, const RCPBM &bman)
Teuchos::RCP< PV > dual_pvec_
RCPV clone() const
Clone to make a new (uninitialized) vector.
const std::vector< RCPV > vecs_
Teuchos::RCP< BatchManager< Real > > bman_
Real dot(const V &x) const
Compute where .
int dimension() const
Return dimension of the vector space.
RCPV basis(const int i) const
Return i-th basis vector.
void zero()
Set to zero vector.
void plus(const V &x)
Compute , where .
Teuchos::RCP< BatchManager< Real > > RCPBM
SimulatedVector< Real > PV
Real reduce(const Elementwise::ReductionOp< Real > &r) const
void scale(const Real alpha)
Compute where .
void axpy(const Real alpha, const V &x)
Compute where .
void applyUnary(const Elementwise::UnaryFunction< Real > &f)
Teuchos::RCP< const Vector< Real > > get(size_type i) const