ROL
ROL_StdArray.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef ROL_STDARRAY_H
11 #define ROL_STDARRAY_H
12 
13 #include <algorithm>
14 #include <array>
15 #include <utility>
16 #include <random>
17 #include <numeric>
18 #include "ROL_Vector.hpp"
19 
25 namespace ROL {
26 
27 template<typename Real, std::size_t array_size, std::size_t pool_size=100u>
28 class StdArray : public Vector<Real> {
29 public:
30 
31  using data_type = std::array<Real,array_size>;
32 
33  StdArray() {
34  for( auto& vptr : pool_ptr ) {
35  if( getCount(vptr) < 2 ) {
36  data = vptr;
37  break;
38  }
39  }
40  if( is_nullPtr(data) ) {
41  data = makePtr<std::array<Real,array_size>>();
42  }
43  }
44 
45 
46  inline Real& operator[] ( std::size_t i ) { return (*data)[i]; }
47  inline const Real& operator[] ( std::size_t i ) const { return (*data)[i]; }
48 
49  std::array<Real,array_size>& get_array() { return *data; }
50  const std::array<Real,array_size>& get_array() const { return *data; }
51 
52  void set( const Vector<Real> &x ) {
53  const auto& ex = _array(x);
54  std::copy(ex.begin(),ex.end(),data->begin());
55  }
56 
57  void plus( const Vector<Real> &x ) {
58  const auto& ex = _array(x);
59  std::transform(ex.begin(),ex.end(),data->begin(),data->begin(),std::plus<Real>{});
60  }
61 
62  void axpy( const Real alpha, const Vector<Real> &x ) {
63  const auto& ex = _array(x);
64  std::transform(ex.begin(),ex.end(),data->begin(),data->begin(),[alpha](Real x, Real y){ return alpha*x+y; });
65  }
66 
67  void scale( const Real alpha ) {
68  for( auto& e : *data ) e *= alpha;
69  }
70 
71  virtual Real dot( const Vector<Real> &x ) const {
72  Real result = 0;
73  const auto& ex = _array(x);
74  std::inner_product(ex.begin(),ex.end(),data->begin(),result);
75  return result;
76  }
77 
78  Real norm() const {
79  Real norm_squared = 0;
80  for( auto e: *data ) norm_squared += (e*e);
81  return std::sqrt(norm_squared);
82  }
83 
84  virtual Ptr<Vector<Real>> clone() const {
85  return makePtr<StdArray>();
86  }
87 
88  Ptr<Vector<Real>> basis( const int i ) const {
89  auto b_ptr = clone();
90  auto& b_ref = static_cast<StdArray&>(*b_ptr);
91  b_ref.zero();
92  b_ref[i] = Real(1);
93  return b_ptr;
94  }
95 
96  int dimension() const { return static_cast<int>(array_size); }
97 
98  void zero() { data->fill(0); }
99 
100  void applyUnary( const Elementwise::UnaryFunction<Real> &f ) {
101  for( auto& e : *data ) e = f.apply(e);
102  }
103 
104  void applyBinary( const Elementwise::BinaryFunction<Real> &f,
105  const Vector<Real> &x ) {
106  const auto& ex = _array(x);
107  std::transform(ex.begin(),ex.end(),data->begin(),data->begin(),
108  [&f](Real a, Real b){ return f.apply(a,b);});
109  }
110 
111  Real reduce( const Elementwise::ReductionOp<Real> &r ) const {
112  Real result = r.initialValue();
113  for( auto e: *data ) r.reduce(e,result);
114  return result;
115  }
116 
117  void setScalar( const Real alpha ) { data->fill(alpha); }
118 
119  void randomize( const Real l = -1.0, const Real u = 1.0 ) {
120  std::random_device rd;
121  std::mt19937 gen(rd());
122  std::uniform_real_distribution<Real> dis(l, u);
123  for( auto& e : *data ) e = dis(gen);
124  }
125 
126  virtual void print( std::ostream &outStream ) const {
127  for( auto e: *data ) outStream << e << " ";
128  outStream << std::endl;
129  }
130 
131  static void initialize_pool() {
132  for( std::size_t i=0; i<array_size; ++i ) pool_ptr[i] = makePtrFromRef(pool[i]);
133  }
134 
135  // Count how many objects in the pool are currently being used
136  static std::size_t pool_count() {
137  std::size_t count = 0u;
138  for( auto& vptr : pool_ptr ) count += ( getCount(vptr)>1 );
139  return count;
140  }
141 
142 private:
143 
144  StdArray( Ptr<std::array<Real,array_size>> p ) : data(p) {}
145 
146  const std::array<Real,array_size>& _array( const Vector<Real>& x ) const {
147  return static_cast<const StdArray&>(x).get_array();
148  }
149 
150  Ptr<std::array<Real,array_size>> data;
151 
152  // Allocate scratch space at compile time
153  static std::array<std::array<Real,array_size>,pool_size> pool;
154  static std::array<Ptr<std::array<Real,array_size>>,pool_size> pool_ptr;
155 
156 }; // class StdArray
157 
158 template<typename Real, std::size_t array_size, std::size_t pool_size>
159 std::array<std::array<Real,array_size>,pool_size> StdArray<Real,array_size,pool_size>::pool;
160 
161 template<typename Real, std::size_t array_size, std::size_t pool_size>
162 std::array<Ptr<std::array<Real,array_size>>,pool_size> StdArray<Real,array_size,pool_size>::pool_ptr;
163 
164 } // namespace ROL
165 
166 #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:46
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)