Skip to content

Commit 2660c66

Browse files
committed
Pad the file for windows.
1 parent 341c8fb commit 2660c66

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

rabit/include/rabit/internal/io.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
*/
77
#ifndef RABIT_INTERNAL_IO_H_
88
#define RABIT_INTERNAL_IO_H_
9+
10+
#if !defined(NOMINMAX) && defined(_WIN32)
11+
#define NOMINMAX
12+
#endif // !defined(NOMINMAX)
13+
914
#include <cstdio>
1015
#include <vector>
1116
#include <cstring>
@@ -26,6 +31,9 @@ struct MemoryFixSizeBuffer : public SeekStream {
2631
// similar to SEEK_END in libc
2732
static size_t constexpr kSeekEnd = std::numeric_limits<size_t>::max();
2833

34+
protected:
35+
MemoryFixSizeBuffer() = default;
36+
2937
public:
3038
MemoryFixSizeBuffer(void *p_buffer, size_t buffer_size)
3139
: p_buffer_(reinterpret_cast<char*>(p_buffer)),
@@ -62,11 +70,11 @@ struct MemoryFixSizeBuffer : public SeekStream {
6270

6371
protected:
6472
/*! \brief in memory buffer */
65-
char *p_buffer_;
73+
char* p_buffer_{nullptr};
6674
/*! \brief current pointer */
67-
size_t buffer_size_;
75+
std::size_t buffer_size_{ 0 };
6876
/*! \brief current pointer */
69-
size_t curr_ptr_;
77+
std::size_t curr_ptr_{ 0 };
7078
}; // class MemoryFixSizeBuffer
7179

7280
/*! \brief a in memory buffer that can be read and write as stream interface */

src/common/io.cc

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <sys/stat.h>
1212
#include <unistd.h> // for close, getpagesize
1313
#elif defined(_MSC_VER)
14+
#define WIN32_LEAN_AND_MEAN
1415
#include <windows.h>
1516
#endif // defined(__unix__)
1617

@@ -26,6 +27,7 @@
2627

2728
#include "io.h"
2829
#include "xgboost/logging.h"
30+
#include "xgboost/collective/socket.h"
2931

3032
namespace xgboost {
3133
namespace common {
@@ -170,7 +172,8 @@ std::size_t GetPageSize() {
170172
#if defined(_MSC_VER)
171173
SYSTEM_INFO sys_info;
172174
GetSystemInfo(&sys_info);
173-
return sys_info.dwPageSize;
175+
// During testing, `sys_info.dwPageSize` is of size 4096 while `dwAllocationGranularity` is of size 65536.
176+
return sys_info.dwAllocationGranularity;
174177
#else
175178
return getpagesize();
176179
#endif
@@ -190,18 +193,21 @@ std::size_t PadPageForMmap(std::size_t file_bytes, dmlc::Stream* fo) {
190193

191194
struct PrivateMmapStream::MMAPFile {
192195
#if defined(_MSC_VER)
193-
HANDLE fd;
196+
HANDLE fd{ INVALID_HANDLE_VALUE };
194197
#else
195-
std::int32_t fd;
198+
std::int32_t fd {0};
196199
#endif
197200
std::string path;
198201
};
199202

200203
PrivateMmapStream::PrivateMmapStream(std::string path, bool read_only, std::size_t offset,
201204
std::size_t length)
202-
: MemoryFixSizeBuffer{Open(std::move(path), read_only, offset, length), length} {}
205+
: MemoryFixSizeBuffer{} {
206+
this->p_buffer_ = Open(std::move(path), read_only, offset, length);
207+
this->buffer_size_ = length;
208+
}
203209

204-
void* PrivateMmapStream::Open(std::string path, bool read_only, std::size_t offset,
210+
char* PrivateMmapStream::Open(std::string path, bool read_only, std::size_t offset,
205211
std::size_t length) {
206212
#if defined(_MSC_VER)
207213
HANDLE fd = CreateFile(path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING,
@@ -211,7 +217,8 @@ void* PrivateMmapStream::Open(std::string path, bool read_only, std::size_t offs
211217
auto fd = open(path.c_str(), O_RDONLY);
212218
CHECK_GE(fd, 0) << "Failed to open:" << path << ". " << strerror(errno);
213219
#endif
214-
handle_.reset(new MMAPFile{fd, std::move(path)});
220+
handle_ = nullptr;
221+
handle_.reset(new MMAPFile{fd, path});
215222

216223
void* ptr{nullptr};
217224
#if defined(__linux__) || defined(__GLIBC__)
@@ -228,8 +235,12 @@ void* PrivateMmapStream::Open(std::string path, bool read_only, std::size_t offs
228235
access = read_only ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS;
229236
std::uint32_t loff = static_cast<std::uint32_t>(offset);
230237
std::uint32_t hoff = offset >> 32;
238+
CHECK(map_file) << "Failed to map: " << handle_->path << ". " << GetLastError();;
231239
ptr = MapViewOfFile(map_file, access, hoff, loff, length);
232-
CHECK_NE(ptr, nullptr) << "Failed to map: " << handle_->path << ". " << GetLastError();
240+
if (ptr == nullptr) {
241+
system::ThrowAtError("MapViewOfFile");
242+
}
243+
CHECK_NE(ptr, nullptr) << "Failed to map: " << handle_->path << ". " << GetLastError() ;
233244
#else
234245
CHECK_LE(offset, std::numeric_limits<off_t>::max())
235246
<< "File size has exceeded the limit on the current system.";
@@ -240,18 +251,22 @@ void* PrivateMmapStream::Open(std::string path, bool read_only, std::size_t offs
240251
ptr = reinterpret_cast<char*>(mmap(nullptr, length, prot, MAP_PRIVATE, fd_, offset));
241252
CHECK_NE(ptr, MAP_FAILED) << "Failed to map: " << handle_->path << ". " << strerror(errno);
242253
#endif // defined(__linux__)
243-
return ptr;
254+
return reinterpret_cast<char*>(ptr);
244255
}
245256

246257
PrivateMmapStream::~PrivateMmapStream() {
247258
CHECK(handle_);
248259
#if defined(_MSC_VER)
249-
CHECK(UnmapViewOfFile(p_buffer_)) "Faled to munmap." << handle_->path << ". " << GetLastError();
250-
CloseHandle(handle_->fd);
260+
if (p_buffer_) {
261+
CHECK(UnmapViewOfFile(p_buffer_)) "Faled to munmap." << GetLastError();
262+
}
263+
if (handle_->fd != INVALID_HANDLE_VALUE) {
264+
CHECK(CloseHandle(handle_->fd));
265+
}
251266
#else
252267
CHECK_NE(munmap(p_buffer_, buffer_size_), -1)
253268
<< "Faled to munmap." << handle_->path << ". " << strerror(errno);
254-
CHECK_NE(close(fd_), -1) << "Faled to close: " << handle_->path << ". " << strerror(errno);
269+
CHECK_NE(close(handle_->fd), -1) << "Faled to close: " << handle_->path << ". " << strerror(errno);
255270
#endif
256271
}
257272
} // namespace common

src/common/io.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ std::size_t PadPageForMmap(std::size_t file_bytes, dmlc::Stream* fo);
146146
class PrivateMmapStream : public MemoryFixSizeBuffer {
147147
struct MMAPFile;
148148

149-
std::unique_ptr<MMAPFile> handle_;
149+
std::unique_ptr<MMAPFile> handle_{nullptr};
150150

151-
void* Open(std::string path, bool read_only, std::size_t offset, std::size_t length);
151+
char* Open(std::string path, bool read_only, std::size_t offset, std::size_t length);
152152

153153
public:
154154
/**

0 commit comments

Comments
 (0)