|
Teuchos Package Browser (Single Doxygen Collection) Version of the Day
|
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 // This library is free software; you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as 00012 // published by the Free Software Foundation; either version 2.1 of the 00013 // License, or (at your option) any later version. 00014 // 00015 // This library is distributed in the hope that it will be useful, but 00016 // WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 // Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public 00021 // License along with this library; if not, write to the Free Software 00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00023 // USA 00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 00025 // 00026 // *********************************************************************** 00027 // @HEADER 00028 00029 #include <valarray> 00030 00031 #include "Teuchos_Workspace.hpp" 00032 #include "Teuchos_CommandLineProcessor.hpp" 00033 #include "Teuchos_GlobalMPISession.hpp" 00034 #include "Teuchos_Time.hpp" 00035 #include "Teuchos_Version.hpp" 00036 00048 class Transformer { 00049 Teuchos::WorkspaceStore *wss_; 00050 void transform( const int size, double a[], double b[] ) { 00051 b[0] = a[0]; 00052 for( int k = 1; k < size; ++k ) b[k] = a[k]+a[k-1]; 00053 for( int k = 0; k < size; ++k ) a[k] = a[k]-b[k]; 00054 } 00055 public: 00056 Transformer() : wss_(Teuchos::get_default_workspace_store().get()) {} 00057 void transformRaw( const int size, double a[] ) { 00058 double *b = new double[size]; // Should not call constructors! 00059 transform( size, a, b ); 00060 delete [] b; 00061 } 00062 void transformVector( const int size, double a[] ) { 00063 std::vector<double> b(size); // Should call constructors! 00064 transform( size, a, &b[0] ); 00065 } 00066 void transformValarray( const int size, double a[] ) { 00067 std::valarray<double> b(size); // Should not call constructors! 00068 transform( size, a, &b[0] ); 00069 } 00070 void transformWorkspace( const int size, double a[] ) { 00071 Teuchos::Workspace<double> b(wss_,size,false); // Does not call constructors! 00072 transform( size, a, &b[0] ); 00073 } 00074 }; 00075 00076 int main( int argc, char* argv[] ) 00077 { 00078 00079 using Teuchos::CommandLineProcessor; 00080 00081 bool verbose = true; 00082 00083 Teuchos::GlobalMPISession mpiSession(&argc, &argv); 00084 00085 try { 00086 00087 // Read options from the commandline 00088 CommandLineProcessor clp(false); // Don't throw exceptions 00089 00090 clp.setOption( "verbose", "quiet", &verbose, "Set if output is printed or not." ); 00091 00092 double rel_proc_speed = 1e-5; // Should 00093 clp.setOption( "rel-proc-speed", &rel_proc_speed, "Relative processor speed (try around 1.0 for timing)." ); 00094 00095 int size = 1; 00096 clp.setOption( "size", &size, "Size of memory blocks created." ); 00097 00098 bool allocate_workspace = true; 00099 clp.setOption( "allocate-workspace", "no-allocate-workspace", &allocate_workspace, "Preallocate workspace or not." ); 00100 00101 CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv); 00102 if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) return parse_return; 00103 00104 // Determine how many loops to do to get good timings 00105 const long int 00106 default_num_loops = int( 100000000 * rel_proc_speed ), 00107 num_loops = int( default_num_loops / ( size + 100 ) ); 00108 00109 // Allocate workspace 00110 if( allocate_workspace ) 00111 Teuchos::set_default_workspace_store( 00112 Teuchos::rcp(new Teuchos::WorkspaceStoreInitializeable(10*size)) 00113 ); 00114 00115 Teuchos::Time timer(""); 00116 00117 if (verbose) 00118 std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl; 00119 00120 if(verbose) std::cout 00121 << "\n************************************************************************************" 00122 << "\n*** Testing and timing Teuchos::Workspace and other methods for temporary memory ***" 00123 << "\n************************************************************************************\n"; 00124 00125 if(verbose) std::cout 00126 << "\nMemory block size = " << size 00127 << "\nNumber of call loops = " << num_loops 00128 << std::endl; 00129 00130 Transformer t; 00131 std::vector<double> a(size); 00132 00133 if(verbose) std::cout << "\nTiming raw new and delete for temporaries ...\n"; 00134 std::fill_n( &a[0], size, 1.0 ); 00135 timer.start(true); 00136 for( int k = 0; k < num_loops; ++k ) t.transformRaw(size,&a[0]); 00137 timer.stop(); 00138 const double raw_time = timer.totalElapsedTime(); 00139 if(verbose) std::cout << " time = " << timer.totalElapsedTime() << " sec\n"; 00140 00141 if(verbose) std::cout << "\nTiming std::vector for temporaries ...\n"; 00142 std::fill_n( &a[0], size, 1.0 ); 00143 timer.start(true); 00144 for( int k = 0; k < num_loops; ++k ) t.transformVector(size,&a[0]); 00145 timer.stop(); 00146 const double vector_time = timer.totalElapsedTime(); 00147 if(verbose) std::cout << " time = " << timer.totalElapsedTime() << " sec\n"; 00148 00149 if(verbose) std::cout << "\nTiming std::valarray for temporaries ...\n"; 00150 std::fill_n( &a[0], size, 1.0 ); 00151 timer.start(true); 00152 for( int k = 0; k < num_loops; ++k ) t.transformValarray(size,&a[0]); 00153 timer.stop(); 00154 const double valarray_time = timer.totalElapsedTime(); 00155 if(verbose) std::cout << " time = " << timer.totalElapsedTime() << " sec\n"; 00156 00157 if(verbose) std::cout << "\nTiming Teuchos::Workspace for temporaries ...\n"; 00158 std::fill_n( &a[0], size, 1.0 ); 00159 timer.start(true); 00160 for( int k = 0; k < num_loops; ++k ) t.transformWorkspace(size,&a[0]); 00161 timer.stop(); 00162 const double workspace_time = timer.totalElapsedTime(); 00163 if(verbose) std::cout << " time = " << timer.totalElapsedTime() << " sec\n"; 00164 00165 if(verbose) { 00166 Teuchos::print_memory_usage_stats(Teuchos::get_default_workspace_store().get(),std::cout); 00167 std::cout 00168 << "\nRelative time (lower is better):" 00169 << "\n raw new/delete = " << (raw_time/workspace_time) 00170 << "\n std::vector = " << (vector_time/workspace_time) 00171 << "\n std::valarray = " << (valarray_time/workspace_time) 00172 << "\n Teuchos::Workspace = " << (workspace_time/workspace_time) 00173 << std::endl << std::endl; 00174 } 00175 00176 } 00177 catch( const std::exception &excpt ) { 00178 if(verbose) 00179 std::cerr << "*** Caught standard std::exception : " << excpt.what() << std::endl; 00180 return 1; 00181 } 00182 catch( ... ) { 00183 if(verbose) 00184 std::cerr << "*** Caught an unknown std::exception\n"; 00185 return 1; 00186 } 00187 00188 return 0; 00189 00190 }
1.7.4