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

Provide a pread implementation for MSVC #1666

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Changes from all commits
Commits
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
29 changes: 29 additions & 0 deletions mlx/io/load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
#include <limits>
#include <sstream>

// Used by pread implementation.
#ifdef _MSC_VER
#define NOMINMAX
#include <windows.h>
#endif

#include "mlx/io/load.h"
#include "mlx/ops.h"
#include "mlx/primitives.h"
Expand Down Expand Up @@ -100,6 +106,29 @@ Dtype dtype_from_array_protocol(std::string_view t) {
"[from_str] Invalid array protocol type-string: " + std::string(t));
}

#ifdef _MSC_VER
// There is no pread on Windows, emulate it with ReadFile.
int64_t pread(int fd, void* buf, uint64_t size, uint64_t offset) {
HANDLE file = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
if (file == INVALID_HANDLE_VALUE) {
return -1;
}

OVERLAPPED overlapped = {0};
overlapped.Offset = offset & 0xFFFFFFFF;
overlapped.OffsetHigh = (offset >> 32) & 0xFFFFFFFF;

DWORD bytes_read;
if (!ReadFile(file, buf, size, &bytes_read, &overlapped)) {
if (GetLastError() != ERROR_HANDLE_EOF) {
return -1;
}
}

return bytes_read;
}
#endif

} // namespace

/** Save array to out stream in .npy format */
Expand Down