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

Timer interface improvement #3328

Merged
merged 10 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
17 changes: 5 additions & 12 deletions cpp/dolfinx/common/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,14 @@
#include "Timer.h"
#include "TimeLogManager.h"
#include "TimeLogger.h"
#include <optional>
#include <stdexcept>

using namespace dolfinx;
using namespace dolfinx::common;

//-----------------------------------------------------------------------------
Timer::Timer() : Timer::Timer("")
{
// Do nothing
}
//-----------------------------------------------------------------------------
Timer::Timer(const std::string& task) : _task(task)
{
// Do nothing
}
Timer::Timer(std::optional<std::string> task) : _task(task) {}
//-----------------------------------------------------------------------------
Timer::~Timer()
{
Expand All @@ -33,7 +26,7 @@ void Timer::start() { _timer.start(); }
//-----------------------------------------------------------------------------
void Timer::resume()
{
if (!_task.empty())
if (_task.has_value())
{
throw std::runtime_error(
"Resuming is not well-defined for logging timer. Only "
Expand All @@ -46,8 +39,8 @@ double Timer::stop()
{
_timer.stop();
const auto [wall, user, system] = this->elapsed();
if (!_task.empty())
TimeLogManager::logger().register_timing(_task, wall, user, system);
if (_task.has_value())
TimeLogManager::logger().register_timing(_task.value(), wall, user, system);
return wall;
}
//-----------------------------------------------------------------------------
Expand Down
13 changes: 7 additions & 6 deletions cpp/dolfinx/common/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <array>
#include <boost/timer/timer.hpp>
#include <optional>
#include <string>

namespace dolfinx::common
Expand All @@ -30,11 +31,11 @@ namespace dolfinx::common
class Timer
{
public:
/// Create timer without logging
Timer();

/// Create timer with logging
Timer(const std::string& task);
/// Create timer
///
/// If a task name is provided this enables logging to logger, otherwise (i.e.
/// no task provided) nothing gets logged.
Timer(std::optional<std::string> task = std::nullopt);

/// Destructor
~Timer();
Expand All @@ -54,7 +55,7 @@ class Timer

private:
// Name of task
std::string _task;
std::optional<std::string> _task;

// Implementation of timer
boost::timer::cpu_timer _timer;
Expand Down
5 changes: 1 addition & 4 deletions python/dolfinx/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ class Timer:
_cpp_object: _cpp.common.Timer

def __init__(self, name: typing.Optional[str] = None):
if name is None:
self._cpp_object = _cpp.common.Timer()
else:
self._cpp_object = _cpp.common.Timer(name)
self._cpp_object = _cpp.common.Timer(name)

def __enter__(self):
self._cpp_object.start()
Expand Down
10 changes: 5 additions & 5 deletions python/dolfinx/wrappers/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@

#include <complex>
#include <memory>
#include <optional>
#include <span>
#include <string>
#include <vector>

#include <nanobind/nanobind.h>
#include <nanobind/ndarray.h>
#include <nanobind/stl/array.h>
#include <nanobind/stl/optional.h>
#include <nanobind/stl/pair.h>
#include <nanobind/stl/string.h>
#include <nanobind/stl/tuple.h>
#include <nanobind/stl/vector.h>

#include <dolfinx/common/defines.h>
#include <dolfinx/common/defines.h>
#include <dolfinx/common/IndexMap.h>
#include <dolfinx/common/log.h>
#include <dolfinx/common/Scatterer.h>
#include <dolfinx/common/Table.h>
#include <dolfinx/common/Timer.h>
#include <dolfinx/common/defines.h>
#include <dolfinx/common/log.h>
#include <dolfinx/common/timing.h>
#include <dolfinx/common/utils.h>

Expand Down Expand Up @@ -152,8 +153,7 @@ void common(nb::module_& m)
nb::arg("global"));
// dolfinx::common::Timer
nb::class_<dolfinx::common::Timer>(m, "Timer", "Timer class")
.def(nb::init<>())
.def(nb::init<std::string>(), nb::arg("task"))
.def(nb::init<std::optional<std::string>>(), nb::arg("task") = nb::none())
garth-wells marked this conversation as resolved.
Show resolved Hide resolved
.def("start", &dolfinx::common::Timer::start, "Start timer")
.def("stop", &dolfinx::common::Timer::stop, "Stop timer")
.def("resume", &dolfinx::common::Timer::resume)
Expand Down
Loading