|
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 "Teuchos_ConfigDefs.hpp" 00030 #include "Teuchos_TimeMonitor.hpp" 00031 #include "Teuchos_ScalarTraits.hpp" 00032 #include "Teuchos_Version.hpp" 00033 #include "Teuchos_as.hpp" 00034 00035 #ifdef HAVE_MPI 00036 #include <mpi.h> 00037 #endif 00038 00039 using std::string; 00040 using Teuchos::TimeMonitor; 00041 using Teuchos::Time; 00042 using Teuchos::RCP; 00043 using Teuchos::ScalarTraits; 00044 using Teuchos::as; 00045 00046 /* Test of Teuchos timing classes */ 00047 00048 00049 /* create timers for several functions */ 00050 static Time& sqrtTimer() {static RCP<Time> t = TimeMonitor::getNewTimer("square roots"); return *t;} 00051 00052 static Time& factTimer() {static RCP<Time> t = TimeMonitor::getNewTimer("factorials"); return *t;} 00053 00054 static Time& exceptTimer() {static RCP<Time> t = TimeMonitor::getNewTimer("func with std::exception"); return *t;} 00055 00056 static Time& localTimer() {static RCP<Time> t = TimeMonitor::getNewTimer("a function that is not called on all procs"); return *t;} 00057 00058 static Time& anotherTimer() {static RCP<Time> t = TimeMonitor::getNewTimer("another func"); return *t;} 00059 00060 static Time& yetAnotherTimer() {static RCP<Time> t = TimeMonitor::getNewTimer("yet another func"); return *t;} 00061 00062 static Time& yetOneMoreTimer() {static RCP<Time> t = TimeMonitor::getNewTimer("yet one more func"); return *t;} 00063 00064 00065 int main(int argc, char* argv[]) 00066 { 00067 bool verbose = 0; 00068 int procRank = 0; 00069 int FailedTests = 1; // This will be set to 0, if the std::exception is caught! 00070 00071 #ifdef HAVE_MPI 00072 /* initialize MPI if we are running in parallel */ 00073 MPI_Init(&argc, &argv); 00074 MPI_Comm_rank( MPI_COMM_WORLD, &procRank ); 00075 #endif 00076 00077 // Check for verbose flag. 00078 if (argc>1) if (argv[1][0]=='-' && argv[1][1]=='v') verbose = true; 00079 00080 if (verbose && procRank==0) 00081 std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl; 00082 00083 try 00084 { 00085 00086 // Prototypes??? 00087 double sqrtFunc(); 00088 double factFunc(int x); 00089 double exceptFunc(); 00090 double localFunc(); 00091 double anotherFunc(); 00092 double yetAnotherFunc(); 00093 double yetOneMoreFunc(); 00094 00095 /* time a simple function */ 00096 for (int i=0; i<100; i++) 00097 { 00098 double x = 0.0; 00099 x = sqrtFunc(); 00100 } 00101 00102 /* time a reentrant function */ 00103 for (int i=0; i<100; i++) 00104 { 00105 factFunc(100); 00106 } 00107 00108 /* time a couple of silly functions */ 00109 for (int i=0; i<100; i++) 00110 { 00111 anotherFunc(); 00112 yetAnotherFunc(); 00113 yetOneMoreFunc(); 00114 } 00115 00116 /* Time a function that will be called only on the root proc. This 00117 * checks that the TimeMonitor will work properly when different 00118 * processors have different sets of timers. */ 00119 if (procRank==0) 00120 { 00121 for (int i=0; i<100; i++) 00122 { 00123 double x = 0.0; 00124 x = localFunc(); 00125 } 00126 } 00127 00128 /* time a function that throws an std::exception */ 00129 for (int i=0; i<100; i++) 00130 { 00131 double x = 0.0; 00132 x = exceptFunc(); 00133 } 00134 00135 00136 } 00137 catch(std::exception& e) 00138 { 00139 if (verbose && procRank==0) 00140 std::cerr << "Caught std::exception [expected]: " << e.what() << std::endl; 00141 00142 // Return 0 since we caught the std::exception 00143 FailedTests = 0; 00144 } 00145 00146 /* Summarize timings. This must be done before finalizing MPI */ 00147 TimeMonitor::format().setRowsBetweenLines(3); 00148 if (verbose) 00149 TimeMonitor::summarize(); 00150 00151 #ifdef HAVE_MPI 00152 /* clean up MPI if we are running in parallel*/ 00153 MPI_Finalize(); 00154 #endif 00155 00156 if (FailedTests != 0) { 00157 std::cout << "End Result: TEST FAILED" << std::endl; 00158 return 1; 00159 } 00160 00161 std::cout << "End Result: TEST PASSED" << std::endl; 00162 return FailedTests; 00163 } 00164 00165 00166 /* sum std::sqrt(x), x=[0, 10000). */ 00167 double sqrtFunc() 00168 { 00169 /* construct a time monitor. This starts the timer. It will stop when leaving scope */ 00170 TimeMonitor timer(sqrtTimer()); 00171 00172 double sum = 0.0; 00173 00174 for (int i=0; i<10000; i++) 00175 { 00176 TEST_FOR_EXCEPTION(ScalarTraits<double>::squareroot(as<double>(i)) > 1000.0, std::runtime_error, 00177 "throw an std::exception"); 00178 sum += ScalarTraits<double>::squareroot(as<double>(i)); 00179 } 00180 00181 return sum; 00182 } 00183 00184 00185 /* compute log(factorial(x)) */ 00186 double factFunc(int x) 00187 { 00188 /* construct a time monitor. This starts the timer. It will stop when leaving scope */ 00189 TimeMonitor timer(factTimer()); 00190 00191 if (x==0) return 0; 00192 if (x==1) return 1; 00193 return std::log(as<double>(x)) + factFunc(x-1); 00194 } 00195 00196 00197 /* sum std::sqrt(x), x=[0, 10000). */ 00198 double exceptFunc() 00199 { 00200 /* construct a time monitor. This starts the timer. It will stop when leaving scope */ 00201 TimeMonitor timer(exceptTimer()); 00202 00203 double sum = 0.0; 00204 for (int i=0; i<10000; i++) 00205 { 00206 TEST_FOR_EXCEPTION( 00207 ScalarTraits<double>::squareroot(as<double>(i)) > 60.0, std::runtime_error, 00208 "throw an std::exception"); 00209 sum += ScalarTraits<double>::squareroot(as<double>(i)); 00210 } 00211 return sum; 00212 } 00213 00214 00215 /* sum x, x=[0, 10000). */ 00216 double localFunc() 00217 { 00218 /* construct a time monitor. This starts the timer. It will stop when leaving scope */ 00219 TimeMonitor timer(localTimer()); 00220 00221 double sum = 0.0; 00222 00223 for (int i=0; i<10000; i++) 00224 { 00225 sum += i; 00226 } 00227 00228 return sum; 00229 } 00230 00231 00232 /* sum x^2, x=[0, 10000). */ 00233 double anotherFunc() 00234 { 00235 /* construct a time monitor. This starts the timer. It will stop when leaving scope */ 00236 TimeMonitor timer(anotherTimer()); 00237 00238 double sum = 0.0; 00239 00240 for (int i=0; i<10000; i++) 00241 { 00242 sum += i*i; 00243 } 00244 00245 return sum; 00246 } 00247 00248 00249 /* sum x^3, x=[0, 10000). */ 00250 double yetAnotherFunc() 00251 { 00252 /* construct a time monitor. This starts the timer. It will stop when leaving scope */ 00253 TimeMonitor timer(yetAnotherTimer()); 00254 00255 double sum = 0.0; 00256 00257 for (int i=0; i<10000; i++) 00258 { 00259 sum += i*i*i; 00260 } 00261 00262 return sum; 00263 } 00264 00265 00266 /* sum x+1, x=[0, 10000). */ 00267 double yetOneMoreFunc() 00268 { 00269 /* construct a time monitor. This starts the timer. It will stop when leaving scope */ 00270 TimeMonitor timer(yetOneMoreTimer()); 00271 00272 double sum = 0.0; 00273 00274 for (int i=0; i<10000; i++) 00275 { 00276 sum += i+1; 00277 } 00278 00279 return sum; 00280 }
1.7.4