Skip to content

Commit

Permalink
Make the changes non-breaking
Browse files Browse the repository at this point in the history
  • Loading branch information
kingcrimsontianyu committed Nov 8, 2024
1 parent 2701118 commit 115dce3
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 41 deletions.
5 changes: 2 additions & 3 deletions cpp/examples/basic_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cpp/examples/basic_no_cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions cpp/include/kvikio/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ inline void buffer_register(const void* devPtr_base,
int flags = 0,
const std::vector<int>& errors_to_ignore = std::vector<int>())
{
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`
Expand All @@ -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));
}

Expand Down
37 changes: 23 additions & 14 deletions cpp/include/kvikio/defaults.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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`
Expand Down Expand Up @@ -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()`
Expand All @@ -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.
*
Expand Down
28 changes: 13 additions & 15 deletions cpp/include/kvikio/file_handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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{};

Expand All @@ -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
Expand All @@ -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{})}
{
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
11 changes: 5 additions & 6 deletions cpp/src/file_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <system_error>

#include <kvikio/file_handle.hpp>
#include "kvikio/defaults.hpp"

namespace kvikio {

Expand Down Expand Up @@ -119,26 +118,26 @@ 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
}

// Try to open the file with the O_DIRECT flag. Fall back to compatibility mode, if it fails.
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)
Expand Down

0 comments on commit 115dce3

Please sign in to comment.