diff --git a/cpp/include/raft/common/detail/nvtx.cuh b/cpp/include/raft/common/detail/nvtx.cuh index 83ba124c92..c4df6d5554 100644 --- a/cpp/include/raft/common/detail/nvtx.cuh +++ b/cpp/include/raft/common/detail/nvtx.cuh @@ -16,42 +16,43 @@ #pragma once -#include -#include -#include #include -#include -#include namespace raft { namespace common { namespace detail { +#ifdef NVTX_ENABLED + +#include +#include +#include +#include +#include +#include + /** * @brief An internal struct to store associated state with the color * generator */ struct ColorGenState { /** collection of all tagged colors generated so far */ - static std::unordered_map allColors; + static inline std::unordered_map allColors; /** mutex for accessing the above map */ - static std::mutex mapMutex; + static inline std::mutex mapMutex; /** saturation */ - static constexpr float S = 0.9f; + static inline constexpr float S = 0.9f; /** value */ - static constexpr float V = 0.85f; + static inline constexpr float V = 0.85f; /** golden ratio */ - static constexpr float Phi = 1.61803f; + static inline constexpr float Phi = 1.61803f; /** inverse golden ratio */ - static constexpr float InvPhi = 1.f / Phi; + static inline constexpr float InvPhi = 1.f / Phi; }; -std::unordered_map ColorGenState::allColors; -std::mutex ColorGenState::mapMutex; - // all h, s, v are in range [0, 1] // Ref: http://en.wikipedia.org/wiki/HSL_and_HSV#Converting_to_RGB -uint32_t hsv2rgb(float h, float s, float v) +inline uint32_t hsv2rgb(float h, float s, float v) { uint32_t out = 0xff000000u; if (s <= 0.0f) { return out; } @@ -117,8 +118,11 @@ uint32_t hsv2rgb(float h, float s, float v) * associate the currently generated color with it * @return returns 32b RGB integer with alpha channel set of 0xff */ -uint32_t generateNextColor(const std::string& tag) +inline uint32_t generateNextColor(const std::string& tag) { + // std::unordered_map ColorGenState::allColors; + // std::mutex ColorGenState::mapMutex; + std::lock_guard guard(ColorGenState::mapMutex); if (!tag.empty()) { auto itr = ColorGenState::allColors.find(tag); @@ -132,13 +136,9 @@ uint32_t generateNextColor(const std::string& tag) return rgb; } -#ifdef NVTX_ENABLED - -#include - -nvtxDomainHandle_t domain = nvtxDomainCreateA("raft"); +static inline nvtxDomainHandle_t domain = nvtxDomainCreateA("raft"); -void pushRange_name(const char* name) +inline void pushRange_name(const char* name) { nvtxEventAttributes_t eventAttrib = {0}; eventAttrib.version = NVTX_VERSION; @@ -151,7 +151,7 @@ void pushRange_name(const char* name) } template -void pushRange(const char* format, Args... args) +inline void pushRange(const char* format, Args... args) { if constexpr (sizeof...(args) > 0) { int length = std::snprintf(nullptr, 0, format, args...); @@ -165,15 +165,15 @@ void pushRange(const char* format, Args... args) } template -void pushRange(rmm::cuda_stream_view stream, const char* format, Args... args) +inline void pushRange(rmm::cuda_stream_view stream, const char* format, Args... args) { stream.synchronize(); pushRange(format, args...); } -void popRange() { nvtxDomainRangePop(domain); } +inline void popRange() { nvtxDomainRangePop(domain); } -void popRange(rmm::cuda_stream_view stream) +inline void popRange(rmm::cuda_stream_view stream) { stream.synchronize(); popRange(); @@ -182,18 +182,18 @@ void popRange(rmm::cuda_stream_view stream) #else // NVTX_ENABLED template -void pushRange(const char* format, Args... args) +inline void pushRange(const char* format, Args... args) { } template -void pushRange(rmm::cuda_stream_view stream, const char* format, Args... args) +inline void pushRange(rmm::cuda_stream_view stream, const char* format, Args... args) { } -void popRange() {} +inline void popRange() {} -void popRange(rmm::cuda_stream_view stream) {} +inline void popRange(rmm::cuda_stream_view stream) {} #endif // NVTX_ENABLED diff --git a/cpp/include/raft/common/nvtx.hpp b/cpp/include/raft/common/nvtx.hpp index a51722152e..8489fd749b 100644 --- a/cpp/include/raft/common/nvtx.hpp +++ b/cpp/include/raft/common/nvtx.hpp @@ -27,7 +27,7 @@ namespace common { * @param args the arguments for the printf-style formatting */ template -void PUSH_RANGE(const char* format, Args... args) +inline void PUSH_RANGE(const char* format, Args... args) { detail::pushRange(format, args...); } @@ -39,19 +39,19 @@ void PUSH_RANGE(const char* format, Args... args) * @param stream stream to synchronize */ template -void PUSH_RANGE(rmm::cuda_stream_view stream, const char* format, Args... args) +inline void PUSH_RANGE(rmm::cuda_stream_view stream, const char* format, Args... args) { detail::pushRange(stream, format, args...); } /** Pop the latest range */ -void POP_RANGE() { detail::popRange(); } +inline void POP_RANGE() { detail::popRange(); } /** * @brief Synchronize CUDA stream and pop the latest nvtx range * @param stream stream to synchronize */ -void POP_RANGE(rmm::cuda_stream_view stream) { detail::popRange(stream); } +inline void POP_RANGE(rmm::cuda_stream_view stream) { detail::popRange(stream); } /** Push a named nvtx range that would be popped at the end of the object lifetime. */ class AUTO_RANGE { diff --git a/cpp/test/distance/distance_base.cuh b/cpp/test/distance/distance_base.cuh index ec9d35bb09..f20f9dd6ba 100644 --- a/cpp/test/distance/distance_base.cuh +++ b/cpp/test/distance/distance_base.cuh @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -410,6 +411,9 @@ class DistanceTest : public ::testing::TestWithParam> { void SetUp() override { + auto testInfo = testing::UnitTest::GetInstance()->current_test_info(); + RAFT_USING_RANGE("test::%s/%s", testInfo->test_suite_name(), testInfo->name()); + raft::random::Rng r(params.seed); int m = params.m; int n = params.n; diff --git a/cpp/test/eigen_solvers.cu b/cpp/test/eigen_solvers.cu index dc7de92eb8..1354124d6a 100644 --- a/cpp/test/eigen_solvers.cu +++ b/cpp/test/eigen_solvers.cu @@ -14,6 +14,7 @@ * limitations under the License. */ +#include #include #include @@ -27,6 +28,7 @@ namespace raft { TEST(Raft, EigenSolvers) { + RAFT_USING_RANGE("test::EigenSolvers"); using namespace matrix; using index_type = int; using value_type = double; @@ -67,6 +69,7 @@ TEST(Raft, EigenSolvers) TEST(Raft, SpectralSolvers) { + RAFT_USING_RANGE("test::SpectralSolvers"); using namespace matrix; using index_type = int; using value_type = double;