ROL
ROL_StdArray.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_STDARRAY_H
45 #define ROL_STDARRAY_H
46 
47 #include <algorithm>
48 #include <array>
49 #include <utility>
50 #include <random>
51 #include <numeric>
52 #include "ROL_Vector.hpp"
53 
59 namespace ROL {
60 
61 template<typename Real, std::size_t array_size, std::size_t pool_size=100u>
62 class StdArray : public Vector<Real> {
63 public:
64 
65  using data_type = std::array<Real,array_size>;
66 
67  StdArray() {
68  for( auto& vptr : pool_ptr ) {
69  if( getCount(vptr) < 2 ) {
70  data = vptr;
71  break;
72  }
73  }
74  if( is_nullPtr(data) ) {
75  data = makePtr<std::array<Real,array_size>>();
76  }
77  }
78 
79 
80  inline Real& operator[] ( std::size_t i ) { return (*data)[i]; }
81  inline const Real& operator[] ( std::size_t i ) const { return (*data)[i]; }
82 
83  std::array<Real,array_size>& get_array() { return *data; }
84  const std::array<Real,array_size>& get_array() const { return *data; }
85 
86  void set( const Vector<Real> &x ) {
87  const auto& ex = _array(x);
88  std::copy(ex.begin(),ex.end(),data->begin());
89  }
90 
91  void plus( const Vector<Real> &x ) {
92  const auto& ex = _array(x);
93  std::transform(ex.begin(),ex.end(),data->begin(),data->begin(),std::plus<Real>{});
94  }
95 
96  void axpy( const Real alpha, const Vector<Real> &x ) {
97  const auto& ex = _array(x);
98  std::transform(ex.begin(),ex.end(),data->begin(),data->begin(),[alpha](Real x, Real y){ return alpha*x+y; });
99  }
100 
101  void scale( const Real alpha ) {
102  for( auto& e : *data ) e *= alpha;
103  }
104 
105  virtual Real dot( const Vector<Real> &x ) const {
106  Real result = 0;
107  const auto& ex = _array(x);
108  std::inner_product(ex.begin(),ex.end(),data->begin(),result);
109  return result;
110  }
111 
112  Real norm() const {
113  Real norm_squared = 0;
114  for( auto e: *data ) norm_squared += (e*e);
115  return std::sqrt(norm_squared);
116  }
117 
118  virtual Ptr<Vector<Real>> clone() const {
119  return makePtr<StdArray>();
120  }
121 
122  Ptr<Vector<Real>> basis( const int i ) const {
123  auto b_ptr = clone();
124  auto& b_ref = static_cast<StdArray&>(*b_ptr);
125  b_ref.zero();
126  b_ref[i] = Real(1);
127  return b_ptr;
128  }
129 
130  int dimension() const { return static_cast<int>(array_size); }
131 
132  void zero() { data->fill(0); }
133 
134  void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
135  for( auto& e : *data ) e = f.apply(e);
136  }
137 
138  void applyBinary( const Elementwise::BinaryFunction<Real> &f,
139  const Vector<Real> &x ) {
140  const auto& ex = _array(x);
141  std::transform(ex.begin(),ex.end(),data->begin(),data->begin(),
142  [&f](Real a, Real b){ return f.apply(a,b);});
143  }
144 
145  Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
146  Real result = r.initialValue();
147  for( auto e: *data ) r.reduce(e,result);
148  return result;
149  }
150 
151  void setScalar( const Real alpha ) { data->fill(alpha); }
152 
153  void randomize( const Real l = -1.0, const Real u = 1.0 ) {
154  std::random_device rd;
155  std::mt19937 gen(rd());
156  std::uniform_real_distribution<Real> dis(l, u);
157  for( auto& e : *data ) e = dis(gen);
158  }
159 
160  virtual void print( std::ostream &outStream ) const {
161  for( auto e: *data ) outStream << e << " ";
162  outStream << std::endl;
163  }
164 
165  static void initialize_pool() {
166  for( std::size_t i=0; i<array_size; ++i ) pool_ptr[i] = makePtrFromRef(pool[i]);
167  }
168 
169  // Count how many objects in the pool are currently being used
170  static std::size_t pool_count() {
171  std::size_t count = 0u;
172  for( auto& vptr : pool_ptr ) count += ( getCount(vptr)>1 );
173  return count;
174  }
175 
176 private:
177 
178  StdArray( Ptr<std::array<Real,array_size>> p ) : data(p) {}
179 
180  const std::array<Real,array_size>& _array( const Vector<Real>& x ) const {
181  return static_cast<const StdArray&>(x).get_array();
182  }
183 
184  Ptr<std::array<Real,array_size>> data;
185 
186  // Allocate scratch space at compile time
187  static std::array<std::array<Real,array_size>,pool_size> pool;
188  static std::array<Ptr<std::array<Real,array_size>>,pool_size> pool_ptr;
189 
190 }; // class StdArray
191 
192 template<typename Real, std::size_t array_size, std::size_t pool_size>
193 std::array<std::array<Real,array_size>,pool_size> StdArray<Real,array_size,pool_size>::pool;
194 
195 template<typename Real, std::size_t array_size, std::size_t pool_size>
196 std::array<Ptr<std::array<Real,array_size>>,pool_size> StdArray<Real,array_size,pool_size>::pool_ptr;
197 
198 } // namespace ROL
199 
200 #endif
static std::size_t pool_count()
void zero()
Set to zero vector.
const std::array< Real, array_size > & _array(const Vector< Real > &x) const
void set(const Vector< Real > &x)
Set where .
std::array< Real, array_size > data_type
void applyUnary(const Elementwise::UnaryFunction< Real > &f)
Real reduce(const Elementwise::ReductionOp< Real > &r) const
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
StdArray(Ptr< std::array< Real, array_size >> p)
virtual Ptr< Vector< Real > > clone() const
Clone to make a new (uninitialized) vector.
Provides the std::array implementation of the ROL::Vector interface.
int dimension() const
Return dimension of the vector space.
void applyBinary(const Elementwise::BinaryFunction< Real > &f, const Vector< Real > &x)
void plus(const Vector< Real > &x)
Compute , where .
void scale(const Real alpha)
Compute where .
static std::array< std::array< Real, array_size >, pool_size > pool
static void initialize_pool()
Ptr< std::array< Real, array_size > > data
virtual void print(std::ostream &outStream) const
void setScalar(const Real alpha)
Set where .
const std::array< Real, array_size > & get_array() const
virtual Real dot(const Vector< Real > &x) const
Compute where .
Real norm() const
Returns where .
static std::array< Ptr< std::array< Real, array_size > >, pool_size > pool_ptr
void randomize(const Real l=-1.0, const Real u=1.0)
Set vector to be uniform random between [l,u].
std::array< Real, array_size > & get_array()
Ptr< Vector< Real > > basis(const int i) const
Return i-th basis vector.
void axpy(const Real alpha, const Vector< Real > &x)
Compute where .
Real & operator[](std::size_t i)