Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TimeMonitor_UnitTests.cpp
Go to the documentation of this file.
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Teuchos: Common Tools Package
6 // Copyright (2004) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
44 #include "Teuchos_TimeMonitor.hpp"
45 #include "Teuchos_DefaultComm.hpp"
47 
48 namespace {
49 
50  void func_time_monitor1()
51  {
52  TEUCHOS_FUNC_TIME_MONITOR("FUNC_TIME_MONITOR1");
53  }
54 
55 
56  void func_time_monitor2()
57  {
58  TEUCHOS_FUNC_TIME_MONITOR("FUNC_TIME_MONITOR2");
59  {
60  TEUCHOS_FUNC_TIME_MONITOR_DIFF("FUNC_TIME_MONITOR2_inner", inner);
61  }
62  }
63 
64  // Slowly compute the n-th Fibonacci number. This gives timers
65  // something to time. Be careful not to make n too large, or you'll
66  // run out of stack space.
67  int
68  fib (const int n)
69  {
70  if (n <= 0) {
71  return 0;
72  } else if (n == 1) {
73  return 1;
74  }
75  else {
76  // You should never compute the n-th Fibonacci number like this.
77  // This is exponentially slow in n. The point of using a slow
78  // algorithm like this is to exercise timers.
79  return fib (n-1) + fib (n-2);
80  }
81  }
82 
83  // Do a number of arithmetic operations proportional to n^3, in
84  // order to have something to time. Unlike fib() above, this
85  // function shouldn't take up a lot of stack space.
86  double
87  slowLoop (const size_t n)
88  {
89  const double inc = 1 / static_cast<double> (n);
90  double x = 1.0;
91 
92  for (size_t i = 0; i < n; ++i) {
93  for (size_t j = 0; j < n; ++j) {
94  for (size_t k = 0; k < n; ++k) {
95  x = x + inc;
96  }
97  }
98  }
99  return x;
100  }
101 
102 } // namespace (anonymous)
103 
104 
105 namespace Teuchos {
106  //
107  // Basic TimeMonitor test: create and exercise a timer on all (MPI)
108  // processes, and make sure that TimeMonitor::summarize() reports it.
109  //
110  TEUCHOS_UNIT_TEST( TimeMonitor, FUNC_TIME_MONITOR )
111  {
113  using Teuchos::parameterList;
114  using Teuchos::RCP;
115 
116  func_time_monitor1 (); // Function to time.
117 
118  { // Repeat test for default output format.
119  std::ostringstream oss;
121 
122  // Echo summarize() output to the FancyOStream out (which is a
123  // standard unit test argument). Output should only appear in
124  // show-all-test-details mode.
125  out << oss.str () << std::endl;
126 
127  // Make sure that the timer's name shows up in the output.
128  const size_t substr_i = oss.str ().find ("FUNC_TIME_MONITOR1");
129  TEST_INEQUALITY(substr_i, std::string::npos);
130  }
131 
132  { // Repeat test for YAML output, compact style.
133  std::ostringstream oss;
134  RCP<ParameterList> reportParams =
135  parameterList (* (TimeMonitor::getValidReportParameters ()));
136  reportParams->set ("Report format", "YAML");
137  reportParams->set ("YAML style", "compact");
138  TimeMonitor::report (oss, reportParams);
139 
140  // Echo output to the FancyOStream out (which is a standard unit
141  // test argument). Output should only appear in "show all test
142  // details" mode.
143  out << oss.str () << std::endl;
144 
145  // Make sure that the timer's name shows up in the output.
146  const size_t substr_i = oss.str ().find ("FUNC_TIME_MONITOR1");
147  TEST_INEQUALITY(substr_i, std::string::npos);
148  }
149 
150  { // Repeat test for YAML output, spacious style.
151  std::ostringstream oss;
152  RCP<ParameterList> reportParams =
153  parameterList (* (TimeMonitor::getValidReportParameters ()));
154  reportParams->set ("Report format", "YAML");
155  reportParams->set ("YAML style", "spacious");
156  TimeMonitor::report (oss, reportParams);
157 
158  // Echo output to the FancyOStream out (which is a standard unit
159  // test argument). Output should only appear in "show all test
160  // details" mode.
161  out << oss.str () << std::endl;
162 
163  // Make sure that the timer's name shows up in the output.
164  const size_t substr_i = oss.str ().find ("FUNC_TIME_MONITOR1");
165  TEST_INEQUALITY(substr_i, std::string::npos);
166  }
167 
168  // This sets up for the next unit test.
170  }
171 
172  //
173  // Test for TimeMonitor's enableTimer and disableTimer methods.
174  //
176  {
177  using Teuchos::Array;
178  using Teuchos::OSTab;
180  using Teuchos::parameterList;
181  using Teuchos::RCP;
182  using Teuchos::rcpFromRef;
183  using Teuchos::Time;
184  using Teuchos::TimeMonitor;
185  using std::endl;
186  typedef Teuchos::Array<RCP<Time> >::size_type size_type;
187 
188  out << "Testing TimeMonitor's disableTimer() and enableTimer() methods"
189  << endl;
190  OSTab (rcpFromRef (out));
191 
192  out << "Creating timers" << endl;
193  const int numTrials = 5;
194  const int numTimers = 3;
195  Array<RCP<Time> > timers (numTimers);
196  for (size_type i = 0; i < numTimers; ++i) {
197  std::ostringstream os; // construct timer name
198  os << "Timer " << i;
199  timers[i] = TimeMonitor::getNewTimer (os.str ());
200  }
201 
202  out << "Running all timers without disabling any" << endl;
203  // The actual number of operations in slowloop is proportional to
204  // the cube of the loop length. Adjust loopLength as necessary to
205  // ensure the timer reports a nonzero elapsed time for each of the
206  // invocations.
207  const size_t loopLength = 25;
208  for (int k = 0; k < numTrials; ++k) {
209  for (size_type i = 0; i < numTimers; ++i) {
210  TimeMonitor timeMon (* timers[i]);
211  slowLoop (loopLength);
212  }
213  }
214  for (size_type i = 0; i < numTimers; ++i) {
215  TEST_EQUALITY( timers[i]->numCalls(), numTrials );
216  }
217 
218  out << "Disabling one timer and trying again" << endl;
219  // Disable timers[0] only, and repeat the above loops.
221  for (int k = 0; k < numTrials; ++k) {
222  for (size_type i = 0; i < numTimers; ++i) {
223  TimeMonitor timeMon (* timers[i]);
224  slowLoop (loopLength);
225  }
226  }
227  TEST_EQUALITY( timers[0]->numCalls(), numTrials );
228  for (size_type i = 1; i < numTimers; ++i) {
229  TEST_EQUALITY( timers[i]->numCalls(), 2*numTrials );
230  }
231 
232  out << "Reenabling the timer and trying again" << endl;
233  // Enable timers[0] and repeat the above loops.
234  TEST_NOTHROW( TimeMonitor::enableTimer ("Timer 0") );
235  for (int k = 0; k < numTrials; ++k) {
236  for (size_type i = 0; i < numTimers; ++i) {
237  TimeMonitor timeMon (* timers[i]);
238  slowLoop (loopLength);
239  }
240  }
241  TEST_EQUALITY( timers[0]->numCalls(), 2*numTrials );
242  for (size_type i = 1; i < numTimers; ++i) {
243  TEST_EQUALITY( timers[i]->numCalls(), 3*numTrials );
244  }
245 
246  out << "Test that summarize() reports enabled and disabled timers" << endl;
247  // Make sure that summarize() reports all timers. Disabling a
248  // timer must _not_ exclude it from the list of timers printed by
249  // summarize(). Disable a different timer this time just for fun.
251  {
252  std::ostringstream oss;
254 
255  // Echo summarize() output to the FancyOStream out (which is a
256  // standard unit test argument). Output should only appear in
257  // show-all-test-details mode.
258  out << oss.str () << std::endl;
259 
260  // Make sure that each timer's name shows up in the output.
261  for (size_type i = 0; i < numTimers; ++i) {
262  const std::string name = timers[i]->name ();
263  const size_t substr_i = oss.str ().find (name);
264  TEST_INEQUALITY(substr_i, std::string::npos);
265  }
266  }
267 
268  // This sets up for the next unit test.
270  }
271 
272 
273  //
274  // Test correct quoting of labels for TimeMonitor's YAML output.
275  //
276  TEUCHOS_UNIT_TEST( TimeMonitor, YamlLabelQuoting )
277  {
278  using Teuchos::Array;
280  using Teuchos::parameterList;
281  using Teuchos::RCP;
282  using Teuchos::Time;
283  typedef Array<std::string>::size_type size_type;
284 
285  Array<std::string> inputLabels, outputLabels;
286 
287  // Make sure to exercise things that don't need quoting, like
288  // spaces and certain punctuation, as well as things that do need
289  // quoting, like colons, inner double quotes, and backslashes.
290  inputLabels.push_back ("NoQuotingNeeded");
291  inputLabels.push_back ("No quoting needed");
292  inputLabels.push_back ("\"AlreadyQuotedNoQuotingNeeded\"");
293  inputLabels.push_back ("\"Already quoted, no quoting needed\"");
294  inputLabels.push_back ("\"Already quoted: quoting needed\"");
295  inputLabels.push_back ("NotQuoted:QuotingNeeded");
296  inputLabels.push_back ("Not quoted: quoting needed");
297  // Test both individual double quotes, and pairs of double quotes.
298  inputLabels.push_back ("Not quoted \" quoting needed");
299  inputLabels.push_back ("Not quoted \" \" quoting needed");
300  inputLabels.push_back ("\"Already quoted \" quoting needed\"");
301  inputLabels.push_back ("\"Already quoted \" \" quoting needed\"");
302  // Remember that in C strings, a double backslash turns into a
303  // single backslash. Our YAML output routine should turn each
304  // single backslash back into a double backslash.
305  inputLabels.push_back ("Not quoted \\ quoting needed");
306  inputLabels.push_back ("Not quoted \\\\ quoting needed");
307  inputLabels.push_back ("Not quoted \\ \\ quoting needed");
308  inputLabels.push_back ("\"Already quoted \\ quoting needed\"");
309  inputLabels.push_back ("\"Already quoted \\\\ quoting needed\"");
310  inputLabels.push_back ("\"Already quoted \\ \\ quoting needed\"");
311 
312  outputLabels.push_back ("NoQuotingNeeded");
313  outputLabels.push_back ("No quoting needed");
314  outputLabels.push_back ("\"AlreadyQuotedNoQuotingNeeded\"");
315  outputLabels.push_back ("\"Already quoted, no quoting needed\"");
316  outputLabels.push_back ("\"Already quoted: quoting needed\"");
317  outputLabels.push_back ("\"NotQuoted:QuotingNeeded\"");
318  outputLabels.push_back ("\"Not quoted: quoting needed\"");
319  outputLabels.push_back ("\"Not quoted \\\" quoting needed\"");
320  outputLabels.push_back ("\"Not quoted \\\" \\\" quoting needed\"");
321  outputLabels.push_back ("\"Already quoted \\\" quoting needed\"");
322  outputLabels.push_back ("\"Already quoted \\\" \\\" quoting needed\"");
323  outputLabels.push_back ("\"Not quoted \\\\ quoting needed\"");
324  outputLabels.push_back ("\"Not quoted \\\\\\\\ quoting needed\"");
325  outputLabels.push_back ("\"Not quoted \\\\ \\\\ quoting needed\"");
326  outputLabels.push_back ("\"Already quoted \\\\ quoting needed\"");
327  outputLabels.push_back ("\"Already quoted \\\\\\\\ quoting needed\"");
328  outputLabels.push_back ("\"Already quoted \\\\ \\\\ quoting needed\"");
329 
330  // Sanity check.
332  inputLabels.size () != outputLabels.size (),
333  std::logic_error,
334  "The number of input labels is different than the number of output labels."
335  " Please ask a Teuchos developer to make sure that every test input "
336  "label has a corresponding output label.");
337 
338  Array<RCP<Time> > timers;
339  for (size_type i = 0; i < inputLabels.size (); ++i) {
340  timers.push_back (TimeMonitor::getNewCounter (inputLabels[i]));
341  }
342 
343  // The actual number of operations in the loop is proportional to
344  // the cube of the loop length. Adjust the quantities below as
345  // necessary to ensure the timer reports a nonzero elapsed time
346  // for each of the invocations.
347  const size_t loopLength = 25;
348  for (int k = 0; k < 3; ++k) {
349  for (size_type i = 0; i < timers.size (); ++i) {
350  TimeMonitor timeMon (* timers[i]);
351  slowLoop (loopLength);
352  }
353  }
354 
355  { // YAML output, compact style.
356  std::ostringstream oss;
357  RCP<ParameterList> reportParams =
358  parameterList (* (TimeMonitor::getValidReportParameters ()));
359  reportParams->set ("Report format", "YAML");
360  reportParams->set ("YAML style", "compact");
361  TimeMonitor::report (oss, reportParams);
362 
363  // Echo output to the FancyOStream out (which is a standard unit
364  // test argument). Output should only appear in "show all test
365  // details" mode.
366  out << oss.str () << std::endl;
367 
368  // Make sure that all timer labels appear correctly in the output.
369  for (size_type i = 0; i < inputLabels.size(); ++i) {
370  const size_t pos = oss.str ().find (outputLabels[i]);
371  TEST_INEQUALITY(pos, std::string::npos);
372  }
373  }
374 
375  { // YAML output, spacious style.
376  std::ostringstream oss;
377  RCP<ParameterList> reportParams =
378  parameterList (* (TimeMonitor::getValidReportParameters ()));
379  reportParams->set ("Report format", "YAML");
380  reportParams->set ("YAML style", "spacious");
381  TimeMonitor::report (oss, reportParams);
382 
383  // Echo output to the FancyOStream out (which is a standard unit
384  // test argument). Output should only appear in "show all test
385  // details" mode.
386  out << oss.str () << std::endl;
387 
388  // Make sure that all timer labels appear correctly in the output.
389  for (size_type i = 0; i < inputLabels.size(); ++i) {
390  const size_t pos = oss.str ().find (outputLabels[i]);
391  TEST_INEQUALITY(pos, std::string::npos);
392  }
393  }
394 
395  // This sets up for the next unit test.
397  }
398 
399 
400  //
401  // Test filtering of timer labels.
402  //
403  TEUCHOS_UNIT_TEST( TimeMonitor, TimerLabelFiltering )
404  {
405  using Teuchos::Array;
407  using Teuchos::parameterList;
408  using Teuchos::RCP;
409  using Teuchos::Time;
410  typedef Array<std::string>::size_type size_type;
411 
412  // Filters to use in the test.
413  Array<std::string> filters;
414  filters.push_back ("Foo:");
415  filters.push_back ("Bar:");
416  filters.push_back ("Baz:");
417 
418  // All the timer labels.
419  Array<std::string> labels;
420  labels.push_back ("Foo: timer 1");
421  labels.push_back ("Foo: timer 2");
422  labels.push_back ("Foo: timer 3");
423  labels.push_back ("Bar: timer 1");
424  labels.push_back ("Bar: timer 2");
425  labels.push_back ("Baz: timer 1");
426  labels.push_back ("Xyzzy");
427  labels.push_back ("This is not a pipe");
428  labels.push_back ("You should not see this");
429 
430  Array<Array<std::string> > outLabels (3);
431  // Label(s) that should be printed for filters[0]
432  outLabels[0].push_back ("Foo: timer 1");
433  outLabels[0].push_back ("Foo: timer 2");
434  outLabels[0].push_back ("Foo: timer 3");
435  // Label(s) that should be printed for filters[1]
436  outLabels[1].push_back ("Bar: timer 1");
437  outLabels[1].push_back ("Bar: timer 2");
438  // Label(s) that should be printed for filters[2]
439  outLabels[2].push_back ("Baz: timer 1");
440 
441  // Labels that should not be printed for any of the filters below.
442  Array<std::string> otherLabels;
443  otherLabels.push_back ("Xyzzy");
444  otherLabels.push_back ("This is not a pipe");
445  otherLabels.push_back ("You should not see this");
446 
447  Array<RCP<Time> > timers;
448  for (size_type i = 0; i < labels.size (); ++i) {
449  timers.push_back (TimeMonitor::getNewCounter (labels[i]));
450  }
451 
452  // The actual number of operations in the loop is proportional to
453  // the cube of the loop length. Adjust the quantities below as
454  // necessary to ensure the timer reports a nonzero elapsed time
455  // for each of the invocations.
456  const size_t loopLength = 25;
457  for (int k = 0; k < 3; ++k) {
458  for (size_type i = 0; i < timers.size (); ++i) {
459  TimeMonitor timeMon (* timers[i]);
460  slowLoop (loopLength);
461  }
462  }
463 
464  try {
465  // FIXME (mfh 21 Aug 2012) We don't yet have a test ensuring that
466  // the filter only selects at the beginning of the timer label.
467 
468  // Test for each filter.
469  for (size_type i = 0; i < filters.size (); ++i) {
470  { // Default (tabular) output format.
471  std::ostringstream oss;
472  RCP<ParameterList> reportParams =
473  parameterList (* (TimeMonitor::getValidReportParameters ()));
474  TimeMonitor::report (oss, filters[i], reportParams);
475 
476  // Echo output to the FancyOStream out (which is a standard unit
477  // test argument). Output should only appear in "show all test
478  // details" mode.
479  out << oss.str () << std::endl;
480 
481  // Check whether the labels that were supposed to be printed
482  // were actually printed.
483  for (size_type j = 0; j < outLabels[i].size(); ++j) {
484  const size_t pos = oss.str ().find (outLabels[i][j]);
485  TEST_INEQUALITY(pos, std::string::npos);
486  }
487 
488  // Check whether the labels that were _not_ supposed to be
489  // printed were actually printed.
490  //
491  // First, check the labels that should only be printed with
492  // the other filters.
493  for (size_type ii = 0; ii < outLabels.size(); ++ii) {
494  if (ii != i) {
495  for (size_type j = 0; j < outLabels[ii].size(); ++j) {
496  const size_t pos = oss.str ().find (outLabels[ii][j]);
497  TEST_EQUALITY(pos, std::string::npos);
498  }
499  }
500  }
501  // Next, check the labels that should not be printed for any
502  // filters.
503  for (size_type j = 0; j < otherLabels.size(); ++j) {
504  const size_t pos = oss.str ().find (otherLabels[j]);
505  TEST_EQUALITY(pos, std::string::npos);
506  }
507  }
508 
509  { // YAML output, compact style.
510  std::ostringstream oss;
511  RCP<ParameterList> reportParams =
512  parameterList (* (TimeMonitor::getValidReportParameters ()));
513  reportParams->set ("Report format", "YAML");
514  reportParams->set ("YAML style", "compact");
515  TimeMonitor::report (oss, filters[i], reportParams);
516 
517  // Echo output to the FancyOStream out (which is a standard unit
518  // test argument). Output should only appear in "show all test
519  // details" mode.
520  out << oss.str () << std::endl;
521 
522  // Check whether the labels that were supposed to be printed
523  // were actually printed.
524  for (size_type j = 0; j < outLabels[i].size(); ++j) {
525  const size_t pos = oss.str ().find (outLabels[i][j]);
526  TEST_INEQUALITY(pos, std::string::npos);
527  }
528 
529  // Check whether the labels that were _not_ supposed to be
530  // printed were actually printed.
531  //
532  // First, check the labels that should only be printed with
533  // the other filters.
534  for (size_type ii = 0; ii < outLabels.size(); ++ii) {
535  if (ii != i) {
536  for (size_type j = 0; j < outLabels[ii].size(); ++j) {
537  const size_t pos = oss.str ().find (outLabels[ii][j]);
538  TEST_EQUALITY(pos, std::string::npos);
539  }
540  }
541  }
542  // Next, check the labels that should not be printed for any
543  // filters.
544  for (size_type j = 0; j < otherLabels.size(); ++j) {
545  const size_t pos = oss.str ().find (otherLabels[j]);
546  TEST_EQUALITY(pos, std::string::npos);
547  }
548  }
549 
550  { // YAML output, spacious style.
551  std::ostringstream oss;
552  RCP<ParameterList> reportParams =
553  parameterList (* (TimeMonitor::getValidReportParameters ()));
554  reportParams->set ("Report format", "YAML");
555  reportParams->set ("YAML style", "spacious");
556  TimeMonitor::report (oss, filters[i], reportParams);
557 
558  // Echo output to the FancyOStream out (which is a standard unit
559  // test argument). Output should only appear in "show all test
560  // details" mode.
561  out << oss.str () << std::endl;
562 
563  // Check whether the labels that were supposed to be printed
564  // were actually printed.
565  for (size_type j = 0; j < outLabels[i].size(); ++j) {
566  const size_t pos = oss.str ().find (outLabels[i][j]);
567  TEST_INEQUALITY(pos, std::string::npos);
568  }
569 
570  // Check whether the labels that were _not_ supposed to be
571  // printed were actually printed.
572  //
573  // First, check the labels that should only be printed with
574  // the other filters.
575  for (size_type ii = 0; ii < outLabels.size(); ++ii) {
576  if (ii != i) {
577  for (size_type j = 0; j < outLabels[ii].size(); ++j) {
578  const size_t pos = oss.str ().find (outLabels[ii][j]);
579  TEST_EQUALITY(pos, std::string::npos);
580  }
581  }
582  }
583  // Next, check the labels that should not be printed for any
584  // filters.
585  for (size_type j = 0; j < otherLabels.size(); ++j) {
586  const size_t pos = oss.str ().find (otherLabels[j]);
587  TEST_EQUALITY(pos, std::string::npos);
588  }
589  }
590  }
591  }
592  catch (...) {
593  // Make sure to clear the counters, so that they don't pollute
594  // the remaining tests. (The Teuchos unit test framework may
595  // catch any exceptions that the above code throws, but allow
596  // the remaining tests to continue.)
598  throw;
599  }
600 
601  // This sets up for the next unit test.
603  }
604 
605 
606 
607  //
608  // TimeMonitor nested timers test: create two timers on all (MPI)
609  // processes, use the second inside the scope of the first, and make
610  // sure that TimeMonitor::summarize() reports both timers.
611  //
612  TEUCHOS_UNIT_TEST( TimeMonitor, FUNC_TIME_MONITOR_tested )
613  {
615  using Teuchos::parameterList;
616  using Teuchos::RCP;
617 
618  func_time_monitor2 ();
619 
620  {
621  std::ostringstream oss;
623 
624  // Echo summarize() output to the FancyOStream out (which is a
625  // standard unit test argument). Output should only appear in
626  // show-all-test-details mode.
627  out << oss.str() << std::endl;
628 
629  const size_t substr_i = oss.str().find ("FUNC_TIME_MONITOR2");
630  TEST_INEQUALITY(substr_i, std::string::npos);
631  const size_t substr_inner_i = oss.str().find ("FUNC_TIME_MONITOR2_inner");
632  TEST_INEQUALITY(substr_inner_i, std::string::npos);
633  }
634 
635  { // Repeat test for YAML output, compact style.
636  std::ostringstream oss;
637  RCP<ParameterList> reportParams =
638  parameterList (* (TimeMonitor::getValidReportParameters ()));
639  reportParams->set ("Report format", "YAML");
640  reportParams->set ("YAML style", "compact");
641  TimeMonitor::report (oss, reportParams);
642 
643  // Echo output to the FancyOStream out (which is a standard unit
644  // test argument). Output should only appear in "show all test
645  // details" mode.
646  out << oss.str () << std::endl;
647 
648  const size_t substr_i = oss.str().find ("FUNC_TIME_MONITOR2");
649  TEST_INEQUALITY(substr_i, std::string::npos);
650  const size_t substr_inner_i = oss.str().find ("FUNC_TIME_MONITOR2_inner");
651  TEST_INEQUALITY(substr_inner_i, std::string::npos);
652  }
653 
654  { // Repeat test for YAML output, spacious style.
655  std::ostringstream oss;
656  RCP<ParameterList> reportParams =
657  parameterList (* (TimeMonitor::getValidReportParameters ()));
658  reportParams->set ("Report format", "YAML");
659  reportParams->set ("YAML style", "spacious");
660  TimeMonitor::report (oss, reportParams);
661 
662  // Echo output to the FancyOStream out (which is a standard unit
663  // test argument). Output should only appear in "show all test
664  // details" mode.
665  out << oss.str () << std::endl;
666 
667  const size_t substr_i = oss.str().find ("FUNC_TIME_MONITOR2");
668  TEST_INEQUALITY(substr_i, std::string::npos);
669  const size_t substr_inner_i = oss.str().find ("FUNC_TIME_MONITOR2_inner");
670  TEST_INEQUALITY(substr_inner_i, std::string::npos);
671  }
672 
673  // This sets up for the next unit test.
675  }
676 
677  //
678  // Test whether TimeMonitor::summarize() does the right thing when
679  // different MPI processes create different sets of timers. This
680  // test is only meaningful if running with more than one MPI
681  // process, but it should also pass if running in a non-MPI build or
682  // with only one MPI process.
683  //
684  TEUCHOS_UNIT_TEST( TimeMonitor, SUMMARIZE_diffTimerSets )
685  {
687  const int numProcs = comm->getSize ();
688  const int myRank = comm->getRank ();
689 
690  // If MPI has not been initialized, turn the MPI communicator into
691  // a "serial" communicator. This may not be necessary when using
692  // the Teuchos Unit Test Framework.
693 #ifdef HAVE_MPI
694  {
695  int mpiHasBeenInitialized = 0;
696  MPI_Initialized (&mpiHasBeenInitialized);
697  if (! mpiHasBeenInitialized) {
699  }
700  }
701 #endif // HAVE_MPI
702 
703  // Store the output of TimeMonitor::summarize() here.
704  std::ostringstream oss;
705 
706  // Timer A gets created on all MPI processes.
707  // Timer B only gets created on Proc 0.
708  RCP<Time> timerA = TimeMonitor::getNewCounter ("Timer A");
709  RCP<Time> timerB;
710  if (myRank == 0) {
711  timerB = TimeMonitor::getNewCounter ("Timer B");
712  }
713  else {
714  timerB = null; // True anyway, but I want to make this explicit.
715  }
716 
717  // The actual number of operations in the loop is proportional to
718  // the cube of the loop length. Adjust the quantities below as
719  // necessary to ensure the timer reports a nonzero elapsed time
720  // for each of the invocations.
721  const size_t timerA_loopLength = 150;
722  const size_t timerB_loopLength = 200;
723 
724  // Timer A gets a call count of 3.
725  for (size_t k = 0; k < 3; ++k) {
726  TimeMonitor monitorA (*timerA);
727  slowLoop (timerA_loopLength);
728  }
729  if (myRank == 0) { // Timer B gets a call count of 1 on Proc 0.
730  TimeMonitor monitorB (*timerB);
731  slowLoop (timerB_loopLength);
732  }
733 
734  const bool alwaysWriteLocal = false; // the default
735  const bool writeGlobalStats = true; // the default
736  const bool writeZeroTimers = true; // the default
737  TimeMonitor::summarize (oss, alwaysWriteLocal, writeGlobalStats,
738  writeZeroTimers, Intersection);
739 
740  // Echo summarize() output to the FancyOStream out (which is a
741  // standard unit test argument). Output should only appear in
742  // show-all-test-details mode.
743  out << std::endl << "Testing intersection of timers:" << std::endl
744  << oss.str() << std::endl;
745 
746  // Since setOp == Intersection, only Timer A should be reported,
747  // unless there is only one (MPI) process.
748  size_t substr_i = oss.str().find ("Timer A");
749  TEST_INEQUALITY(substr_i, std::string::npos);
750  if (numProcs > 1) {
751  substr_i = oss.str().find ("Timer B");
752  TEST_EQUALITY(substr_i, std::string::npos);
753  }
754 
755  // Now call summarize(), but ask for a union of timers.
756  std::ostringstream ossUnion;
757  TimeMonitor::summarize (ossUnion, alwaysWriteLocal, writeGlobalStats,
758  writeZeroTimers, Union);
759 
760  // Echo summarize() output to the FancyOStream out (which is a
761  // standard unit test argument). Output should only appear in
762  // show-all-test-details mode.
763  out << std::endl << "Testing union of timers:" << std::endl
764  << ossUnion.str() << std::endl;
765 
766  // Since setOp == Union, both Timer A and Timer B should be
767  // reported.
768  substr_i = ossUnion.str().find ("Timer A");
769  TEST_INEQUALITY(substr_i, std::string::npos);
770  substr_i = ossUnion.str().find ("Timer B");
771  TEST_INEQUALITY(substr_i, std::string::npos);
772 
773  // This sets up for the next unit test.
775  }
776 
777  //
778  // Test whether the option to filter out zero timers works, for the
779  // case that all (MPI) processes have the same call counts.
780  //
781  TEUCHOS_UNIT_TEST( TimeMonitor, FILTER_ZERO_TIMERS_sameParallelCallCounts )
782  {
783  // Store the output of TimeMonitor::summarize() here.
784  std::ostringstream oss;
785 
786  RCP<Time> timerA = TimeMonitor::getNewCounter ("Timer A");
787  RCP<Time> timerB = TimeMonitor::getNewCounter ("Timer B");
788  RCP<Time> timerC = TimeMonitor::getNewCounter ("Timer C");
789 
790  // Timers A and B get nonzero elapsed times and call counts on all
791  // (MPI) processes. Timer C never gets started, so it should have
792  // a zero elapsed time and zero call count on all processes.
793 
794  // The actual number of operations in the loop is proportional to
795  // the cube of the loop length. Adjust the quantities below as
796  // necessary to ensure the timer reports a nonzero elapsed time
797  // for each of the invocations.
798  const size_t timerA_loopLength = 200;
799  const size_t timerB_loopLength = 250;
800 
801  // Timer A gets a call count of 3.
802  for (size_t k = 0; k < 3; ++k) {
803  TimeMonitor monitorA (*timerA);
804  slowLoop (size_t (timerA_loopLength));
805  }
806  // Timer B gets a call count of 1.
807  {
808  TimeMonitor monitorB (*timerB);
809  slowLoop (size_t (timerB_loopLength));
810  }
811 
812  const bool alwaysWriteLocal = false; // the default
813  const bool writeGlobalStats = true; // the default
814  const bool writeZeroTimers = false; // NOT the default
815  TimeMonitor::summarize (oss, alwaysWriteLocal, writeGlobalStats,
816  writeZeroTimers);
817 
818  // Echo summarize() output to the FancyOStream out (which is a
819  // standard unit test argument). Output should only appear in
820  // show-all-test-details mode.
821  out << oss.str() << std::endl;
822 
823  // Timers A and B should be reported.
824  size_t substr_i = oss.str().find ("Timer A");
825  TEST_INEQUALITY(substr_i, std::string::npos);
826  substr_i = oss.str().find ("Timer B");
827  TEST_INEQUALITY(substr_i, std::string::npos);
828 
829  // Timer C should NOT be reported.
830  substr_i = oss.str().find ("Timer C");
831  TEST_EQUALITY(substr_i, std::string::npos);
832 
833  // This sets up for the next unit test.
835  }
836 
837 
838  //
839  // Test whether the option to filter out zero timers works, for the
840  // case that different (MPI) processes have different call counts.
841  //
842  TEUCHOS_UNIT_TEST( TimeMonitor, FILTER_ZERO_TIMERS_differentParallelCallCounts )
843  {
845  const int myRank = comm->getRank ();
846 
847  // If MPI has not been initialized, turn the MPI communicator into
848  // a "serial" communicator. This may not be necessary when using
849  // the Teuchos Unit Test Framework.
850 #ifdef HAVE_MPI
851  {
852  int mpiHasBeenInitialized = 0;
853  MPI_Initialized (&mpiHasBeenInitialized);
854  if (! mpiHasBeenInitialized) {
856  }
857  }
858 #endif // HAVE_MPI
859 
860  // Store the output of TimeMonitor::summarize() here.
861  std::ostringstream oss;
862 
863  RCP<Time> timerA = TimeMonitor::getNewCounter ("Timer A");
864  RCP<Time> timerB = TimeMonitor::getNewCounter ("Timer B");
865  RCP<Time> timerC = TimeMonitor::getNewCounter ("Timer C");
866 
867  // Timer A gets a nonzero elapsed time and call count on Proc 0,
868  // but a zero elapsed time and call count elsewhere.
869  //
870  // Timer B gets the same nonzero elapsed time and call count on
871  // all MPI procs.
872  //
873  // Timer C gets a zero elapsed time and call count on all procs.
874  //
875  // The actual number of operations in the loop is proportional to
876  // the cube of the loop length. Adjust the quantities below as
877  // necessary to ensure the timer reports a nonzero elapsed time
878  // for each of the invocations.
879  const size_t timerA_loopLength = 200;
880  const size_t timerB_loopLength = 500;
881 
882  if (myRank == 0) {
883  // Timer A gets a call count of 3 on Proc 0.
884  for (int k = 0; k < 3; ++k) {
885  TimeMonitor monitorA (*timerA);
886  slowLoop (size_t (timerA_loopLength));
887  }
888  }
889 
890  // Timer B gets a call count of 1 on all procs.
891  {
892  TimeMonitor monitorB (*timerB);
893  slowLoop (size_t (timerB_loopLength));
894  }
895 
896  const bool alwaysWriteLocal = false; // the default
897  const bool writeGlobalStats = true; // the default
898  const bool writeZeroTimers = false; // NOT the default
899  TimeMonitor::summarize (oss, alwaysWriteLocal, writeGlobalStats,
900  writeZeroTimers, Union);
901 
902  // Echo summarize() output to the FancyOStream out (which is a
903  // standard unit test argument). Output should only appear in
904  // show-all-test-details mode.
905  out << oss.str() << std::endl;
906 
907  // Timers A and B should both be reported.
908  size_t substr_i = oss.str().find ("Timer A");
909  TEST_INEQUALITY(substr_i, std::string::npos);
910  substr_i = oss.str().find ("Timer B");
911  TEST_INEQUALITY(substr_i, std::string::npos);
912 
913  // Timer C should NOT be reported.
914  substr_i = oss.str().find ("Timer C");
915  TEST_EQUALITY(substr_i, std::string::npos);
916 
917  // This sets up for the next unit test (if there is one).
919  }
920 
921 
922 } // namespace Teuchos
#define TEUCHOS_FUNC_TIME_MONITOR(FUNCNAME)
Defines a timer for a specific function.
#define TEST_INEQUALITY(v1, v2)
Assert the inequality of v1 and v2.
static Teuchos::RCP< const Comm< OrdinalType > > getDefaultSerialComm(const Teuchos::RCP< const Comm< OrdinalType > > &comm)
Return a serial comm if the input comm is null.
static void clearCounters()
&quot;Forget&quot; about all counters created with getNewCounter().
#define TEST_NOTHROW(code)
Asserr that the statement &#39;code&#39; does not thrown any excpetions.
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Set a parameter whose value has type T.
static RCP< Time > getNewCounter(const std::string &name)
Create a new counter with the specified name and add it to a global set of counters of this type...
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
static void disableTimer(const std::string &name)
Disable the timer with the given name.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
static Teuchos::RCP< const Comm< OrdinalType > > getComm()
Return the default global communicator.
static RCP< const ParameterList > getValidReportParameters()
Default parameters (with validators) for report().
basic_OSTab< char > OSTab
static RCP< Time > getNewTimer(const std::string &name)
Return a new timer with the given name (class method).
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="")
Print summary statistics for all timers on the given communicator.
TEUCHOS_UNIT_TEST(ConstNonconstObjectContainer, create)
Wall-clock timer.
Unit testing support.
A list of parameters of arbitrary type.
void push_back(const value_type &x)
size_type size() const
Scope protection wrapper for Teuchos::Time, with timer reporting functionality.
Smart reference counting pointer class for automatic garbage collection.
static void enableTimer(const std::string &name)
Enable the timer with the given name.
A scope-safe timer wrapper class, that can compute global timer statistics.
static void report(Ptr< const Comm< int > > comm, std::ostream &out, const std::string &filter, const RCP< ParameterList > &params=null)
Report timer statistics to the given output stream.
#define TEUCHOS_FUNC_TIME_MONITOR_DIFF(FUNCNAME, DIFF)
Defines a timer for a specific function (with differentiator).
Memory-safe templated array class that encapsulates std::vector.