Teuchos Package Browser (Single Doxygen Collection) Version of the Day
test/Workspace/cxx_main.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #include <valarray>
00043 
00044 #include "Teuchos_Workspace.hpp"
00045 #include "Teuchos_CommandLineProcessor.hpp"
00046 #include "Teuchos_GlobalMPISession.hpp"
00047 #include "Teuchos_Time.hpp"
00048 #include "Teuchos_Version.hpp"
00049 
00061 class Transformer {
00062   Teuchos::WorkspaceStore *wss_;
00063   void transform( const int size, double a[], double b[] ) {
00064     b[0] = a[0];
00065     for( int k = 1; k < size; ++k )  b[k]  = a[k]+a[k-1];
00066     for( int k = 0; k < size; ++k )  a[k]  = a[k]-b[k];
00067   }
00068 public:
00069   Transformer() : wss_(Teuchos::get_default_workspace_store().get()) {}
00070   void transformRaw( const int size, double a[] ) {
00071     double *b = new double[size]; // Should not call constructors!
00072     transform( size, a, b );
00073     delete [] b;
00074   }
00075   void transformVector( const int size, double a[] ) {
00076     std::vector<double> b(size); // Should call constructors!
00077     transform( size, a, &b[0] );
00078   }
00079   void transformValarray( const int size, double a[] ) {
00080     std::valarray<double> b(size); // Should not call constructors!
00081     transform( size, a, &b[0] );
00082   }
00083   void transformWorkspace( const int size, double a[] ) {
00084     Teuchos::Workspace<double> b(wss_,size,false); // Does not call constructors!
00085     transform( size, a, &b[0] );
00086   }
00087 };
00088 
00089 int main( int argc, char* argv[] )
00090 {
00091 
00092   using Teuchos::CommandLineProcessor;
00093 
00094   bool verbose = true;
00095 
00096   Teuchos::GlobalMPISession mpiSession(&argc, &argv);
00097 
00098   try {
00099 
00100     // Read options from the commandline
00101     CommandLineProcessor  clp(false); // Don't throw exceptions
00102 
00103     clp.setOption( "verbose", "quiet", &verbose, "Set if output is printed or not." );
00104 
00105     double rel_proc_speed = 1e-5; // Should 
00106     clp.setOption( "rel-proc-speed", &rel_proc_speed, "Relative processor speed (try around 1.0 for timing)." );
00107 
00108     int size = 1;
00109     clp.setOption( "size", &size, "Size of memory blocks created." );
00110 
00111     bool allocate_workspace = true;
00112     clp.setOption( "allocate-workspace", "no-allocate-workspace", &allocate_workspace, "Preallocate workspace or not." );
00113 
00114     CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
00115     if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) return parse_return;
00116 
00117     // Determine how many loops to do to get good timings
00118     const long int
00119       default_num_loops = int( 100000000 * rel_proc_speed ),
00120       num_loops         = int( default_num_loops / ( size + 100 ) );
00121 
00122     // Allocate workspace
00123     if( allocate_workspace )
00124       Teuchos::set_default_workspace_store(
00125         Teuchos::rcp(new Teuchos::WorkspaceStoreInitializeable(10*size))
00126         );
00127 
00128     Teuchos::Time timer("");
00129 
00130     if (verbose)
00131       std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
00132 
00133     if(verbose) std::cout
00134       << "\n************************************************************************************"
00135       << "\n*** Testing and timing Teuchos::Workspace and other methods for temporary memory ***"
00136       << "\n************************************************************************************\n";
00137 
00138     if(verbose) std::cout
00139       << "\nMemory block size    = " << size
00140       << "\nNumber of call loops = " << num_loops
00141       << std::endl;
00142 
00143     Transformer t;
00144     std::vector<double> a(size);
00145 
00146     if(verbose) std::cout << "\nTiming raw new and delete for temporaries ...\n";
00147     std::fill_n( &a[0], size, 1.0 );
00148     timer.start(true);
00149     for( int k = 0; k < num_loops; ++k ) t.transformRaw(size,&a[0]);
00150     timer.stop();
00151     const double raw_time = timer.totalElapsedTime();
00152     if(verbose) std::cout << "  time = " << timer.totalElapsedTime() << " sec\n";
00153 
00154     if(verbose) std::cout << "\nTiming std::vector for temporaries ...\n";
00155     std::fill_n( &a[0], size, 1.0 );
00156     timer.start(true);
00157     for( int k = 0; k < num_loops; ++k ) t.transformVector(size,&a[0]);
00158     timer.stop();
00159     const double vector_time = timer.totalElapsedTime();
00160     if(verbose) std::cout << "  time = " << timer.totalElapsedTime() << " sec\n";
00161 
00162     if(verbose) std::cout << "\nTiming std::valarray for temporaries ...\n";
00163     std::fill_n( &a[0], size, 1.0 );
00164     timer.start(true);
00165     for( int k = 0; k < num_loops; ++k ) t.transformValarray(size,&a[0]);
00166     timer.stop();
00167     const double valarray_time = timer.totalElapsedTime();
00168     if(verbose) std::cout << "  time = " << timer.totalElapsedTime() << " sec\n";
00169 
00170     if(verbose) std::cout << "\nTiming Teuchos::Workspace for temporaries ...\n";
00171     std::fill_n( &a[0], size, 1.0 );
00172     timer.start(true);
00173     for( int k = 0; k < num_loops; ++k ) t.transformWorkspace(size,&a[0]);
00174     timer.stop();
00175     const double workspace_time = timer.totalElapsedTime();
00176     if(verbose) std::cout << "  time = " << timer.totalElapsedTime() << " sec\n";
00177 
00178     if(verbose) {
00179       Teuchos::print_memory_usage_stats(Teuchos::get_default_workspace_store().get(),std::cout);
00180       std::cout
00181         << "\nRelative time (lower is better):"
00182         << "\n   raw new/delete      = " << (raw_time/workspace_time)
00183         << "\n   std::vector         = " << (vector_time/workspace_time)
00184         << "\n   std::valarray       = " << (valarray_time/workspace_time)
00185         << "\n   Teuchos::Workspace  = " << (workspace_time/workspace_time)
00186         << std::endl << std::endl;
00187     }
00188 
00189   }
00190   catch( const std::exception &excpt ) {
00191     if(verbose)
00192       std::cerr << "*** Caught standard std::exception : " << excpt.what() << std::endl;
00193     return 1;
00194   }
00195   catch( ... ) {
00196     if(verbose)
00197       std::cerr << "*** Caught an unknown std::exception\n";
00198     return 1;
00199   }
00200   
00201   return 0;
00202   
00203 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines