-
Notifications
You must be signed in to change notification settings - Fork 67
/
Nvtx.cpp
188 lines (140 loc) · 6.02 KB
/
Nvtx.cpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC.
// See top-level LICENSE file for details.
// Caliper NVidia profiler annotation binding
#include "caliper/AnnotationBinding.h"
#include "caliper/common/Attribute.h"
#include "caliper/common/Node.h"
#include "caliper/common/RuntimeConfig.h"
#include <nvToolsExt.h>
#include <atomic>
#include <cassert>
#include <map>
#include <mutex>
#include <unordered_map>
namespace cali
{
class NvtxBinding : public AnnotationBinding
{
static const ConfigSet::Entry s_configdata[];
static const uint32_t s_colors[];
static const int s_num_colors = 14;
Attribute m_color_attr;
std::atomic<int> m_color_id;
std::map<cali_id_t, nvtxDomainHandle_t> m_domain_map;
std::mutex m_domain_mutex;
bool m_cycle_colors;
std::unordered_map<std::string, uint32_t> m_color_map;
std::mutex m_color_map_mutex;
uint32_t get_attribute_color(const Attribute& attr)
{
cali_id_t color_attr_id = m_color_attr.id();
const Node* node = nullptr;
for (node = attr.node()->first_child(); node; node = node->next_sibling())
if (node->attribute() == color_attr_id)
break;
return node ? static_cast<uint32_t>(node->data().to_uint()) : s_colors[0];
}
uint32_t get_value_color(const Variant& value)
{
std::string valstr = value.to_string();
uint32_t color = s_colors[0];
{
std::lock_guard<std::mutex> g(m_color_map_mutex);
auto it = m_color_map.find(valstr);
if (it == m_color_map.end()) {
color = s_colors[m_color_id++ % s_num_colors];
m_color_map.emplace(std::make_pair(std::move(valstr), color));
} else
color = it->second;
}
return color;
}
uint32_t get_color(const Attribute& attr, const Variant& value)
{
if (m_cycle_colors)
return get_value_color(value);
else
return get_attribute_color(attr);
}
public:
void initialize(Caliper* c, Channel* chn)
{
std::string name = "nvtx.color#";
name.append(std::to_string(chn->id()));
m_color_attr = c->create_attribute("nvtx.color", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS | CALI_ATTR_HIDDEN);
m_cycle_colors = chn->config().init("nvtx", s_configdata).get("cycle_colors").to_bool();
}
const char* service_tag() const { return "nvtx"; }
void on_mark_attribute(Caliper* c, Channel*, const Attribute& attr)
{
if (m_cycle_colors)
return;
// When cycle_colors is on, we cycle colors for each value.
// Otherwise, we cycle colors for attributes, and mark the attribute
// color here.
// Set the color flag
Variant v_color(static_cast<uint64_t>(s_colors[m_color_id++ % s_num_colors]));
c->make_tree_entry(m_color_attr, v_color, attr.node());
}
void on_begin(Caliper*, Channel*, const Attribute& attr, const Variant& value)
{
nvtxEventAttributes_t eventAttrib = { 0 };
eventAttrib.version = NVTX_VERSION;
eventAttrib.size = NVTX_EVENT_ATTRIB_STRUCT_SIZE;
eventAttrib.colorType = NVTX_COLOR_ARGB;
eventAttrib.color = get_color(attr, value);
eventAttrib.messageType = NVTX_MESSAGE_TYPE_ASCII;
eventAttrib.message.ascii =
(value.type() == CALI_TYPE_STRING ? static_cast<const char*>(value.data()) : value.to_string().c_str());
// For properly nested attributes, just use default push/pop.
// For other attributes, create a domain.
if (attr.is_nested()) {
nvtxRangePushEx(&eventAttrib);
} else {
nvtxDomainHandle_t domain;
{
std::lock_guard<std::mutex> g(m_domain_mutex);
auto it = m_domain_map.find(attr.id());
if (it == m_domain_map.end()) {
domain = nvtxDomainCreateA(attr.name().c_str());
m_domain_map.insert(std::make_pair(attr.id(), domain));
} else {
domain = it->second;
}
}
nvtxDomainRangePushEx(domain, &eventAttrib);
}
}
void on_end(Caliper*, Channel*, const Attribute& attr, const Variant& value)
{
if (attr.is_nested()) {
nvtxRangePop();
} else {
nvtxDomainHandle_t domain;
{
std::lock_guard<std::mutex> g(m_domain_mutex);
auto it = m_domain_map.find(attr.id());
if (it == m_domain_map.end()) {
Log(0).stream() << "nvtx: on_end(): error: domain for attribute " << attr.name() << " not found!"
<< std::endl;
return;
}
domain = it->second;
}
nvtxDomainRangePop(domain);
}
}
};
const uint32_t NvtxBinding::s_colors[] = { 0x0000cc00, 0x000000cc, 0x00cccc00, 0x00cc00cc, 0x0000cccc,
0x00cc0000, 0x00cccccc, 0x00008800, 0x00000088, 0x00888800,
0x00880088, 0x00008888, 0x00880000, 0x00888888 };
const ConfigSet::Entry NvtxBinding::s_configdata[] = { { "cycle_colors",
CALI_TYPE_BOOL,
"true",
"Use a different color for each annotation entry",
"Use a different color for each annotation entry" },
ConfigSet::Terminator };
CaliperService nvtx_service { "nvtx", &AnnotationBinding::make_binding<NvtxBinding> };
// Keep deprecated "nvprof" alias for nvtx service
CaliperService nvprof_service { "nvprof", &AnnotationBinding::make_binding<NvtxBinding> };
} // namespace cali