File: DynamicCounter.cpp

package info (click to toggle)
pytorch-cuda 2.6.0%2Bdfsg-7
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 161,620 kB
  • sloc: python: 1,278,832; cpp: 900,322; ansic: 82,710; asm: 7,754; java: 3,363; sh: 2,811; javascript: 2,443; makefile: 597; ruby: 195; xml: 84; objc: 68
file content (82 lines) | stat: -rw-r--r-- 2,393 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
#include <c10/util/DynamicCounter.h>

#include <c10/util/Synchronized.h>

#include <stdexcept>
#include <string>
#include <unordered_set>
#include <vector>

namespace c10::monitor {

namespace {
using DynamicCounterBackends =
    std::vector<std::shared_ptr<detail::DynamicCounterBackendIf>>;

Synchronized<DynamicCounterBackends>& dynamicCounterBackends() {
  static auto instance = new Synchronized<DynamicCounterBackends>();
  return *instance;
}

Synchronized<std::unordered_set<std::string>>& registeredCounters() {
  static auto instance = new Synchronized<std::unordered_set<std::string>>();
  return *instance;
}
} // namespace

namespace detail {
void registerDynamicCounterBackend(
    std::unique_ptr<DynamicCounterBackendIf> backend) {
  dynamicCounterBackends().withLock(
      [&](auto& backends) { backends.push_back(std::move(backend)); });
}
} // namespace detail

struct DynamicCounter::Guard {
  Guard(std::string_view key, Callback&& getCounterCallback)
      : key_{key},
        getCounterCallback_(std::move(getCounterCallback)),
        backends_{dynamicCounterBackends().withLock(
            [](auto& backends) { return backends; })} {
    registeredCounters().withLock([&](auto& registeredCounters) {
      if (!registeredCounters.insert(std::string(key)).second) {
        throw std::logic_error(
            "Counter " + std::string(key) + " already registered");
      }
    });

    for (const auto& backend : backends_) {
      // Avoid copying the user-provided callback to avoid unexpected behavior
      // changes when more than one backend is registered.
      backend->registerCounter(key, [&]() { return getCounterCallback_(); });
    }
  }

  Guard(Guard&& other) = delete;
  Guard(const Guard&) = delete;
  Guard& operator=(const Guard&) = delete;
  Guard& operator=(Guard&&) = delete;

  ~Guard() {
    for (const auto& backend : backends_) {
      backend->unregisterCounter(key_);
    }

    registeredCounters().withLock(
        [&](auto& registeredCounters) { registeredCounters.erase(key_); });
  }

 private:
  std::string key_;
  Callback getCounterCallback_;
  DynamicCounterBackends backends_;
};

DynamicCounter::DynamicCounter(
    std::string_view key,
    Callback getCounterCallback)
    : guard_{std::make_unique<Guard>(key, std::move(getCounterCallback))} {}

DynamicCounter::~DynamicCounter() = default;

} // namespace c10::monitor