Debian Codegraph @

packages/stk/stk_unit_tests/stk_util/diag/UnitTestTimer.cpp

raw
1 // Copyright 2002 - 2008, 2010, 2011 National Technology Engineering
2 // Solutions of Sandia, LLC (NTESS). Under the terms of Contract
3 // DE-NA0003525 with NTESS, the U.S. Government retains certain rights
4 // in this software.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 //
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following
15 // disclaimer in the documentation and/or other materials provided
16 // with the distribution.
17 //
18 // * Neither the name of NTESS nor the names of its contributors
19 // may be used to endorse or promote products derived from this
20 // software without specific prior written permission.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 //
34
35 #include "gtest/gtest.h"
36 #include "stk_unit_test_utils/getOption.h" // for get_command_line_option
37 #include "stk_util/diag/PrintTimer.hpp" // for printTimersTable
38 #include "stk_util/diag/Timer.hpp" // for Timer, TimeBlock, Timer::Metric, createRo...
39 #include "stk_util/diag/TimerMetricTraits.hpp" // for LapCount (ptr only), MetricTraits, Metric...
40 #include "stk_util/parallel/Parallel.hpp" // for MPI_COMM_WORLD
41 #include <cstddef> // for size_t
42 #include <chrono> // for milliseconds
43 #include <cmath> // for sin
44 #include <iostream> // for ostringstream, operator<<, basic_ostream
45 #include <memory> // for allocator_traits<>::value_type
46 #include <string> // for string, char_traits
47 #include <thread> // for sleep_for
48 #include <vector> // for vector
49
50 using stk::unit_test_util::get_command_line_option;
51
52 enum {
53 TIMER_DOMAIN = 0x00001000, ///< Enable domain timers
54 TIMER_REGION = 0x00002000, ///< Enable region timers
55 TIMER_PROCEDURE = 0x00004000, ///< Enable procedure timers
56 TIMER_MECHANICS = 0x00008000, ///< Enable mechanics timers
57 TIMER_ALGORITHM = 0x00010000, ///< Enable algorithm timers
58 TIMER_SOLVER = 0x00020000, ///< Enable solver timers
59 TIMER_CONTACT = 0x00040000, ///< Enable contact timers
60 TIMER_MATERIAL = 0x00080000, ///< Enable material timers
61 TIMER_SEARCH = 0x00100000, ///< Enable search timers
62 TIMER_TRANSFER = 0x00200000, ///< Enable transfer timers
63 TIMER_ADAPTIVITY = 0x00400000 ///< Enable adaptivity
64 };
65
66 enum {
67 TIMER_UNUSED_1 = 0x00001000, ///< Enable unused 1
68 TIMER_PROFILE_1 = 0x00002000, ///< Enable profile 1 timers
69 TIMER_PROFILE_2 = 0x00004000, ///< Enable profile 2 timers
70 TIMER_PROFILE_3 = 0x00008000, ///< Enable profile 3 timers
71 TIMER_PROFILE_4 = 0x00010000, ///< Enable profile 4 timers
72 TIMER_APP_1 = 0x00020000, ///< Enable application defined 1
73 TIMER_APP_2 = 0x00040000, ///< Enable application defined 2
74 TIMER_APP_3 = 0x00080000 ///< Enable application defined 3
75 };
76
77 namespace {
78
79 double
80 work(int repetitions = 1000)
81 {
82 double x = 1.0;
83
84 for (int i = 0; i < repetitions; ++i) {
85 double di = i;
86 x += std::sin(di);
87 }
88 return x;
89 }
90
91
92 stk::diag::TimerSet &
93 unitTestTimerSet()
94 {
95 static stk::diag::TimerSet s_unitTestTimerSet(TIMER_REGION);
96
97 return s_unitTestTimerSet;
98 }
99
100
101 stk::diag::TimerSet &
102 unitTestSecondTimerSet()
103 {
104 static stk::diag::TimerSet s_unitTestSecondTimerSet(TIMER_APP_3);
105
106 return s_unitTestSecondTimerSet;
107 }
108
109
110 stk::diag::Timer &unitTestTimer() {
111 const std::string name("Unit test timer");
112 static stk::diag::Timer s_unitTestTimer (stk::diag::createRootTimer(name, unitTestTimerSet()));
113
114 return s_unitTestTimer;
115 }
116
117
118 struct RootObject
119 {
120 RootObject()
121 : m_timer("Root object", TIMER_REGION, unitTestTimer())
122 {}
123
124 stk::diag::Timer m_timer;
125 };
126
127
128 struct Object
129 {
130 Object(const std::string &name, RootObject &root_object)
131 : m_id(0),
132 m_name(name),
133 m_timer(name, root_object.m_timer)
134 {}
135
136 Object(int id, const Object &parent)
137 : m_id(id),
138 m_name(id_name(id)),
139 m_timer(m_name, parent.m_timer)
140 {}
141
142 static std::string id_name(int id) {
143 std::ostringstream s;
144 s << "Object id " << id << " run";
145 return s.str();
146 }
147
148 void run() {
149 stk::diag::TimeBlock _time(m_timer);
150 m_x += work();
151 }
152
153 int m_id;
154 std::string m_name;
155 stk::diag::Timer m_timer;
156 double m_x;
157 };
158
159 } // namespace <empty>
160
161 constexpr double millisecTolerance = 1.e-2; //10 millis for robustness
162
163 TEST(UnitTestTimer, UnitTest)
164 {
165 stk::diag::TimeBlock root_time_block(unitTestTimer());
166
167 std::ostringstream strout;
168
169 // Create subtimer and test lap time
170 {
171 static stk::diag::Timer lap_timer("One second Wall time twice", unitTestTimer());
172
173 stk::diag::TimeBlock _time(lap_timer);
174 double x = work(100);
175 static_cast<void>(x);
176 std::ostringstream oss;
177 oss << x << std::endl;
178
179 std::this_thread::sleep_for(std::chrono::milliseconds(20));
180
181 lap_timer.lap();
182
183 stk::diag::MetricTraits<stk::diag::WallTime>::Type lap_time = lap_timer.getMetric<stk::diag::WallTime>().getLap();
184
185 EXPECT_GE(lap_time, (0.02 - millisecTolerance));
186
187 std::this_thread::sleep_for(std::chrono::milliseconds(20));
188
189 lap_timer.stop();
190
191 lap_time = lap_timer.getMetric<stk::diag::WallTime>().getLap();
192
193 EXPECT_GE(lap_time, (0.04 - millisecTolerance));
194 }
195
196 {
197 static stk::diag::Timer run_timer("Run 100 times twice", unitTestTimer());
198
199 for (int i = 0; i < 100; ++i) {
200 stk::diag::TimeBlock _time(run_timer);
201 work();
202 }
203
204 stk::diag::MetricTraits<stk::diag::LapCount>::Type lap_count = run_timer.getMetric<stk::diag::LapCount>().getAccumulatedLap(false);
205
206 ASSERT_TRUE(lap_count == 100);
207 }
208
209 // Create second timer set
210 {
211 static stk::diag::Timer second_timer("Second timer set", unitTestTimer(), unitTestSecondTimerSet());
212 static stk::diag::Timer second_timer_on_default("On default", second_timer);
213 static stk::diag::Timer second_timer_on("On", TIMER_APP_3, second_timer);
214 static stk::diag::Timer second_timer_off("Off", TIMER_APP_1, second_timer);
215
216 stk::diag::TimeBlock _time(second_timer);
217 stk::diag::TimeBlock _time1(second_timer_on_default);
218 stk::diag::TimeBlock _time2(second_timer_on);
219 stk::diag::TimeBlock _time3(second_timer_off);
220
221 std::this_thread::sleep_for(std::chrono::milliseconds(10));
222 }
223
224 // Grab previous subtimer and run 100 laps
225 {
226 static stk::diag::Timer run_timer("Run 100 times twice", unitTestTimer());
227
228 for (int i = 0; i < 100; ++i) {
229 stk::diag::TimeBlock _time(run_timer);
230 work();
231 }
232
233 stk::diag::MetricTraits<stk::diag::LapCount>::Type lap_count = run_timer.getMetric<stk::diag::LapCount>().getAccumulatedLap(false);
234
235 ASSERT_TRUE(lap_count == 200);
236 }
237
238 // Create root object
239 RootObject root_object;
240
241 {
242 stk::diag::MetricTraits<stk::diag::LapCount>::Type lap_count = root_object.m_timer.getMetric<stk::diag::LapCount>().getAccumulatedLap(false);
243
244 ASSERT_TRUE(lap_count == 0);
245 }
246
247 // Create object
248 {
249 Object time_object("One object", root_object);
250
251 for (int i = 0; i < 100; ++i) {
252 time_object.run();
253 }
254
255 stk::diag::MetricTraits<stk::diag::LapCount>::Type lap_count = time_object.m_timer.getMetric<stk::diag::LapCount>().getAccumulatedLap(false);
256
257 ASSERT_TRUE(lap_count == 100);
258 }
259
260 // Create object tree
261 {
262 std::vector<Object> object_vector;
263 object_vector.push_back(Object("Object Tree", root_object));
264
265 int id = 0;
266 for (size_t i = 0; i < 2; ++i) {
267 size_t ix = object_vector.size();
268 object_vector.push_back(Object(id++, object_vector[0]));
269 for (size_t j = 0; j < 2; ++j) {
270 size_t jx = object_vector.size();
271 object_vector.push_back(Object(id++, object_vector[ix]));
272 for (int k = 0; k < 2; ++k) {
273 object_vector.push_back(Object(id++, object_vector[jx]));
274 }
275 }
276 }
277
278 stk::diag::printTimersTable(strout, unitTestTimer(), stk::diag::METRICS_ALL, false);
279
280 stk::diag::MetricTraits<stk::diag::LapCount>::Type lap_count = 0;
281 for (size_t j = 0; j < object_vector.size(); ++j)
282 lap_count += object_vector[j].m_timer.getMetric<stk::diag::LapCount>().getAccumulatedLap(false);
283
284 EXPECT_EQ(lap_count, stk::diag::MetricTraits<stk::diag::LapCount>::Type(0));
285
286 for (size_t j = 0; j < object_vector.size(); ++j)
287 object_vector[j].run();
288
289 stk::diag::printTimersTable(strout, unitTestTimer(), stk::diag::METRICS_ALL, false);
290
291 //root node has 15, children node 7, grandchildren 3, greatgrandchildren 1
292 // 15 + 2*7 + 4*3 + 8*1 = 49 for laptimes that are cumulative
293 EXPECT_EQ(15u, object_vector[0].m_timer.getMetric<stk::diag::LapCount>().getAccumulatedLap());
294 lap_count = 0;
295 for (size_t j = 0; j < object_vector.size(); ++j)
296 lap_count += object_vector[j].m_timer.getMetric<stk::diag::LapCount>().getAccumulatedLap(false);
297
298 EXPECT_EQ(lap_count, stk::diag::MetricTraits<stk::diag::LapCount>::Type(49));
299
300 for (size_t i = 1; i < 100; ++i)
301 for (size_t j = 0; j < object_vector.size(); ++j)
302 object_vector[j].run();
303
304 stk::diag::printTimersTable(strout, unitTestTimer(), stk::diag::METRICS_ALL, false);
305
306 lap_count = 0;
307 for (size_t j = 0; j < object_vector.size(); ++j)
308 lap_count += object_vector[j].m_timer.getMetric<stk::diag::LapCount>().getAccumulatedLap(false);
309
310 EXPECT_EQ(lap_count, stk::diag::MetricTraits<stk::diag::LapCount>::Type(4900));
311
312 stk::diag::printTimersTable(strout, unitTestTimer(), stk::diag::METRICS_ALL, true);
313
314 for (size_t i = 1; i < 100; ++i)
315 for (size_t j = 0; j < object_vector.size(); ++j)
316 object_vector[j].run();
317
318 stk::diag::printTimersTable(strout, unitTestTimer(), stk::diag::METRICS_ALL, true);
319 }
320 }
321
322 TEST(UnitTestTimer, YuugeNumberOfTimers)
323 {
324 std::ostringstream strout;
325
326 stk::diag::TimerSet unitTestSecondTimerSet(TIMER_APP_3);
327 const std::string name("Unit Test Timer");
328 stk::diag::Timer rootTimer (stk::diag::createRootTimer(name, unitTestSecondTimerSet));
329 {
330 stk::diag::TimeBlock root_time_block(rootTimer);
331
332 unsigned numTimers = 100;
333 numTimers = stk::unit_test_util::get_command_line_option("-numTimers", numTimers);
334 {
335 static std::vector<stk::diag::Timer> lap_timers;
336 for (unsigned i = 0; i < numTimers; ++i)
337 {
338 std::ostringstream os;
339 os << "Really Great Timer For Index with lots of space in between the name and index" << i;
340 lap_timers.emplace_back(os.str(), rootTimer);
341 stk::diag::TimeBlock _time(lap_timers[i]);
342 // work(1);
343 }
344 for (unsigned i = 0; i < numTimers; ++i)
345 {
346 stk::diag::MetricTraits<stk::diag::LapCount>::Type lap_count = lap_timers[i].getMetric<stk::diag::LapCount>().getAccumulatedLap(false);
347 EXPECT_EQ(1u, lap_count);
348 }
349 }
350
351 stk::diag::printTimersTable(strout, rootTimer, stk::diag::METRICS_ALL, false, MPI_COMM_WORLD);
352 }
353 stk::diag::deleteRootTimer(rootTimer);
354 }
355
356 TEST(UnitTestTimer, lapCount_2start_1stop)
357 {
358 stk::diag::TimerSet timerSet(TIMER_APP_3);
359 stk::diag::Timer rootTimer(stk::diag::createRootTimer("Root Timer", timerSet));
360 rootTimer.start();
361 rootTimer.start();
362 rootTimer.stop();
363 unsigned expectedLapCount = 1;
364 EXPECT_EQ(expectedLapCount, rootTimer.getMetric<stk::diag::LapCount>().getAccumulatedLap());
365 stk::diag::deleteRootTimer(rootTimer);
366 }
367
368 TEST(UnitTestTimer, MultipleStarts)
369 {
370 stk::diag::TimerSet timerSet(TIMER_APP_3);
371 stk::diag::Timer rootTimer(stk::diag::createRootTimer("Root Timer", timerSet));
372 {
373 stk::diag::TimeBlock root_time_block(rootTimer);
374 stk::diag::Timer childTimer("Child Timer", rootTimer);
375
376 std::ostringstream strout;
377
378 {
379 stk::diag::TimeBlock childBlock(childTimer);
380 std::this_thread::sleep_for(std::chrono::milliseconds(100));
381 }
382 {
383 childTimer.start();
384 childTimer.start();
385 std::this_thread::sleep_for(std::chrono::milliseconds(100));
386 childTimer.stop();
387 childTimer.stop();
388 }
389 std::this_thread::sleep_for(std::chrono::milliseconds(50));
390 double expectedTime = 0.2;
391 double measuredTime = childTimer.getMetric<stk::diag::WallTime>().getAccumulatedLap();
392 double relativeError = (std::abs(expectedTime - measuredTime)/expectedTime);
393 double relativeTolerance = 0.1;
394 EXPECT_TRUE(relativeError < relativeTolerance);
395 EXPECT_EQ(childTimer.getMetric<stk::diag::LapCount>().getAccumulatedLap(), 2u);
396 stk::diag::printTimersTable(strout, rootTimer, stk::diag::METRICS_ALL, false, MPI_COMM_WORLD);
397 }
398 stk::diag::deleteRootTimer(rootTimer);
399 }
400
401 TEST(UnitTestTimer, NestedTimers)
402 {
403 stk::diag::TimerSet timerSet(TIMER_APP_3);
404 stk::diag::Timer rootTimer(stk::diag::createRootTimer("Root Timer", timerSet));
405 {
406 stk::diag::TimeBlock root_time_block(rootTimer);
407 stk::diag::Timer childTimer("Child Timer", rootTimer);
408 stk::diag::Timer grandChildTimer(" Grandchild Timer", childTimer);
409
410 std::ostringstream strout;
411
412 {
413 stk::diag::TimeBlock childBlock(childTimer);
414 stk::diag::TimeBlock grandChildBlock(grandChildTimer);
415 std::this_thread::sleep_for(std::chrono::milliseconds(50));
416 }
417 {
418 stk::diag::TimeBlock lap_timer(grandChildTimer);
419 std::this_thread::sleep_for(std::chrono::milliseconds(50));
420 }
421 EXPECT_GE(childTimer.getMetric<stk::diag::WallTime>().getAccumulatedLap(), (0.1 - millisecTolerance));
422 EXPECT_EQ(childTimer.getMetric<stk::diag::LapCount>().getAccumulatedLap(), 2u);
423 stk::diag::printTimersTable(strout, rootTimer, stk::diag::METRICS_ALL, false, MPI_COMM_WORLD);
424 }
425 stk::diag::deleteRootTimer(rootTimer);
426 }
427
428 TEST(UnitTestTimer, ReferenceCounting)
429 {
430 stk::diag::TimerSet timerSet(TIMER_APP_3);
431 stk::diag::Timer rootTimer(stk::diag::createRootTimer("Root Timer", timerSet));
432 {
433 stk::diag::TimeBlock root_time_block(rootTimer);
434 stk::diag::Timer childTimer("Child Timer", rootTimer);
435 stk::diag::Timer grandChildTimer("Grandchild Timer", childTimer);
436 stk::diag::Timer grandChildTimer2("Grandchild Timer 2", childTimer);
437
438 std::ostringstream strout;
439
440 {
441 stk::diag::TimeBlock grandChildBlock(grandChildTimer);
442 stk::diag::TimeBlock grandChildBlock2(grandChildTimer);
443 std::this_thread::sleep_for(std::chrono::milliseconds(10));
444 }
445
446 EXPECT_GE(childTimer.getMetric<stk::diag::WallTime>().getAccumulatedLap(), 0.01);
447 EXPECT_EQ(childTimer.getMetric<stk::diag::LapCount>().getAccumulatedLap(), 1u);
448
449 {
450 stk::diag::TimeBlock grandChildBlock(grandChildTimer);
451 {
452 stk::diag::TimeBlock grandChildBlock2(grandChildTimer2);
453 std::this_thread::sleep_for(std::chrono::milliseconds(10));
454 }
455 std::this_thread::sleep_for(std::chrono::milliseconds(10));
456 }
457 EXPECT_GE(childTimer.getMetric<stk::diag::WallTime>().getAccumulatedLap(), 0.03);
458 EXPECT_EQ(childTimer.getMetric<stk::diag::LapCount>().getAccumulatedLap(), 2u);
459
460 {
461 stk::diag::TimeBlock childBlock(childTimer);
462 {
463 stk::diag::TimeBlock grandChildBlock(grandChildTimer);
464 {
465 stk::diag::TimeBlock grandChildBlock2(grandChildTimer2);
466 std::this_thread::sleep_for(std::chrono::milliseconds(10));
467 }
468 std::this_thread::sleep_for(std::chrono::milliseconds(10));
469 }
470 std::this_thread::sleep_for(std::chrono::milliseconds(10));
471 }
472 EXPECT_GE(childTimer.getMetric<stk::diag::WallTime>().getAccumulatedLap(), 0.06);
473 EXPECT_EQ(childTimer.getMetric<stk::diag::LapCount>().getAccumulatedLap(), 3u);
474
475 stk::diag::printTimersTable(strout, rootTimer, stk::diag::METRICS_ALL, false, MPI_COMM_WORLD);
476 }
477 stk::diag::deleteRootTimer(rootTimer);
478 }