All Classes Namespaces Functions Enumerations Enumerator Modules Pages
nvtx.hpp
1 /*
2  * Copyright (c) 2025, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <cstdint>
19 
20 #ifdef KVIKIO_CUDA_FOUND
21 #include <nvtx3/nvtx3.hpp>
22 #endif
23 
24 #include <kvikio/shim/cuda.hpp>
25 #include <kvikio/utils.hpp>
26 
27 namespace kvikio {
28 
29 #ifdef KVIKIO_CUDA_FOUND
33 struct libkvikio_domain {
34  static constexpr char const* name{"libkvikio"};
35 };
36 
37 using nvtx_scoped_range_type = nvtx3::scoped_range_in<libkvikio_domain>;
38 using nvtx_registered_string_type = nvtx3::registered_string_in<libkvikio_domain>;
39 
40 // Macro to concatenate two tokens x and y.
41 #define KVIKIO_CONCAT_HELPER(x, y) x##y
42 #define KVIKIO_CONCAT(x, y) KVIKIO_CONCAT_HELPER(x, y)
43 
44 // Macro to create a static, registered string that will not have a name conflict with any
45 // registered string defined in the same scope.
46 #define KVIKIO_REGISTER_STRING(message) \
47  [](const char* a_message) -> auto& { \
48  static kvikio::nvtx_registered_string_type a_reg_str{a_message}; \
49  return a_reg_str; \
50  }(message)
51 
52 // Implementation of KVIKIO_NVTX_FUNC_RANGE()
53 #define KVIKIO_NVTX_FUNC_RANGE_IMPL() NVTX3_FUNC_RANGE_IN(kvikio::libkvikio_domain)
54 
55 // Implementation of KVIKIO_NVTX_SCOPED_RANGE(...)
56 #define KVIKIO_NVTX_SCOPED_RANGE_IMPL_3(message, payload_v, color) \
57  kvikio::nvtx_scoped_range_type KVIKIO_CONCAT(_kvikio_nvtx_range, __LINE__) \
58  { \
59  nvtx3::event_attributes \
60  { \
61  KVIKIO_REGISTER_STRING(message), nvtx3::payload{kvikio::convert_to_64bit(payload_v)}, color \
62  } \
63  }
64 #define KVIKIO_NVTX_SCOPED_RANGE_IMPL_2(message, payload) \
65  KVIKIO_NVTX_SCOPED_RANGE_IMPL_3(message, payload, kvikio::NvtxManager::default_color())
66 #define KVIKIO_NVTX_SCOPED_RANGE_SELECTOR(_1, _2, _3, NAME, ...) NAME
67 #define KVIKIO_NVTX_SCOPED_RANGE_IMPL(...) \
68  KVIKIO_NVTX_SCOPED_RANGE_SELECTOR( \
69  __VA_ARGS__, KVIKIO_NVTX_SCOPED_RANGE_IMPL_3, KVIKIO_NVTX_SCOPED_RANGE_IMPL_2) \
70  (__VA_ARGS__)
71 
72 // Implementation of KVIKIO_NVTX_MARKER(message, payload)
73 #define KVIKIO_NVTX_MARKER_IMPL(message, payload_v) \
74  nvtx3::mark_in<kvikio::libkvikio_domain>(nvtx3::event_attributes{ \
75  KVIKIO_REGISTER_STRING(message), nvtx3::payload{kvikio::convert_to_64bit(payload_v)}})
76 
77 #endif
78 
79 #ifdef KVIKIO_CUDA_FOUND
80 using nvtx_color_type = nvtx3::color;
81 #else
82 using nvtx_color_type = int;
83 #endif
84 
88 class NvtxManager {
89  public:
90  static NvtxManager& instance() noexcept;
91 
97  static const nvtx_color_type& default_color() noexcept;
98 
107  static const nvtx_color_type& get_color_by_index(std::uint64_t idx) noexcept;
108 
115  static void rename_current_thread(std::string_view new_name) noexcept;
116 
117  NvtxManager(NvtxManager const&) = delete;
118  NvtxManager& operator=(NvtxManager const&) = delete;
119  NvtxManager(NvtxManager&&) = delete;
120  NvtxManager& operator=(NvtxManager&&) = delete;
121 
122  private:
123  NvtxManager() = default;
124 };
125 
141 #ifdef KVIKIO_CUDA_FOUND
142 #define KVIKIO_NVTX_FUNC_RANGE() KVIKIO_NVTX_FUNC_RANGE_IMPL()
143 #else
144 #define KVIKIO_NVTX_FUNC_RANGE(...) \
145  do { \
146  } while (0)
147 #endif
148 
166 #ifdef KVIKIO_CUDA_FOUND
167 #define KVIKIO_NVTX_SCOPED_RANGE(...) KVIKIO_NVTX_SCOPED_RANGE_IMPL(__VA_ARGS__)
168 #else
169 #define KVIKIO_NVTX_SCOPED_RANGE(message, payload, ...) \
170  do { \
171  } while (0)
172 #endif
173 
192 #ifdef KVIKIO_CUDA_FOUND
193 #define KVIKIO_NVTX_MARKER(message, payload) KVIKIO_NVTX_MARKER_IMPL(message, payload)
194 #else
195 #define KVIKIO_NVTX_MARKER(message, payload) \
196  do { \
197  } while (0)
198 #endif
199 
200 } // namespace kvikio
Utility singleton class for NVTX annotation.
Definition: nvtx.hpp:88
static void rename_current_thread(std::string_view new_name) noexcept
Rename the current thread under the KvikIO NVTX domain.
static const nvtx_color_type & get_color_by_index(std::uint64_t idx) noexcept
Return the color at the given index from the internal color palette whose size n is a power of 2....
static const nvtx_color_type & default_color() noexcept
Return the default color.
KvikIO namespace.
Definition: batch.hpp:27