Skip to content

Commit

Permalink
Merge pull request #98 from linqiaozhi/master
Browse files Browse the repository at this point in the history
Fixing compile errors on Windows
  • Loading branch information
linqiaozhi authored Apr 19, 2020
2 parents d14f741 + 9390f6e commit 03715ad
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 11 deletions.
2 changes: 1 addition & 1 deletion fast_tsne.R
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
2 changes: 1 addition & 1 deletion fast_tsne.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion fast_tsne.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 31 additions & 6 deletions src/annoylib.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ typedef unsigned __int64 uint64_t;
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include "mman.h"
#include "winlibs/mman.h"
#include <windows.h>
#else
#include <sys/mman.h>
Expand Down Expand Up @@ -881,7 +881,12 @@ template<typename S, typename T, typename Distance, typename Random>

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);
Expand Down Expand Up @@ -981,7 +986,11 @@ template<typename S, typename T, typename Distance, typename Random>
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);

Expand Down Expand Up @@ -1022,12 +1031,20 @@ template<typename S, typename T, typename Distance, typename Random>

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
Expand All @@ -1039,14 +1056,22 @@ template<typename S, typename T, typename Distance, typename Random>
}

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);
Expand Down
2 changes: 1 addition & 1 deletion src/tsne.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions src/winlibs/mman.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 7 additions & 1 deletion src/winlibs/mman.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */

0 comments on commit 03715ad

Please sign in to comment.