From 495969da28e78d2055c506ea238a95cc078f4eb8 Mon Sep 17 00:00:00 2001 From: linqiaozhi Date: Sat, 18 Apr 2020 19:57:14 -0400 Subject: [PATCH 1/2] Fixed Windows compile errors When FIt-SNE was originally adapted to Windows, key changes were made to annoylib.h, mman.c, and mman.h in order to allow it to compile on Windows. When we updated annoylib.h, those changes were not carried forward. This commit restores those changes to the new annoylib.h file, so that it can be compiled on Windows. --- src/annoylib.h | 37 +++++++++++++++++++++++++++++++------ src/winlibs/mman.c | 25 +++++++++++++++++++++++++ src/winlibs/mman.h | 8 +++++++- 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/src/annoylib.h b/src/annoylib.h index 08440d5..5cb8207 100644 --- a/src/annoylib.h +++ b/src/annoylib.h @@ -39,7 +39,7 @@ typedef unsigned __int64 uint64_t; #ifndef NOMINMAX #define NOMINMAX #endif - #include "mman.h" + #include "winlibs/mman.h" #include #else #include @@ -881,7 +881,12 @@ template bool on_disk_build(const char* file, char** error=NULL) { _on_disk = true; - _fd = open(file, O_RDWR | O_CREAT | O_TRUNC, (int) 0600); + +#ifdef _WIN32 + _fd = _open(file, O_RDWR | O_CREAT | O_TRUNC, (int)0600); +#else + _fd = open(file, O_RDWR | O_CREAT | O_TRUNC, (int)0600); +#endif if (_fd == -1) { showUpdate("Error: file descriptor is -1\n"); if (error) *error = strerror(errno); @@ -981,7 +986,11 @@ template return true; } else { // Delete file if it already exists (See issue #335) - unlink(filename); +#ifdef _WIN32 + _unlink(filename); +#else + unlink(filename); +#endif printf("path: %s\n", filename); @@ -1022,12 +1031,20 @@ template void unload() { if (_on_disk && _fd) { +#ifdef _WIN32 + _close(_fd); +#else close(_fd); +#endif munmap(_nodes, _s * _nodes_size); } else { if (_fd) { // we have mmapped data - close(_fd); +#ifdef _WIN32 + _close(_fd); +#else + close(_fd); +#endif munmap(_nodes, _n_nodes * _s); } else if (_nodes) { // We have heap allocated data @@ -1039,14 +1056,22 @@ template } bool load(const char* filename, bool prefault=false, char** error=NULL) { - _fd = open(filename, O_RDONLY, (int)0400); +#ifdef _WIN32 + _fd = _open(filename, O_RDONLY, (int)0400); +#else + _fd = open(filename, O_RDONLY, (int)0400); +#endif if (_fd == -1) { showUpdate("Error: file descriptor is -1\n"); if (error) *error = strerror(errno); _fd = 0; return false; } - off_t size = lseek(_fd, 0, SEEK_END); +#ifdef _WIN32 + off_t size = _lseek(_fd, 0, SEEK_END); +#else + off_t size = lseek(_fd, 0, SEEK_END); +#endif if (size == -1) { showUpdate("lseek returned -1\n"); if (error) *error = strerror(errno); diff --git a/src/winlibs/mman.c b/src/winlibs/mman.c index 9a2df41..b7e685c 100644 --- a/src/winlibs/mman.c +++ b/src/winlibs/mman.c @@ -178,3 +178,28 @@ int munlock(const void *addr, size_t len) return -1; } +#if !defined(__MINGW32__) +int ftruncate(int fd, unsigned int size) { + if (fd < 0) { + errno = EBADF; + return -1; + } + + HANDLE h = (HANDLE)_get_osfhandle(fd); + unsigned int cur = SetFilePointer(h, 0, NULL, FILE_CURRENT); + if (cur == ~0 || SetFilePointer(h, size, NULL, FILE_BEGIN) == ~0 || !SetEndOfFile(h)) { + int error = GetLastError(); + switch (GetLastError()) { + case ERROR_INVALID_HANDLE: + errno = EBADF; + break; + default: + errno = EIO; + break; + } + return -1; + } + + return 0; +} +#endif \ No newline at end of file diff --git a/src/winlibs/mman.h b/src/winlibs/mman.h index 047d3a0..9ea6a4e 100644 --- a/src/winlibs/mman.h +++ b/src/winlibs/mman.h @@ -68,9 +68,15 @@ MMANSHARED_EXPORT int _mprotect(void *addr, size_t len, int prot); MMANSHARED_EXPORT int msync(void *addr, size_t len, int flags); MMANSHARED_EXPORT int mlock(const void *addr, size_t len); MMANSHARED_EXPORT int munlock(const void *addr, size_t len); - +#if !defined(__MINGW32__) +MMANSHARED_EXPORT int ftruncate(int fd, unsigned int size); +#endif #ifdef __cplusplus } #endif + + + + #endif /* _SYS_MMAN_H_ */ From 9390f6ee1705ae69127babea350ab807615859c8 Mon Sep 17 00:00:00 2001 From: linqiaozhi Date: Sat, 18 Apr 2020 20:02:19 -0400 Subject: [PATCH 2/2] Updated version numbers to 1.2.1 --- fast_tsne.R | 2 +- fast_tsne.m | 2 +- fast_tsne.py | 2 +- src/tsne.cpp | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/fast_tsne.R b/fast_tsne.R index e2e9d6d..3dc9f8e 100644 --- a/fast_tsne.R +++ b/fast_tsne.R @@ -96,7 +96,7 @@ fftRtsne <- function(X, fast_tsne_path = NULL, nthreads = 0, perplexity_list = NULL, get_costs = FALSE, df = 1.0) { - version_number <- '1.2.0' + version_number <- '1.2.1' if (is.null(fast_tsne_path)) { if (.Platform$OS.type == "unix") { diff --git a/fast_tsne.m b/fast_tsne.m index 4475855..d7a5446 100644 --- a/fast_tsne.m +++ b/fast_tsne.m @@ -121,7 +121,7 @@ % IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY % OF SUCH DAMAGE. - version_number = '1.2.0'; + version_number = '1.2.1'; % default parameters and flags p.perplexity = 30; diff --git a/fast_tsne.py b/fast_tsne.py index 5755d84..3751e47 100644 --- a/fast_tsne.py +++ b/fast_tsne.py @@ -169,7 +169,7 @@ def fast_tsne( is True. """ - version_number = "1.2.0" + version_number = "1.2.1" # X should be a numpy array of 64-bit doubles X = np.array(X).astype(float) diff --git a/src/tsne.cpp b/src/tsne.cpp index e24abdb..1a9a709 100644 --- a/src/tsne.cpp +++ b/src/tsne.cpp @@ -2038,7 +2038,7 @@ void TSNE::save_data(const char *result_path, double* data, double* costs, int n int main(int argc, char *argv[]) { - const char version_number[] = "1.2.0"; + const char version_number[] = "1.2.1"; printf("=============== t-SNE v%s ===============\n", version_number); // Define some variables