Skip to content

Commit

Permalink
Revised Timer, now using std::chrono (#3484)
Browse files Browse the repository at this point in the history
* Remove resume functionality
This is not used and is only available for non logging timers whcih makes it inconsistent.

* Move to chrono timings

* Return duration instead of scalar

* Remove boost timer dependency

* Switch to microseconds as default

* Fix with cast

* Addapt logging to new std::chrono clocks

* Remove missed boost timer dependencies

* Partially revert boost dependency removal

* Remove list_timings from python wrapper

* Readd elapsed for pyhton testing

* Ruff

* Ruff 2

* Doc

* Some simplifications

* Try removing cast

* Simplification

* Doc improvement

* Fixes for timer

* Dox updates

* Updates

* Various fixes

* Doc improvements

* Remove debug

---------

Co-authored-by: schnellerhase <[email protected]>
  • Loading branch information
garth-wells and schnellerhase authored Oct 28, 2024
1 parent 5c1b88e commit a33486f
Show file tree
Hide file tree
Showing 20 changed files with 175 additions and 226 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ccpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ jobs:
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install catch2 cmake g++ libboost-dev libboost-timer-dev libhdf5-mpi-dev libparmetis-dev libpugixml-dev libspdlog-dev mpi-default-dev ninja-build pkg-config
sudo apt-get install catch2 cmake g++ libboost-dev libhdf5-mpi-dev libparmetis-dev libpugixml-dev libspdlog-dev mpi-default-dev ninja-build pkg-config
- name: Set up Python
uses: actions/setup-python@v5
with:
Expand Down
2 changes: 1 addition & 1 deletion cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ endif()

set(Boost_USE_MULTITHREADED $ENV{BOOST_USE_MULTITHREADED})
set(Boost_VERBOSE TRUE)
find_package(Boost 1.70 REQUIRED timer)
find_package(Boost 1.70 REQUIRED)
set_package_properties(
Boost PROPERTIES
TYPE REQUIRED
Expand Down
2 changes: 1 addition & 1 deletion cpp/cmake/templates/DOLFINXConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if(DEFINED ENV{BOOST_ROOT} OR DEFINED BOOST_ROOT)
endif()
set(Boost_USE_MULTITHREADED $ENV{BOOST_USE_MULTITHREADED})
set(Boost_VERBOSE TRUE)
find_package(Boost 1.70 REQUIRED timer)
find_package(Boost 1.70 REQUIRED)

if(@ufcx_FOUND@)
find_dependency(ufcx)
Expand Down
2 changes: 1 addition & 1 deletion cpp/demo/custom_kernel/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ void assemble(MPI_Comm comm)
assemble_matrix1<T>(mesh->geometry(), *V->dofmap(), kernel_a, cells);
assemble_vector1<T>(mesh->geometry(), *V->dofmap(), kernel_L, cells);

list_timings(comm, {TimingType::wall});
list_timings(comm);
}

int main(int argc, char* argv[])
Expand Down
1 change: 0 additions & 1 deletion cpp/dolfinx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ target_link_libraries(dolfinx PUBLIC Basix::basix)

# Boost
target_link_libraries(dolfinx PUBLIC Boost::headers)
target_link_libraries(dolfinx PUBLIC Boost::timer)

# MPI
target_link_libraries(dolfinx PUBLIC MPI::MPI_CXX)
Expand Down
1 change: 0 additions & 1 deletion cpp/dolfinx/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions cpp/dolfinx/common/TimeLogManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#pragma once

#include "TimeLogger.h"

namespace dolfinx::common
{

Expand Down
60 changes: 16 additions & 44 deletions cpp/dolfinx/common/TimeLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,34 @@
#include "MPI.h"
#include "log.h"
#include <iostream>
#include <variant>

using namespace dolfinx;
using namespace dolfinx::common;

//-----------------------------------------------------------------------------
void TimeLogger::register_timing(std::string task, double wall, double user,
double system)
void TimeLogger::register_timing(std::string task, double time)
{
assert(wall >= 0.0);
assert(user >= 0.0);
assert(system >= 0.0);
assert(time >= 0.0);

// Print a message
std::string line = "Elapsed wall, usr, sys time: " + std::to_string(wall)
+ ", " + std::to_string(user) + ", "
+ std::to_string(system) + " (" + task + ")";
std::string line
= "Elapsed time: " + std::to_string(time) + " (" + task + ")";
spdlog::debug(line.c_str());

// Store values for summary
if (auto it = _timings.find(task); it != _timings.end())
{
std::get<0>(it->second) += 1;
std::get<1>(it->second) += wall;
std::get<2>(it->second) += user;
std::get<3>(it->second) += system;
std::get<1>(it->second) += time;
}
else
_timings.insert({task, {1, wall, user, system}});
_timings.insert({task, {1, time}});
}
//-----------------------------------------------------------------------------
void TimeLogger::list_timings(MPI_Comm comm, std::set<TimingType> type,
Table::Reduction reduction)
void TimeLogger::list_timings(MPI_Comm comm, Table::Reduction reduction) const
{
// Format and reduce to rank 0
Table timings = this->timings(type);
Table timings = this->timings();
timings = timings.reduce(comm, reduction);
const std::string str = "\n" + timings.str();

Expand All @@ -52,44 +44,23 @@ void TimeLogger::list_timings(MPI_Comm comm, std::set<TimingType> type,
std::cout << str << std::endl;
}
//-----------------------------------------------------------------------------
Table TimeLogger::timings(std::set<TimingType> type)
Table TimeLogger::timings() const
{
// Generate log::timing table
Table table("Summary of timings");

bool time_wall = type.find(TimingType::wall) != type.end();
bool time_user = type.find(TimingType::user) != type.end();
bool time_sys = type.find(TimingType::system) != type.end();

for (auto& it : _timings)
{
const std::string task = it.first;
const auto [num_timings, wall, usr, sys] = it.second;
// NB - the cast to std::variant should not be needed: needed by Intel
// compiler.
table.set(task, "reps",
std::variant<std::string, int, double>(num_timings));
if (time_wall)
{
table.set(task, "wall avg", wall / static_cast<double>(num_timings));
table.set(task, "wall tot", wall);
}
if (time_user)
{
table.set(task, "usr avg", usr / static_cast<double>(num_timings));
table.set(task, "usr tot", usr);
}
if (time_sys)
{
table.set(task, "sys avg", sys / static_cast<double>(num_timings));
table.set(task, "sys tot", sys);
}
std::string task = it.first;
auto [num_timings, time] = it.second;
table.set(task, "reps", num_timings);
table.set(task, "avg", time / static_cast<double>(num_timings));
table.set(task, "tot", time);
}

return table;
}
//-----------------------------------------------------------------------------
std::tuple<int, double, double, double> TimeLogger::timing(std::string task)
std::pair<int, double> TimeLogger::timing(std::string task) const
{
// Find timing
auto it = _timings.find(task);
Expand All @@ -98,6 +69,7 @@ std::tuple<int, double, double, double> TimeLogger::timing(std::string task)
throw std::runtime_error("No timings registered for task \"" + task
+ "\".");
}

return it->second;
}
//-----------------------------------------------------------------------------
21 changes: 8 additions & 13 deletions cpp/dolfinx/common/TimeLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@
#include "timing.h"
#include <map>
#include <mpi.h>
#include <set>
#include <string>
#include <tuple>
#include <utility>

namespace dolfinx::common
{

/// Timer logging

class TimeLogger
{
public:
Expand All @@ -35,29 +33,26 @@ class TimeLogger
~TimeLogger() = default;

/// Register timing (for later summary)
void register_timing(std::string task, double wall, double user,
double system);
void register_timing(std::string task, double wall);

/// Return a summary of timings and tasks in a Table
Table timings(std::set<TimingType> type);
Table timings() const;

/// List a summary of timings and tasks. Reduction type is
/// printed.
/// @param comm MPI Communicator
/// @param type Set of possible timings: wall, user or system
/// @param reduction Reduction type (min, max or average)
void list_timings(MPI_Comm comm, std::set<TimingType> type,
Table::Reduction reduction);
void list_timings(MPI_Comm comm, Table::Reduction reduction) const;

/// Return timing
/// @brief Return timing.
/// @param[in] task The task name to retrieve the timing for
/// @returns Values (count, total wall time, total user time, total
/// system time) for given task.
std::tuple<int, double, double, double> timing(std::string task);
std::pair<int, double> timing(std::string task) const;

private:
// List of timings for tasks, map from string to (num_timings,
// total_wall_time, total_user_time, total_system_time)
std::map<std::string, std::tuple<int, double, double, double>> _timings;
// total_wall_time)
std::map<std::string, std::pair<int, double>> _timings;
};
} // namespace dolfinx::common
55 changes: 0 additions & 55 deletions cpp/dolfinx/common/Timer.cpp

This file was deleted.

Loading

0 comments on commit a33486f

Please sign in to comment.