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 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) 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 Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #include <valarray>
43 
44 #include "Teuchos_Workspace.hpp"
47 #include "Teuchos_Time.hpp"
48 #include "Teuchos_Version.hpp"
49 
61 class Transformer {
63  void transform( const int size, double a[], double b[] ) {
64  b[0] = a[0];
65  for( int k = 1; k < size; ++k ) b[k] = a[k]+a[k-1];
66  for( int k = 0; k < size; ++k ) a[k] = a[k]-b[k];
67  }
68 public:
69  Transformer() : wss_(Teuchos::get_default_workspace_store().get()) {}
70  void transformRaw( const int size, double a[] ) {
71  double *b = new double[size]; // Should not call constructors!
72  transform( size, a, b );
73  delete [] b;
74  }
75  void transformVector( const int size, double a[] ) {
76  std::vector<double> b(size); // Should call constructors!
77  transform( size, a, &b[0] );
78  }
79  void transformValarray( const int size, double a[] ) {
80  std::valarray<double> b(size); // Should not call constructors!
81  transform( size, a, &b[0] );
82  }
83  void transformWorkspace( const int size, double a[] ) {
84  Teuchos::Workspace<double> b(wss_,size,false); // Does not call constructors!
85  transform( size, a, &b[0] );
86  }
87 };
88 
89 int main( int argc, char* argv[] )
90 {
91 
93 
94  bool verbose = true;
95 
96  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
97 
98  try {
99 
100  // Read options from the commandline
101  CommandLineProcessor clp(false); // Don't throw exceptions
102 
103  clp.setOption( "verbose", "quiet", &verbose, "Set if output is printed or not." );
104 
105  double rel_proc_speed = 1e-5; // Should
106  clp.setOption( "rel-proc-speed", &rel_proc_speed, "Relative processor speed (try around 1.0 for timing)." );
107 
108  int size = 1;
109  clp.setOption( "size", &size, "Size of memory blocks created." );
110 
111  bool allocate_workspace = true;
112  clp.setOption( "allocate-workspace", "no-allocate-workspace", &allocate_workspace, "Preallocate workspace or not." );
113 
114  CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
115  if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) return parse_return;
116 
117  // Determine how many loops to do to get good timings
118  const long int
119  default_num_loops = int( 100000000 * rel_proc_speed ),
120  num_loops = int( default_num_loops / ( size + 100 ) );
121 
122  // Allocate workspace
123  if( allocate_workspace )
126  );
127 
128  Teuchos::Time timer("");
129 
130  if (verbose)
131  std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
132 
133  if(verbose) std::cout
134  << "\n************************************************************************************"
135  << "\n*** Testing and timing Teuchos::Workspace and other methods for temporary memory ***"
136  << "\n************************************************************************************\n";
137 
138  if(verbose) std::cout
139  << "\nMemory block size = " << size
140  << "\nNumber of call loops = " << num_loops
141  << std::endl;
142 
143  Transformer t;
144  std::vector<double> a(size);
145 
146  if(verbose) std::cout << "\nTiming raw new and delete for temporaries ...\n";
147  std::fill_n( &a[0], size, 1.0 );
148  timer.start(true);
149  for( int k = 0; k < num_loops; ++k ) t.transformRaw(size,&a[0]);
150  timer.stop();
151  const double raw_time = timer.totalElapsedTime();
152  if(verbose) std::cout << " time = " << timer.totalElapsedTime() << " sec\n";
153 
154  if(verbose) std::cout << "\nTiming std::vector for temporaries ...\n";
155  std::fill_n( &a[0], size, 1.0 );
156  timer.start(true);
157  for( int k = 0; k < num_loops; ++k ) t.transformVector(size,&a[0]);
158  timer.stop();
159  const double vector_time = timer.totalElapsedTime();
160  if(verbose) std::cout << " time = " << timer.totalElapsedTime() << " sec\n";
161 
162  if(verbose) std::cout << "\nTiming std::valarray for temporaries ...\n";
163  std::fill_n( &a[0], size, 1.0 );
164  timer.start(true);
165  for( int k = 0; k < num_loops; ++k ) t.transformValarray(size,&a[0]);
166  timer.stop();
167  const double valarray_time = timer.totalElapsedTime();
168  if(verbose) std::cout << " time = " << timer.totalElapsedTime() << " sec\n";
169 
170  if(verbose) std::cout << "\nTiming Teuchos::Workspace for temporaries ...\n";
171  std::fill_n( &a[0], size, 1.0 );
172  timer.start(true);
173  for( int k = 0; k < num_loops; ++k ) t.transformWorkspace(size,&a[0]);
174  timer.stop();
175  const double workspace_time = timer.totalElapsedTime();
176  if(verbose) std::cout << " time = " << timer.totalElapsedTime() << " sec\n";
177 
178  if(verbose) {
180  std::cout
181  << "\nRelative time (lower is better):"
182  << "\n raw new/delete = " << (raw_time/workspace_time)
183  << "\n std::vector = " << (vector_time/workspace_time)
184  << "\n std::valarray = " << (valarray_time/workspace_time)
185  << "\n Teuchos::Workspace = " << (workspace_time/workspace_time)
186  << std::endl << std::endl;
187  }
188 
189  }
190  catch( const std::exception &excpt ) {
191  if(verbose)
192  std::cerr << "*** Caught standard std::exception : " << excpt.what() << std::endl;
193  return 1;
194  }
195  catch( ... ) {
196  if(verbose)
197  std::cerr << "*** Caught an unknown std::exception\n";
198  return 1;
199  }
200 
201  return 0;
202 
203 }
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().