File: TimeLogger.cpp

package info (click to toggle)
fenics-dolfinx 1%3A0.10.0.post4-1exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 6,028 kB
  • sloc: cpp: 36,535; python: 25,391; makefile: 226; sh: 171; xml: 55
file content (89 lines) | stat: -rw-r--r-- 2,658 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (C) 2003-2016 Anders Logg
//
// This file is part of DOLFINx (https://www.fenicsproject.org)
//
// SPDX-License-Identifier:    LGPL-3.0-or-later

#include "TimeLogger.h"
#include "MPI.h"
#include "log.h"
#include <iostream>

using namespace dolfinx;
using namespace dolfinx::common;

//-----------------------------------------------------------------------------
TimeLogger& TimeLogger::instance()
{
  static TimeLogger _instance{};
  return _instance;
}

//-----------------------------------------------------------------------------
void TimeLogger::register_timing(
    const std::string& task, std::chrono::duration<double, std::ratio<1>> time)
{
  // Print a message
  std::string line
      = "Elapsed time: " + std::to_string(time.count()) + " (" + 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) += time;
  }
  else
    _timings.insert({task, {1, time}});
}
//-----------------------------------------------------------------------------
void TimeLogger::list_timings(MPI_Comm comm, Table::Reduction reduction) const
{
  // Format and reduce to rank 0
  Table timings = this->timing_table();
  timings = timings.reduce(comm, reduction);
  const std::string str = "\n" + timings.str();

  // Print just on rank 0
  if (dolfinx::MPI::rank(comm) == 0)
    std::cout << str << '\n';
}
//-----------------------------------------------------------------------------
Table TimeLogger::timing_table() const
{
  // Generate log::timing table
  Table table("Summary of timings (s)");
  for (auto& it : _timings)
  {
    std::string task = it.first;
    auto [num_timings, time] = it.second;
    table.set(task, "reps", num_timings);
    table.set(task, "avg", time.count() / static_cast<double>(num_timings));
    table.set(task, "tot", time.count());
  }

  return table;
}
//-----------------------------------------------------------------------------
std::pair<int, std::chrono::duration<double, std::ratio<1>>>
TimeLogger::timing(const std::string& task) const
{
  // Find timing
  auto it = _timings.find(task);
  if (it == _timings.end())
  {
    throw std::runtime_error("No timings registered for task \"" + task
                             + "\".");
  }

  return it->second;
}
//-----------------------------------------------------------------------------
std::map<std::string,
         std::pair<int, std::chrono::duration<double, std::ratio<1>>>>
TimeLogger::timings() const
{
  return _timings;
}
//-----------------------------------------------------------------------------