Skip to content
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

Remove the shared state and the mutex from NVTX internals #2310

Merged
merged 2 commits into from
May 15, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 10 additions & 22 deletions cpp/include/raft/core/detail/nvtx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,19 @@

#include <cstdint>
#include <cstdlib>
#include <mutex>
#include <limits>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <vector>

namespace raft::common::nvtx::detail {

/**
* @brief An internal struct to store associated state with the color
* generator
* @brief An internal struct to to initialize the color generator
*/
struct color_gen_state {
/** collection of all tagged colors generated so far */
static inline std::unordered_map<std::string, uint32_t> all_colors_;
/** mutex for accessing the above map */
static inline std::mutex map_mutex_;
struct color_gen {
/** This determines how many bits of the hash to use for the generator */
using hash_type = uint16_t;
/** saturation */
static inline constexpr float kS = 0.9f;
/** value */
Expand Down Expand Up @@ -121,20 +117,12 @@ inline auto hsv2rgb(float h, float s, float v) -> uint32_t
*/
achirkin marked this conversation as resolved.
Show resolved Hide resolved
inline auto generate_next_color(const std::string& tag) -> uint32_t
{
// std::unordered_map<std::string, uint32_t> color_gen_state::all_colors_;
// std::mutex color_gen_state::map_mutex_;

std::lock_guard<std::mutex> guard(color_gen_state::map_mutex_);
if (!tag.empty()) {
auto itr = color_gen_state::all_colors_.find(tag);
if (itr != color_gen_state::all_colors_.end()) { return itr->second; }
}
auto h = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
h += color_gen_state::kInvPhi;
auto x = static_cast<color_gen::hash_type>(std::hash<std::string>{}(tag));
auto u = std::numeric_limits<color_gen::hash_type>::max();
auto h = static_cast<float>(x) / static_cast<float>(u);
h += color_gen::kInvPhi;
if (h >= 1.f) h -= 1.f;
auto rgb = hsv2rgb(h, color_gen_state::kS, color_gen_state::kV);
if (!tag.empty()) { color_gen_state::all_colors_[tag] = rgb; }
return rgb;
return hsv2rgb(h, color_gen::kS, color_gen::kV);
}

template <typename Domain, typename = Domain>
Expand Down
Loading