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 applyUnary_;
85  Ordinal applyBinary_;
86  Ordinal reduce_;
87  Ordinal setScalar_;
88  Ordinal randomize_;
90  constructor_(0), destructor_(0), plus_(0), scale_(0), dot_(0), norm_(0), clone_(0),
91  axpy_(0), zero_(0), basis_(0), dimension_(0), set_(0), dual_(0), applyUnary_(0),
92  applyBinary_(0), reduce_(0), setScalar_(0), randomize_(0) {}
93 
94 }; // struct VectorFunctionCalls
95 
96 
97 // Forward declaration for friend functions
98 template<class Ordinal,class Real>
100 
101 template<class Ordinal,class Real>
103  return x.functionCalls_;
104 }
105 
106 template<class Ordinal, class Real>
107 void printVectorFunctionCalls( const ProfiledVector<Ordinal,Real> &x, std::ostream &outStream = std::cout ) {
108  outStream << "Total Vector Function Calls" << std::endl;
109  outStream << "---------------------------" << std::endl;
110  outStream << "Constructor : " << x.functionCalls_.constructor_ << std::endl;
111  outStream << "Destructor : " << x.functionCalls_.destructor_ << std::endl;
112  outStream << "set : " << x.functionCalls_.set_ << std::endl;
113  outStream << "plus : " << x.functionCalls_.plus_ << std::endl;
114  outStream << "axpy : " << x.functionCalls_.axpy_ << std::endl;
115  outStream << "scale : " << x.functionCalls_.scale_ << std::endl;
116  outStream << "dot : " << x.functionCalls_.dot_ << std::endl;
117  outStream << "zero : " << x.functionCalls_.zero_ << std::endl;
118  outStream << "norm : " << x.functionCalls_.norm_ << std::endl;
119  outStream << "clone : " << x.functionCalls_.clone_ << std::endl;
120  outStream << "basis : " << x.functionCalls_.basis_ << std::endl;
121  outStream << "dual : " << x.functionCalls_.dual_ << std::endl;
122  outStream << "dimension : " << x.functionCalls_.dimension_ << std::endl;
123  outStream << "applyUnary : " << x.functionCalls_.applyUnary_ << std::endl;
124  outStream << "applyBinary : " << x.functionCalls_.applyBinary_ << std::endl;
125  outStream << "reduce : " << x.functionCalls_.reduce_ << std::endl;
126  outStream << "setScalar : " << x.functionCalls_.setScalar_ << std::endl;
127  outStream << "randomize : " << x.functionCalls_.randomize_ << std::endl;
128 }
129 
130 
131 
132 template<class Ordinal,class Real>
133 class ProfiledVector : public Vector<Real> {
134 
135  typedef Vector<Real> V;
136 
137 private:
138  ROL::Ptr<Vector<Real> > v_;
140 public:
141 
142  ProfiledVector( const ROL::Ptr<Vector<Real> > &v ) {
143  // Make sure that given vector is not itself a ProfiledVector to avoid recursion
144  ROL::Ptr<ProfiledVector> pv = ROL::nullPtr;
145  pv = ROL::dynamicPtrCast<ProfiledVector>(v);
146  ROL_TEST_FOR_EXCEPTION( pv != ROL::nullPtr, std::logic_error, "ProfiledVector class "
147  "cannot encapsulate a ProfiledVector object!");
148 
149  v_ = v;
150 
151  functionCalls_.constructor_++;
152  }
153 
154  virtual ~ProfiledVector() {
155  functionCalls_.destructor_++;
156  }
157 
158  void plus( const Vector<Real> &x ) {
159  ROL::Ptr<const V> xp = dynamic_cast<const ProfiledVector&>(x).getVector();
160 
161  functionCalls_.plus_++;
162  v_->plus(*xp);
163  }
164 
165  void scale( const Real alpha ) {
166  functionCalls_.scale_++;
167  v_->scale(alpha);
168  }
169 
170  Real dot( const Vector<Real> &x ) const {
171  ROL::Ptr<const V> xp = dynamic_cast<const ProfiledVector&>(x).getVector();
172  functionCalls_.dot_++;
173  return v_->dot(*xp);
174  }
175 
176  Real norm() const {
177  functionCalls_.norm_++;
178  return v_->norm();
179  }
180 
181  ROL::Ptr<Vector<Real> > clone() const {
182  functionCalls_.clone_++;
183  return ROL::makePtr<ProfiledVector>( v_->clone() );
184  }
185 
186  void axpy( const Real alpha, const Vector<Real> &x ) {
187  ROL::Ptr<const V> xp = dynamic_cast<const ProfiledVector&>(x).getVector();
188  functionCalls_.axpy_++;
189  return v_->axpy(alpha,*xp);
190  }
191 
192  void zero() {
193  functionCalls_.zero_++;
194  v_->zero();
195  }
196 
197  ROL::Ptr<Vector<Real> > basis( const int i ) const {
198  functionCalls_.basis_++;
199  return ROL::makePtr<ProfiledVector>( v_->basis(i) );
200  }
201 
202  int dimension() const {
203  functionCalls_.dimension_++;
204  return v_->dimension();
205  }
206 
207  void set( const Vector<Real> &x ) {
208  ROL::Ptr<const V> xp = dynamic_cast<const ProfiledVector&>(x).getVector();
209  functionCalls_.set_++;
210  v_->set(*xp);
211  }
212 
213  // TODO: determine the correct way to handle dual when v_ is a generic ROL::Ptr<ROL::Vector>
214  const Vector<Real> & dual() const {
215  functionCalls_.dual_++;
216  return *this;
217  }
218 
219  ROL::Ptr<Vector<Real> > getVector() {
220  return v_;
221  }
222 
223  ROL::Ptr<const Vector<Real> > getVector() const {
224  return v_;
225  }
226 
227  void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
228  functionCalls_.applyUnary_++;
229  v_->applyUnary(f);
230  }
231 
232  void applyBinary( const Elementwise::BinaryFunction<Real> &f, const Vector<Real> &x ) {
233  functionCalls_.applyBinary_++;
234  v_->applyBinary(f,x);
235  }
236 
237  Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
238  functionCalls_.reduce_++;
239  return v_->reduce(r);
240  }
241 
242  void setScalar( const Real C ) {
243  functionCalls_.setScalar_++;
244  v_->setScalar(C);
245  }
246 
247  void randomize( const Real l=0.0, const Real u=1.0) {
248  functionCalls_.randomize_++;
249  v_->randomize(l,u);
250  }
251 
252  void print( std::ostream &outStream ) const {
253  v_->print(outStream);
254  }
255 
256  friend VectorFunctionCalls<Ordinal> getVectorFunctionCalls<>( const ProfiledVector<Ordinal,Real> & );
257  friend void printVectorFunctionCalls<>( const ProfiledVector<Ordinal,Real> &, std::ostream & );
258 
259 };
260 
261 
262 } // namespace ROL
263 
264 #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.
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 .