Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
core/test/Workspace/cxx_main.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include <valarray>
11 
12 #include "Teuchos_Workspace.hpp"
15 #include "Teuchos_Time.hpp"
16 #include "Teuchos_Version.hpp"
17 
29 class Transformer {
31  void transform( const int size, double a[], double b[] ) {
32  b[0] = a[0];
33  for( int k = 1; k < size; ++k ) b[k] = a[k]+a[k-1];
34  for( int k = 0; k < size; ++k ) a[k] = a[k]-b[k];
35  }
36 public:
37  Transformer() : wss_(Teuchos::get_default_workspace_store().get()) {}
38  void transformRaw( const int size, double a[] ) {
39  double *b = new double[size]; // Should not call constructors!
40  transform( size, a, b );
41  delete [] b;
42  }
43  void transformVector( const int size, double a[] ) {
44  std::vector<double> b(size); // Should call constructors!
45  transform( size, a, &b[0] );
46  }
47  void transformValarray( const int size, double a[] ) {
48  std::valarray<double> b(size); // Should not call constructors!
49  transform( size, a, &b[0] );
50  }
51  void transformWorkspace( const int size, double a[] ) {
52  Teuchos::Workspace<double> b(wss_,size,false); // Does not call constructors!
53  transform( size, a, &b[0] );
54  }
55 };
56 
57 int main( int argc, char* argv[] )
58 {
59 
61 
62  bool verbose = true;
63 
64  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
65 
66  try {
67 
68  // Read options from the commandline
69  CommandLineProcessor clp(false); // Don't throw exceptions
70 
71  clp.setOption( "verbose", "quiet", &verbose, "Set if output is printed or not." );
72 
73  double rel_proc_speed = 1e-5; // Should
74  clp.setOption( "rel-proc-speed", &rel_proc_speed, "Relative processor speed (try around 1.0 for timing)." );
75 
76  int size = 1;
77  clp.setOption( "size", &size, "Size of memory blocks created." );
78 
79  bool allocate_workspace = true;
80  clp.setOption( "allocate-workspace", "no-allocate-workspace", &allocate_workspace, "Preallocate workspace or not." );
81 
82  CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
83  if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) return parse_return;
84 
85  // Determine how many loops to do to get good timings
86  const long int
87  default_num_loops = int( 100000000 * rel_proc_speed ),
88  num_loops = int( default_num_loops / ( size + 100 ) );
89 
90  // Allocate workspace
91  if( allocate_workspace )
94  );
95 
96  Teuchos::Time timer("");
97 
98  if (verbose)
99  std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
100 
101  if(verbose) std::cout
102  << "\n************************************************************************************"
103  << "\n*** Testing and timing Teuchos::Workspace and other methods for temporary memory ***"
104  << "\n************************************************************************************\n";
105 
106  if(verbose) std::cout
107  << "\nMemory block size = " << size
108  << "\nNumber of call loops = " << num_loops
109  << std::endl;
110 
111  Transformer t;
112  std::vector<double> a(size);
113 
114  if(verbose) std::cout << "\nTiming raw new and delete for temporaries ...\n";
115  std::fill_n( &a[0], size, 1.0 );
116  timer.start(true);
117  for( int k = 0; k < num_loops; ++k ) t.transformRaw(size,&a[0]);
118  timer.stop();
119  const double raw_time = timer.totalElapsedTime();
120  if(verbose) std::cout << " time = " << timer.totalElapsedTime() << " sec\n";
121 
122  if(verbose) std::cout << "\nTiming std::vector for temporaries ...\n";
123  std::fill_n( &a[0], size, 1.0 );
124  timer.start(true);
125  for( int k = 0; k < num_loops; ++k ) t.transformVector(size,&a[0]);
126  timer.stop();
127  const double vector_time = timer.totalElapsedTime();
128  if(verbose) std::cout << " time = " << timer.totalElapsedTime() << " sec\n";
129 
130  if(verbose) std::cout << "\nTiming std::valarray for temporaries ...\n";
131  std::fill_n( &a[0], size, 1.0 );
132  timer.start(true);
133  for( int k = 0; k < num_loops; ++k ) t.transformValarray(size,&a[0]);
134  timer.stop();
135  const double valarray_time = timer.totalElapsedTime();
136  if(verbose) std::cout << " time = " << timer.totalElapsedTime() << " sec\n";
137 
138  if(verbose) std::cout << "\nTiming Teuchos::Workspace for temporaries ...\n";
139  std::fill_n( &a[0], size, 1.0 );
140  timer.start(true);
141  for( int k = 0; k < num_loops; ++k ) t.transformWorkspace(size,&a[0]);
142  timer.stop();
143  const double workspace_time = timer.totalElapsedTime();
144  if(verbose) std::cout << " time = " << timer.totalElapsedTime() << " sec\n";
145 
146  if(verbose) {
148  std::cout
149  << "\nRelative time (lower is better):"
150  << "\n raw new/delete = " << (raw_time/workspace_time)
151  << "\n std::vector = " << (vector_time/workspace_time)
152  << "\n std::valarray = " << (valarray_time/workspace_time)
153  << "\n Teuchos::Workspace = " << (workspace_time/workspace_time)
154  << std::endl << std::endl;
155  }
156 
157  }
158  catch( const std::exception &excpt ) {
159  if(verbose)
160  std::cerr << "*** Caught standard std::exception : " << excpt.what() << std::endl;
161  return 1;
162  }
163  catch( ... ) {
164  if(verbose)
165  std::cerr << "*** Caught an unknown std::exception\n";
166  return 1;
167  }
168 
169  return 0;
170 
171 }
Teuchos::WorkspaceStore * wss_
Templated class for workspace creation.
This class implements a simple (useless) transformation that requires workspace.
TEUCHOSCORE_LIB_DLL_EXPORT void set_default_workspace_store(const Teuchos::RCP< WorkspaceStore > &default_workspace_store)
Set pointer to global workspace object.
WorkspaceStore class that can be used to actually reinitialize memory.
Basic wall-clock timer class.
void transformVector(const int size, double a[])
void transformWorkspace(const int size, double a[])
Initialize, finalize, and query the global MPI session.
void transform(const int size, double a[], double b[])
void start(bool reset=false)
Start the timer, if the timer is enabled (see disable()).
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
double stop()
Stop the timer, if the timer is enabled (see disable()).
Wall-clock timer.
void transformRaw(const int size, double a[])
void transformValarray(const int size, double a[])
Workspace encapsulation class.
std::string Teuchos_Version()
int size(const Comm< Ordinal > &comm)
Get the number of processes in the communicator.
int main(int argc, char *argv[])
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
Basic command line parser for input from (argc,argv[])
double totalElapsedTime(bool readCurrentTime=false) const
The total time in seconds accumulated by this timer.
TEUCHOSCORE_LIB_DLL_EXPORT void print_memory_usage_stats(const WorkspaceStore *workspace_store, std::ostream &out)
Print statistics on memory usage.
Class that helps parse command line input arguments from (argc,argv[]) and set options.
TEUCHOSCORE_LIB_DLL_EXPORT Teuchos::RCP< WorkspaceStore > get_default_workspace_store()
Get the global workspace object set by set_default_workspace_store().