Skip to content

Commit

Permalink
Build profilers by default and add option to enable them at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
mscuttari committed Nov 18, 2024
1 parent e40f58d commit 717d254
Show file tree
Hide file tree
Showing 20 changed files with 770 additions and 506 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ endif()
#------------------------------------------------------------------------------

# Profiling.
option(MARCO_PROFILING "Generate code for runtime profiling." OFF)
option(MARCO_PROFILING "Generate code for runtime profiling." ON)

# Include directories.
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include ${MLIR_INCLUDE_DIRS})
Expand Down
99 changes: 64 additions & 35 deletions include/marco/Runtime/Printers/CSV/Profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,75 @@

#include "marco/Runtime/Profiling/Profiling.h"
#include "marco/Runtime/Profiling/Timer.h"
#include "marco/Runtime/Simulation/Options.h"
#include <mutex>

namespace marco::runtime::profiling
{
class PrintProfiler : public Profiler
{
public:
PrintProfiler();

void reset() override;

void print() const override;

public:
Timer booleanValues;
Timer integerValues;
Timer floatValues;
Timer stringValues;

mutable std::mutex mutex;
};

PrintProfiler& printProfiler();
}

#define PRINT_PROFILER_BOOL_START ::marco::runtime::profiling::printProfiler().booleanValues.start()
#define PRINT_PROFILER_BOOL_STOP ::marco::runtime::profiling::printProfiler().booleanValues.stop()

#define PRINT_PROFILER_INT_START ::marco::runtime::profiling::printProfiler().integerValues.start()
#define PRINT_PROFILER_INT_STOP ::marco::runtime::profiling::printProfiler().integerValues.stop()

#define PRINT_PROFILER_FLOAT_START ::marco::runtime::profiling::printProfiler().floatValues.start()
#define PRINT_PROFILER_FLOAT_STOP ::marco::runtime::profiling::printProfiler().floatValues.stop()

#define PRINT_PROFILER_STRING_START ::marco::runtime::profiling::printProfiler().stringValues.start()
#define PRINT_PROFILER_STRING_STOP ::marco::runtime::profiling::printProfiler().stringValues.stop()
namespace marco::runtime::profiling {
class PrintProfiler : public Profiler {
public:
PrintProfiler();

void reset() override;

void print() const override;

public:
Timer booleanValues;
Timer integerValues;
Timer floatValues;
Timer stringValues;

mutable std::mutex mutex;
};

PrintProfiler &printProfiler();
} // namespace marco::runtime::profiling

// clang-format off
#define PRINT_PROFILER_BOOL_START \
if (::marco::runtime::simulation::getOptions().profiling) { \
::marco::runtime::profiling::printProfiler().booleanValues.start(); \
}

#define PRINT_PROFILER_BOOL_STOP \
if (::marco::runtime::simulation::getOptions().profiling) { \
::marco::runtime::profiling::printProfiler().booleanValues.stop(); \
}

#define PRINT_PROFILER_INT_START \
if (::marco::runtime::simulation::getOptions().profiling) { \
::marco::runtime::profiling::printProfiler().integerValues.start(); \
}

#define PRINT_PROFILER_INT_STOP \
if (::marco::runtime::simulation::getOptions().profiling) { \
::marco::runtime::profiling::printProfiler().integerValues.stop(); \
}

#define PRINT_PROFILER_FLOAT_START \
if (::marco::runtime::simulation::getOptions().profiling) { \
::marco::runtime::profiling::printProfiler().floatValues.start(); \
}

#define PRINT_PROFILER_FLOAT_STOP \
if (::marco::runtime::simulation::getOptions().profiling) { \
::marco::runtime::profiling::printProfiler().floatValues.stop(); \
}

#define PRINT_PROFILER_STRING_START \
if (::marco::runtime::simulation::getOptions().profiling) { \
::marco::runtime::profiling::printProfiler().stringValues.start(); \
}

#define PRINT_PROFILER_STRING_STOP \
if (::marco::runtime::simulation::getOptions().profiling) { \
::marco::runtime::profiling::printProfiler().stringValues.stop(); \
}
// clang-format on

#else

#define PRINT_PROFILER_DO_NOTHING static_assert(true)
#define PRINT_PROFILER_DO_NOTHING static_assert(true);

#define PRINT_PROFILER_BOOL_START PRINT_PROFILER_DO_NOTHING
#define PRINT_PROFILER_BOOL_STOP PRINT_PROFILER_DO_NOTHING
Expand Down
55 changes: 28 additions & 27 deletions include/marco/Runtime/Profiling/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,42 @@
#include <chrono>
#include <mutex>

namespace marco::runtime::profiling
{
class Timer
{
public:
Timer();
namespace marco::runtime::profiling {
class Timer {
public:
Timer();

Timer(const Timer& other) = delete;
Timer(const Timer &other) = delete;

Timer& operator=(const Timer& other) = delete;
Timer &operator=(const Timer &other) = delete;

void start();
void start();

void stop();
void stop();

void reset();
void reset();

template<typename Period = std::nano, typename Rep = double>
Rep totalElapsedTime() const
{
auto casted = static_cast<std::chrono::duration<Rep, Period>>(totalElapsed());
return casted.count();
}
template <typename Period = std::nano, typename Rep = double>
Rep totalElapsedTime() const {
auto casted =
static_cast<std::chrono::duration<Rep, Period>>(totalElapsed());
return casted.count();
}

private:
std::chrono::nanoseconds elapsed() const;
private:
std::chrono::nanoseconds elapsed() const;

std::chrono::nanoseconds totalElapsed() const;
std::chrono::nanoseconds totalElapsed() const;

private:
int running_ = 0;
std::chrono::steady_clock::time_point start_;
std::chrono::nanoseconds accumulatedTime_;
mutable std::mutex mutex;
};
}
private:
int running_ = 0;
std::chrono::steady_clock::time_point start_;
std::chrono::nanoseconds accumulatedTime_;

#ifdef THREADS_ENABLE
mutable std::mutex mutex;
#endif
};
} // namespace marco::runtime::profiling

#endif // MARCO_RUNTIME_PROFILING_TIMER_H
1 change: 1 addition & 0 deletions include/marco/Runtime/Simulation/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ enum class SchedulerPolicy { Sequential, Multithreaded };
namespace marco::runtime::simulation {
struct Options {
bool debug = false;
bool profiling = false;

double startTime = 0;
double endTime = 10;
Expand Down
114 changes: 75 additions & 39 deletions include/marco/Runtime/Simulation/Profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,86 @@

#include "marco/Runtime/Profiling/Profiling.h"
#include "marco/Runtime/Profiling/Timer.h"
#include "marco/Runtime/Simulation/Options.h"
#include <mutex>

namespace marco::runtime::profiling
{
class SimulationProfiler : public Profiler
{
public:
SimulationProfiler();

void reset() override;

void print() const override;

public:
Timer commandLineArgs;
Timer initialization;
Timer initialModel;
Timer dynamicModel;
Timer printing;

mutable std::mutex mutex;
};

SimulationProfiler& simulationProfiler();
}

#define SIMULATION_PROFILER_ARG_START ::marco::runtime::profiling::simulationProfiler().commandLineArgs.start()
#define SIMULATION_PROFILER_ARG_STOP ::marco::runtime::profiling::simulationProfiler().commandLineArgs.stop()

#define SIMULATION_PROFILER_INIT_START ::marco::runtime::profiling::simulationProfiler().initialization.start()
#define SIMULATION_PROFILER_INIT_STOP ::marco::runtime::profiling::simulationProfiler().initialization.stop()

#define SIMULATION_PROFILER_INITIAL_MODEL_START ::marco::runtime::profiling::simulationProfiler().initialModel.start()
#define SIMULATION_PROFILER_INITIAL_MODEL_STOP ::marco::runtime::profiling::simulationProfiler().initialModel.stop()

#define SIMULATION_PROFILER_DYNAMIC_MODEL_START ::marco::runtime::profiling::simulationProfiler().dynamicModel.start()
#define SIMULATION_PROFILER_DYNAMIC_MODEL_STOP ::marco::runtime::profiling::simulationProfiler().dynamicModel.stop()

#define SIMULATION_PROFILER_PRINTING_START ::marco::runtime::profiling::simulationProfiler().printing.start()
#define SIMULATION_PROFILER_PRINTING_STOP ::marco::runtime::profiling::simulationProfiler().printing.stop()
namespace marco::runtime::profiling {
class SimulationProfiler : public Profiler {
public:
SimulationProfiler();

void reset() override;

void print() const override;

public:
Timer commandLineArgs;
Timer initialization;
Timer initialModel;
Timer dynamicModel;
Timer printing;

mutable std::mutex mutex;
};

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(); \
}

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

#define SIMULATION_PROFILER_INITIAL_MODEL_START \
if (::marco::runtime::simulation::getOptions().profiling) { \
::marco::runtime::profiling::simulationProfiler().initialModel.start(); \
}

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

#define SIMULATION_PROFILER_DYNAMIC_MODEL_START \
if (::marco::runtime::simulation::getOptions().profiling) { \
::marco::runtime::profiling::simulationProfiler().dynamicModel.start(); \
}

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

#define SIMULATION_PROFILER_PRINTING_START \
if (::marco::runtime::simulation::getOptions().profiling) { \
::marco::runtime::profiling::simulationProfiler().printing.start(); \
}

#define SIMULATION_PROFILER_PRINTING_STOP \
if (::marco::runtime::simulation::getOptions().profiling) { \
::marco::runtime::profiling::simulationProfiler().printing.stop(); \
}
// clang-format on

#else

#define SIMULATION_PROFILER_DO_NOTHING static_assert(true)
#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
Expand Down
Loading

0 comments on commit 717d254

Please sign in to comment.