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

[ENH] [5/5] Header structure: isolate logger and memory pool #1441

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d9801e8
MV: add -inl suffix to header paths
ahendriksen Apr 20, 2023
50b374d
MV: raft_runtime src files
ahendriksen Apr 13, 2023
8974ae3
FIX: add missing includes
ahendriksen Apr 20, 2023
95ef31b
FIX: getWorkspaceSize
ahendriksen Apr 20, 2023
7edcb6a
PREP: Separate rbf_fin_op
ahendriksen Apr 20, 2023
71de7bd
PREP: registers: Add _types header
ahendriksen Apr 20, 2023
541cabc
Change RAFT_COMPILED from INTERFACE to PUBLIC
ahendriksen Apr 20, 2023
d81b14e
Define RAFT_EXPLICIT and RAFT_EXPLICIT_INSTANTIATE_ONLY
ahendriksen Apr 20, 2023
48ea769
Update docs
ahendriksen Apr 20, 2023
e6bb5d5
Replace specializations by split headers
ahendriksen Apr 20, 2023
ff79abf
Deprecate specialization headers
ahendriksen Apr 20, 2023
c9e7413
Add interleaved scan instances
ahendriksen Apr 20, 2023
0c889dc
Separate fused_l2_nn_helpers
ahendriksen Apr 20, 2023
f97b2a8
Remove pairwise_matrix_instantiation_point
ahendriksen Apr 20, 2023
fb637f7
Rename specialization => instantiation
ahendriksen Apr 20, 2023
7b065af
test/neighbors/selection.cu: Expose kFaissMaxK
ahendriksen Apr 20, 2023
d5b5673
Remove includes of specialization headers
ahendriksen Apr 20, 2023
94d8117
test/distance/dist_adj.cu: Add instance
ahendriksen Apr 20, 2023
361570b
test/cluster/linkage.cu: Allow instance
ahendriksen Apr 20, 2023
5171de3
test/sparse/neighbors/connect_components.cu: Allow instance
ahendriksen Apr 20, 2023
4426c50
test/neighbors/ann_ivf_pq/test_float_uint32_t.cu: Allow instance
ahendriksen Apr 20, 2023
e527efc
test/matrix/select_k.cu: Change index type
ahendriksen Apr 20, 2023
17902e9
test/neighbors/fused_l2_knn.cu: Change index type
ahendriksen Apr 20, 2023
976189b
Force explicit instantiations in tests
ahendriksen Apr 20, 2023
bdae61d
Force explicit instantiations in benchmarks
ahendriksen Apr 20, 2023
b0b8fe5
Test that headers are free standing
ahendriksen Apr 20, 2023
7d1aa77
Split ivf-pq and ivf-flat search
ahendriksen Apr 20, 2023
6109c12
Isolate logger and memory pool
ahendriksen Apr 20, 2023
273e371
Add documentation to RAFT_INLINE_CONDITIONAL
ahendriksen Apr 26, 2023
e2b5c18
logger: Do not include cudart_utils.hpp
ahendriksen Apr 27, 2023
22bade9
Move RAFT_INLINE_CONDITIONAL to macros.hpp
ahendriksen Apr 27, 2023
6a0be4c
Define RAFT_WEAK_FUNCTION
ahendriksen Apr 27, 2023
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
2 changes: 2 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ set_target_properties(raft_compiled PROPERTIES EXPORT_NAME compiled)
if(RAFT_COMPILE_LIBRARY)
add_library(
raft_lib
src/core/logger.cpp
src/distance/detail/pairwise_matrix/dispatch_canberra_double_double_double_int.cu
src/distance/detail/pairwise_matrix/dispatch_canberra_float_float_float_int.cu
src/distance/detail/pairwise_matrix/dispatch_correlation_double_double_double_int.cu
Expand Down Expand Up @@ -390,6 +391,7 @@ if(RAFT_COMPILE_LIBRARY)
src/spatial/knn/detail/fused_l2_knn_int32_t_float.cu
src/spatial/knn/detail/fused_l2_knn_int64_t_float.cu
src/spatial/knn/detail/fused_l2_knn_uint32_t_float.cu
src/util/memory_pool.cpp
)
set_target_properties(
raft_lib
Expand Down
128 changes: 128 additions & 0 deletions cpp/include/raft/core/logger-ext.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright (c) 2022-2023, 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 <memory> // std::unique_ptr
#include <string> // std::string
#include <unordered_map> // std::unordered_map

namespace raft {

static const std::string RAFT_NAME = "raft";
static const std::string default_log_pattern("[%L] [%H:%M:%S.%f] %v");

/**
* @brief The main Logging class for raft library.
*
* This class acts as a thin wrapper over the underlying `spdlog` interface. The
* design is done in this way in order to avoid us having to also ship `spdlog`
* header files in our installation.
*
* @todo This currently only supports logging to stdout. Need to add support in
* future to add custom loggers as well [Issue #2046]
*/
class logger {
public:
// @todo setting the logger once per process with
logger(std::string const& name_ = "");
/**
* @brief Singleton method to get the underlying logger object
*
* @return the singleton logger object
*/
static logger& get(std::string const& name = "");

/**
* @brief Set the logging level.
*
* Only messages with level equal or above this will be printed
*
* @param[in] level logging level
*
* @note The log level will actually be set only if the input is within the
* range [RAFT_LEVEL_TRACE, RAFT_LEVEL_OFF]. If it is not, then it'll
* be ignored. See documentation of decisiontree for how this gets used
*/
void set_level(int level);

/**
* @brief Set the logging pattern
*
* @param[in] pattern the pattern to be set. Refer this link
* https://github.com/gabime/spdlog/wiki/3.-Custom-formatting
* to know the right syntax of this pattern
*/
void set_pattern(const std::string& pattern);

/**
* @brief Register a callback function to be run in place of usual log call
*
* @param[in] callback the function to be run on all logged messages
*/
void set_callback(void (*callback)(int lvl, const char* msg));

/**
* @brief Register a flush function compatible with the registered callback
*
* @param[in] flush the function to use when flushing logs
*/
void set_flush(void (*flush)());

/**
* @brief Tells whether messages will be logged for the given log level
*
* @param[in] level log level to be checked for
* @return true if messages will be logged for this level, else false
*/
bool should_log_for(int level) const;
/**
* @brief Query for the current log level
*
* @return the current log level
*/
int get_level() const;

/**
* @brief Get the current logging pattern
* @return the pattern
*/
std::string get_pattern() const;

/**
* @brief Main logging method
*
* @param[in] level logging level of this message
* @param[in] fmt C-like format string, followed by respective params
*/
void log(int level, const char* fmt, ...);

/**
* @brief Flush logs by calling flush on underlying logger
*/
void flush();

~logger();

private:
logger();
// pimpl pattern:
// https://learn.microsoft.com/en-us/cpp/cpp/pimpl-for-compile-time-encapsulation-modern-cpp?view=msvc-170
class impl;
std::unique_ptr<impl> pimpl;
static inline std::unordered_map<std::string, std::shared_ptr<raft::logger>> log_map;
}; // class logger

}; // namespace raft
156 changes: 156 additions & 0 deletions cpp/include/raft/core/logger-inl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Copyright (c) 2022-2023, 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 <stdarg.h>

#include <algorithm>

#include <memory>
#include <mutex>
#include <sstream>
#include <string>
#include <unordered_map>

#include <stdarg.h>

#include "logger-macros.hpp"
// The logger-ext.hpp file contains the class declaration of the logger class.
// In this case, it is okay to include the logger-ext.hpp file because it
// contains no RAFT_EXPLICIT template instantiations.
#include "logger-ext.hpp"

#define SPDLOG_HEADER_ONLY
#include <raft/core/detail/callback_sink.hpp>
#include <raft/util/cudart_utils.hpp>
ahendriksen marked this conversation as resolved.
Show resolved Hide resolved
#include <raft/util/inline.hpp> // RAFT_INLINE_CONDITIONAL
#include <spdlog/sinks/stdout_color_sinks.h> // NOLINT
#include <spdlog/spdlog.h> // NOLINT

namespace raft {

namespace detail {

inline std::string format(const char* fmt, va_list& vl)
{
va_list vl_copy;
va_copy(vl_copy, vl);
int length = std::vsnprintf(nullptr, 0, fmt, vl_copy);
assert(length >= 0);
std::vector<char> buf(length + 1);
std::vsnprintf(buf.data(), length + 1, fmt, vl);
return std::string(buf.data());
}

inline std::string format(const char* fmt, ...)
{
va_list vl;
va_start(vl, fmt);
std::string str = format(fmt, vl);
va_end(vl);
return str;
}

inline int convert_level_to_spdlog(int level)
{
level = std::max(RAFT_LEVEL_OFF, std::min(RAFT_LEVEL_TRACE, level));
return RAFT_LEVEL_TRACE - level;
}

} // namespace detail

class logger::impl { // defined privately here
// ... all private data and functions: all of these
// can now change without recompiling callers ...
public:
std::shared_ptr<spdlog::sinks::callback_sink_mt> sink;
std::shared_ptr<spdlog::logger> spdlogger;
std::string cur_pattern;
int cur_level;

impl(std::string const& name_ = "")
: sink{std::make_shared<spdlog::sinks::callback_sink_mt>()},
spdlogger{std::make_shared<spdlog::logger>(name_, sink)},
cur_pattern()
{
}
}; // class logger::impl

RAFT_INLINE_CONDITIONAL logger::logger(std::string const& name_) : pimpl(new impl(name_))
{
set_pattern(default_log_pattern);
set_level(RAFT_ACTIVE_LEVEL);
}

RAFT_INLINE_CONDITIONAL logger& logger::get(std::string const& name)
{
if (log_map.find(name) == log_map.end()) { log_map[name] = std::make_shared<raft::logger>(name); }
return *log_map[name];
}

RAFT_INLINE_CONDITIONAL void logger::set_level(int level)
{
level = raft::detail::convert_level_to_spdlog(level);
pimpl->spdlogger->set_level(static_cast<spdlog::level::level_enum>(level));
}

RAFT_INLINE_CONDITIONAL void logger::set_pattern(const std::string& pattern)
{
pimpl->cur_pattern = pattern;
pimpl->spdlogger->set_pattern(pattern);
}

RAFT_INLINE_CONDITIONAL void logger::set_callback(void (*callback)(int lvl, const char* msg))
{
pimpl->sink->set_callback(callback);
}

RAFT_INLINE_CONDITIONAL void logger::set_flush(void (*flush)()) { pimpl->sink->set_flush(flush); }

RAFT_INLINE_CONDITIONAL bool logger::should_log_for(int level) const
{
level = raft::detail::convert_level_to_spdlog(level);
auto level_e = static_cast<spdlog::level::level_enum>(level);
return pimpl->spdlogger->should_log(level_e);
}

RAFT_INLINE_CONDITIONAL int logger::get_level() const
{
auto level_e = pimpl->spdlogger->level();
return RAFT_LEVEL_TRACE - static_cast<int>(level_e);
}

RAFT_INLINE_CONDITIONAL std::string logger::get_pattern() const { return pimpl->cur_pattern; }

RAFT_INLINE_CONDITIONAL void logger::log(int level, const char* fmt, ...)
{
level = raft::detail::convert_level_to_spdlog(level);
auto level_e = static_cast<spdlog::level::level_enum>(level);
// explicit check to make sure that we only expand messages when required
if (pimpl->spdlogger->should_log(level_e)) {
va_list vl;
va_start(vl, fmt);
auto msg = raft::detail::format(fmt, vl);
va_end(vl);
pimpl->spdlogger->log(level_e, msg);
}
}

RAFT_INLINE_CONDITIONAL void logger::flush() { pimpl->spdlogger->flush(); }

RAFT_INLINE_CONDITIONAL logger::~logger() {}

}; // namespace raft
Loading