24 #if defined(HAVE_TEUCHOS_KOKKOS_PROFILING) && defined(HAVE_TEUCHOSCORE_KOKKOS)
25 #include "Kokkos_Core.hpp"
79 const int myRank = Teuchos::rank(*comm);
84 timer.
start(
"Total Time");
86 for (
int i=0; i < 10; ++i) {
88 timer.
start(
"Assembly");
89 std::this_thread::sleep_for(std::chrono::milliseconds{100});
90 timer.
stop(
"Assembly");
95 std::this_thread::sleep_for(std::chrono::milliseconds{50});
100 const std::string label =
"Rank 0 ONLY";
102 std::this_thread::sleep_for(std::chrono::milliseconds{50});
107 timer.
start(
"Not Rank 0");
108 std::this_thread::sleep_for(std::chrono::milliseconds{50});
110 timer.
stop(
"Not Rank 0");
118 timer.
stop(
"Total Time");
143 std::stringstream sout1;
144 timer.
report(sout1, comm, options);
148 std::stringstream sout2;
149 timer.
report(sout2, comm, options);
153 std::vector<std::tuple<std::string,double,unsigned long>> lineChecks;
154 lineChecks.push_back(std::make_tuple(
"My New Timer:",2.0,1));
155 lineChecks.push_back(std::make_tuple(
"Total Time:",2.0,1));
156 lineChecks.push_back(std::make_tuple(
"Assembly:",1.0,10));
157 lineChecks.push_back(std::make_tuple(
"Solve:",1.0,10));
158 lineChecks.push_back(std::make_tuple(
"Prec:",0.5,10));
167 #if !defined(__GNUC__) \
168 || ( defined(__GNUC__) && (__GNUC__ > 4) ) \
169 || ( defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC__MINOR__ > 8) )
172 const double timerTolerance = 0.25;
173 std::istringstream is(sout1.str());
174 for (
const auto& check : lineChecks) {
177 std::getline(is,line);
178 std::smatch regexSMatch;
179 std::regex timerName(std::get<0>(check));
180 std::regex_search(line,regexSMatch,timerName);
184 std::regex delimiter(
":\\s|\\s\\[|\\]\\s");
185 std::sregex_token_iterator tok(line.begin(), line.end(),delimiter,-1);
187 const std::string timeAsString = (++tok)->str();
188 const double time = std::stod(timeAsString);
191 const std::string countAsString = (++tok)->str();
192 const unsigned long count = std::stoul(countAsString);
199 out <<
"\n### Printing default report ###" << std::endl;
201 timer.
report(out, comm, defaultOptions);
204 out <<
"\n### Printing aligned_column with timers names on left ###" << std::endl;
211 timer.
report(out, comm, options);
215 out <<
"\n### Printing aligned_column with timers names on right ###" << std::endl;
223 std::string reportOut;
225 std::ostringstream reportOut1;
226 timer.
report(reportOut1, comm, options);
227 std::ostringstream reportOut2;
228 timer.
report(reportOut2, comm, options);
229 reportOut = reportOut1.str();
232 std::string reportXmlOut;
234 std::ostringstream reportOut1;
235 timer.
reportXML(reportOut1,
"2020_01_01",
"2020-01-01T01:02:03", comm);
236 std::ostringstream reportOut2;
237 timer.
reportXML(reportOut2,
"2020_01_01",
"2020-01-01T01:02:03", comm);
238 reportXmlOut = reportOut1.str();
241 out << reportOut <<
'\n';
242 out << reportXmlOut <<
'\n';
248 const int myRank = Teuchos::rank(*comm);
252 timer->startBaseTimer();
253 for (
int i=0; i < 10; ++i) {
254 timer-> start(
"Subtask");
255 timer->incrementUpdates();
256 timer->incrementUpdates(2);
257 timer-> stop(
"Subtask");
259 timer->stopBaseTimer();
264 auto sub_timer =
const_cast<Teuchos::BaseTimer*
>(timer->findBaseTimer(
"Total Time@Subtask"));
269 TEST_THROW(timer->findBaseTimer(
"Testing misspelled timer name!"),std::runtime_error);
283 top_timer->setAccumulatedTime(5000.0);
284 top_timer->overrideNumCallsForUnitTesting(2);
285 top_timer->overrideNumUpdatesForUnitTesting(3);
286 sub_timer->setAccumulatedTime(4000.0);
287 sub_timer->overrideNumCallsForUnitTesting(4);
288 sub_timer->overrideNumUpdatesForUnitTesting(5);
290 const double timerTolerance = 100.0 * std::numeric_limits<double>::epsilon();
317 TEST_ASSERT(timeMonitorDefaultStackedTimer != timer);
321 timer->start(
"Total Time");
323 for (
int i=0; i < 10; ++i) {
325 timer->start(
"Assembly");
329 std::this_thread::sleep_for(std::chrono::milliseconds{25});
333 std::this_thread::sleep_for(std::chrono::milliseconds{75});
336 std::this_thread::sleep_for(std::chrono::milliseconds{100});
338 timer->stop(
"Assembly");
339 timer->start(
"Solve");
343 std::this_thread::sleep_for(std::chrono::milliseconds{50});
347 std::this_thread::sleep_for(std::chrono::milliseconds{50});
350 std::this_thread::sleep_for(std::chrono::milliseconds{100});
352 timer->stop(
"Solve");
353 std::this_thread::sleep_for(std::chrono::milliseconds{100});
356 timer->stop(
"Total Time");
357 timer->stopBaseTimer();
359 assert(
size(*comm)>0);
361 TEST_EQUALITY((timer->findTimer(
"TM:Interoperability@Total Time")).count, 1);
362 TEST_EQUALITY((timer->findTimer(
"TM:Interoperability@Total Time@Assembly")).count, 10);
365 #ifdef HAVE_TEUCHOS_ADD_TIME_MONITOR_TO_STACKED_TIMER
366 TEST_EQUALITY((timer->findTimer(
"TM:Interoperability@Total Time@Solve@Prec")).count, 10);
367 TEST_EQUALITY((timer->findTimer(
"TM:Interoperability@Total Time@Solve@GMRES")).count, 10);
371 out <<
"\n### Printing default report ###" << std::endl;
375 timer->report(out, comm, options);
377 out <<
"\n### Printing aligned_column with timers names on left ###" << std::endl;
379 timer->report(out, comm, options);
381 out <<
"\n### Printing aligned_column with timers names on right ###" << std::endl;
388 timer->report(out, comm, options);
391 out <<
"\n### Printing with max_levels=2 ###" << std::endl;
395 timer->report(out, comm, options);
425 const int myRank = Teuchos::rank(*comm);
429 out <<
"\n### Printing default report ###" << std::endl;
433 timer.
report(out, comm, options);
435 std::ostringstream os;
436 timer.
report(os, comm, options);
438 TEST_ASSERT(os.str().find(
"L2a") == std::string::npos);
439 TEST_ASSERT(os.str().find(
"L2b") != std::string::npos);
440 TEST_ASSERT(os.str().find(
"L1b") == std::string::npos);
444 out <<
"\n### Printing aligned_column with timers names on left ###" << std::endl;
446 timer.
report(out, comm, options);
448 std::ostringstream os;
449 timer.
report(os, comm, options);
451 TEST_ASSERT(os.str().find(
"L2a") == std::string::npos);
452 TEST_ASSERT(os.str().find(
"L2b") != std::string::npos);
453 TEST_ASSERT(os.str().find(
"L1b") == std::string::npos);
457 out <<
"\n### Printing aligned_column with timers names on right ###" << std::endl;
460 timer.
report(out, comm, options);
462 std::ostringstream os;
463 timer.
report(os, comm, options);
465 TEST_ASSERT(os.str().find(
"L2a") == std::string::npos);
466 TEST_ASSERT(os.str().find(
"L2b") != std::string::npos);
467 TEST_ASSERT(os.str().find(
"L1b") == std::string::npos);
479 if (comm->getSize() < 2)
481 const int myRank = Teuchos::rank(*comm);
485 else if (myRank == 1)
492 out <<
"\n### Printing default report ###" << std::endl;
498 timer.
report(out, comm, options);
500 std::ostringstream os;
501 timer.
report(os, comm, options);
503 TEST_ASSERT(os.str().find(
"proc min=0") != std::string::npos);
504 TEST_ASSERT(os.str().find(
"proc max=1") != std::string::npos);
527 timer.
start(
"Outer");
528 timer.
start(
"Inner");
537 #ifdef HAVE_TEUCHOS_ADD_TIME_MONITOR_TO_STACKED_TIMER
555 int main(
int argc,
char* argv[] )
560 #if defined(HAVE_TEUCHOS_KOKKOS_PROFILING) && defined(HAVE_TEUCHOSCORE_KOKKOS)
561 Kokkos::initialize(argc,argv);
570 #if defined(HAVE_TEUCHOS_KOKKOS_PROFILING) && defined(HAVE_TEUCHOSCORE_KOKKOS)
571 if (Kokkos::is_initialized())
579 #if !defined(__GNUC__) || ( defined(__GNUC__) && (__GNUC__ > 4) )
586 std::ostringstream os;
597 out << os.str() << std::endl;
603 TEST_ASSERT(os.str().find(
"L1") != std::string::npos);
604 TEST_ASSERT(os.str().find(
"L2") != std::string::npos);
605 TEST_ASSERT(os.str().find(
"L3") == std::string::npos);
614 timer.
start(
"Total Time");
616 for (
int i=0; i < 10; ++i) {
618 timer.
start(
"Assembly");
619 timer.
stop(
"Assembly");
625 timer.
start(
"Solve");
638 timer.
start(
"Restarted");
639 timer.
stop(
"Restarted");
642 timer.
stop(
"Total Time");
void setVerboseOstream(const Teuchos::RCP< std::ostream > &os)
Set the ostream for verbose mode(defaults to std::cout).
static Teuchos::RCP< Teuchos::StackedTimer > getStackedTimer()
The StackedTimer used by the TimeMonitor.
#define TEST_ASSERT(v1)
Assert the given statement is true.
void stop(const std::string &name, const bool pop_kokkos_profiling_region=true)
void enableVerboseTimestamps(const unsigned levels)
Enable timestamps in verbose mode for the number of levels specified.
void enableVerbose(const bool enable_verbose)
If set to true, print timer start/stop to verbose ostream.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#define TEST_THROW(code, ExceptType)
Assert that the statement 'code' throws the exception 'ExceptType' (otherwise the test fails)...
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
static Teuchos::RCP< const Comm< OrdinalType > > getComm()
Return the default global communicator.
Initialize, finalize, and query the global MPI session.
bool is_null(const ArrayRCP< T > &p)
Returns true if p.get()==NULL.
static RCP< Time > getNewTimer(const std::string &name)
Return a new timer with the given name (class method).
static int runUnitTestsFromMain(int argc, char *argv[])
Run the unit tests from main() passing in (argc, argv).
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
void start(const std::string name, const bool push_kokkos_profiling_region=true)
std::ostream subclass that performs the magic of indenting data sent to an std::ostream object among ...
The basic timer used internally, uses std::chrono::high_resolution_clock.
void reportXML(std::ostream &os, const std::string &datestamp, const std::string ×tamp, Teuchos::RCP< const Teuchos::Comm< int > > comm)
static void setStackedTimer(const Teuchos::RCP< Teuchos::StackedTimer > &t)
Sets the StackedTimer into which the TimeMonitor will insert timings.
bool print_names_before_values
static void setGloballyReduceTestResult(const bool globallyReduceUnitTestResult)
Set if the unit tests should reduce pass/fail across processes.
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
Set the stream to print only on the (MPI) process with the given rank.
int size(const Comm< Ordinal > &comm)
Get the number of processes in the communicator.
const BaseTimer * findBaseTimer(const std::string &name) const
void unsortedMergePair(const Array< std::string > &localNames, Array< std::string > &globalNames, const ECounterSetOp setOp)
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
Assert the relative floating-point equality of rel_error(v1,v2) <= tol.
int main(int argc, char *argv[])
void push_back(const value_type &x)
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
void report(std::ostream &os)
Scope guard for Teuchos::Time, with MPI collective timer reporting.
Smart reference counting pointer class for automatic garbage collection.
BaseTimer::TimeInfo findTimer(const std::string &name)
This class allows one to push and pop timers on and off a stack.
bool output_total_updates
Scope guard for Time, that can compute MPI collective timer statistics.
bool nonnull(const ArrayRCP< T > &p)
Returns true if p.get()!=NULL.