-
Notifications
You must be signed in to change notification settings - Fork 19
17 Timing framework #1179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
17 Timing framework #1179
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
3a1f318
[wip] timing framework?
reneSchm 964bf5d
[wip] testing runtime map
reneSchm 0ee71ff
refactor timer structure
reneSchm b20a85a
fix includes, add functions to get max threads and thread IDs
reneSchm e5af5eb
move timing code into its own directory
reneSchm 3ca277c
rename example file
reneSchm e29d8f6
add Tag to registered timer name
reneSchm 6ad898c
add Printer interface,
reneSchm 004dedc
Merge branch '17-time-measurement' of github.com:SciCompMod/memilio i…
reneSchm 7d7ee9c
[wip] documentation and renames
reneSchm 617f983
Merge branch 'main' into 17-time-measurement
reneSchm f607351
upgrade to c++20
reneSchm a0574b8
Introduce StringLiteral, replacing the CONST_LITERAL makro.
reneSchm e7ed6af
add a test for BasicTimer and AutoTimer
reneSchm 4767fb1
Fix TimeSeriesIteratorBase iterator requirements
reneSchm 772b7b0
change comparison operators to use Derived type only, to avoid ambiguity
reneSchm 82bc9eb
mark a template index assignment constexpr to avoid a unused variable…
reneSchm 376085c
fix typing of TimeSeriesIteratorBase::pointer::operator->
reneSchm 5253889
fix constness in equality operators
reneSchm 580cded
try to work around some compiler warnings
reneSchm 857e932
Merge branch '17-time-measurement' of github.com:SciCompMod/memilio i…
reneSchm f377b5e
more compiler warning workarounds
reneSchm 71a627a
bump gtest version fixing msvc bug
reneSchm 09fed9a
move named- and autotimer into their own headers.
reneSchm 1a82580
move some definitions into cpp files
reneSchm 76f1fe5
make timer test CI resilient
reneSchm 85b089f
fix reset function
reneSchm 557b77e
add more tests
reneSchm 7d8eaa4
review suggestions
reneSchm a20409d
fully cover table_printer
reneSchm 43d6f84
include function name in error message, and split some code into a .cpp
reneSchm 522a65b
add a class to redirect the spdlog output
reneSchm 5514287
test BasicTimer error messages
reneSchm 39f2929
add documentation
reneSchm e55a81d
[ci skip] add a small readme
reneSchm 331ca9e
[ci skip] delete todo
reneSchm 2085609
[ci skip] fix typos
reneSchm 20edabc
[ci skip] update c++ version in coding guidelines
reneSchm 9d0ff65
update c++ version in ast.py
reneSchm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* | ||
| * Copyright (C) 2020-2025 MEmilio | ||
| * | ||
| * Authors: Rene Schmieding | ||
| * | ||
| * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de> | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| #include "memilio/timer/auto_timer.h" | ||
| #include "memilio/timer/table_printer.h" | ||
|
|
||
| #include <thread> // This is only used for the example load function. | ||
|
|
||
| /// @brief Workload for the timers to report on. | ||
| void load() | ||
| { | ||
| // Two very simple work loads to test the timers. Choose one, an comment out the other. | ||
|
|
||
| // Option a: increment a counter. Extremely cheap load, so for large N, the timing overhead will matter. | ||
| // Uses volatile in an attempt to avoid compiler optimisations. | ||
|
|
||
| // volatile static int ctr = 0; | ||
| // ++ctr; | ||
|
|
||
| // Option b: let the current thread briefly wait. The timing overhead should be negligible. | ||
|
|
||
| std::this_thread::sleep_for(std::chrono::milliseconds(3)); | ||
| } | ||
|
|
||
| int main() | ||
| { | ||
| // Specify the namespace of AutoTimer, so we don't have to repeat it. Do not do this with entire namespaces. | ||
| using mio::timing::AutoTimer; | ||
|
|
||
| // Start immediately timing the main function. An AutoTimer starts timing upon its creation, and ends timing when | ||
| // it is destroyed. This usually happens when a function returns, or a scope indicated by {curly braces} ends. | ||
| // The name of the AutoTimer object (here timer_ms) does not matter, but the name given to it as a template | ||
| // (i.e. "main") does, as it is used to identify the NamedTimer that is used internally, it works like a map key. | ||
| AutoTimer<"main"> timer_ms; | ||
|
|
||
| // Set a Printer to output the timers. | ||
| // These only have to be set if you want to customize the output. By default, TimerRegistrar prints all timers at | ||
| // the end of the programm. This can be disabled by calling `TimerRegistrar::disable_final_timer_summary()`. | ||
| auto printer = std::make_unique<mio::timing::TablePrinter>(); | ||
| printer->set_time_format("{:e}"); | ||
| mio::timing::TimerRegistrar::get_instance().set_printer(std::move(printer)); | ||
|
|
||
| // To manually print all timers, use `TimerRegistrar::print_timers()`, but make sure that no timers are running. | ||
| // The "main" timer in this example would make that difficult, but you can simply add another scope around it, | ||
| // similar to the "compute loops" timer below. | ||
|
|
||
| const int N = 1000; // Number of iterations. Be wary of increasing this number when using the sleep_for load! | ||
|
|
||
| std::cout << "Num threads: " << mio::omp::get_max_threads() << "\n"; | ||
|
|
||
| // Open a new scope to time computations. | ||
| { | ||
| // This is another a good example of how to use AutoTimer, now inside an unnamed scope. | ||
| AutoTimer<"compute loops"> timer_cs; | ||
|
|
||
| // First compute loop. | ||
| { | ||
| // With the second string we define the scope of the timer. Here it is kind of misused, the scope should | ||
| // usually name the surrounding class, struct, or function. This has two reasons: First, it makes name | ||
| // collisions less likely. Second, the scope can be used to analyze the results. Hence, "first loop" would | ||
| // be way too granular, the appropriate scope in this case would be "main", if any. | ||
| AutoTimer<"compute", "first loop"> timer_out; | ||
|
|
||
| PRAGMA_OMP(parallel for) | ||
| for (int i = 0; i < N; i++) { | ||
| load(); | ||
| } | ||
| } | ||
| // Second compute loop, with an additional timer during computation. While the timing overhead is minimal, it is | ||
| // still measurable, so it should be generally avoided to time inside main compute loops. | ||
| { | ||
| // Again, we use a not quite optimal name for the scope, but it is shared with the inner timer. In this | ||
| // example, it would be enough to include "second loop" in the name. The scope should be used for | ||
| // differentiating between, e.g., different models or simulations. | ||
| AutoTimer<"compute (with inner timer)", "second loop"> timer_out; | ||
|
|
||
| PRAGMA_OMP(parallel for) | ||
| for (int i = 0; i < N; i++) { | ||
| // Note that when using multithreading, this timer will measure a longer (aggregated) time than its | ||
| // outer timer. | ||
| AutoTimer<"inner timer", "second loop"> timer_in; | ||
| load(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # MEmilio C++ timing framework | ||
|
|
||
| This directory contains MEmilio's performance timers. | ||
|
|
||
| To get started, check out the | ||
| [official documentation](https://memilio.readthedocs.io/en/latest/cpp/performance_timers.html), or the | ||
| [code example](../../examples/performance_timers.cpp). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * Copyright (C) 2020-2025 MEmilio | ||
| * | ||
| * Authors: Rene Schmieding | ||
| * | ||
| * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de> | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| #ifndef MIO_TIMER_AUTO_TIMER_H | ||
| #define MIO_TIMER_AUTO_TIMER_H | ||
|
|
||
| #include "memilio/timer/basic_timer.h" | ||
| #include "memilio/timer/named_timer.h" | ||
| #include "memilio/utils/string_literal.h" | ||
|
|
||
| namespace mio | ||
| { | ||
| namespace timing | ||
| { | ||
|
|
||
| /** | ||
| * @brief Timer that automatically starts when it is created, and stops when it is destroyed. | ||
| * @tparam Name, Scope The name and scope of a NamedTimer. Do not set these if you want to use a BasicTimer. | ||
| */ | ||
| template <StringLiteral Name, StringLiteral Scope = ""> | ||
| class AutoTimer | ||
| { | ||
| public: | ||
| /// @brief Run the NamedTimer given by the template parameter(s) Name (and Scope). | ||
| AutoTimer() | ||
| : m_timer(NamedTimer<Name, Scope>::get_instance()) | ||
| { | ||
| m_timer.start(); | ||
| } | ||
|
|
||
| /// @brief Run the given BasicTimer. Does not take ownership, so mind the timer's lifetime! | ||
| AutoTimer(BasicTimer& timer) | ||
| : m_timer(timer) | ||
| { | ||
| static_assert(Name.empty() && Scope.empty(), | ||
| "Do not set the Name and Scope templates when using this constructor."); | ||
| m_timer.start(); | ||
| } | ||
|
|
||
| AutoTimer(AutoTimer&) = delete; | ||
| AutoTimer(AutoTimer&&) = delete; | ||
|
|
||
| ~AutoTimer() | ||
| { | ||
| m_timer.stop(); | ||
| } | ||
|
|
||
| private: | ||
| BasicTimer& m_timer; ///< Reference to the timer so it can be stopped in AutoTimer's destructor. | ||
| }; | ||
|
|
||
| // Deduction guide that allows omitting the template parameter when using the BasicTimer constructor. | ||
reneSchm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| AutoTimer(BasicTimer& timer) -> AutoTimer<"">; | ||
|
|
||
| } // namespace timing | ||
| } // namespace mio | ||
|
|
||
| #endif // MIO_TIMER_AUTO_TIMER_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #include "memilio/timer/basic_timer.h" | ||
| #include "memilio/utils/logging.h" | ||
|
|
||
| namespace mio | ||
| { | ||
| namespace timing | ||
| { | ||
|
|
||
| void BasicTimer::set_running(bool new_state) | ||
| { | ||
| #ifndef NDEBUG | ||
| m_is_running = new_state; | ||
| #else | ||
| mio::unused(new_state); | ||
| #endif | ||
| } | ||
|
|
||
| void BasicTimer::should_be_running(bool expected, const std::string_view function) const | ||
| { | ||
| #ifndef NDEBUG | ||
| if (m_is_running != expected) { | ||
| mio::log_error("A BasicTimer was {}running while expected to be {}. " | ||
| "The offending call was {}. " | ||
| "Consider using an AutoTimer with name (and scope) to avoid this.", | ||
| m_is_running ? "" : "not ", expected ? "started" : "stopped", function); | ||
| } | ||
| // else: everything ok. | ||
|
|
||
| #else | ||
| mio::unused(expected, function); | ||
| #endif | ||
| } | ||
|
|
||
| } // namespace timing | ||
| } // namespace mio |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.