ROL
function/sketching/test_01.cpp
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 
15 #include "ROL_StdVector.hpp"
16 #include "ROL_Stream.hpp"
17 #include "ROL_Sketch.hpp"
18 #include "Teuchos_GlobalMPISession.hpp"
19 
20 #include <iostream>
21 
22 typedef double RealT;
23 
24 int main(int argc, char *argv[]) {
25 
26  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
27 
28  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
29  int iprint = argc - 1;
30  ROL::Ptr<std::ostream> outStream;
31  ROL::nullstream bhs; // outputs nothing
32  if (iprint > 0)
33  outStream = ROL::makePtrFromRef(std::cout);
34  else
35  outStream = ROL::makePtrFromRef(bhs);
36 
37  // Save the format state of the original std::cout.
38  ROL::nullstream oldFormatState;
39  oldFormatState.copyfmt(std::cout);
40 
41  int errorFlag = 0;
42 
43  // *** Test body.
44  try {
45 
46  int nrow = 128, ncol = 100, rank = 1, testrank = 6;
47  ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(nrow, 0.0);
48  ROL::StdVector<RealT> x(x_ptr);
49 
50  ROL::Sketch<RealT> sketch(x,ncol,rank,ROL::ROL_EPSILON<RealT>());
51  bool flag = true;
52  *outStream << std::endl;
53  for (int i = 0; i < testrank; ++i) {
54  *outStream << "Rank = " << i+1 << " Test Rank = " << testrank << std::endl;
55  sketch.setRank(i+1);
56  flag = sketch.test(testrank,*outStream,1);
57  }
58  errorFlag += (flag ? 0 : 1);
59  }
60  catch (std::logic_error& err) {
61  *outStream << err.what() << "\n";
62  errorFlag = -1000;
63  }; // end try
64 
65  if (errorFlag != 0)
66  std::cout << "End Result: TEST FAILED\n";
67  else
68  std::cout << "End Result: TEST PASSED\n";
69 
70  // reset format state of std::cout
71  std::cout.copyfmt(oldFormatState);
72 
73  return 0;
74 
75 }
76 
Provides an interface for randomized sketching.
Definition: ROL_Sketch.hpp:31
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
basic_nullstream< char, std::char_traits< char >> nullstream
Definition: ROL_Stream.hpp:36
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
bool test(const int rank, std::ostream &outStream=std::cout, const int verbosity=0)
Definition: ROL_Sketch.hpp:360
int main(int argc, char *argv[])
void setRank(int rank)
Definition: ROL_Sketch.hpp:260