Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
comm/test/Time/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 "Teuchos_ConfigDefs.hpp"
43 #include "Teuchos_TimeMonitor.hpp"
44 #include "Teuchos_ScalarTraits.hpp"
45 #include "Teuchos_Version.hpp"
46 #include "Teuchos_as.hpp"
47 
48 #ifdef HAVE_MPI
49 #include <mpi.h>
50 #endif
51 
52 using std::string;
54 using Teuchos::Time;
55 using Teuchos::RCP;
57 using Teuchos::as;
58 
59 /* Test of Teuchos timing classes */
60 
61 
62 /* create timers for several functions */
63 static Time& sqrtTimer() {static RCP<Time> t = TimeMonitor::getNewTimer("square roots"); return *t;}
64 
65 static Time& factTimer() {static RCP<Time> t = TimeMonitor::getNewTimer("factorials"); return *t;}
66 
67 static Time& exceptTimer() {static RCP<Time> t = TimeMonitor::getNewTimer("func with std::exception"); return *t;}
68 
69 static Time& localTimer() {static RCP<Time> t = TimeMonitor::getNewTimer("a function that is not called on all procs"); return *t;}
70 
71 static Time& anotherTimer() {static RCP<Time> t = TimeMonitor::getNewTimer("another func"); return *t;}
72 
73 static Time& yetAnotherTimer() {static RCP<Time> t = TimeMonitor::getNewTimer("yet another func"); return *t;}
74 
75 static Time& yetOneMoreTimer() {static RCP<Time> t = TimeMonitor::getNewTimer("yet one more func"); return *t;}
76 
77 
78 int main(int argc, char* argv[])
79 {
80  bool verbose = 0;
81  int procRank = 0;
82  int FailedTests = 1; // This will be set to 0, if the std::exception is caught!
83 
84 #ifdef HAVE_MPI
85  /* initialize MPI if we are running in parallel */
86  MPI_Init(&argc, &argv);
87  MPI_Comm_rank( MPI_COMM_WORLD, &procRank );
88 #endif
89 
90  // Check for verbose flag.
91  if (argc>1) if (argv[1][0]=='-' && argv[1][1]=='v') verbose = true;
92 
93  if (verbose && procRank==0)
94  std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
95 
96  try
97  {
98 
99  // Prototypes???
100  double sqrtFunc();
101  double factFunc(int x);
102  double exceptFunc();
103  double localFunc();
104  double anotherFunc();
105  double yetAnotherFunc();
106  double yetOneMoreFunc();
107 
108  /* time a simple function */
109  for (int i=0; i<100; i++)
110  {
111  double x = 0.0;
112  x = sqrtFunc();
113  (void)x; // Not used!
114  }
115 
116  /* time a reentrant function */
117  for (int i=0; i<100; i++)
118  {
119  factFunc(100);
120  }
121 
122  /* time a couple of silly functions */
123  for (int i=0; i<100; i++)
124  {
125  anotherFunc();
126  yetAnotherFunc();
127  yetOneMoreFunc();
128  }
129 
130  /* Time a function that will be called only on the root proc. This
131  * checks that the TimeMonitor will work properly when different
132  * processors have different sets of timers. */
133  if (procRank==0)
134  {
135  for (int i=0; i<100; i++)
136  {
137  double x = 0.0;
138  x = localFunc();
139  (void)x; // Not used!
140  }
141  }
142 
143  /* time a function that throws an std::exception */
144  for (int i=0; i<100; i++)
145  {
146  double x = 0.0;
147  x = exceptFunc();
148  (void)x; // Not used!
149  }
150 
151 
152  }
153  catch(std::exception& e)
154  {
155  if (verbose && procRank==0)
156  std::cerr << "Caught std::exception [expected]: " << e.what() << std::endl;
157 
158  // Return 0 since we caught the std::exception
159  FailedTests = 0;
160  }
161 
162  /* Summarize timings. This must be done before finalizing MPI */
163  TimeMonitor::format().setRowsBetweenLines(3);
164  if (verbose)
165  TimeMonitor::summarize();
166 
167 #ifdef HAVE_MPI
168  /* clean up MPI if we are running in parallel*/
169  MPI_Finalize();
170 #endif
171 
172  if (FailedTests != 0) {
173  std::cout << "End Result: TEST FAILED" << std::endl;
174  return 1;
175  }
176 
177  std::cout << "End Result: TEST PASSED" << std::endl;
178  return FailedTests;
179 }
180 
181 
182 /* sum std::sqrt(x), x=[0, 10000). */
183 double sqrtFunc()
184 {
185  /* construct a time monitor. This starts the timer. It will stop when leaving scope */
186  TimeMonitor timer(sqrtTimer());
187 
188  double sum = 0.0;
189 
190  for (int i=0; i<10000; i++)
191  {
192  TEUCHOS_TEST_FOR_EXCEPTION(ScalarTraits<double>::squareroot(as<double>(i)) > 1000.0, std::runtime_error,
193  "throw an std::exception");
194  sum += ScalarTraits<double>::squareroot(as<double>(i));
195  }
196 
197  return sum;
198 }
199 
200 
201 /* compute log(factorial(x)) */
202 double factFunc(int x)
203 {
204  /* construct a time monitor. This starts the timer. It will stop when leaving scope */
205  TimeMonitor timer(factTimer());
206 
207  if (x==0) return 0;
208  if (x==1) return 1;
209  return std::log(as<double>(x)) + factFunc(x-1);
210 }
211 
212 
213 /* sum std::sqrt(x), x=[0, 10000). */
214 double exceptFunc()
215 {
216  /* construct a time monitor. This starts the timer. It will stop when leaving scope */
217  TimeMonitor timer(exceptTimer());
218 
219  double sum = 0.0;
220  for (int i=0; i<10000; i++)
221  {
223  ScalarTraits<double>::squareroot(as<double>(i)) > 60.0, std::runtime_error,
224  "throw an std::exception");
225  sum += ScalarTraits<double>::squareroot(as<double>(i));
226  }
227  return sum;
228 }
229 
230 
231 /* sum x, x=[0, 10000). */
232 double localFunc()
233 {
234  /* construct a time monitor. This starts the timer. It will stop when leaving scope */
235  TimeMonitor timer(localTimer());
236 
237  double sum = 0.0;
238 
239  for (int i=0; i<10000; i++)
240  {
241  sum += i;
242  }
243 
244  return sum;
245 }
246 
247 
248 /* sum x^2, x=[0, 10000). */
249 double anotherFunc()
250 {
251  /* construct a time monitor. This starts the timer. It will stop when leaving scope */
252  TimeMonitor timer(anotherTimer());
253 
254  double sum = 0.0;
255 
256  for (int i=0; i<10000; i++)
257  {
258  sum += i*i;
259  }
260 
261  return sum;
262 }
263 
264 
265 /* sum x^3, x=[0, 10000). */
267 {
268  /* construct a time monitor. This starts the timer. It will stop when leaving scope */
269  TimeMonitor timer(yetAnotherTimer());
270 
271  double sum = 0.0;
272 
273  for (int i=0; i<10000; i++)
274  {
275  sum += i*i*i;
276  }
277 
278  return sum;
279 }
280 
281 
282 /* sum x+1, x=[0, 10000). */
284 {
285  /* construct a time monitor. This starts the timer. It will stop when leaving scope */
286  TimeMonitor timer(yetOneMoreTimer());
287 
288  double sum = 0.0;
289 
290  for (int i=0; i<10000; i++)
291  {
292  sum += i+1;
293  }
294 
295  return sum;
296 }
double anotherFunc()
int main(int argc, char *argv[])
static Time & yetAnotherTimer()
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
This structure defines some basic traits for a scalar field type.
static Time & anotherTimer()
double exceptFunc()
double factFunc(int x)
static Time & factTimer()
Wall-clock timer.
double yetOneMoreFunc()
static Time & yetOneMoreTimer()
static Time & exceptTimer()
std::string Teuchos_Version()
static Time & localTimer()
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
Definition: Teuchos_as.hpp:287
static Time & sqrtTimer()
double sqrtFunc()
double localFunc()
Defines basic traits for the scalar field type.
Scope protection wrapper for Teuchos::Time, with timer reporting functionality.
Smart reference counting pointer class for automatic garbage collection.
double yetAnotherFunc()
Definition of Teuchos::as, for conversions between types.
A scope-safe timer wrapper class, that can compute global timer statistics.