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 all explicit usage of fmtlib #1724

Merged
merged 23 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6066861
Remove all explicit usage of fmtlib
harrism Nov 7, 2024
fbbb1b5
Use detail::logger() in RMM_LOGGING_ASSERT to eliminate deprecation w…
harrism Nov 7, 2024
966f706
Remove temporary try/catch from debugging.
harrism Nov 7, 2024
6e31e47
\n @ EOF
harrism Nov 7, 2024
f00beb7
copyright
harrism Nov 7, 2024
92f179f
Added `formatted_log` utility and switch to printf-style placeholders…
harrism Nov 11, 2024
cfcdf4c
Copyright
harrism Nov 11, 2024
708ffba
Improve typing and docs in formatted_log
harrism Nov 12, 2024
99f4150
Remove unnecessary temporary creation in emplace_back
harrism Nov 12, 2024
7bde8d1
Replace all uses of %d with more appropriate formats
harrism Nov 13, 2024
0df63a7
Fix error in format_bytes, and make format_stream output hex
harrism Nov 13, 2024
e6f8be7
use `c_str()` on all calls to `format_*` passed to %s
harrism Nov 13, 2024
60a8043
Remove deprecated use of logger() in replay bench
harrism Nov 13, 2024
0de97c5
Remove explicit fetching and linking of fmt in cmake
harrism Nov 13, 2024
4f18c40
Style
harrism Nov 13, 2024
58e8a55
Merge branch 'branch-24.12' into fea-remove-explicit-fmt
harrism Nov 13, 2024
89fceea
Specialize formatted_log for string without arguments (fix potential …
harrism Nov 13, 2024
5330063
Update intersphinx_mapping for new cuda.py docs
harrism Nov 13, 2024
00d66a5
style
harrism Nov 13, 2024
34fa430
Fix potential for warnings
harrism Nov 13, 2024
6f18ccc
Merge branch 'branch-24.12' into fea-remove-explicit-fmt
harrism Nov 13, 2024
fa9dbc3
Automatically support std::string arguments to formatted_log
harrism Nov 14, 2024
702cbc6
Remove unnecessary added include
harrism Nov 14, 2024
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
4 changes: 2 additions & 2 deletions benchmarks/utilities/log_parser.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2021, NVIDIA CORPORATION.
* Copyright (c) 2020-2024, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -151,7 +151,7 @@ inline std::vector<event> parse_csv(std::string const& filename)

auto parse_pointer = [](std::string const& str, uintptr_t& ptr) {
auto const base{16};
ptr = std::stoll(str, nullptr, base);
ptr = (str == "(nil)") ? 0 : std::stoll(str, nullptr, base);
vyasr marked this conversation as resolved.
Show resolved Hide resolved
};

std::vector<uintptr_t> pointers = csv.GetColumn<uintptr_t>("Pointer", parse_pointer);
Expand Down
75 changes: 75 additions & 0 deletions include/rmm/detail/format.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2024, 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 <rmm/cuda_stream_view.hpp>

#include <array>
#include <cstdio>
#include <memory>
#include <stdexcept>
#include <string>

namespace RMM_NAMESPACE {
namespace detail {

/**
* @brief Format a message string with printf-style formatting
*
* This function performs printf-style formatting to avoid the need for fmt
* or spdlog's own templated APIs (which would require exposing spdlog
* symbols publicly) and returns the formatted message as a `std::string`.
*
wence- marked this conversation as resolved.
Show resolved Hide resolved
* @param format The format string
* @param args The format arguments
*/
template <typename... Args>
std::string formatted_log(std::string const& format, Args&&... args)
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
auto size = static_cast<size_t>(std::snprintf(nullptr, 0, format.c_str(), args...) + 1);
if (size <= 0) { throw std::runtime_error("Error during formatting."); }
harrism marked this conversation as resolved.
Show resolved Hide resolved
std::unique_ptr<char[]> buf(new char[size]);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
std::snprintf(buf.get(), size, format.c_str(), args...);
return {buf.get(), buf.get() + size - 1}; // drop '\0'
}

// Stringify a size in bytes to a human-readable value
inline std::string format_bytes(std::size_t value)
{
static std::array units{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"};

int index = 0;
auto size = static_cast<double>(value);
while (size > 1024) {
size /= 1024;
index++;
}

return std::to_string(value) + ' ' + units.at(index);
}

// Stringify a stream ID
inline std::string format_stream(rmm::cuda_stream_view stream)
vyasr marked this conversation as resolved.
Show resolved Hide resolved
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return std::to_string(reinterpret_cast<std::uintptr_t>(stream.value()));
}

} // namespace detail
} // namespace RMM_NAMESPACE
4 changes: 2 additions & 2 deletions include/rmm/detail/logging_assert.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, NVIDIA CORPORATION.
* Copyright (c) 2020-2024, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -38,7 +38,7 @@
if (!success) { \
RMM_LOG_CRITICAL( \
"[" __FILE__ ":" RMM_STRINGIFY(__LINE__) "] Assertion " RMM_STRINGIFY(_expr) " failed."); \
rmm::logger().flush(); \
rmm::detail::logger().flush(); \
/* NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay) */ \
assert(success); \
} \
Expand Down
55 changes: 14 additions & 41 deletions include/rmm/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@

#pragma once

#include <rmm/cuda_stream_view.hpp>
#include <rmm/detail/export.hpp>
#include <rmm/detail/format.hpp>

#include <fmt/format.h>
#include <fmt/ostream.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/spdlog.h>

#include <array>
#include <iostream>
#include <string>

namespace RMM_NAMESPACE {
Expand Down Expand Up @@ -70,32 +68,6 @@ struct logger_wrapper {
}
};

/**
* @brief Represent a size in number of bytes.
*/
struct bytes {
std::size_t value; ///< The size in bytes

/**
* @brief Construct a new bytes object
*
* @param os The output stream
* @param value The size in bytes
*/
friend std::ostream& operator<<(std::ostream& os, bytes const& value)
{
static std::array units{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"};

int index = 0;
auto size = static_cast<double>(value.value);
while (size > 1024) {
size /= 1024;
index++;
}
return os << size << ' ' << units.at(index);
}
};

inline spdlog::logger& logger()
{
static detail::logger_wrapper wrapped{};
Expand Down Expand Up @@ -125,20 +97,21 @@ logger()
// The default is INFO, but it should be used sparingly, so that by default a log file is only
// output if there is important information, warnings, errors, and critical failures
// Log messages that require computation should only be used at level TRACE and DEBUG
#define RMM_LOG_TRACE(...) SPDLOG_LOGGER_TRACE(&rmm::detail::logger(), __VA_ARGS__)
#define RMM_LOG_DEBUG(...) SPDLOG_LOGGER_DEBUG(&rmm::detail::logger(), __VA_ARGS__)
#define RMM_LOG_INFO(...) SPDLOG_LOGGER_INFO(&rmm::detail::logger(), __VA_ARGS__)
#define RMM_LOG_WARN(...) SPDLOG_LOGGER_WARN(&rmm::detail::logger(), __VA_ARGS__)
#define RMM_LOG_ERROR(...) SPDLOG_LOGGER_ERROR(&rmm::detail::logger(), __VA_ARGS__)
#define RMM_LOG_CRITICAL(...) SPDLOG_LOGGER_CRITICAL(&rmm::detail::logger(), __VA_ARGS__)
#define RMM_LOG_TRACE(...) \
SPDLOG_LOGGER_TRACE(&rmm::detail::logger(), rmm::detail::formatted_log(__VA_ARGS__))
#define RMM_LOG_DEBUG(...) \
SPDLOG_LOGGER_DEBUG(&rmm::detail::logger(), rmm::detail::formatted_log(__VA_ARGS__))
#define RMM_LOG_INFO(...) \
SPDLOG_LOGGER_INFO(&rmm::detail::logger(), rmm::detail::formatted_log(__VA_ARGS__))
#define RMM_LOG_WARN(...) \
SPDLOG_LOGGER_WARN(&rmm::detail::logger(), rmm::detail::formatted_log(__VA_ARGS__))
#define RMM_LOG_ERROR(...) \
SPDLOG_LOGGER_ERROR(&rmm::detail::logger(), rmm::detail::formatted_log(__VA_ARGS__))
#define RMM_LOG_CRITICAL(...) \
SPDLOG_LOGGER_CRITICAL(&rmm::detail::logger(), rmm::detail::formatted_log(__VA_ARGS__))

//! @endcond

} // namespace RMM_NAMESPACE

// Doxygen doesn't like this because we're overloading something from fmt
//! @cond Doxygen_Suppress
template <>
struct fmt::formatter<rmm::detail::bytes> : fmt::ostream_formatter {};

//! @endcond
4 changes: 3 additions & 1 deletion include/rmm/mr/device/arena_memory_resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <rmm/aligned.hpp>
#include <rmm/detail/error.hpp>
#include <rmm/detail/export.hpp>
#include <rmm/detail/format.hpp>
#include <rmm/detail/logging_assert.hpp>
#include <rmm/logger.hpp>
#include <rmm/mr/device/detail/arena.hpp>
Expand Down Expand Up @@ -335,7 +336,8 @@ class arena_memory_resource final : public device_memory_resource {
void dump_memory_log(size_t bytes)
{
logger_->info("**************************************************");
logger_->info("Ran out of memory trying to allocate {}.", rmm::detail::bytes{bytes});
logger_->info(rmm::detail::formatted_log("Ran out of memory trying to allocate %s.",
rmm::detail::format_bytes(bytes)));
logger_->info("**************************************************");
logger_->info("Global arena:");
global_arena_.dump_memory_log(logger_);
Expand Down
36 changes: 20 additions & 16 deletions include/rmm/mr/device/detail/arena.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
#include <rmm/cuda_stream_view.hpp>
#include <rmm/detail/error.hpp>
#include <rmm/detail/export.hpp>
#include <rmm/detail/format.hpp>
#include <rmm/detail/logging_assert.hpp>
#include <rmm/logger.hpp>
#include <rmm/resource_ref.hpp>

#include <cuda_runtime_api.h>

#include <fmt/core.h>
#include <spdlog/common.h>

#include <algorithm>
Expand Down Expand Up @@ -651,33 +651,37 @@ class global_arena final {
{
std::lock_guard lock(mtx_);

logger->info(" Arena size: {}", rmm::detail::bytes{upstream_block_.size()});
logger->info(" # superblocks: {}", superblocks_.size());
logger->info(rmm::detail::formatted_log(" Arena size: %s",
rmm::detail::format_bytes(upstream_block_.size())));
logger->info(rmm::detail::formatted_log(" # superblocks: %d", superblocks_.size()));
if (!superblocks_.empty()) {
logger->debug(" Total size of superblocks: {}",
rmm::detail::bytes{total_memory_size(superblocks_)});
logger->debug(
rmm::detail::formatted_log(" Total size of superblocks: %s",
rmm::detail::format_bytes(total_memory_size(superblocks_))));
auto const total_free = total_free_size(superblocks_);
auto const max_free = max_free_size(superblocks_);
auto const fragmentation = (1 - max_free / static_cast<double>(total_free)) * 100;
logger->info(" Total free memory: {}", rmm::detail::bytes{total_free});
logger->info(" Largest block of free memory: {}", rmm::detail::bytes{max_free});
logger->info(" Fragmentation: {:.2f}%", fragmentation);
logger->info(rmm::detail::formatted_log(" Total free memory: %s",
rmm::detail::format_bytes(total_free)));
logger->info(rmm::detail::formatted_log(" Largest block of free memory: %s",
rmm::detail::format_bytes(max_free)));
logger->info(rmm::detail::formatted_log(" Fragmentation: %0.2s", fragmentation));

auto index = 0;
char* prev_end{};
for (auto const& sblk : superblocks_) {
if (prev_end == nullptr) { prev_end = sblk.pointer(); }
logger->debug(
" Superblock {}: start={}, end={}, size={}, empty={}, # free blocks={}, max free={}, "
"gap={}",
logger->debug(rmm::detail::formatted_log(
" Superblock %d: start=%p, end=%p, size=%d, empty=%d, # free blocks=%d, max free=%s, "
"gap=%s",
index,
fmt::ptr(sblk.pointer()),
fmt::ptr(sblk.end()),
rmm::detail::bytes{sblk.size()},
sblk.pointer(),
sblk.end(),
rmm::detail::format_bytes(sblk.size()),
sblk.empty(),
sblk.free_blocks(),
rmm::detail::bytes{sblk.max_free_size()},
rmm::detail::bytes{static_cast<size_t>(sblk.pointer() - prev_end)});
rmm::detail::format_bytes(sblk.max_free_size()),
rmm::detail::format_bytes(static_cast<size_t>(sblk.pointer() - prev_end))));
prev_end = sblk.end();
index++;
}
Expand Down
9 changes: 2 additions & 7 deletions include/rmm/mr/device/detail/coalescing_free_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
#include <rmm/detail/export.hpp>
#include <rmm/mr/device/detail/free_list.hpp>

#include <fmt/core.h>

#include <algorithm>
#include <cassert>
#include <cstddef>
Expand Down Expand Up @@ -131,10 +129,7 @@ struct block : public block_base {
/**
* @brief Print this block. For debugging.
*/
inline void print() const
{
std::cout << fmt::format("{} {} B", fmt::ptr(pointer()), size()) << std::endl;
}
inline void print() const { std::cout << pointer() << " " << size() << " B" << std::endl; }
#endif

private:
Expand All @@ -146,7 +141,7 @@ struct block : public block_base {
/// Print block on an ostream
inline std::ostream& operator<<(std::ostream& out, const block& blk)
{
out << fmt::format("{} {} B\n", fmt::ptr(blk.pointer()), blk.size());
out << blk.pointer() << " " << blk.size() << " B" << std::endl;
return out;
}
#endif
Expand Down
29 changes: 14 additions & 15 deletions include/rmm/mr/device/detail/stream_ordered_memory_resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
#include <rmm/cuda_device.hpp>
#include <rmm/detail/error.hpp>
#include <rmm/detail/export.hpp>
#include <rmm/detail/format.hpp>
#include <rmm/logger.hpp>
#include <rmm/mr/device/device_memory_resource.hpp>

#include <cuda_runtime_api.h>

#include <fmt/core.h>

#include <cstddef>
#include <map>
#include <mutex>
Expand Down Expand Up @@ -201,7 +200,7 @@ class stream_ordered_memory_resource : public crtp<PoolResource>, public device_
*/
void* do_allocate(std::size_t size, cuda_stream_view stream) override
{
RMM_LOG_TRACE("[A][stream {:p}][{}B]", fmt::ptr(stream.value()), size);
RMM_LOG_TRACE("[A][stream %s][%dB]", rmm::detail::format_stream(stream), size);
harrism marked this conversation as resolved.
Show resolved Hide resolved

if (size <= 0) { return nullptr; }

Expand All @@ -215,10 +214,10 @@ class stream_ordered_memory_resource : public crtp<PoolResource>, public device_
rmm::out_of_memory);
auto const block = this->underlying().get_block(size, stream_event);

RMM_LOG_TRACE("[A][stream {:p}][{}B][{:p}]",
fmt::ptr(stream_event.stream),
RMM_LOG_TRACE("[A][stream %s][%dB][%p]",
rmm::detail::format_stream(stream_event.stream),
size,
fmt::ptr(block.pointer()));
block.pointer());

log_summary_trace();

Expand All @@ -234,7 +233,7 @@ class stream_ordered_memory_resource : public crtp<PoolResource>, public device_
*/
void do_deallocate(void* ptr, std::size_t size, cuda_stream_view stream) override
{
RMM_LOG_TRACE("[D][stream {:p}][{}B][{:p}]", fmt::ptr(stream.value()), size, ptr);
RMM_LOG_TRACE("[D][stream %s][%dB][%p]", rmm::detail::format_stream(stream), size, ptr);

if (size <= 0 || ptr == nullptr) { return; }

Expand Down Expand Up @@ -384,10 +383,10 @@ class stream_ordered_memory_resource : public crtp<PoolResource>, public device_
if (merge_first) {
merge_lists(stream_event, blocks, other_event, std::move(other_blocks));

RMM_LOG_DEBUG("[A][Stream {:p}][{}B][Merged stream {:p}]",
fmt::ptr(stream_event.stream),
RMM_LOG_DEBUG("[A][Stream %s][%dB][Merged stream %s]",
rmm::detail::format_stream(stream_event.stream),
size,
fmt::ptr(iter->first.stream));
rmm::detail::format_stream(iter->first.stream));

stream_free_blocks_.erase(iter);

Expand All @@ -414,11 +413,11 @@ class stream_ordered_memory_resource : public crtp<PoolResource>, public device_
block_type const block = find_block(iter);

if (block.is_valid()) {
RMM_LOG_DEBUG((merge_first) ? "[A][Stream {:p}][{}B][Found after merging stream {:p}]"
: "[A][Stream {:p}][{}B][Taken from stream {:p}]",
fmt::ptr(stream_event.stream),
RMM_LOG_DEBUG((merge_first) ? "[A][Stream %s][%dB][Found after merging stream %s]"
: "[A][Stream %s][%dB][Taken from stream %s]",
rmm::detail::format_stream(stream_event.stream),
size,
fmt::ptr(iter->first.stream));
rmm::detail::format_stream(iter->first.stream));
return block;
}
}
Expand Down Expand Up @@ -471,7 +470,7 @@ class stream_ordered_memory_resource : public crtp<PoolResource>, public device_
max_block = std::max(summary.first, max_block);
free_mem += summary.second;
});
RMM_LOG_TRACE("[Summary][Free lists: {}][Blocks: {}][Max Block: {}][Total Free: {}]",
RMM_LOG_TRACE("[Summary][Free lists: %d][Blocks: %d][Max Block: %d][Total Free: %d]",
stream_free_blocks_.size(),
num_blocks,
max_block,
Expand Down
Loading
Loading