ROL
ROL_ProfiledVector.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_VECTORPROFILER_H
45 #define ROL_VECTORPROFILER_H
46 
47 #include "ROL_Vector.hpp"
48 #include <ostream>
49 
50 namespace ROL {
51 
69 template<class Ordinal>
71  Ordinal constructor_;
72  Ordinal destructor_;
73  Ordinal plus_;
74  Ordinal scale_;
75  Ordinal dot_;
76  Ordinal norm_;
77  Ordinal clone_;
78  Ordinal axpy_;
79  Ordinal zero_;
80  Ordinal basis_;
81  Ordinal dimension_;
82  Ordinal set_;
83  Ordinal dual_;
84  Ordinal apply_;
85  Ordinal applyUnary_;
86  Ordinal applyBinary_;
87  Ordinal reduce_;
88  Ordinal setScalar_;
89  Ordinal randomize_;
91  constructor_(0), destructor_(0), plus_(0), scale_(0), dot_(0), norm_(0), clone_(0),
92  axpy_(0), zero_(0), basis_(0), dimension_(0), set_(0), dual_(0), apply_(0),
94 
95 }; // struct VectorFunctionCalls
96 
97 
98 // Forward declaration for friend functions
99 template<class Ordinal,class Real>
101 
102 template<class Ordinal,class Real>
104  return x.functionCalls_;
105 }
106 
107 template<class Ordinal, class Real>
108 void printVectorFunctionCalls( const ProfiledVector<Ordinal,Real> &x, std::ostream &outStream = std::cout ) {
109  outStream << "Total Vector Function Calls" << std::endl;
110  outStream << "---------------------------" << std::endl;
111  outStream << "Constructor : " << x.functionCalls_.constructor_ << std::endl;
112  outStream << "Destructor : " << x.functionCalls_.destructor_ << std::endl;
113  outStream << "set : " << x.functionCalls_.set_ << std::endl;
114  outStream << "plus : " << x.functionCalls_.plus_ << std::endl;
115  outStream << "axpy : " << x.functionCalls_.axpy_ << std::endl;
116  outStream << "scale : " << x.functionCalls_.scale_ << std::endl;
117  outStream << "dot : " << x.functionCalls_.dot_ << std::endl;
118  outStream << "zero : " << x.functionCalls_.zero_ << std::endl;
119  outStream << "norm : " << x.functionCalls_.norm_ << std::endl;
120  outStream << "clone : " << x.functionCalls_.clone_ << std::endl;
121  outStream << "basis : " << x.functionCalls_.basis_ << std::endl;
122  outStream << "dual : " << x.functionCalls_.dual_ << std::endl;
123  outStream << "apply : " << x.functionCalls_.apply_ << std::endl;
124  outStream << "dimension : " << x.functionCalls_.dimension_ << std::endl;
125  outStream << "applyUnary : " << x.functionCalls_.applyUnary_ << std::endl;
126  outStream << "applyBinary : " << x.functionCalls_.applyBinary_ << std::endl;
127  outStream << "reduce : " << x.functionCalls_.reduce_ << std::endl;
128  outStream << "setScalar : " << x.functionCalls_.setScalar_ << std::endl;
129  outStream << "randomize : " << x.functionCalls_.randomize_ << std::endl;
130 }
131 
132 
133 
134 template<class Ordinal,class Real>
135 class ProfiledVector : public Vector<Real> {
136 
137  typedef Vector<Real> V;
138 
139 private:
140  ROL::Ptr<Vector<Real> > v_;
142 public:
143 
144  ProfiledVector( const ROL::Ptr<Vector<Real> > &v ) {
145  // Make sure that given vector is not itself a ProfiledVector to avoid recursion
146  ROL::Ptr<ProfiledVector> pv = ROL::nullPtr;
147  pv = ROL::dynamicPtrCast<ProfiledVector>(v);
148  ROL_TEST_FOR_EXCEPTION( pv != ROL::nullPtr, std::logic_error, "ProfiledVector class "
149  "cannot encapsulate a ProfiledVector object!");
150 
151  v_ = v;
152 
153  functionCalls_.constructor_++;
154  }
155 
156  virtual ~ProfiledVector() {
157  functionCalls_.destructor_++;
158  }
159 
160  void plus( const Vector<Real> &x ) {
161  ROL::Ptr<const V> xp = dynamic_cast<const ProfiledVector&>(x).getVector();
162 
163  functionCalls_.plus_++;
164  v_->plus(*xp);
165  }
166 
167  void scale( const Real alpha ) {
168  functionCalls_.scale_++;
169  v_->scale(alpha);
170  }
171 
172  Real dot( const Vector<Real> &x ) const {
173  ROL::Ptr<const V> xp = dynamic_cast<const ProfiledVector&>(x).getVector();
174  functionCalls_.dot_++;
175  return v_->dot(*xp);
176  }
177 
178  Real norm() const {
179  functionCalls_.norm_++;
180  return v_->norm();
181  }
182 
183  ROL::Ptr<Vector<Real> > clone() const {
184  functionCalls_.clone_++;
185  return ROL::makePtr<ProfiledVector>( v_->clone() );
186  }
187 
188  void axpy( const Real alpha, const Vector<Real> &x ) {
189  ROL::Ptr<const V> xp = dynamic_cast<const ProfiledVector&>(x).getVector();
190  functionCalls_.axpy_++;
191  return v_->axpy(alpha,*xp);
192  }
193 
194  void zero() {
195  functionCalls_.zero_++;
196  v_->zero();
197  }
198 
199  ROL::Ptr<Vector<Real> > basis( const int i ) const {
200  functionCalls_.basis_++;
201  return ROL::makePtr<ProfiledVector>( v_->basis(i) );
202  }
203 
204  int dimension() const {
205  functionCalls_.dimension_++;
206  return v_->dimension();
207  }
208 
209  void set( const Vector<Real> &x ) {
210  ROL::Ptr<const V> xp = dynamic_cast<const ProfiledVector&>(x).getVector();
211  functionCalls_.set_++;
212  v_->set(*xp);
213  }
214 
215  // TODO: determine the correct way to handle dual when v_ is a generic ROL::Ptr<ROL::Vector>
216  const Vector<Real> & dual() const {
217  functionCalls_.dual_++;
218  return *this;
219  }
220 
221  Real apply(const Vector<Real> &x) const {
222  functionCalls_.apply_++;
223  return v_->apply(x);
224  }
225 
226  ROL::Ptr<Vector<Real> > getVector() {
227  return v_;
228  }
229 
230  ROL::Ptr<const Vector<Real> > getVector() const {
231  return v_;
232  }
233 
234  void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
235  functionCalls_.applyUnary_++;
236  v_->applyUnary(f);
237  }
238 
239  void applyBinary( const Elementwise::BinaryFunction<Real> &f, const Vector<Real> &x ) {
240  functionCalls_.applyBinary_++;
241  v_->applyBinary(f,x);
242  }
243 
244  Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
245  functionCalls_.reduce_++;
246  return v_->reduce(r);
247  }
248 
249  void setScalar( const Real C ) {
250  functionCalls_.setScalar_++;
251  v_->setScalar(C);
252  }
253 
254  void randomize( const Real l=0.0, const Real u=1.0) {
255  functionCalls_.randomize_++;
256  v_->randomize(l,u);
257  }
258 
259  void print( std::ostream &outStream ) const {
260  v_->print(outStream);
261  }
262 
263  friend VectorFunctionCalls<Ordinal> getVectorFunctionCalls<>( const ProfiledVector<Ordinal,Real> & );
264  friend void printVectorFunctionCalls<>( const ProfiledVector<Ordinal,Real> &, std::ostream & );
265 
266 };
267 
268 
269 } // namespace ROL
270 
271 #endif // ROL_RANDOMVECTOR_H
void print(std::ostream &outStream) const
Real reduce(const Elementwise::ReductionOp< Real > &r) const
ROL::VectorFunctionCalls< int > functionCalls_
Real norm() const
Returns where .
ROL::Ptr< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Real apply(const Vector< Real > &x) const
Apply to a dual vector. This is equivalent to the call .
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
void scale(const Real alpha)
Compute where .
ROL::Ptr< Vector< Real > > getVector()
int dimension() const
Return dimension of the vector space.
ProfiledVector(const ROL::Ptr< Vector< Real > > &v)
static VectorFunctionCalls< Ordinal > functionCalls_
ROL::Ptr< Vector< Real > > basis(const int i) const
Return i-th basis vector.
void zero()
Set to zero vector.
void randomize(const Real l=0.0, const Real u=1.0)
Set vector to be uniform random between [l,u].
void applyUnary(const Elementwise::UnaryFunction< Real > &f)
ROL::Ptr< Vector< Real > > v_
void printVectorFunctionCalls(const ProfiledVector< Ordinal, Real > &x, std::ostream &outStream=std::cout)
By keeping a pointer to this in a derived Vector class, a tally of all methods is kept for profiling ...
void set(const Vector< Real > &x)
Set where .
void applyBinary(const Elementwise::BinaryFunction< Real > &f, const Vector< Real > &x)
void plus(const Vector< Real > &x)
Compute , where .
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
const Vector< Real > & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
VectorFunctionCalls< Ordinal > getVectorFunctionCalls(const ProfiledVector< Ordinal, Real > &x)
void setScalar(const Real C)
Set where .
ROL::Ptr< const Vector< Real > > getVector() const
Real dot(const Vector< Real > &x) const
Compute where .