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] [FINAL] Header structure: combine all PRs into one #1469

Merged
Merged
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
4dc403e
MV: add -inl suffix to header paths
ahendriksen Apr 20, 2023
6947ed2
MV: raft_runtime src files
ahendriksen Apr 13, 2023
0178c39
FIX: add missing includes
ahendriksen Apr 20, 2023
f04a2ea
FIX: getWorkspaceSize
ahendriksen Apr 20, 2023
c5509a7
PREP: Separate rbf_fin_op
ahendriksen Apr 20, 2023
d7e7479
PREP: registers: Add _types header
ahendriksen Apr 20, 2023
8d3f015
Change RAFT_COMPILED from INTERFACE to PUBLIC
ahendriksen Apr 20, 2023
e0ef4ba
Define RAFT_EXPLICIT and RAFT_EXPLICIT_INSTANTIATE_ONLY
ahendriksen Apr 20, 2023
8d4f526
Update docs
ahendriksen Apr 20, 2023
77a8bfe
Replace specializations by split headers
ahendriksen Apr 20, 2023
dbbbcef
Deprecate specialization headers
ahendriksen Apr 20, 2023
3845cd9
Add interleaved scan instances
ahendriksen Apr 20, 2023
10180b8
Separate fused_l2_nn_helpers
ahendriksen Apr 20, 2023
8dd3861
Remove pairwise_matrix_instantiation_point
ahendriksen Apr 20, 2023
d2ef3d6
Rename specialization => instantiation
ahendriksen Apr 20, 2023
c3a72b6
test/neighbors/selection.cu: Expose kFaissMaxK
ahendriksen Apr 20, 2023
8021304
Update docs/source/developer_guide.md
ahendriksen Apr 27, 2023
5bdc435
Update docs/source/developer_guide.md
ahendriksen Apr 27, 2023
e1c67d7
Update docs/source/using_libraft.md
ahendriksen Apr 27, 2023
4235ae0
Update docs/source/using_libraft.md
ahendriksen Apr 27, 2023
eca9128
dev guide: Point out to document correct header
ahendriksen Apr 27, 2023
7af7711
Replace !defined by ifndef for consistency
ahendriksen Apr 27, 2023
8174472
Remove includes of specialization headers
ahendriksen Apr 20, 2023
85e9109
test/distance/dist_adj.cu: Add instance
ahendriksen Apr 20, 2023
27c50fe
test/cluster/linkage.cu: Allow instance
ahendriksen Apr 20, 2023
d3dd7f0
test/sparse/neighbors/connect_components.cu: Allow instance
ahendriksen Apr 20, 2023
117f66e
test/neighbors/ann_ivf_pq/test_float_uint32_t.cu: Allow instance
ahendriksen Apr 20, 2023
e50ebd0
test/matrix/select_k.cu: Change index type
ahendriksen Apr 20, 2023
a65a8c8
test/neighbors/fused_l2_knn.cu: Change index type
ahendriksen Apr 20, 2023
7a4ffea
Force explicit instantiations in tests
ahendriksen Apr 27, 2023
12063c6
Force explicit instantiations in benchmarks
ahendriksen Apr 20, 2023
ca56e56
Test that headers are free standing
ahendriksen Apr 20, 2023
7d338b4
Update cpp/test/matrix/select_k.cu
ahendriksen Apr 27, 2023
3f55667
Split ivf-pq and ivf-flat search
ahendriksen Apr 20, 2023
522bd64
Make ivf_flat_search RAFT_EXPLICIT
ahendriksen Apr 26, 2023
9038e29
Isolate logger and memory pool
ahendriksen Apr 20, 2023
bc1e512
Add documentation to RAFT_INLINE_CONDITIONAL
ahendriksen Apr 26, 2023
2937ad6
logger: Do not include cudart_utils.hpp
ahendriksen Apr 27, 2023
7cdef04
Move RAFT_INLINE_CONDITIONAL to macros.hpp
ahendriksen Apr 27, 2023
0368286
Define RAFT_WEAK_FUNCTION
ahendriksen Apr 27, 2023
fd8386c
Run clang-format
ahendriksen Apr 27, 2023
6d8ec7b
cub_wrapper: Fix uninitialized worksize
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
Prev Previous commit
Next Next commit
Isolate logger and memory pool
These two include files are likely to transitively include spdlog, which
increases compilation times.
ahendriksen committed Apr 27, 2023
commit 9038e29925c3d1364301ea5ff04cf48d2f98e6d6
2 changes: 2 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
@@ -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
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>
#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