From f108999bc367846c249eaa9b1611cafa97e46609 Mon Sep 17 00:00:00 2001 From: Tianyu Liu Date: Wed, 4 Dec 2024 15:46:54 -0500 Subject: [PATCH] Fix a bug --- cpp/src/file_handle.cpp | 47 +++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/cpp/src/file_handle.cpp b/cpp/src/file_handle.cpp index 9c82d4836a..ade1f8bd9a 100644 --- a/cpp/src/file_handle.cpp +++ b/cpp/src/file_handle.cpp @@ -128,31 +128,38 @@ FileHandle::FileHandle(const std::string& file_path, return; // Nothing to do in compatibility mode } - if (_compat_mode == CompatMode::AUTO) { - // 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; - } catch (const std::invalid_argument&) { + // Try to open the file with the O_DIRECT flag. Fall back to compatibility mode, if it fails. + auto handle_0_direct_except = [this] { + if (_compat_mode == CompatMode::AUTO) { _compat_mode = CompatMode::ON; + } else { // CompatMode::OFF + throw; } + }; + + try { + _fd_direct_on = open_fd(file_path, flags, true, mode); + } catch (const std::system_error&) { + handle_0_direct_except(); + } catch (const std::invalid_argument&) { + handle_0_direct_except(); } + if (_compat_mode == CompatMode::ON) { return; } + // Create a cuFile handle, if not in compatibility mode - if (!is_compat_mode_preferred()) { - CUfileDescr_t desc{}; // It is important to set to zero! - desc.type = CU_FILE_HANDLE_TYPE_OPAQUE_FD; - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) - desc.handle.fd = _fd_direct_on; - - auto error_code = cuFileAPI::instance().HandleRegister(&_handle, &desc); - // For the AUTO mode, if the first cuFile API call fails, fall back to the compatibility mode. - if (_compat_mode == CompatMode::AUTO && error_code.err != CU_FILE_SUCCESS) { - _compat_mode = CompatMode::ON; - } else { - CUFILE_TRY(error_code); - } + CUfileDescr_t desc{}; // It is important to set to zero! + desc.type = CU_FILE_HANDLE_TYPE_OPAQUE_FD; + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) + desc.handle.fd = _fd_direct_on; + + auto error_code = cuFileAPI::instance().HandleRegister(&_handle, &desc); + // For the AUTO mode, if the first cuFile API call fails, fall back to the compatibility + // mode. + if (_compat_mode == CompatMode::AUTO && error_code.err != CU_FILE_SUCCESS) { + _compat_mode = CompatMode::ON; + } else { + CUFILE_TRY(error_code); } }