Skip to content
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

Improve profiling #28

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 5 additions & 7 deletions include/marco/Runtime/Profiling/Profiling.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
#include "marco/Runtime/Profiling/Profiler.h"
#include <memory>

namespace marco::runtime
{
void registerProfiler(profiling::Profiler& profiler);
void registerProfiler(std::shared_ptr<profiling::Profiler> profiler);
namespace marco::runtime {
void registerProfiler(profiling::Profiler &profiler);
void registerProfiler(std::shared_ptr<profiling::Profiler> profiler);

void profilingInit();
void printProfilingStats();
}
void printProfilingStats();
} // namespace marco::runtime

#endif // MARCO_RUNTIME_PROFILING_PROFILING_H
14 changes: 0 additions & 14 deletions include/marco/Runtime/Simulation/Profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class SimulationProfiler : public Profiler {
void print() const override;

public:
Timer commandLineArgs;
Timer initialization;
Timer initialModel;
Timer dynamicModel;
Expand All @@ -31,16 +30,6 @@ SimulationProfiler &simulationProfiler();
} // namespace marco::runtime::profiling

// clang-format off
#define SIMULATION_PROFILER_ARG_START \
if (::marco::runtime::simulation::getOptions().profiling) { \
::marco::runtime::profiling::simulationProfiler().commandLineArgs.start(); \
}

#define SIMULATION_PROFILER_ARG_STOP \
if (::marco::runtime::simulation::getOptions().profiling) { \
::marco::runtime::profiling::simulationProfiler().commandLineArgs.stop(); \
}

#define SIMULATION_PROFILER_INIT_START \
if (::marco::runtime::simulation::getOptions().profiling) { \
::marco::runtime::profiling::simulationProfiler().initialization.start(); \
Expand Down Expand Up @@ -86,9 +75,6 @@ SimulationProfiler &simulationProfiler();

#define SIMULATION_PROFILER_DO_NOTHING static_assert(true);

#define SIMULATION_PROFILER_ARG_START SIMULATION_PROFILER_DO_NOTHING
#define SIMULATION_PROFILER_ARG_STOP SIMULATION_PROFILER_DO_NOTHING

#define SIMULATION_PROFILER_INIT_START SIMULATION_PROFILER_DO_NOTHING
#define SIMULATION_PROFILER_INIT_STOP SIMULATION_PROFILER_DO_NOTHING

Expand Down
6 changes: 3 additions & 3 deletions include/marco/Runtime/Solvers/IDA/Profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ class IDAProfiler : public Profiler {

public:
Timer initialConditionsTimer;
int64_t stepsCounter;
int64_t stepsCounter{0};
Timer stepsTimer;
Timer algebraicVariablesTimer;
int64_t residualsCallCounter;
int64_t residualsCallCounter{0};
Timer residualsTimer;
int64_t partialDerivativesCallCounter;
int64_t partialDerivativesCallCounter{0};
Timer partialDerivativesTimer;
Timer copyVarsFromMARCOTimer;
Timer copyVarsIntoMARCOTimer;
Expand Down
4 changes: 2 additions & 2 deletions include/marco/Runtime/Solvers/KINSOL/Profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class KINSOLProfiler : public Profiler {
void incrementPartialDerivativesCallCounter();

public:
int64_t residualsCallCounter;
int64_t residualsCallCounter{0};
Timer residualsTimer;
int64_t partialDerivativesCallCounter;
int64_t partialDerivativesCallCounter{0};
Timer partialDerivativesTimer;
Timer copyVarsFromMARCOTimer;
Timer copyVarsIntoMARCOTimer;
Expand Down
30 changes: 9 additions & 21 deletions lib/Profiling/Profiling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,19 @@

using namespace ::marco::runtime::profiling;

static Statistics& statistics()
{
static Statistics &statistics() {
static Statistics obj;
return obj;
}

namespace marco::runtime
{
void profilingInit()
{
::statistics().reset();
}
namespace marco::runtime {
void printProfilingStats() { ::statistics().print(); }

void printProfilingStats()
{
::statistics().print();
}

void registerProfiler(Profiler& profiler)
{
::statistics().registerProfiler(profiler);
}
void registerProfiler(Profiler &profiler) {
::statistics().registerProfiler(profiler);
}

void registerProfiler(std::shared_ptr<Profiler> profiler)
{
::statistics().registerProfiler(profiler);
}
void registerProfiler(std::shared_ptr<Profiler> profiler) {
::statistics().registerProfiler(profiler);
}
} // namespace marco::runtime
85 changes: 37 additions & 48 deletions lib/Simulation/Profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,43 @@

#ifdef MARCO_PROFILING

namespace marco::runtime::profiling
{
SimulationProfiler::SimulationProfiler()
: Profiler("Simulation")
{
registerProfiler(*this);
}

void SimulationProfiler::reset()
{
std::lock_guard<std::mutex> lockGuard(mutex);

commandLineArgs.reset();
initialization.reset();
initialModel.reset();
dynamicModel.reset();
printing.reset();
}

void SimulationProfiler::print() const
{
std::lock_guard<std::mutex> lockGuard(mutex);

std::cerr << "Time spent on command-line arguments processing: "
<< commandLineArgs.totalElapsedTime<std::milli>() << " ms"
<< std::endl;

std::cerr << "Time spent on initialization: "
<< initialization.totalElapsedTime<std::milli>() << " ms"
<< std::endl;

std::cerr << "Time spent solving the initial model: "
<< initialModel.totalElapsedTime<std::milli>() << " ms"
<< std::endl;

std::cerr << "Time spent solving the dynamic model: "
<< dynamicModel.totalElapsedTime<std::milli>() << " ms"
<< std::endl;

std::cerr << "Time spent on values printing: "
<< printing.totalElapsedTime<std::milli>() << " ms" << std::endl;
}

SimulationProfiler& simulationProfiler()
{
static SimulationProfiler obj;
return obj;
}
namespace marco::runtime::profiling {
SimulationProfiler::SimulationProfiler() : Profiler("Simulation") {
registerProfiler(*this);
}

void SimulationProfiler::reset() {
std::lock_guard<std::mutex> lockGuard(mutex);

initialization.reset();
initialModel.reset();
dynamicModel.reset();
printing.reset();
}

void SimulationProfiler::print() const {
std::lock_guard<std::mutex> lockGuard(mutex);

std::cerr << "Time spent on initialization: "
<< initialization.totalElapsedTime<std::milli>() << " ms"
<< std::endl;

std::cerr << "Time spent solving the initial model: "
<< initialModel.totalElapsedTime<std::milli>() << " ms"
<< std::endl;

std::cerr << "Time spent solving the dynamic model: "
<< dynamicModel.totalElapsedTime<std::milli>() << " ms"
<< std::endl;

std::cerr << "Time spent on values printing: "
<< printing.totalElapsedTime<std::milli>() << " ms" << std::endl;
}

SimulationProfiler &simulationProfiler() {
static SimulationProfiler obj;
return obj;
}
} // namespace marco::runtime::profiling

#endif
9 changes: 0 additions & 9 deletions lib/Simulation/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,6 @@ void Simulation::setPrinter(Printer *newPrinter) {

namespace {
Simulation runtimeInit() {
#ifdef MARCO_PROFILING
if (simulation::getOptions().profiling) {
profilingInit();
}
#endif

Simulation result;

// Number of array variables of the model (both state and algebraic ones).
Expand Down Expand Up @@ -238,7 +232,6 @@ void runtimeDeinit(Simulation &simulationInfo) {

// Parse the command-line arguments.
#ifdef CLI_ENABLE
SIMULATION_PROFILER_ARG_START
auto &cli = getCLI();
cli += simulation::getCLIOptions();

Expand All @@ -259,8 +252,6 @@ void runtimeDeinit(Simulation &simulationInfo) {
for (size_t i = 0; i < cli.size(); ++i) {
cli[i].parseCommandLineOptions(cmdl);
}

SIMULATION_PROFILER_ARG_STOP
#endif // CLI_ENABLE

SIMULATION_PROFILER_INIT_START
Expand Down
Loading