1#include <torch/csrc/profiler/stubs/base.h>
2
3#include <c10/util/Exception.h>
4
5namespace torch {
6namespace profiler {
7namespace impl {
8
9ProfilerStubs::~ProfilerStubs() = default;
10
11namespace {
12struct DefaultStubs : public ProfilerStubs {
13 DefaultStubs(const char* name) : name_{name} {}
14
15 void record(int*, ProfilerEventStub*, int64_t*) const override {
16 fail();
17 }
18 float elapsed(const ProfilerEventStub*, const ProfilerEventStub*)
19 const override {
20 fail();
21 return 0.f;
22 }
23 void mark(const char*) const override {
24 fail();
25 }
26 void rangePush(const char*) const override {
27 fail();
28 }
29 void rangePop() const override {
30 fail();
31 }
32 bool enabled() const override {
33 return false;
34 }
35 void onEachDevice(std::function<void(int)>) const override {
36 fail();
37 }
38 void synchronize() const override {
39 fail();
40 }
41 ~DefaultStubs() override = default;
42
43 private:
44 void fail() const {
45 AT_ERROR(name_, " used in profiler but not enabled.");
46 }
47
48 const char* const name_;
49};
50} // namespace
51
52#define REGISTER_DEFAULT(name, upper_name) \
53 namespace { \
54 const DefaultStubs default_##name##_stubs{#upper_name}; \
55 constexpr const DefaultStubs* default_##name##_stubs_addr = \
56 &default_##name##_stubs; \
57 \
58 /* Constant initialization, so it is guaranteed to be initialized before*/ \
59 /* static initialization calls which may invoke register<name>Methods*/ \
60 inline const ProfilerStubs*& name##_stubs() { \
61 static const ProfilerStubs* stubs_ = \
62 static_cast<const ProfilerStubs*>(default_##name##_stubs_addr); \
63 return stubs_; \
64 } \
65 } /*namespace*/ \
66 \
67 const ProfilerStubs* name##Stubs() { \
68 return name##_stubs(); \
69 } \
70 \
71 void register##upper_name##Methods(ProfilerStubs* stubs) { \
72 name##_stubs() = stubs; \
73 }
74
75REGISTER_DEFAULT(cuda, CUDA)
76REGISTER_DEFAULT(itt, ITT)
77#undef REGISTER_DEFAULT
78
79} // namespace impl
80} // namespace profiler
81} // namespace torch
82