Skip to content

Commit

Permalink
Make sure the helpers can be used in multiple tranlsation units.
Browse files Browse the repository at this point in the history
  • Loading branch information
achirkin committed Dec 10, 2021
1 parent 6c3cdc9 commit 76af545
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 34 deletions.
60 changes: 30 additions & 30 deletions cpp/include/raft/common/detail/nvtx.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,43 @@

#pragma once

#include <stdint.h>
#include <stdlib.h>
#include <mutex>
#include <rmm/cuda_stream_view.hpp>
#include <string>
#include <unordered_map>

namespace raft {
namespace common {
namespace detail {

#ifdef NVTX_ENABLED

#include <nvToolsExt.h>
#include <stdint.h>
#include <stdlib.h>
#include <mutex>
#include <string>
#include <unordered_map>

/**
* @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<std::string, uint32_t> allColors;
static inline std::unordered_map<std::string, uint32_t> 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<std::string, uint32_t> 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; }
Expand Down Expand Up @@ -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<std::string, uint32_t> ColorGenState::allColors;
// std::mutex ColorGenState::mapMutex;

std::lock_guard<std::mutex> guard(ColorGenState::mapMutex);
if (!tag.empty()) {
auto itr = ColorGenState::allColors.find(tag);
Expand All @@ -132,13 +136,9 @@ uint32_t generateNextColor(const std::string& tag)
return rgb;
}

#ifdef NVTX_ENABLED

#include <nvToolsExt.h>

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;
Expand All @@ -151,7 +151,7 @@ void pushRange_name(const char* name)
}

template <typename... Args>
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...);
Expand All @@ -165,15 +165,15 @@ void pushRange(const char* format, Args... args)
}

template <typename... Args>
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();
Expand All @@ -182,18 +182,18 @@ void popRange(rmm::cuda_stream_view stream)
#else // NVTX_ENABLED

template <typename... Args>
void pushRange(const char* format, Args... args)
inline void pushRange(const char* format, Args... args)
{
}

template <typename... Args>
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

Expand Down
8 changes: 4 additions & 4 deletions cpp/include/raft/common/nvtx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace common {
* @param args the arguments for the printf-style formatting
*/
template <typename... Args>
void PUSH_RANGE(const char* format, Args... args)
inline void PUSH_RANGE(const char* format, Args... args)
{
detail::pushRange(format, args...);
}
Expand All @@ -39,19 +39,19 @@ void PUSH_RANGE(const char* format, Args... args)
* @param stream stream to synchronize
*/
template <typename... Args>
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 {
Expand Down
4 changes: 4 additions & 0 deletions cpp/test/distance/distance_base.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <gtest/gtest.h>
#include <raft/cudart_utils.h>
#include <raft/common/nvtx.hpp>
#include <raft/cuda_utils.cuh>
#include <raft/distance/distance.hpp>
#include <raft/random/rng.hpp>
Expand Down Expand Up @@ -410,6 +411,9 @@ class DistanceTest : public ::testing::TestWithParam<DistanceInputs<DataType>> {

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;
Expand Down
3 changes: 3 additions & 0 deletions cpp/test/eigen_solvers.cu
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

#include <raft/common/nvtx.hpp>
#include <raft/handle.hpp>
#include <raft/spectral/partition.hpp>

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 76af545

Please sign in to comment.