-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmynvtx.h
144 lines (125 loc) · 3.47 KB
/
mynvtx.h
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
/*
* mynvtx.h
*
* Created on: 11 déc. 2024
* Author: rb263871
*/
#ifndef MYNVTX_H_
#define MYNVTX_H_
#ifdef __CUDACC__
#include <nvtx3/nvToolsExt.h>
#include <string.h>
/*
* Function to convert a color name (e.g. "red") to a 32-bit ARGB color.
* Adjust or add more colors if needed.
*/
static inline uint32_t colorFromName(const char *colorName)
{
uint32_t color = 0xFFFFFFFF; // Default white: ARGB = FF FF FF FF
if (strcasecmp(colorName, "red") == 0)
{
color = 0xFFFF0000; // Red
}
else if (strcasecmp(colorName, "green") == 0)
{
color = 0xFF00FF00; // Green
}
else if (strcasecmp(colorName, "blue") == 0)
{
color = 0xFF0000FF; // Blue
}
else if (strcasecmp(colorName, "white") == 0)
{
color = 0xFFFFFFFF; // White
}
else if (strcasecmp(colorName, "black") == 0)
{
color = 0xFF000000; // Black
}
else if (strcasecmp(colorName, "yellow") == 0)
{
color = 0xFFFFFF00; // Yellow
}
else if (strcasecmp(colorName, "cyan") == 0)
{
color = 0xFF00FFFF; // Cyan
}
else if (strcasecmp(colorName, "magenta") == 0)
{
color = 0xFFFF00FF; // Magenta
}
else if (strcasecmp(colorName, "gray") == 0)
{
color = 0xFF808080; // Gray
}
else if (strcasecmp(colorName, "orange") == 0)
{
color = 0xFFFFA500; // Orange
}
else if (strcasecmp(colorName, "purple") == 0)
{
color = 0xFF800080; // Purple
}
else if (strcasecmp(colorName, "pink") == 0)
{
color = 0xFFFFC0CB; // Pink
}
else
{
std::cout<<"mynvtx ERROR: unsupported color:"<<colorName<<std::endl;
}
return color;
}
/*
* Helper function to create NVTX event attributes given a message and color.
*/
static inline nvtxEventAttributes_t createEventAttributes(const char *msg, const char *colorName)
{
nvtxEventAttributes_t eventAttrib = {0};
eventAttrib.version = NVTX_VERSION;
eventAttrib.size = NVTX_EVENT_ATTRIB_STRUCT_SIZE;
eventAttrib.messageType = NVTX_MESSAGE_TYPE_ASCII;
eventAttrib.message.ascii = msg;
eventAttrib.colorType = NVTX_COLOR_ARGB;
eventAttrib.color = colorFromName(colorName);
return eventAttrib;
}
/*
* This is the wrapper function that you’ll call.
* Example usage: mynvtxRangePush("myMessage", red);
*/
static inline void mynvtxRangePush(const char *msg, const char *colorName="black")
{
nvtxEventAttributes_t attrib = createEventAttributes(msg, colorName);
nvtxRangePushEx(&attrib);
}
//Wrapper if inputs are std::string
static inline void mynvtxRangePush(const std::string msg_string, const std::string color_string="black")
{
mynvtxRangePush(msg_string.c_str(), color_string.c_str());
}
/*
* Corresponding pop for the NVTX range.
* You must call nvtxRangePop() when you finish.
*/
static inline void mynvtxRangePop()
{
nvtxRangePop();
}
static inline void mynvtxRangePop(const char * msg)
{
nvtxRangePop();
}
static inline void mynvtxRangePop(const std::string msg_string)
{
nvtxRangePop();
}
#else // __CUDACC__
//If no cuda compiler, do nothing
static inline void mynvtxRangePush(const char *msg, const char *colorName="black"){}
static inline void mynvtxRangePush(const std::string msg_string, const std::string color_string){}
static inline void mynvtxRangePop(){}
static inline void mynvtxRangePop(const char * msg){}
static inline void mynvtxRangePop(const std::string msg_string){}
#endif //__CUDACC__
#endif /* MYNVTX_H_ */