ROL
ROL_Sketch.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_SKETCH_H
45 #define ROL_SKETCH_H
46 
47 #include "ROL_Vector.hpp"
48 #include "ROL_LinearAlgebra.hpp"
49 #include "ROL_LAPACK.hpp"
50 #include "ROL_Types.hpp"
51 
59 namespace ROL {
60 
61 template <class Real>
62 class Sketch {
63 private:
64  std::vector<Ptr<Vector<Real>>> Psi_, Y_;
65 
66  LA::Matrix<Real> Omega_, W_;
67 
68  int ncol_, rank_, k_, l_;
69 
70  ROL::LAPACK<int,Real> lapack_;
72 
73  bool flagQ_, flagX_;
74 
75  void mgs(std::vector<Ptr<Vector<Real>>> &Y) const {
76  const Real one(1);
77  Real rii(0), rij(0);
78  for (int i = 0; i < k_; ++i) {
79  rii = Y[i]->norm();
80  if (rii > ROL_EPSILON<Real>()) { // Ignore update if Y[i] is zero.
81  Y[i]->scale(one/rii);
82  for (int j = i+1; j < k_; ++j) {
83  rij = Y[i]->dot(*Y[j]);
84  Y[j]->axpy(-rij,*Y[i]);
85  }
86  }
87  }
88  }
89 
90  void computeQ(void) {
91  if (!flagQ_) {
92  mgs(Y_);
93  mgs(Y_);
94  flagQ_ = true;
95  }
96  }
97 
98  void computeX(void) {
99  computeQ();
100  if (!flagX_) {
101  LA::Matrix<Real> Z(l_,k_);
102  for (int i = 0; i < l_; ++i) {
103  for (int j = 0; j < k_; ++j) {
104  Z(i,j) = Psi_[i]->dot(*Y_[j]);
105  }
106  }
107  // Solve least squares problem using LAPACK
108  int M = l_;
109  int N = k_;
110  int NRHS = ncol_;
111  int LDA = M;
112  int LDB = M;
113  std::vector<Real> WORK(1);
114  int LWORK = -1;
115  int INFO;
116  if (solverType_ == 0 ) { // QR
117  char TRANS = 'N';
118  lapack_.GELS(TRANS,M,N,NRHS,Z.values(),LDA,W_.values(),LDB,&WORK[0],LWORK,&INFO);
119  LWORK = static_cast<int>(WORK[0]);
120  WORK.resize(LWORK);
121  lapack_.GELS(TRANS,M,N,NRHS,Z.values(),LDA,W_.values(),LDB,&WORK[0],LWORK,&INFO);
122  }
123  else { // SVD
124  std::vector<Real> S(N);
125  Real RCOND = -1.0;
126  int RANK;
127  lapack_.GELSS(M,N,NRHS,Z.values(),LDA,W_.values(),LDB,&S[0],RCOND,&RANK,&WORK[0],LWORK,&INFO);
128  LWORK = static_cast<int>(WORK[0]);
129  WORK.resize(LWORK);
130  lapack_.GELSS(M,N,NRHS,Z.values(),LDA,W_.values(),LDB,&S[0],RCOND,&RANK,&WORK[0],LWORK,&INFO);
131  }
132  // Set flag
133  flagX_ = true;
134  }
135  }
136 
137  bool testQ(std::ostream &outStream = std::cout, const int verbosity = 0) {
138  const Real one(1), tol(std::sqrt(ROL_EPSILON<Real>()));
139  computeQ();
140  Real qij(0), err(0), maxerr(0);
141  std::ios_base::fmtflags oflags(outStream.flags());
142  if (verbosity > 0) {
143  outStream << std::scientific << std::setprecision(3);
144  }
145  if (verbosity > 1) {
146  outStream << std::endl
147  << " Printing Q'Q...This should be approximately equal to I"
148  << std::endl << std::endl;
149  }
150  for (int i = 0; i < k_; ++i) {
151  for (int j = 0; j < k_; ++j) {
152  qij = Y_[i]->dot(*Y_[j]);
153  err = (i==j ? std::abs(qij-one) : std::abs(qij));
154  maxerr = (err > maxerr ? err : maxerr);
155  if (verbosity > 1) {
156  outStream << std::setw(12) << std::right << qij;
157  }
158  }
159  if (verbosity > 1) {
160  outStream << std::endl;
161  }
162  }
163  if (verbosity > 0) {
164  outStream << std::endl << " TEST ORTHOGONALIZATION: Max Error = "
165  << std::setw(12) << std::right << maxerr
166  << std::endl;
167  outStream.flags(oflags);
168  }
169  return (maxerr < tol ? true : false);
170  }
171 
172  void reset(void) {
173  const Real one(1);
174  // Randomize Psi and Omega and zero W and Y
175  for (int i = 0; i < l_; ++i) {
176  Psi_[i]->randomize(-one,one);
177  for (int j = 0; j < ncol_; ++j) {
178  W_(i,j) = static_cast<Real>(0);
179  }
180  }
181  Real a(2), b(-1), x(0);
182  for (int i = 0; i < k_; ++i) {
183  Y_[i]->zero();
184  for (int j = 0; j < ncol_; ++j) {
185  x = static_cast<Real>(rand())/static_cast<Real>(RAND_MAX);
186  Omega_(j,i) = a*x + b;
187  }
188  }
189  }
190 
191 public:
192  virtual ~Sketch(void) {}
193 
194  Sketch(const Vector<Real> &x, const int ncol, const int rank, const int solverType=0)
195  : ncol_(ncol), rank_(rank), solverType_(solverType), flagQ_(false), flagX_(false) {
196  // Compute reduced dimensions
197  l_ = std::min(4*rank_+3,ncol_);
198  k_ = std::min(2*rank_+1,l_);
199  // Initialize matrix storage
200  Psi_.clear(); Y_.clear();
201  Omega_.reshape(ncol_,k_); W_.reshape(l_,ncol_);
202  for (int i = 0; i < l_; ++i) {
203  Psi_.push_back(x.clone());
204  }
205  for (int i = 0; i < k_; ++i) {
206  Y_.push_back(x.clone());
207  }
208  // Randomize Psi and Omega and zero W and Y
209  reset();
210  }
211 
212  void setRank(const int rank) {
213  rank_ = rank;
214  // Compute reduced dimensions
215  l_ = std::min(4*rank_+3,ncol_);
216  k_ = std::min(2*rank_+1,l_);
217  update();
218  }
219 
220  void update(void) {
221  flagQ_ = false;
222  flagX_ = false;
223  // Randomize Psi and Omega and zero W and Y
224  reset();
225  }
226 
227  void advance(const Real alpha, Vector<Real> &h, const int col, const Real beta = 1.0) {
228  // Check to see if col is less than ncol_
229  if ( col >= ncol_ ) {
230  throw Exception::NotImplemented(">>> ROL::Sketch::advance: Input column index exceeds total number of columns!");
231  }
232  if (!flagQ_ && !flagX_) {
233  // Update Y
234  for (int i = 0; i < k_; ++i) {
235  Y_[i]->scale(beta);
236  Y_[i]->axpy(alpha*Omega_(col,i),h);
237  }
238  // Update W
239  for (int i = 0; i < l_; ++i) {
240  for (int j = 0; j < ncol_; ++j) {
241  W_(i,j) *= beta;
242  }
243  W_(i,col) += alpha*h.dot(*Psi_[i]);
244  }
245  }
246  else {
247  throw Exception::NotImplemented(">>> ROL::Sketch::advance: Reconstruct has already been called!");
248  }
249  }
250 
251  void reconstruct(Vector<Real> &a, const int col) {
252  // Check to see if col is less than ncol_
253  if ( col >= ncol_ ) {
254  throw Exception::NotImplemented(">>> ROL::Sketch::reconstruct: Input column index exceeds total number of columns!");
255  }
256  // Compute QR factorization of Y store in Y
257  computeQ();
258  // Solve (Psi Q)X = W (over determined least squares) store in W
259  computeX();
260  // Apply Q to col column of X
261  a.zero();
262  for (int i = 0; i < k_; ++i) {
263  a.axpy(W_(i,col),*Y_[i]);
264  }
265  }
266 
267  bool test(const int rank, std::ostream &outStream = std::cout, const int verbosity = 0) {
268  const Real one(1), tol(std::sqrt(ROL_EPSILON<Real>()));
269  // Initialize low rank factors
270  std::vector<Ptr<Vector<Real>>> U(rank);
271  LA::Matrix<Real> V(ncol_,rank);
272  for (int i = 0; i < rank; ++i) {
273  U[i] = Y_[0]->clone();
274  U[i]->randomize(-one,one);
275  for (int j = 0; j < ncol_; ++j) {
276  V(j,i) = static_cast<Real>(rand())/static_cast<Real>(RAND_MAX);
277  }
278  }
279  // Initialize A and build sketch
280  update();
281  std::vector<Ptr<Vector<Real>>> A(ncol_);
282  for (int i = 0; i < ncol_; ++i) {
283  A[i] = Y_[0]->clone(); A[i]->zero();
284  for (int j = 0; j < rank; ++j) {
285  A[i]->axpy(V(i,j),*U[j]);
286  }
287  advance(one,*A[i],i,one);
288  }
289  // Test QR decomposition of Y
290  bool flagQ = testQ(outStream, verbosity);
291  // Test reconstruction of A
292  Real nerr(0), maxerr(0);
293  Ptr<Vector<Real>> err = Y_[0]->clone();
294  for (int i = 0; i < ncol_; ++i) {
295  reconstruct(*err,i);
296  err->axpy(-one,*A[i]);
297  nerr = err->norm();
298  maxerr = (nerr > maxerr ? nerr : maxerr);
299  }
300  if (verbosity > 0) {
301  std::ios_base::fmtflags oflags(outStream.flags());
302  outStream << std::scientific << std::setprecision(3);
303  outStream << " TEST RECONSTRUCTION: Max Error = "
304  << std::setw(12) << std::right << maxerr
305  << std::endl << std::endl;
306  outStream.flags(oflags);
307  }
308  return flagQ & (maxerr < tol ? true : false);
309  }
310 
311 }; // class Sketch
312 
313 } // namespace ROL
314 
315 #endif
std::vector< Ptr< Vector< Real > > > Y_
Definition: ROL_Sketch.hpp:64
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:153
void computeQ(void)
Definition: ROL_Sketch.hpp:90
Contains definitions of custom data types in ROL.
Provides an interface for randomized sketching.
Definition: ROL_Sketch.hpp:62
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:167
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
void update(void)
Definition: ROL_Sketch.hpp:220
virtual Real dot(const Vector &x) const =0
Compute where .
Vector< Real > V
void reconstruct(Vector< Real > &a, const int col)
Definition: ROL_Sketch.hpp:251
ROL::LAPACK< int, Real > lapack_
Definition: ROL_Sketch.hpp:70
bool testQ(std::ostream &outStream=std::cout, const int verbosity=0)
Definition: ROL_Sketch.hpp:137
void mgs(std::vector< Ptr< Vector< Real >>> &Y) const
Definition: ROL_Sketch.hpp:75
std::vector< Ptr< Vector< Real > > > Psi_
Definition: ROL_Sketch.hpp:64
Sketch(const Vector< Real > &x, const int ncol, const int rank, const int solverType=0)
Definition: ROL_Sketch.hpp:194
void setRank(const int rank)
Definition: ROL_Sketch.hpp:212
void reset(void)
Definition: ROL_Sketch.hpp:172
LA::Matrix< Real > W_
Definition: ROL_Sketch.hpp:66
bool test(const int rank, std::ostream &outStream=std::cout, const int verbosity=0)
Definition: ROL_Sketch.hpp:267
LA::Matrix< Real > Omega_
Definition: ROL_Sketch.hpp:66
void computeX(void)
Definition: ROL_Sketch.hpp:98
void advance(const Real alpha, Vector< Real > &h, const int col, const Real beta=1.0)
Definition: ROL_Sketch.hpp:227
virtual ~Sketch(void)
Definition: ROL_Sketch.hpp:192