-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable tracing of thread pool tasks using NVTX #630
Open
kingcrimsontianyu
wants to merge
9
commits into
rapidsai:branch-25.04
Choose a base branch
from
kingcrimsontianyu:add-nvtx-to-task
base: branch-25.04
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+370
−135
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
085c4b4
Initial implementation
kingcrimsontianyu 90ca1ed
Simplify impl. Enable color coding
kingcrimsontianyu 684cdf0
Revert accidental changes to the test
kingcrimsontianyu 3f34a0d
Add thread renaming. Add comments
kingcrimsontianyu 75fdbf5
Add more comment
kingcrimsontianyu 4efb3cf
Rename vars
kingcrimsontianyu cb0f989
Rename vars and types
kingcrimsontianyu 222992c
Add more comments
kingcrimsontianyu 786d20b
Improve comments
kingcrimsontianyu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
/* | ||
* Copyright (c) 2025, NVIDIA CORPORATION. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
#ifdef KVIKIO_CUDA_FOUND | ||
#include <nvtx3/nvtx3.hpp> | ||
#endif | ||
|
||
#include <kvikio/shim/cuda.hpp> | ||
#include <kvikio/utils.hpp> | ||
|
||
namespace kvikio { | ||
|
||
#ifdef KVIKIO_CUDA_FOUND | ||
/** | ||
* @brief Tag type for libkvikio's NVTX domain. | ||
*/ | ||
struct libkvikio_domain { | ||
static constexpr char const* name{"libkvikio"}; | ||
}; | ||
|
||
using nvtx_scoped_range_type = nvtx3::scoped_range_in<libkvikio_domain>; | ||
using nvtx_registered_string_type = nvtx3::registered_string_in<libkvikio_domain>; | ||
|
||
// Macro to concatenate two tokens x and y. | ||
#define KVIKIO_CONCAT_HELPER(x, y) x##y | ||
#define KVIKIO_CONCAT(x, y) KVIKIO_CONCAT_HELPER(x, y) | ||
|
||
// Macro to create a static, registered string that will not have a name conflict with any | ||
// registered string defined in the same scope. | ||
#define KVIKIO_REGISTER_STRING(message) \ | ||
[](const char* a_message) -> auto& { \ | ||
static kvikio::nvtx_registered_string_type a_reg_str{a_message}; \ | ||
return a_reg_str; \ | ||
}(message) | ||
|
||
// Implementation of KVIKIO_NVTX_FUNC_RANGE() | ||
#define KVIKIO_NVTX_FUNC_RANGE_IMPL() NVTX3_FUNC_RANGE_IN(kvikio::libkvikio_domain) | ||
|
||
// Implementation of KVIKIO_NVTX_SCOPED_RANGE(...) | ||
#define KVIKIO_NVTX_SCOPED_RANGE_IMPL_3(message, payload_v, color) \ | ||
kvikio::nvtx_scoped_range_type KVIKIO_CONCAT(_kvikio_nvtx_range, __LINE__) \ | ||
{ \ | ||
nvtx3::event_attributes \ | ||
{ \ | ||
KVIKIO_REGISTER_STRING(message), nvtx3::payload{kvikio::convert_to_64bit(payload_v)}, color \ | ||
} \ | ||
} | ||
#define KVIKIO_NVTX_SCOPED_RANGE_IMPL_2(message, payload) \ | ||
KVIKIO_NVTX_SCOPED_RANGE_IMPL_3(message, payload, kvikio::nvtx_manager::default_color()) | ||
#define KVIKIO_NVTX_SCOPED_RANGE_SELECTOR(_1, _2, _3, NAME, ...) NAME | ||
#define KVIKIO_NVTX_SCOPED_RANGE_IMPL(...) \ | ||
KVIKIO_NVTX_SCOPED_RANGE_SELECTOR( \ | ||
__VA_ARGS__, KVIKIO_NVTX_SCOPED_RANGE_IMPL_3, KVIKIO_NVTX_SCOPED_RANGE_IMPL_2) \ | ||
(__VA_ARGS__) | ||
|
||
// Implementation of KVIKIO_NVTX_MARKER(message, payload) | ||
#define KVIKIO_NVTX_MARKER_IMPL(message, payload_v) \ | ||
nvtx3::mark_in<kvikio::libkvikio_domain>(nvtx3::event_attributes{ \ | ||
KVIKIO_REGISTER_STRING(message), nvtx3::payload{kvikio::convert_to_64bit(payload_v)}}) | ||
|
||
#endif | ||
|
||
#ifdef KVIKIO_CUDA_FOUND | ||
using nvtx_color_type = nvtx3::color; | ||
#else | ||
using nvtx_color_type = int; | ||
#endif | ||
|
||
/** | ||
* @brief Utility singleton class for NVTX annotation. | ||
*/ | ||
class nvtx_manager { | ||
public: | ||
static nvtx_manager& instance() noexcept; | ||
|
||
/** | ||
* @brief Return the default color. | ||
* | ||
* @return Default color. | ||
*/ | ||
static const nvtx_color_type& default_color() noexcept; | ||
|
||
/** | ||
* @brief Return the color at the given index from the internal color palette whose size n is a | ||
* power of 2. The index may exceed the size of the color palette, in which case it wraps around, | ||
* i.e. (idx mod n). | ||
* | ||
* @param idx The index value. | ||
* @return The color picked from the internal color palette. | ||
*/ | ||
static const nvtx_color_type& get_color_by_index(std::uint64_t idx) noexcept; | ||
|
||
/** | ||
* @brief Rename the current thread under the KvikIO NVTX domain. | ||
* | ||
* @note This NVTX feature is currently not supported by the Nsight System profiler. As a result, | ||
* the OS thread will not be renamed in the nsys-ui. | ||
*/ | ||
static void rename_current_thread(std::string_view new_name) noexcept; | ||
|
||
nvtx_manager(nvtx_manager const&) = delete; | ||
nvtx_manager& operator=(nvtx_manager const&) = delete; | ||
nvtx_manager(nvtx_manager&&) = delete; | ||
nvtx_manager& operator=(nvtx_manager&&) = delete; | ||
|
||
private: | ||
nvtx_manager() = default; | ||
}; | ||
|
||
/** | ||
* @brief Convenience macro for generating an NVTX range in the `libkvikio` domain | ||
* from the lifetime of a function. | ||
* | ||
* Takes no argument. The name of the immediately enclosing function returned by `__func__` is used | ||
* as the message. | ||
* | ||
* Example: | ||
* ``` | ||
* void some_function(){ | ||
* KVIKIO_NVTX_FUNC_RANGE(); // The name `some_function` is used as the message | ||
* ... | ||
* } | ||
* ``` | ||
*/ | ||
#ifdef KVIKIO_CUDA_FOUND | ||
#define KVIKIO_NVTX_FUNC_RANGE() KVIKIO_NVTX_FUNC_RANGE_IMPL() | ||
#else | ||
#define KVIKIO_NVTX_FUNC_RANGE(...) \ | ||
do { \ | ||
} while (0) | ||
#endif | ||
|
||
/** | ||
* @brief Convenience macro for generating an NVTX scoped range in the `libkvikio` domain to | ||
* annotate a time duration. | ||
* | ||
* @param message String literal for NVTX annotation. To improve profile-time performance, the | ||
* string literal is registered in NVTX. | ||
* @param payload NVTX payload. | ||
* @param color (Optional) NVTX color. If unspecified, a default NVTX color is used. | ||
* | ||
* Example: | ||
* ``` | ||
* void some_function(){ | ||
* KVIKIO_NVTX_SCOPED_RANGE("my function", 42); | ||
* ... | ||
* } | ||
* ``` | ||
*/ | ||
#ifdef KVIKIO_CUDA_FOUND | ||
#define KVIKIO_NVTX_SCOPED_RANGE(...) KVIKIO_NVTX_SCOPED_RANGE_IMPL(__VA_ARGS__) | ||
#else | ||
#define KVIKIO_NVTX_SCOPED_RANGE(message, payload, ...) \ | ||
do { \ | ||
} while (0) | ||
#endif | ||
|
||
/** | ||
* @brief Convenience macro for generating an NVTX marker in the `libkvikio` domain to annotate a | ||
* certain time point. | ||
* | ||
* @param message String literal for NVTX annotation. To improve profile-time performance, the | ||
* string literal is registered in NVTX. | ||
* @param payload NVTX payload. | ||
* | ||
* Example: | ||
* ``` | ||
* std::future<void> some_function(){ | ||
* size_t io_size{2077}; | ||
* KVIKIO_NVTX_MARKER("I/O operation", io_size); | ||
* perform_async_io_operation(io_size); | ||
* ... | ||
* } | ||
* ``` | ||
*/ | ||
#ifdef KVIKIO_CUDA_FOUND | ||
#define KVIKIO_NVTX_MARKER(message, payload) KVIKIO_NVTX_MARKER_IMPL(message, payload) | ||
#else | ||
#define KVIKIO_NVTX_MARKER(message, payload) \ | ||
do { \ | ||
} while (0) | ||
#endif | ||
|
||
} // namespace kvikio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why the variable has to be named
payload_v
. Otherwisepayload
would cause compile errors. Perhaps a name look-up related issue.