diff --git a/cpp/dolfinx/common/CMakeLists.txt b/cpp/dolfinx/common/CMakeLists.txt index 91425be408..5f46222cee 100644 --- a/cpp/dolfinx/common/CMakeLists.txt +++ b/cpp/dolfinx/common/CMakeLists.txt @@ -25,7 +25,6 @@ target_sources( ${CMAKE_CURRENT_SOURCE_DIR}/log.cpp ${CMAKE_CURRENT_SOURCE_DIR}/MPI.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Table.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/Timer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/TimeLogger.cpp ${CMAKE_CURRENT_SOURCE_DIR}/TimeLogManager.cpp ${CMAKE_CURRENT_SOURCE_DIR}/timing.cpp diff --git a/cpp/dolfinx/common/Timer.cpp b/cpp/dolfinx/common/Timer.cpp deleted file mode 100644 index 443901cbbf..0000000000 --- a/cpp/dolfinx/common/Timer.cpp +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (C) 2010 Garth N. Wells, 2015 Jan Blechta -// -// This file is part of DOLFINx (https://www.fenicsproject.org) -// -// SPDX-License-Identifier: LGPL-3.0-or-later - -#include "Timer.h" -#include "TimeLogManager.h" -#include "TimeLogger.h" -#include -#include - -using namespace dolfinx; -using namespace dolfinx::common; - -//----------------------------------------------------------------------------- -Timer::Timer(std::optional task) : _task(task) {} -//----------------------------------------------------------------------------- -Timer::~Timer() -{ - if (!_timer.is_stopped()) - stop(); -} -//----------------------------------------------------------------------------- -void Timer::start() { _timer.start(); } -//----------------------------------------------------------------------------- -double Timer::stop() -{ - _timer.stop(); - const auto [wall, user, system] = this->elapsed(); - if (_task.has_value()) - TimeLogManager::logger().register_timing(_task.value(), wall, user, system); - return wall; -} -//----------------------------------------------------------------------------- -std::array Timer::elapsed() const -{ - const boost::timer::cpu_times elapsed = _timer.elapsed(); - const double wall = static_cast(elapsed.wall) * 1e-9; - const double user = static_cast(elapsed.user) * 1e-9; - const double system = static_cast(elapsed.system) * 1e-9; - return {wall, user, system}; -} -//----------------------------------------------------------------------------- diff --git a/cpp/dolfinx/common/Timer.h b/cpp/dolfinx/common/Timer.h index 2f4e438229..9216459b39 100644 --- a/cpp/dolfinx/common/Timer.h +++ b/cpp/dolfinx/common/Timer.h @@ -1,4 +1,4 @@ -// Copyright (C) 2008 Anders Logg, 2015 Jan Blechta +// Copyright (C) 2008 Anders Logg, 2015 Jan Blechta, 2024 Paul T. Kühner // // This file is part of DOLFINx (https://www.fenicsproject.org) // @@ -7,7 +7,7 @@ #pragma once #include -#include +#include #include #include @@ -28,6 +28,7 @@ namespace dolfinx::common /// /// list_timings(); +template class Timer { public: @@ -35,26 +36,32 @@ class Timer /// /// If a task name is provided this enables logging to logger, otherwise (i.e. /// no task provided) nothing gets logged. - Timer(std::optional task = std::nullopt); + Timer(std::optional task = std::nullopt) : _task(task) {} /// Destructor - ~Timer(); + ~Timer() = default; /// Zero and start timer - void start(); + void start() { _start_time = chrono_timer::now(); } /// Stop timer, return wall time elapsed and store timing data into /// logger - double stop(); - - /// Return wall, user and system time in seconds - std::array elapsed() const; + template + double stop() + { + auto end_time = chrono_timer::now(); + auto elapsed = unit(end_time - _start_time).count(); + // TODO: reactivate + // if (_task.has_value()) + // TimeLogManager::logger().register_timing(_task.value(), wall, user, + // system); + return elapsed; + } private: // Name of task std::optional _task; - // Implementation of timer - boost::timer::cpu_timer _timer; + chrono_timer::time_point _start_time; }; } // namespace dolfinx::common diff --git a/python/dolfinx/wrappers/common.cpp b/python/dolfinx/wrappers/common.cpp index a4740421c4..9568141687 100644 --- a/python/dolfinx/wrappers/common.cpp +++ b/python/dolfinx/wrappers/common.cpp @@ -164,11 +164,10 @@ void common(nb::module_& m) }, nb::arg("global")); // dolfinx::common::Timer - nb::class_(m, "Timer", "Timer class") + nb::class_>(m, "Timer", "Timer class") .def(nb::init>(), nb::arg("task").none()) - .def("start", &dolfinx::common::Timer::start, "Start timer") - .def("stop", &dolfinx::common::Timer::stop, "Stop timer") - .def("elapsed", &dolfinx::common::Timer::elapsed); + .def("start", &dolfinx::common::Timer<>::start, "Start timer") + .def("stop", &dolfinx::common::Timer<>::stop, "Stop timer"); // dolfinx::common::Timer enum nb::enum_(m, "TimingType")