18 #if defined(HAVE_TEUCHOS_KOKKOS_PROFILING) && defined(HAVE_TEUCHOSCORE_KOKKOSCORE)
19 #include "Kokkos_Core.hpp"
73 const int myRank = Teuchos::rank(*comm);
78 timer.
start(
"Total Time");
80 for (
int i=0; i < 10; ++i) {
82 timer.
start(
"Assembly");
83 std::this_thread::sleep_for(std::chrono::milliseconds{100});
84 timer.
stop(
"Assembly");
89 std::this_thread::sleep_for(std::chrono::milliseconds{50});
94 const std::string label =
"Rank 0 ONLY";
96 std::this_thread::sleep_for(std::chrono::milliseconds{50});
101 timer.
start(
"Not Rank 0");
102 std::this_thread::sleep_for(std::chrono::milliseconds{50});
104 timer.
stop(
"Not Rank 0");
112 timer.
stop(
"Total Time");
137 std::stringstream sout1;
138 timer.
report(sout1, comm, options);
142 std::stringstream sout2;
143 timer.
report(sout2, comm, options);
147 std::vector<std::tuple<std::string,double,unsigned long>> lineChecks;
148 lineChecks.push_back(std::make_tuple(
"My New Timer:",2.0,1));
149 lineChecks.push_back(std::make_tuple(
"Total Time:",2.0,1));
150 lineChecks.push_back(std::make_tuple(
"Assembly:",1.0,10));
151 lineChecks.push_back(std::make_tuple(
"Solve:",1.0,10));
152 lineChecks.push_back(std::make_tuple(
"Prec:",0.5,10));
161 #if !defined(__GNUC__) \
162 || ( defined(__GNUC__) && (__GNUC__ > 4) ) \
163 || ( defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC__MINOR__ > 8) )
166 const double timerTolerance = 0.25;
167 std::istringstream is(sout1.str());
168 for (
const auto& check : lineChecks) {
171 std::getline(is,line);
172 std::smatch regexSMatch;
173 std::regex timerName(std::get<0>(check));
174 std::regex_search(line,regexSMatch,timerName);
178 std::regex delimiter(
":\\s|\\s\\[|\\]\\s");
179 std::sregex_token_iterator tok(line.begin(), line.end(),delimiter,-1);
181 const std::string timeAsString = (++tok)->str();
182 const double time = std::stod(timeAsString);
185 const std::string countAsString = (++tok)->str();
186 const unsigned long count = std::stoul(countAsString);
193 out <<
"\n### Printing default report ###" << std::endl;
195 timer.
report(out, comm, defaultOptions);
198 out <<
"\n### Printing aligned_column with timers names on left ###" << std::endl;
205 timer.
report(out, comm, options);
209 out <<
"\n### Printing aligned_column with timers names on right ###" << std::endl;
211 timer.
report(out, comm, options);
217 const int myRank = Teuchos::rank(*comm);
221 timer->startBaseTimer();
222 for (
int i=0; i < 10; ++i) {
223 timer-> start(
"Subtask");
224 timer->incrementUpdates();
225 timer->incrementUpdates(2);
226 timer-> stop(
"Subtask");
228 timer->stopBaseTimer();
233 auto sub_timer =
const_cast<Teuchos::BaseTimer*
>(timer->findBaseTimer(
"Total Time@Subtask"));
238 TEST_THROW(timer->findBaseTimer(
"Testing misspelled timer name!"),std::runtime_error);
252 top_timer->setAccumulatedTime(5000.0);
253 top_timer->overrideNumCallsForUnitTesting(2);
254 top_timer->overrideNumUpdatesForUnitTesting(3);
255 sub_timer->setAccumulatedTime(4000.0);
256 sub_timer->overrideNumCallsForUnitTesting(4);
257 sub_timer->overrideNumUpdatesForUnitTesting(5);
259 const double timerTolerance = 100.0 * std::numeric_limits<double>::epsilon();
286 TEST_ASSERT(timeMonitorDefaultStackedTimer != timer);
290 timer->start(
"Total Time");
292 for (
int i=0; i < 10; ++i) {
294 timer->start(
"Assembly");
298 std::this_thread::sleep_for(std::chrono::milliseconds{25});
302 std::this_thread::sleep_for(std::chrono::milliseconds{75});
305 std::this_thread::sleep_for(std::chrono::milliseconds{100});
307 timer->stop(
"Assembly");
308 timer->start(
"Solve");
312 std::this_thread::sleep_for(std::chrono::milliseconds{50});
316 std::this_thread::sleep_for(std::chrono::milliseconds{50});
319 std::this_thread::sleep_for(std::chrono::milliseconds{100});
321 timer->stop(
"Solve");
322 std::this_thread::sleep_for(std::chrono::milliseconds{100});
325 timer->stop(
"Total Time");
326 timer->stopBaseTimer();
328 assert(
size(*comm)>0);
330 TEST_EQUALITY((timer->findTimer(
"TM:Interoperability@Total Time")).count, 1);
331 TEST_EQUALITY((timer->findTimer(
"TM:Interoperability@Total Time@Assembly")).count, 10);
334 #ifdef HAVE_TEUCHOS_ADD_TIME_MONITOR_TO_STACKED_TIMER
335 TEST_EQUALITY((timer->findTimer(
"TM:Interoperability@Total Time@Solve@Prec")).count, 10);
336 TEST_EQUALITY((timer->findTimer(
"TM:Interoperability@Total Time@Solve@GMRES")).count, 10);
340 out <<
"\n### Printing default report ###" << std::endl;
344 timer->report(out, comm, options);
346 out <<
"\n### Printing aligned_column with timers names on left ###" << std::endl;
348 timer->report(out, comm, options);
350 out <<
"\n### Printing aligned_column with timers names on right ###" << std::endl;
357 timer->report(out, comm, options);
360 out <<
"\n### Printing with max_levels=2 ###" << std::endl;
364 timer->report(out, comm, options);
384 timer.
start(
"Outer");
385 timer.
start(
"Inner");
394 #ifdef HAVE_TEUCHOS_ADD_TIME_MONITOR_TO_STACKED_TIMER
412 int main(
int argc,
char* argv[] )
417 #if defined(HAVE_TEUCHOS_KOKKOS_PROFILING) && defined(HAVE_TEUCHOSCORE_KOKKOSCORE)
418 Kokkos::initialize(argc,argv);
427 #if defined(HAVE_TEUCHOS_KOKKOS_PROFILING) && defined(HAVE_TEUCHOSCORE_KOKKOSCORE)
428 if (Kokkos::is_initialized())
429 Kokkos::finalize_all();
void setVerboseOstream(const Teuchos::RCP< std::ostream > &os)
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 enableVerbose(const bool enable_verbose)
#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 elsewhere, uses MPI_Wtime for time
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.
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
Struct for controlling output options like histograms.
Scope guard for Time, that can compute MPI collective timer statistics.
bool nonnull(const ArrayRCP< T > &p)
Returns true if p.get()!=NULL.