Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
3a1f318
[wip] timing framework?
reneSchm Jan 8, 2025
964bf5d
[wip] testing runtime map
reneSchm Feb 7, 2025
0ee71ff
refactor timer structure
reneSchm Mar 14, 2025
b20a85a
fix includes, add functions to get max threads and thread IDs
reneSchm Mar 14, 2025
e5af5eb
move timing code into its own directory
reneSchm Mar 14, 2025
3ca277c
rename example file
reneSchm Mar 14, 2025
e29d8f6
add Tag to registered timer name
reneSchm Mar 31, 2025
6ad898c
add Printer interface,
reneSchm Mar 31, 2025
004dedc
Merge branch '17-time-measurement' of github.com:SciCompMod/memilio i…
reneSchm Apr 14, 2025
7d7ee9c
[wip] documentation and renames
reneSchm Apr 25, 2025
617f983
Merge branch 'main' into 17-time-measurement
reneSchm Apr 28, 2025
f607351
upgrade to c++20
reneSchm Apr 28, 2025
a0574b8
Introduce StringLiteral, replacing the CONST_LITERAL makro.
reneSchm Apr 29, 2025
e7ed6af
add a test for BasicTimer and AutoTimer
reneSchm Apr 29, 2025
4767fb1
Fix TimeSeriesIteratorBase iterator requirements
reneSchm Apr 29, 2025
772b7b0
change comparison operators to use Derived type only, to avoid ambiguity
reneSchm Apr 30, 2025
82bc9eb
mark a template index assignment constexpr to avoid a unused variable…
reneSchm Apr 30, 2025
376085c
fix typing of TimeSeriesIteratorBase::pointer::operator->
reneSchm Apr 30, 2025
5253889
fix constness in equality operators
reneSchm Apr 30, 2025
580cded
try to work around some compiler warnings
reneSchm Apr 30, 2025
857e932
Merge branch '17-time-measurement' of github.com:SciCompMod/memilio i…
reneSchm Apr 30, 2025
f377b5e
more compiler warning workarounds
reneSchm Apr 30, 2025
71a627a
bump gtest version fixing msvc bug
reneSchm May 5, 2025
09fed9a
move named- and autotimer into their own headers.
reneSchm May 8, 2025
1a82580
move some definitions into cpp files
reneSchm May 8, 2025
76f1fe5
make timer test CI resilient
reneSchm May 8, 2025
85b089f
fix reset function
reneSchm May 8, 2025
557b77e
add more tests
reneSchm May 9, 2025
7d8eaa4
review suggestions
reneSchm May 9, 2025
a20409d
fully cover table_printer
reneSchm May 9, 2025
43d6f84
include function name in error message, and split some code into a .cpp
reneSchm May 13, 2025
522a65b
add a class to redirect the spdlog output
reneSchm May 13, 2025
5514287
test BasicTimer error messages
reneSchm May 13, 2025
39f2929
add documentation
reneSchm May 15, 2025
e55a81d
[ci skip] add a small readme
reneSchm May 15, 2025
331ca9e
[ci skip] delete todo
reneSchm May 15, 2025
2085609
[ci skip] fix typos
reneSchm May 22, 2025
20edabc
[ci skip] update c++ version in coding guidelines
reneSchm May 22, 2025
9d0ff65
update c++ version in ast.py
reneSchm May 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ cmake_minimum_required(VERSION 3.13)

project(memilio VERSION 1.0.0)

set(CMAKE_CXX_STANDARD "20")
set(CMAKE_CXX_STANDARD_REQUIRED "20")

option(MEMILIO_BUILD_TESTS "Build memilio unit tests." ON)
option(MEMILIO_BUILD_EXAMPLES "Build memilio examples." ON)
option(MEMILIO_BUILD_MODELS "Build memilio models." ON)
Expand Down Expand Up @@ -125,15 +128,29 @@ if(MEMILIO_ENABLE_WARNINGS)
"-Wall;-Wextra;-Wshadow;--pedantic;")
endif()
endif()

# exclude some warnings we accept
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
string(APPEND MEMILIO_CXX_FLAGS_ENABLE_WARNING_ERRORS
"-Wno-unknown-warning;-Wno-pragmas;-Wno-deprecated-copy;-Wno-expansion-to-defined;-Wno-stringop-overread;")
"-Wno-unknown-warning;-Wno-pragmas;-Wno-deprecated-copy;-Wno-expansion-to-defined;")
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
string(APPEND MEMILIO_CXX_FLAGS_ENABLE_WARNING_ERRORS
"-Wno-unknown-warning-option;-Wno-deprecated;-Wno-gnu-zero-variadic-macro-arguments;-Wno-stringop-overread;")
"-Wno-unknown-warning-option;-Wno-deprecated;-Wno-gnu-zero-variadic-macro-arguments;")
endif()

# woyrkarounds for compiler bugs or overzealous optimization/analysis
if(CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "RELEASE")
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
string(APPEND MEMILIO_CXX_FLAGS_ENABLE_WARNING_ERRORS
"-Wno-restrict;" # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105329
"-Wno-maybe-uninitialized;" # https://gcc.gnu.org/bugzilla/buglist.cgi?quicksearch=may%20be%20uninitialized
"-Wno-stringop-overread;"
)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
string(APPEND MEMILIO_CXX_FLAGS_ENABLE_WARNING_ERRORS
"-Wno-stringop-overread;"
)
endif()
endif()
# finalize string by setting warnings as errors
if(MEMILIO_ENABLE_WARNINGS_AS_ERRORS)
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
string(APPEND MEMILIO_CXX_FLAGS_ENABLE_WARNING_ERRORS
Expand Down
4 changes: 4 additions & 0 deletions cpp/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ add_executable(euler_example euler_test.cpp)
target_link_libraries(euler_example PRIVATE memilio)
target_compile_options(euler_example PRIVATE ${MEMILIO_CXX_FLAGS_ENABLE_WARNING_ERRORS})

add_executable(performance_timer_example performance_timers.cpp)
target_link_libraries(performance_timer_example PRIVATE memilio)
target_compile_options(performance_timer_example PRIVATE ${MEMILIO_CXX_FLAGS_ENABLE_WARNING_ERRORS})

add_executable(ode_secir_parameter_sampling_example ode_secir_parameter_sampling.cpp)
target_link_libraries(ode_secir_parameter_sampling_example PRIVATE memilio ode_secir)
target_compile_options(ode_secir_parameter_sampling_example PRIVATE ${MEMILIO_CXX_FLAGS_ENABLE_WARNING_ERRORS})
Expand Down
104 changes: 104 additions & 0 deletions cpp/examples/performance_timers.cpp
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;
}
12 changes: 11 additions & 1 deletion cpp/memilio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ add_library(memilio
mobility/graph_simulation.cpp
mobility/graph.h
mobility/graph.cpp
timer/auto_timer.h
timer/basic_timer.cpp
timer/basic_timer.h
timer/definitions.cpp
timer/definitions.h
timer/list_printer.h
timer/named_timer.h
timer/registration.h
timer/timer_registrar.h
utils/visitor.h
utils/uncertain_value.h
utils/uncertain_value.cpp
Expand All @@ -95,6 +104,8 @@ add_library(memilio
utils/miompi.h
utils/miompi.cpp
utils/mioomp.h
utils/mioomp.cpp
utils/string_literal.h
utils/type_list.h
)

Expand All @@ -104,7 +115,6 @@ target_include_directories(memilio PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

target_compile_features(memilio PUBLIC cxx_std_17)
target_link_libraries(memilio PUBLIC spdlog::spdlog Eigen3::Eigen Boost::boost Boost::filesystem Boost::disable_autolinking Random123 AD::AD)
target_compile_options(memilio
PRIVATE
Expand Down
7 changes: 7 additions & 0 deletions cpp/memilio/timer/README.md
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).
74 changes: 74 additions & 0 deletions cpp/memilio/timer/auto_timer.h
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.
AutoTimer(BasicTimer& timer) -> AutoTimer<"">;

} // namespace timing
} // namespace mio

#endif // MIO_TIMER_AUTO_TIMER_H
35 changes: 35 additions & 0 deletions cpp/memilio/timer/basic_timer.cpp
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
Loading