From 115dce3dcb5f4068f95199b2f1950e69b11e038d Mon Sep 17 00:00:00 2001 From: Tianyu Liu Date: Fri, 8 Nov 2024 15:28:27 -0500 Subject: [PATCH] Make the changes non-breaking --- cpp/examples/basic_io.cpp | 5 ++-- cpp/examples/basic_no_cuda.cpp | 2 +- cpp/include/kvikio/buffer.hpp | 4 ++-- cpp/include/kvikio/defaults.hpp | 37 +++++++++++++++++++----------- cpp/include/kvikio/file_handle.hpp | 28 +++++++++++----------- cpp/src/file_handle.cpp | 11 ++++----- 6 files changed, 46 insertions(+), 41 deletions(-) diff --git a/cpp/examples/basic_io.cpp b/cpp/examples/basic_io.cpp index 0acf1c7ae0..1eabd8fdee 100644 --- a/cpp/examples/basic_io.cpp +++ b/cpp/examples/basic_io.cpp @@ -65,7 +65,7 @@ int main() check(cudaSetDevice(0) == cudaSuccess); cout << "KvikIO defaults: " << endl; - if (kvikio::defaults::compat_mode() == kvikio::CompatMode::ON) { + if (kvikio::defaults::compat_mode()) { cout << " Compatibility mode: enabled" << endl; } else { kvikio::DriverInitializer manual_init_driver; @@ -181,8 +181,7 @@ int main() cout << "Parallel POSIX read (" << kvikio::defaults::thread_pool_nthreads() << " threads): " << read << endl; } - if (kvikio::is_batch_and_stream_available() && - kvikio::defaults::compat_mode() == kvikio::CompatMode::OFF) { + if (kvikio::is_batch_and_stream_available() && !kvikio::defaults::compat_mode()) { std::cout << std::endl; Timer timer; // Here we use the batch API to read "/tmp/test-file" into `b_dev` by diff --git a/cpp/examples/basic_no_cuda.cpp b/cpp/examples/basic_no_cuda.cpp index 965b79b07a..0d79a52883 100644 --- a/cpp/examples/basic_no_cuda.cpp +++ b/cpp/examples/basic_no_cuda.cpp @@ -41,7 +41,7 @@ constexpr int LARGE_SIZE = 8 * SIZE; // LARGE SIZE to test partial s int main() { cout << "KvikIO defaults: " << endl; - if (kvikio::defaults::compat_mode() == kvikio::CompatMode::ON) { + if (kvikio::defaults::compat_mode()) { cout << " Compatibility mode: enabled" << endl; } else { kvikio::DriverInitializer manual_init_driver; diff --git a/cpp/include/kvikio/buffer.hpp b/cpp/include/kvikio/buffer.hpp index 55a0e90cde..c0aa7f9fbc 100644 --- a/cpp/include/kvikio/buffer.hpp +++ b/cpp/include/kvikio/buffer.hpp @@ -49,7 +49,7 @@ inline void buffer_register(const void* devPtr_base, int flags = 0, const std::vector& errors_to_ignore = std::vector()) { - if (defaults::compat_mode() == CompatMode::ON) { return; } + if (defaults::compat_mode()) { return; } CUfileError_t status = cuFileAPI::instance().BufRegister(devPtr_base, size, flags); if (status.err != CU_FILE_SUCCESS) { // Check if `status.err` is in `errors_to_ignore` @@ -67,7 +67,7 @@ inline void buffer_register(const void* devPtr_base, */ inline void buffer_deregister(const void* devPtr_base) { - if (defaults::compat_mode() == CompatMode::ON) { return; } + if (defaults::compat_mode()) { return; } CUFILE_TRY(cuFileAPI::instance().BufDeregister(devPtr_base)); } diff --git a/cpp/include/kvikio/defaults.hpp b/cpp/include/kvikio/defaults.hpp index b484dfe9e6..acfafe6be8 100644 --- a/cpp/include/kvikio/defaults.hpp +++ b/cpp/include/kvikio/defaults.hpp @@ -123,7 +123,6 @@ class defaults { private: BS::thread_pool _thread_pool{get_num_threads_from_env()}; CompatMode _compat_mode; - CompatMode _requested_compat_mode; // Initial compatibility mode requested by the user std::size_t _task_size; std::size_t _gds_threshold; std::size_t _bounce_buffer_size; @@ -137,22 +136,27 @@ class defaults { return ret; } + void readjust_compat_mode() + { + if (is_cufile_available()) { + _compat_mode = CompatMode::OFF; + } else { + _compat_mode = CompatMode::ON; + } + } + defaults() { // Determine the default value of `compat_mode` { if (std::getenv("KVIKIO_COMPAT_MODE") != nullptr) { // Setting `KVIKIO_COMPAT_MODE` take precedence - _requested_compat_mode = detail::getenv_or("KVIKIO_COMPAT_MODE", CompatMode::ALLOW); + _compat_mode = detail::getenv_or("KVIKIO_COMPAT_MODE", CompatMode::ALLOW); } - if (_requested_compat_mode == CompatMode::ALLOW) { + if (_compat_mode == CompatMode::ALLOW) { // If `KVIKIO_COMPAT_MODE` isn't set, we infer based on runtime environment - if (is_cufile_available()) { - _compat_mode = CompatMode::OFF; - } else { - _compat_mode = CompatMode::ON; - } + readjust_compat_mode(); } } // Determine the default value of `task_size` @@ -208,7 +212,7 @@ class defaults { * * @return The boolean answer */ - [[nodiscard]] static CompatMode compat_mode() { return instance()->_compat_mode; } + [[nodiscard]] static bool compat_mode() { return instance()->_compat_mode == CompatMode::ON; } /** * @brief Reset the value of `kvikio::defaults::compat_mode()` @@ -218,16 +222,21 @@ class defaults { * * @param enable Whether to enable compatibility mode or not. */ - static void compat_mode_reset(CompatMode compat_mode) + static void compat_mode_reset(bool compat_mode) { - instance()->_requested_compat_mode = compat_mode; - if (is_cufile_available()) { - instance()->_compat_mode = CompatMode::OFF; - } else { + if (compat_mode) { instance()->_compat_mode = CompatMode::ON; + } else { + instance()->_compat_mode = CompatMode::OFF; } } + static void compat_mode_reset(CompatMode compat_mode) + { + instance()->_compat_mode = compat_mode; + if (compat_mode == CompatMode::ALLOW) { instance()->readjust_compat_mode(); } + } + /** * @brief Get the default thread pool. * diff --git a/cpp/include/kvikio/file_handle.hpp b/cpp/include/kvikio/file_handle.hpp index a1bc170c21..141c17371a 100644 --- a/cpp/include/kvikio/file_handle.hpp +++ b/cpp/include/kvikio/file_handle.hpp @@ -46,7 +46,7 @@ class FileHandle { int _fd_direct_on{-1}; int _fd_direct_off{-1}; bool _initialized{false}; - CompatMode _compat_mode{CompatMode::ALLOW}; + bool _compat_mode{false}; mutable std::size_t _nbytes{0}; // The size of the underlying file, zero means unknown. CUfileHandle_t _handle{}; @@ -73,7 +73,7 @@ class FileHandle { FileHandle(const std::string& file_path, const std::string& flags = "r", mode_t mode = m644, - CompatMode compat_mode = defaults::compat_mode()); + bool compat_mode = defaults::compat_mode()); /** * @brief FileHandle support move semantic but isn't copyable @@ -84,7 +84,7 @@ class FileHandle { : _fd_direct_on{std::exchange(o._fd_direct_on, -1)}, _fd_direct_off{std::exchange(o._fd_direct_off, -1)}, _initialized{std::exchange(o._initialized, false)}, - _compat_mode{std::exchange(o._compat_mode, CompatMode::ALLOW)}, + _compat_mode{std::exchange(o._compat_mode, false)}, _nbytes{std::exchange(o._nbytes, 0)}, _handle{std::exchange(o._handle, CUfileHandle_t{})} { @@ -94,7 +94,7 @@ class FileHandle { _fd_direct_on = std::exchange(o._fd_direct_on, -1); _fd_direct_off = std::exchange(o._fd_direct_off, -1); _initialized = std::exchange(o._initialized, false); - _compat_mode = std::exchange(o._compat_mode, CompatMode::ALLOW); + _compat_mode = std::exchange(o._compat_mode, false); _nbytes = std::exchange(o._nbytes, 0); _handle = std::exchange(o._handle, CUfileHandle_t{}); return *this; @@ -110,7 +110,7 @@ class FileHandle { { if (closed()) { return; } - if (_compat_mode == CompatMode::OFF) { cuFileAPI::instance().HandleDeregister(_handle); } + if (!_compat_mode) { cuFileAPI::instance().HandleDeregister(_handle); } ::close(_fd_direct_off); if (_fd_direct_on != -1) { ::close(_fd_direct_on); } _fd_direct_on = -1; @@ -129,7 +129,7 @@ class FileHandle { [[nodiscard]] CUfileHandle_t handle() { if (closed()) { throw CUfileException("File handle is closed"); } - if (_compat_mode == CompatMode::ON) { + if (_compat_mode) { throw CUfileException("The underlying cuFile handle isn't available in compatibility mode"); } return _handle; @@ -202,7 +202,7 @@ class FileHandle { std::size_t devPtr_offset, bool sync_default_stream = true) { - if (_compat_mode == CompatMode::ON) { + if (_compat_mode) { return detail::posix_device_read( _fd_direct_off, devPtr_base, size, file_offset, devPtr_offset); } @@ -254,7 +254,7 @@ class FileHandle { { _nbytes = 0; // Invalidate the computed file size - if (_compat_mode == CompatMode::ON) { + if (_compat_mode) { return detail::posix_device_write( _fd_direct_off, devPtr_base, size, file_offset, devPtr_offset); } @@ -333,7 +333,7 @@ class FileHandle { } // Let's synchronize once instead of in each task. - if (sync_default_stream && _compat_mode == CompatMode::OFF) { + if (sync_default_stream && !_compat_mode) { PushAndPopContext c(ctx); CUDA_DRIVER_TRY(cudaAPI::instance().StreamSynchronize(nullptr)); } @@ -410,7 +410,7 @@ class FileHandle { } // Let's synchronize once instead of in each task. - if (sync_default_stream && _compat_mode == CompatMode::OFF) { + if (sync_default_stream && !_compat_mode) { PushAndPopContext c(ctx); CUDA_DRIVER_TRY(cudaAPI::instance().StreamSynchronize(nullptr)); } @@ -471,8 +471,7 @@ class FileHandle { { // When checking for availability, we also check if cuFile's config file exist. This is because // even when the stream API is available, it doesn't work if no config file exist. - if (kvikio::is_batch_and_stream_available() && _compat_mode == CompatMode::OFF && - !config_path().empty()) { + if (kvikio::is_batch_and_stream_available() && !_compat_mode && !config_path().empty()) { CUFILE_TRY(cuFileAPI::instance().ReadAsync( _handle, devPtr_base, size_p, file_offset_p, devPtr_offset_p, bytes_read_p, stream)); return; @@ -564,8 +563,7 @@ class FileHandle { { // When checking for availability, we also check if cuFile's config file exist. This is because // even when the stream API is available, it doesn't work if no config file exist. - if (kvikio::is_batch_and_stream_available() && _compat_mode == CompatMode::OFF && - !config_path().empty()) { + if (kvikio::is_batch_and_stream_available() && !_compat_mode && !config_path().empty()) { CUFILE_TRY(cuFileAPI::instance().WriteAsync( _handle, devPtr_base, size_p, file_offset_p, devPtr_offset_p, bytes_written_p, stream)); return; @@ -621,7 +619,7 @@ class FileHandle { * * @return compatibility mode state for the object */ - [[nodiscard]] bool is_compat_mode_on() const noexcept { return _compat_mode == CompatMode::ON; } + [[nodiscard]] bool is_compat_mode_on() const noexcept { return _compat_mode; } }; } // namespace kvikio diff --git a/cpp/src/file_handle.cpp b/cpp/src/file_handle.cpp index 5ed4676051..c5b7ada59a 100644 --- a/cpp/src/file_handle.cpp +++ b/cpp/src/file_handle.cpp @@ -24,7 +24,6 @@ #include #include -#include "kvikio/defaults.hpp" namespace kvikio { @@ -119,12 +118,12 @@ int open_fd(const std::string& file_path, const std::string& flags, bool o_direc FileHandle::FileHandle(const std::string& file_path, const std::string& flags, mode_t mode, - CompatMode compat_mode) + bool compat_mode) : _fd_direct_off{open_fd(file_path, flags, false, mode)}, _initialized{true}, _compat_mode{compat_mode} { - if (_compat_mode == CompatMode::ON) { + if (_compat_mode) { return; // Nothing to do in compatibility mode } @@ -132,13 +131,13 @@ FileHandle::FileHandle(const std::string& file_path, try { _fd_direct_on = open_fd(file_path, flags, true, mode); } catch (const std::system_error&) { - _compat_mode = CompatMode::ON; + _compat_mode = true; } catch (const std::invalid_argument&) { - _compat_mode = CompatMode::ON; + _compat_mode = true; } // Create a cuFile handle, if not in compatibility mode - if (_compat_mode == CompatMode::OFF) { + if (!_compat_mode) { CUfileDescr_t desc{}; // It is important to set to zero! desc.type = CU_FILE_HANDLE_TYPE_OPAQUE_FD; // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)