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