mirror of
https://github.com/ml-explore/mlx.git
synced 2025-07-17 06:21:12 +08:00
Provide a pread implementation for MSVC (#1666)
This commit is contained in:
parent
92ab6bdeb8
commit
56db268f47
@ -5,6 +5,12 @@
|
|||||||
#include <limits>
|
#include <limits>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
// Used by pread implementation.
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#define NOMINMAX
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "mlx/io/load.h"
|
#include "mlx/io/load.h"
|
||||||
#include "mlx/ops.h"
|
#include "mlx/ops.h"
|
||||||
#include "mlx/primitives.h"
|
#include "mlx/primitives.h"
|
||||||
@ -100,6 +106,29 @@ Dtype dtype_from_array_protocol(std::string_view t) {
|
|||||||
"[from_str] Invalid array protocol type-string: " + std::string(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
|
} // namespace
|
||||||
|
|
||||||
/** Save array to out stream in .npy format */
|
/** Save array to out stream in .npy format */
|
||||||
|
Loading…
Reference in New Issue
Block a user