nvtx.hpp
1 
5 #pragma once
6 
7 #include <stdexcept>
8 #include <type_traits>
9 
10 #include <nvtx3/nvtx3.hpp>
11 
12 #include <rapidsmpf/utils/misc.hpp>
13 
14 namespace rapidsmpf::detail {
15 
23 template <typename T>
24  requires std::is_integral_v<T>
25 [[nodiscard]] std::int64_t convert_to_64bit(T value) {
26  if constexpr (std::numeric_limits<T>::max()
27  > std::numeric_limits<std::int64_t>::max())
28  {
29  if (value > std::numeric_limits<std::int64_t>::max()) {
30  throw std::overflow_error(
31  "convert_to_64bit(x): x too large to fit std::int64_t"
32  );
33  }
34  }
35  return rapidsmpf::safe_cast<std::int64_t>(value);
36 }
37 
45 template <typename T>
46  requires std::is_floating_point_v<T>
47 [[nodiscard]] double convert_to_64bit(T value) {
48  return double(value);
49 }
50 } // namespace rapidsmpf::detail
51 
56  static constexpr char const* name{"rapidsmpf"};
57 };
58 
59 // Macro to create a static, registered string that will not have a name conflict with any
60 // registered string defined in the same scope.
61 #define RAPIDSMPF_REGISTER_STRING(msg) \
62  [](char const* a_msg) -> auto& { \
63  static nvtx3::registered_string_in<rapidsmpf_domain> a_reg_str{a_msg}; \
64  return a_reg_str; \
65  }(msg)
66 
67 // implement the func range macro with a value
68 #define RAPIDSMPF_NVTX_FUNC_RANGE_IMPL_WITH_VAL(val) \
69  static_assert( \
70  std::is_arithmetic_v<decltype(val)>, \
71  "Value must be integral or floating point type" \
72  ); \
73  nvtx3::scoped_range_in<rapidsmpf_domain> RAPIDSMPF_CONCAT( \
74  _rapidsmpf_nvtx_range, __LINE__ \
75  ) { \
76  nvtx3::event_attributes { \
77  RAPIDSMPF_REGISTER_STRING(__func__), nvtx3::payload { \
78  rapidsmpf::detail::convert_to_64bit(val) \
79  } \
80  } \
81  }
82 
83 // implement the func range macro without a value
84 #define RAPIDSMPF_NVTX_FUNC_RANGE_IMPL_WITHOUT_VAL() NVTX3_FUNC_RANGE_IN(rapidsmpf_domain)
85 
86 // Macro selector for 0 vs 1 arguments
87 #define RAPIDSMPF_GET_MACRO_FUNC(_0, _1, NAME, ...) NAME
88 
89 // unwrap the arguments and call the appropriate macro
90 #define RAPIDSMPF_NVTX_FUNC_RANGE_IMPL(...) \
91  RAPIDSMPF_GET_MACRO_FUNC(dummy __VA_OPT__(, ) __VA_ARGS__, RAPIDSMPF_NVTX_FUNC_RANGE_IMPL_WITH_VAL, RAPIDSMPF_NVTX_FUNC_RANGE_IMPL_WITHOUT_VAL)( \
92  __VA_ARGS__ \
93  )
94 
117 #define RAPIDSMPF_NVTX_FUNC_RANGE(...) RAPIDSMPF_NVTX_FUNC_RANGE_IMPL(__VA_ARGS__)
118 
119 // implement the scoped range macro with a value
120 #define RAPIDSMPF_NVTX_SCOPED_RANGE_IMPL_WITH_VAL(msg, val) \
121  nvtx3::scoped_range_in<rapidsmpf_domain> RAPIDSMPF_CONCAT( \
122  _rapidsmpf_nvtx_range, __LINE__ \
123  ) { \
124  nvtx3::event_attributes { \
125  RAPIDSMPF_REGISTER_STRING(msg), nvtx3::payload { \
126  rapidsmpf::detail::convert_to_64bit(val) \
127  } \
128  } \
129  }
130 
131 // implement the scoped range macro without a value
132 #define RAPIDSMPF_NVTX_SCOPED_RANGE_IMPL_WITHOUT_VAL(msg) \
133  nvtx3::scoped_range_in<rapidsmpf_domain> RAPIDSMPF_CONCAT( \
134  _rapidsmpf_nvtx_range, __LINE__ \
135  ) { \
136  nvtx3::event_attributes { \
137  RAPIDSMPF_REGISTER_STRING(msg) \
138  } \
139  }
140 
141 // Macro to detect number of arguments (1 or 2)
142 #define RAPIDSMPF_GET_MACRO(_1, _2, NAME, ...) NAME
143 
144 // unwrap the arguments and call the appropriate macro
145 #define RAPIDSMPF_NVTX_SCOPED_RANGE_IMPL(...) \
146  RAPIDSMPF_GET_MACRO( \
147  __VA_ARGS__, \
148  RAPIDSMPF_NVTX_SCOPED_RANGE_IMPL_WITH_VAL, \
149  RAPIDSMPF_NVTX_SCOPED_RANGE_IMPL_WITHOUT_VAL \
150  ) \
151  (__VA_ARGS__)
152 
174 #define RAPIDSMPF_NVTX_SCOPED_RANGE(...) RAPIDSMPF_NVTX_SCOPED_RANGE_IMPL(__VA_ARGS__)
175 
197 #if RAPIDSMPF_VERBOSE_INFO
198 #define RAPIDSMPF_NVTX_SCOPED_RANGE_VERBOSE(...) RAPIDSMPF_NVTX_SCOPED_RANGE(__VA_ARGS__)
199 #else
200 #define RAPIDSMPF_NVTX_SCOPED_RANGE_VERBOSE(...)
201 #endif
202 
203 #define RAPIDSMPF_NVTX_MARKER_IMPL(msg, val) \
204  nvtx3::mark_in<rapidsmpf_domain>(nvtx3::event_attributes{ \
205  RAPIDSMPF_REGISTER_STRING(msg), \
206  nvtx3::payload{rapidsmpf::detail::convert_to_64bit(val)} \
207  })
208 
218 #define RAPIDSMPF_NVTX_MARKER(message, payload) \
219  RAPIDSMPF_NVTX_MARKER_IMPL(message, payload)
220 
231 #if RAPIDSMPF_VERBOSE_INFO
232 #define RAPIDSMPF_NVTX_MARKER_VERBOSE(message, payload) \
233  RAPIDSMPF_NVTX_MARKER_IMPL(message, payload)
234 #else
235 #define RAPIDSMPF_NVTX_MARKER_VERBOSE(message, payload)
236 #endif
requires std::is_integral_v< T > std::int64_t convert_to_64bit(T value)
Convert an integral value to a 64-bit signed integer.
Definition: nvtx.hpp:25
Tag type for rapidsmpf's NVTX domain.
Definition: nvtx.hpp:55
static constexpr char const * name
nvtx domain name
Definition: nvtx.hpp:56