Skip to content

Commit

Permalink
Timer interface improvement (#3328)
Browse files Browse the repository at this point in the history
* Adapt Timer

* Remove space

* Switch to member variable being optional

* clang-format

* Fix python export

* Remove move

* Remove none from python export

* Re add nb::none

* Update python/dolfinx/wrappers/common.cpp

---------

Co-authored-by: Garth N. Wells <gnw20@cam.ac.uk>
  • Loading branch information
schnellerhase and garth-wells authored Sep 11, 2024
1 parent fd47df4 commit ad40e35
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 27 deletions.
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").none())
.def("start", &dolfinx::common::Timer::start, "Start timer")
.def("stop", &dolfinx::common::Timer::stop, "Stop timer")
.def("resume", &dolfinx::common::Timer::resume)
Expand Down

0 comments on commit ad40e35

Please sign in to comment.