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_;
88  constructor_(0), destructor_(0), plus_(0), scale_(0), dot_(0), norm_(0), clone_(0),
89  axpy_(0), zero_(0), basis_(0), dimension_(0), set_(0), dual_(0), applyUnary_(0),
90  applyBinary_(0), reduce_(0) {}
91 
92 }; // struct VectorFunctionCalls
93 
94 
95 // Forward declaration for friend functions
96 template<class Ordinal,class Real>
98 
99 template<class Ordinal,class Real>
101  return x.functionCalls_;
102 }
103 
104 template<class Ordinal, class Real>
105 void printVectorFunctionCalls( const ProfiledVector<Ordinal,Real> &x, std::ostream &outStream = std::cout ) {
106  outStream << "Total Vector Function Calls" << std::endl;
107  outStream << "---------------------------" << std::endl;
108  outStream << "Constructor : " << x.functionCalls_.constructor_ << std::endl;
109  outStream << "Destructor : " << x.functionCalls_.destructor_ << std::endl;
110  outStream << "set : " << x.functionCalls_.set_ << std::endl;
111  outStream << "plus : " << x.functionCalls_.plus_ << std::endl;
112  outStream << "axpy : " << x.functionCalls_.axpy_ << std::endl;
113  outStream << "scale : " << x.functionCalls_.scale_ << std::endl;
114  outStream << "dot : " << x.functionCalls_.dot_ << std::endl;
115  outStream << "zero : " << x.functionCalls_.zero_ << std::endl;
116  outStream << "norm : " << x.functionCalls_.norm_ << std::endl;
117  outStream << "clone : " << x.functionCalls_.clone_ << std::endl;
118  outStream << "basis : " << x.functionCalls_.basis_ << std::endl;
119  outStream << "dual : " << x.functionCalls_.dual_ << std::endl;
120  outStream << "dimension : " << x.functionCalls_.dimension_ << std::endl;
121  outStream << "applyUnary : " << x.functionCalls_.applyUnary_ << std::endl;
122  outStream << "applyBinary : " << x.functionCalls_.applyBinary_ << std::endl;
123  outStream << "reduce : " << x.functionCalls_.reduce_ << std::endl;
124 }
125 
126 
127 
128 template<class Ordinal,class Real>
129 class ProfiledVector : public Vector<Real> {
130 
131  template <typename T> using RCP = Teuchos::RCP<T>;
132  typedef Vector<Real> V;
133 
134 private:
135  Teuchos::RCP<Vector<Real> > v_;
137 public:
138 
139  ProfiledVector( const Teuchos::RCP<Vector<Real> > &v ) {
140  // Make sure that given vector is not itself a ProfiledVector to avoid recursion
141  Teuchos::RCP<ProfiledVector> pv = Teuchos::null;
142  pv = Teuchos::rcp_dynamic_cast<ProfiledVector>(v);
143  TEUCHOS_TEST_FOR_EXCEPTION( pv != Teuchos::null, std::logic_error, "ProfiledVector class "
144  "cannot encapsulate a ProfiledVector object!");
145 
146  v_ = v;
147 
148  functionCalls_.constructor_++;
149  }
150 
151  virtual ~ProfiledVector() {
152  functionCalls_.destructor_++;
153  }
154 
155  void plus( const Vector<Real> &x ) {
156  RCP<const V> xp = Teuchos::dyn_cast<const ProfiledVector>(x).getVector();
157 
158  functionCalls_.plus_++;
159  v_->plus(*xp);
160  }
161 
162  void scale( const Real alpha ) {
163  functionCalls_.scale_++;
164  v_->scale(alpha);
165  }
166 
167  Real dot( const Vector<Real> &x ) const {
168  RCP<const V> xp = Teuchos::dyn_cast<const ProfiledVector>(x).getVector();
169  functionCalls_.dot_++;
170  return v_->dot(*xp);
171  }
172 
173  Real norm() const {
174  functionCalls_.norm_++;
175  return v_->norm();
176  }
177 
178  Teuchos::RCP<Vector<Real> > clone() const {
179  functionCalls_.clone_++;
180  return Teuchos::rcp( new ProfiledVector( v_->clone() ) );
181  }
182 
183  void axpy( const Real alpha, const Vector<Real> &x ) {
184  RCP<const V> xp = Teuchos::dyn_cast<const ProfiledVector>(x).getVector();
185  functionCalls_.axpy_++;
186  return v_->axpy(alpha,*xp);
187  }
188 
189  void zero() {
190  functionCalls_.zero_++;
191  v_->zero();
192  }
193 
194  Teuchos::RCP<Vector<Real> > basis( const int i ) const {
195  functionCalls_.basis_++;
196  return Teuchos::rcp( new ProfiledVector( v_->basis(i) ) );
197  }
198 
199  int dimension() const {
200  functionCalls_.dimension_++;
201  return v_->dimension();
202  }
203 
204  void set( const Vector<Real> &x ) {
205  RCP<const V> xp = Teuchos::dyn_cast<const ProfiledVector>(x).getVector();
206  functionCalls_.set_++;
207  v_->set(*xp);
208  }
209 
210  // TODO: determine the correct way to handle dual when v_ is a generic RCP<ROL::Vector>
211  const Vector<Real> & dual() const {
212  functionCalls_.dual_++;
213  return *this;
214  }
215 
216  Teuchos::RCP<Vector<Real> > getVector() {
217  return v_;
218  }
219 
220  Teuchos::RCP<const Vector<Real> > getVector() const {
221  return v_;
222  }
223 
224  void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
225  functionCalls_.applyUnary_++;
226  v_->applyUnary(f);
227  }
228 
229  void applyBinary( const Elementwise::BinaryFunction<Real> &f, const Vector<Real> &x ) {
230  functionCalls_.applyBinary_++;
231  v_->applyBinary(f,x);
232  }
233 
234  Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
235  functionCalls_.reduce_++;
236  return v_->reduce(r);
237  }
238 
239  void print( std::ostream &outStream ) const {
240  v_->print(outStream);
241  }
242 
243  friend VectorFunctionCalls<Ordinal> getVectorFunctionCalls<>( const ProfiledVector<Ordinal,Real> & );
244  friend void printVectorFunctionCalls<>( const ProfiledVector<Ordinal,Real> &, std::ostream & );
245 
246 };
247 
248 
249 } // namespace ROL
250 
251 #endif // ROL_RANDOMVECTOR_H
Teuchos::RCP< Vector< Real > > getVector()
const Vector< Real > & dual() 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 .
void print(std::ostream &outStream) const
Real norm() const
Returns where .
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:76
void scale(const Real alpha)
Compute where .
Teuchos::RCP< Vector< Real > > basis(const int i) const
Return i-th basis vector.
static VectorFunctionCalls< Ordinal > functionCalls_
ProfiledVector(const Teuchos::RCP< Vector< Real > > &v)
void zero()
Set to zero vector.
void applyUnary(const Elementwise::UnaryFunction< Real > &f)
Teuchos::RCP< 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 ...
Real reduce(const Elementwise::ReductionOp< Real > &r) const
void applyBinary(const Elementwise::BinaryFunction< Real > &f, const Vector< Real > &x)
void plus(const Vector< Real > &x)
Compute , where .
Teuchos::RCP< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
VectorFunctionCalls< Ordinal > getVectorFunctionCalls(const ProfiledVector< Ordinal, Real > &x)
Teuchos::RCP< const Vector< Real > > getVector() const
int dimension() const
Return dimension of the vector space.