Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
comm/test/Time/cxx_main.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "Teuchos_ConfigDefs.hpp"
11 #include "Teuchos_TimeMonitor.hpp"
12 #include "Teuchos_ScalarTraits.hpp"
13 #include "Teuchos_Version.hpp"
14 #include "Teuchos_as.hpp"
15 
16 #ifdef HAVE_MPI
17 #include <mpi.h>
18 #endif
19 
20 using std::string;
22 using Teuchos::Time;
23 using Teuchos::RCP;
25 using Teuchos::as;
26 
27 /* Test of Teuchos timing classes */
28 
29 
30 /* create timers for several functions */
31 static Time& sqrtTimer() {static RCP<Time> t = TimeMonitor::getNewTimer("square roots"); return *t;}
32 
33 static Time& factTimer() {static RCP<Time> t = TimeMonitor::getNewTimer("factorials"); return *t;}
34 
35 static Time& exceptTimer() {static RCP<Time> t = TimeMonitor::getNewTimer("func with std::exception"); return *t;}
36 
37 static Time& localTimer() {static RCP<Time> t = TimeMonitor::getNewTimer("a function that is not called on all procs"); return *t;}
38 
39 static Time& anotherTimer() {static RCP<Time> t = TimeMonitor::getNewTimer("another func"); return *t;}
40 
41 static Time& yetAnotherTimer() {static RCP<Time> t = TimeMonitor::getNewTimer("yet another func"); return *t;}
42 
43 static Time& yetOneMoreTimer() {static RCP<Time> t = TimeMonitor::getNewTimer("yet one more func"); return *t;}
44 
45 
46 // Prototypes of the functions we will time below.
47 double sqrtFunc();
48 double factFunc(int x);
49 double exceptFunc();
50 double localFunc();
51 double anotherFunc();
52 double yetAnotherFunc();
53 double yetOneMoreFunc();
54 
55 
56 int main(int argc, char* argv[])
57 {
58  bool verbose = 0;
59  int procRank = 0;
60  int FailedTests = 1; // This will be set to 0, if the std::exception is caught!
61 
62 #ifdef HAVE_MPI
63  /* initialize MPI if we are running in parallel */
64  MPI_Init(&argc, &argv);
65  MPI_Comm_rank( MPI_COMM_WORLD, &procRank );
66 #endif
67 
68  // Check for verbose flag.
69  if (argc>1) if (argv[1][0]=='-' && argv[1][1]=='v') verbose = true;
70 
71  if (verbose && procRank==0)
72  std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
73 
74  try {
75  // time a simple function
76  for (int i=0; i<100; i++)
77  {
78  double x = 0.0;
79  x = sqrtFunc ();
80  (void) x; // forestall "variable set but not used" compiler warning
81  }
82 
83  // time a reentrant function
84  for (int i = 0; i < 100; ++i) {
85  factFunc (100);
86  }
87 
88  /* time a couple of silly functions */
89  for (int i = 0; i < 100; ++i) {
90  anotherFunc ();
91  yetAnotherFunc ();
92  yetOneMoreFunc ();
93  }
94 
95  /* Time a function that will be called only on the root proc. This
96  * checks that the TimeMonitor will work properly when different
97  * processors have different sets of timers. */
98  if (procRank == 0) {
99  for (int i = 0; i < 100; ++i) {
100  double x = 0.0;
101  x = localFunc ();
102  (void) x; // forestall "variable set but not used" compiler warning
103  }
104  }
105 
106  // time a function that throws an exception
107  for (int i = 0; i < 100; ++i) {
108  double x = 0.0;
109  x = exceptFunc ();
110  (void) x; // forestall "variable set but not used" compiler warning
111  }
112  }
113  catch (std::exception& e) {
114  // This _should_ only catch the one exception thrown above by the
115  // first call to exceptFunc().
116  if (verbose && procRank==0) {
117  std::cerr << "Caught std::exception [expected]: " << e.what() << std::endl;
118  }
119  // Return 0 since we caught the std::exception
120  FailedTests = 0;
121  }
122 
123  // Summarize timings. This must be done before finalizing MPI.
124  TimeMonitor::format ().setRowsBetweenLines (3);
125  if (verbose) {
126  TimeMonitor::summarize ();
127  }
128 
129 #ifdef HAVE_MPI
130  /* clean up MPI if we are running in parallel*/
131  MPI_Finalize ();
132 #endif // HAVE_MPI
133 
134  if (FailedTests != 0) {
135  std::cout << "End Result: TEST FAILED" << std::endl;
136  return 1;
137  }
138 
139  std::cout << "End Result: TEST PASSED" << std::endl;
140  return FailedTests;
141 }
142 
143 
144 /* sum std::sqrt(x), x=[0, 10000). */
145 double sqrtFunc()
146 {
147  /* construct a time monitor. This starts the timer. It will stop when leaving scope */
148  TimeMonitor timer(sqrtTimer());
149 
150  double sum = 0.0;
151 
152  for (int i=0; i<10000; i++)
153  {
154  TEUCHOS_TEST_FOR_EXCEPTION(ScalarTraits<double>::squareroot(as<double>(i)) > 1000.0, std::runtime_error,
155  "throw an std::exception");
156  sum += ScalarTraits<double>::squareroot(as<double>(i));
157  }
158 
159  return sum;
160 }
161 
162 
163 /* compute log(factorial(x)) */
164 double factFunc(int x)
165 {
166  /* construct a time monitor. This starts the timer. It will stop when leaving scope */
167  TimeMonitor timer(factTimer());
168 
169  if (x==0) return 0;
170  if (x==1) return 1;
171  return std::log(as<double>(x)) + factFunc(x-1);
172 }
173 
174 
175 /* sum std::sqrt(x), x=[0, 10000). */
176 double exceptFunc()
177 {
178  /* construct a time monitor. This starts the timer. It will stop when leaving scope */
179  TimeMonitor timer(exceptTimer());
180 
181  double sum = 0.0;
182  for (int i=0; i<10000; i++)
183  {
185  ScalarTraits<double>::squareroot(as<double>(i)) > 60.0, std::runtime_error,
186  "throw an std::exception");
187  sum += ScalarTraits<double>::squareroot(as<double>(i));
188  }
189  return sum;
190 }
191 
192 
193 /* sum x, x=[0, 10000). */
194 double localFunc()
195 {
196  /* construct a time monitor. This starts the timer. It will stop when leaving scope */
197  TimeMonitor timer(localTimer());
198 
199  double sum = 0.0;
200 
201  for (int i=0; i<10000; i++)
202  {
203  sum += i;
204  }
205 
206  return sum;
207 }
208 
209 
210 /* sum x^2, x=[0, 10000). */
211 double anotherFunc()
212 {
213  /* construct a time monitor. This starts the timer. It will stop when leaving scope */
214  TimeMonitor timer(anotherTimer());
215 
216  double sum = 0.0;
217 
218  for (int i=0; i<10000; i++)
219  {
220  sum += i*i;
221  }
222 
223  return sum;
224 }
225 
226 
227 /* sum x^3, x=[0, 10000). */
229 {
230  /* construct a time monitor. This starts the timer. It will stop when leaving scope */
231  TimeMonitor timer(yetAnotherTimer());
232 
233  double sum = 0.0;
234 
235  for (int i=0; i<10000; i++)
236  {
237  sum += i*i*i;
238  }
239 
240  return sum;
241 }
242 
243 
244 /* sum x+1, x=[0, 10000). */
246 {
247  /* construct a time monitor. This starts the timer. It will stop when leaving scope */
248  TimeMonitor timer(yetOneMoreTimer());
249 
250  double sum = 0.0;
251 
252  for (int i=0; i<10000; i++)
253  {
254  sum += i+1;
255  }
256 
257  return sum;
258 }
double anotherFunc()
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()
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.
static Time & sqrtTimer()
int main(int argc, char *argv[])
double sqrtFunc()
double factFunc(int x)
double localFunc()
Defines basic traits for the scalar field type.
Scope guard for Teuchos::Time, with MPI collective timer reporting.
Smart reference counting pointer class for automatic garbage collection.
double yetAnotherFunc()
Definition of Teuchos::as, for conversions between types.
Scope guard for Time, that can compute MPI collective timer statistics.