[lua-torch-torch7] 01/05: New upstream version 0~20161002-geb397ad

Zhou Mo cdluminate-guest at moszumanska.debian.org
Mon Oct 3 08:44:19 UTC 2016


This is an automated email from the git hooks/post-receive script.

cdluminate-guest pushed a commit to branch master
in repository lua-torch-torch7.

commit 87b92f41cd404b4479aa62de98d83221d946aaa4
Author: Zhou Mo <cdluminate at gmail.com>
Date:   Mon Oct 3 08:33:34 2016 +0000

    New upstream version 0~20161002-geb397ad
---
 Timer.c                       | 75 ++++++++++++++++++++-------------
 lib/TH/CMakeLists.txt         | 37 +++++++++++++----
 lib/TH/THAllocator.c          | 55 +++++++++---------------
 lib/TH/THAtomic.c             |  4 ++
 lib/TH/THDiskFile.c           | 25 +++++++++--
 lib/TH/THGeneral.c            | 62 ++++++++++++++++++---------
 lib/TH/THGeneral.h.in         | 10 ++++-
 lib/TH/THMemoryFile.c         | 11 ++---
 lib/TH/cmake/FindMKL.cmake    |  7 ++++
 lib/TH/generic/THTensorMath.c |  2 +-
 lib/TH/generic/simd/simd.h    | 14 ++++++-
 lib/TH/vector/NEON.c          |  2 +-
 lib/TH/vector/SSE.c           |  4 ++
 test/longSize.lua             | 97 ++++++++++++++++++++++++++-----------------
 test/test.lua                 | 10 ++++-
 test/test_sharedmem.lua       | 28 ++++++++++---
 test/test_timer.lua           | 52 +++++++++++++++++++++++
 utils.c                       | 20 ++++++---
 18 files changed, 357 insertions(+), 158 deletions(-)

diff --git a/Timer.c b/Timer.c
index 796190b..5498cc1 100644
--- a/Timer.c
+++ b/Timer.c
@@ -1,36 +1,44 @@
 #include "general.h"
 
-#if (defined(_MSC_VER) || defined(__MINGW32__))
-#include <time.h>
+#ifdef _WIN32
+
+#include <windows.h>
+#include <assert.h>
+#define TimeType __int64
+static __declspec( thread ) TimeType ticksPerSecond = 0;
+
+/*
+ * There is an example of getrusage for windows in following link:
+ * https://github.com/openvswitch/ovs/blob/master/lib/getrusage-windows.c
+ */
+
 #else
+
 #include <sys/time.h>
 #include <sys/resource.h>
+#define TimeType double
+
 #endif
 
 typedef struct _Timer
 {
     int isRunning;
 
-    double totalrealtime;
-    double totalusertime;
-    double totalsystime;
-
-    double startrealtime;
-    double startusertime;
-    double startsystime;
-
-#if (defined(_MSC_VER) || defined(__MINGW32__))
-  time_t base_time;
-#endif
+    TimeType totalrealtime;
+    TimeType totalusertime;
+    TimeType totalsystime;
 
+    TimeType startrealtime;
+    TimeType startusertime;
+    TimeType startsystime;
 } Timer;
 
-static double torch_Timer_realtime()
+static TimeType torch_Timer_realtime()
 {
-#ifdef WIN32
-  time_t ltime;
-  time(&ltime);
-  return (double)(ltime);
+#ifdef _WIN32
+  TimeType current;
+  QueryPerformanceCounter(&current);
+  return current;
 #else
   struct timeval current;
   gettimeofday(&current, NULL);
@@ -38,9 +46,9 @@ static double torch_Timer_realtime()
 #endif
 }
 
-static double torch_Timer_usertime()
+static TimeType torch_Timer_usertime()
 {
-#ifdef WIN32
+#ifdef _WIN32
   return torch_Timer_realtime();
 #else
   struct rusage current;
@@ -49,9 +57,9 @@ static double torch_Timer_usertime()
 #endif
 }
 
-static double torch_Timer_systime()
+static TimeType torch_Timer_systime()
 {
-#ifdef WIN32
+#ifdef _WIN32
   return 0;
 #else
   struct rusage current;
@@ -62,12 +70,14 @@ static double torch_Timer_systime()
 
 static int torch_Timer_new(lua_State *L)
 {
-  Timer *timer = luaT_alloc(L, sizeof(Timer));
-#ifdef _MSC_VER
-  timer->base_time = 0;
-  while(!timer->base_time)
-    time(&timer->base_time);
+#ifdef _WIN32
+  if (ticksPerSecond == 0)
+  {
+    assert(sizeof(LARGE_INTEGER) == sizeof(__int64));
+    QueryPerformanceFrequency(&ticksPerSecond);
+  }
 #endif
+  Timer *timer = luaT_alloc(L, sizeof(Timer));
   timer->isRunning = 1;
   timer->totalrealtime = 0;
   timer->totalusertime = 0;
@@ -104,9 +114,9 @@ static int torch_Timer_stop(lua_State *L)
   Timer *timer = luaT_checkudata(L, 1, "torch.Timer");
   if(timer->isRunning)  
   {
-    double realtime = torch_Timer_realtime() - timer->startrealtime;
-    double usertime = torch_Timer_usertime() - timer->startusertime;
-    double systime = torch_Timer_systime() - timer->startsystime;
+    TimeType realtime = torch_Timer_realtime() - timer->startrealtime;
+    TimeType usertime = torch_Timer_usertime() - timer->startusertime;
+    TimeType systime = torch_Timer_systime() - timer->startsystime;
     timer->totalrealtime += realtime;
     timer->totalusertime += usertime;
     timer->totalsystime += systime;
@@ -136,6 +146,11 @@ static int torch_Timer_time(lua_State *L)
   double realtime = (timer->isRunning ? (timer->totalrealtime + torch_Timer_realtime() - timer->startrealtime) : timer->totalrealtime);
   double usertime = (timer->isRunning ? (timer->totalusertime + torch_Timer_usertime() - timer->startusertime) : timer->totalusertime);
   double systime = (timer->isRunning ? (timer->totalsystime + torch_Timer_systime() - timer->startsystime) : timer->totalsystime);
+#ifdef _WIN32
+  realtime /= ticksPerSecond;
+  usertime /= ticksPerSecond;
+  systime  /= ticksPerSecond;
+#endif
   lua_createtable(L, 0, 3);
   lua_pushnumber(L, realtime);
   lua_setfield(L, -2, "real");
diff --git a/lib/TH/CMakeLists.txt b/lib/TH/CMakeLists.txt
index e1610af..a6da933 100644
--- a/lib/TH/CMakeLists.txt
+++ b/lib/TH/CMakeLists.txt
@@ -26,6 +26,11 @@ IF(MSVC)
   ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE=1)
 ENDIF(MSVC)
 
+IF(UNIX)
+  # prevent Unknown CMake command "check_function_exists".
+  INCLUDE(CheckFunctionExists)
+ENDIF(UNIX)
+
 # OpenMP support?
 SET(WITH_OPENMP ON CACHE BOOL "OpenMP support if available?")
 IF (APPLE AND CMAKE_COMPILER_IS_GNUCC)
@@ -80,7 +85,11 @@ ENDIF(C_SSE3_FOUND)
 
 IF(C_AVX_FOUND OR C_SSE4_2_FOUND OR C_SSE4_1_FOUND)
   SET(simd generic/simd/convolve.c)
-  SET_SOURCE_FILES_PROPERTIES(generic/simd/convolve.c PROPERTIES COMPILE_FLAGS "-std=c99")
+  IF(MSVC)
+    SET_SOURCE_FILES_PROPERTIES(generic/simd/convolve.c PROPERTIES COMPILE_FLAGS "/std:c99")
+  ELSE(MSVC)
+    SET_SOURCE_FILES_PROPERTIES(generic/simd/convolve.c PROPERTIES COMPILE_FLAGS "-std=c99")
+  ENDIF(MSVC)
 ENDIF(C_AVX_FOUND OR C_SSE4_2_FOUND OR C_SSE4_1_FOUND)
 
 IF(C_SSE4_1_FOUND)
@@ -91,13 +100,21 @@ IF(C_SSE4_2_FOUND)
 ENDIF(C_SSE4_2_FOUND)
 
 IF(C_SSE4_1_FOUND OR C_SSE4_2_FOUND)
-  SET_SOURCE_FILES_PROPERTIES(generic/simd/convolve5x5_sse.c PROPERTIES COMPILE_FLAGS "-O3 -ffast-math -std=c99")
+  IF(MSVC)
+    SET_SOURCE_FILES_PROPERTIES(generic/simd/convolve5x5_sse.c PROPERTIES COMPILE_FLAGS "/Ox /fp:fast /std:c99")
+  ELSE(MSVC)
+    SET_SOURCE_FILES_PROPERTIES(generic/simd/convolve5x5_sse.c PROPERTIES COMPILE_FLAGS "-O3 -ffast-math -std=c99")
+  ENDIF(MSVC)
   SET(simd ${simd} generic/simd/convolve5x5_sse.c)
 ENDIF(C_SSE4_1_FOUND OR C_SSE4_2_FOUND)
 
 IF(C_AVX_FOUND)
   SET(CMAKE_C_FLAGS "-DUSE_AVX ${CMAKE_C_FLAGS}")
-  SET_SOURCE_FILES_PROPERTIES(generic/simd/convolve5x5_avx.c PROPERTIES COMPILE_FLAGS "-O3 -ffast-math -mavx -std=c99")
+  IF(MSVC)
+      SET_SOURCE_FILES_PROPERTIES(generic/simd/convolve5x5_avx.c PROPERTIES COMPILE_FLAGS "/Ox /fp:fast /arch:AVX /std:c99")
+  ELSE(MSVC)
+    SET_SOURCE_FILES_PROPERTIES(generic/simd/convolve5x5_avx.c PROPERTIES COMPILE_FLAGS "-O3 -ffast-math -mavx -std=c99")
+  ENDIF(MSVC)
   SET(simd ${simd} generic/simd/convolve5x5_avx.c)
 ENDIF(C_AVX_FOUND)
 
@@ -207,7 +224,6 @@ IF (UNIX AND NOT APPLE)
 ENDIF(UNIX AND NOT APPLE)
 
 IF(UNIX)
-  INCLUDE(CheckFunctionExists)
   SET(CMAKE_EXTRA_INCLUDE_FILES "sys/mman.h")
   CHECK_FUNCTION_EXISTS(mmap HAVE_MMAP)
   IF(HAVE_MMAP)
@@ -271,13 +287,16 @@ ENDIF(NOT DEFINED C_INLINE)
 
 # Is __thread supported?
 INCLUDE (CheckCSourceCompiles)
-CHECK_C_SOURCE_COMPILES("static __thread int x = 1; int main() { return x; }" C_HAS_THREAD)
-IF(NOT DEFINED C_HAS_THREAD)
+IF(NOT MSVC)
+  CHECK_C_SOURCE_COMPILES("static __thread int x = 1; int main() { return x; }" C_HAS_THREAD)
+ELSE(NOT MSVC)
+  CHECK_C_SOURCE_COMPILES("static __declspec( thread ) int x = 1; int main() { return x; }" C_HAS_THREAD)
+ENDIF(NOT MSVC)
+IF(NOT C_HAS_THREAD)
   MESSAGE(STATUS "Warning: __thread is not supported, generating thread-unsafe code")
-ENDIF(NOT DEFINED C_HAS_THREAD)
-IF(C_HAS_THREAD)
+ELSE(NOT C_HAS_THREAD)
   SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DTH_HAVE_THREAD")
-ENDIF(C_HAS_THREAD)
+ENDIF(NOT C_HAS_THREAD)
 
 INCLUDE_DIRECTORIES("${CMAKE_CURRENT_BINARY_DIR}")
 CONFIGURE_FILE(THGeneral.h.in "${CMAKE_CURRENT_BINARY_DIR}/THGeneral.h")
diff --git a/lib/TH/THAllocator.c b/lib/TH/THAllocator.c
index d64b752..7730ba5 100644
--- a/lib/TH/THAllocator.c
+++ b/lib/TH/THAllocator.c
@@ -112,8 +112,7 @@ static void *_map_alloc(void* ctx_, long size)
   {
     HANDLE hfile;
     HANDLE hmfile;
-    DWORD size_hi, size_lo;
-    size_t hfilesz;
+    LARGE_INTEGER hfilesz;
 
     if (ctx->flags & TH_ALLOCATOR_MAPPED_EXCLUSIVE)
       THError("exclusive file mapping is not supported on Windows");
@@ -130,78 +129,62 @@ static void *_map_alloc(void* ctx_, long size)
     {
       hfile = CreateFileA(ctx->filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
       if (hfile == INVALID_HANDLE_VALUE)
-        THError("could not open file <%s> in read-write mode", ctx->filename);
+        THError("could not open file <%s> in read-write mode; error code: <%d>", ctx->filename, GetLastError());
     }
     else
     {
       hfile = CreateFileA(ctx->filename, GENERIC_READ, FILE_SHARE_WRITE|FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
       if (hfile == INVALID_HANDLE_VALUE)
-        THError("could not open file <%s> in read-only mode", ctx->filename);
+        THError("could not open file <%s> in read-only mode; error code: <%d>", ctx->filename, GetLastError());
     }
 
-    size_lo = GetFileSize(hfile, &size_hi);
-    if(sizeof(size_t) > 4)
+    if (GetFileSizeEx(hfile, &hfilesz) == 0)
     {
-      hfilesz = ((size_t)size_hi) << 32;
-      hfilesz |= size_lo;
+      THError("could not get file size: <%s>; error code: <%d>", ctx->filename, GetLastError());
     }
-    else
-      hfilesz = (size_t)(size_lo);
 
     if(size > 0)
     {
-      if(size > hfilesz)
+      if(size > hfilesz.QuadPart)
       {
         if(ctx->flags)
         {
-#if SIZEOF_SIZE_T > 4
-          size_hi = (DWORD)((size) >> 32);
-          size_lo = (DWORD)((size) & 0xFFFFFFFF);
-#else
-          size_hi = 0;
-          size_lo = (DWORD)(size);
-#endif
-          if((SetFilePointer(hfile, size_lo, &size_hi, FILE_BEGIN)) == INVALID_SET_FILE_POINTER)
+          hfilesz.QuadPart = size;
+          if(SetFilePointerEx(hfile, hfilesz, NULL, FILE_BEGIN) == 0)
           {
             CloseHandle(hfile);
-            THError("unable to stretch file <%s> to the right size", ctx->filename);
+            THError("unable to stretch file <%s> to the right size; error code: <%d>", ctx->filename, GetLastError());
           }
           if(SetEndOfFile(hfile) == 0)
           {
             CloseHandle(hfile);
-            THError("unable to write to file <%s>", ctx->filename);
+            THError("unable to write to file <%s>; error code: <%d>", ctx->filename, GetLastError());
           }
         }
         else
         {
           CloseHandle(hfile);
-          THError("file <%s> size is smaller than the required mapping size <%ld>", ctx->filename, size);
+          THError("file <%s> size is smaller than the required mapping size <%ld>; error code: <%d>", ctx->filename, size, GetLastError());
         }
       }
     }
     else
-      size = hfilesz;
+      size = hfilesz.QuadPart;
 
     ctx->size = size; /* if we are here, it must be the right size */
 
-#if SIZEOF_SIZE_T > 4
-    size_hi = (DWORD)((ctx->size) >> 32);
-    size_lo = (DWORD)((ctx->size) & 0xFFFFFFFF);
-#else
-    size_hi = 0;
-    size_lo = (DWORD)(ctx->size);
-#endif
+    hfilesz.QuadPart = ctx->size;
 
     /* get map handle */
     if(ctx->flags)
     {
-      if( (hmfile = CreateFileMapping(hfile, NULL, PAGE_READWRITE, size_hi, size_lo, NULL)) == NULL )
-        THError("could not create a map on file <%s>", ctx->filename);
+      if( (hmfile = CreateFileMapping(hfile, NULL, PAGE_READWRITE, hfilesz.HighPart, hfilesz.LowPart, NULL)) == NULL )
+        THError("could not create a map on file <%s>; error code: <%d>", ctx->filename, GetLastError());
     }
     else
     {
-      if( (hmfile = CreateFileMapping(hfile, NULL, PAGE_WRITECOPY, size_hi, size_lo, NULL)) == NULL )
-        THError("could not create a map on file <%s>", ctx->filename);
+      if( (hmfile = CreateFileMapping(hfile, NULL, PAGE_WRITECOPY, hfilesz.HighPart, hfilesz.LowPart, NULL)) == NULL )
+        THError("could not create a map on file <%s>; error code: <%d>", ctx->filename, GetLastError());
     }
 
     /* map the stuff */
@@ -356,7 +339,7 @@ static void THMapAllocator_free(void* ctx_, void* data) {
   THMapAllocatorContext *ctx = ctx_;
 
 #ifdef _WIN32
-  if(!UnmapViewOfFile((LPINT)data))
+  if(UnmapViewOfFile(data) == 0)
     THError("could not unmap the shared memory file");
 #else /* _WIN32 */
   if (ctx->flags & TH_ALLOCATOR_MAPPED_KEEPFD) {
@@ -447,7 +430,7 @@ static void THRefcountedMapAllocator_free(void* ctx_, void* data) {
   THMapAllocatorContext *ctx = ctx_;
 
 #ifdef _WIN32
-  if(!UnmapViewOfFile((LPINT)data))
+  if(UnmapViewOfFile(data) == 0)
     THError("could not unmap the shared memory file");
 #else /* _WIN32 */
 
diff --git a/lib/TH/THAtomic.c b/lib/TH/THAtomic.c
index e04dcb3..aa70d93 100644
--- a/lib/TH/THAtomic.c
+++ b/lib/TH/THAtomic.c
@@ -11,6 +11,7 @@
 
 #if defined(USE_MSC_ATOMICS)
 #include <intrin.h>
+#include <assert.h>
 #endif
 
 #if !defined(USE_MSC_ATOMICS) && !defined(USE_GCC_ATOMICS) && defined(USE_PTHREAD_ATOMICS)
@@ -23,6 +24,7 @@ void THAtomicSet(int volatile *a, int newvalue)
 #if defined(USE_C11_ATOMICS)
   atomic_store(a, newvalue);
 #elif defined(USE_MSC_ATOMICS)
+  assert(sizeof(int) == sizeof(long));
   _InterlockedExchange((long*)a, newvalue);
 #elif defined(USE_GCC_ATOMICS)
   __sync_lock_test_and_set(a, newvalue);
@@ -52,6 +54,7 @@ int THAtomicAdd(int volatile *a, int value)
 #if defined(USE_C11_ATOMICS)
   return atomic_fetch_add(a, value);
 #elif defined(USE_MSC_ATOMICS)
+  assert(sizeof(int) == sizeof(long));
   return _InterlockedExchangeAdd((long*)a, value);
 #elif defined(USE_GCC_ATOMICS)
   return __sync_fetch_and_add(a, value);
@@ -79,6 +82,7 @@ int THAtomicCompareAndSwap(int volatile *a, int oldvalue, int newvalue)
 #if defined(USE_C11_ATOMICS)
   return atomic_compare_exchange_strong(a, &oldvalue, newvalue);
 #elif defined(USE_MSC_ATOMICS)
+  assert(sizeof(int) == sizeof(long));
   return (_InterlockedCompareExchange((long*)a, (long)newvalue, (long)oldvalue) == (long)oldvalue);
 #elif defined(USE_GCC_ATOMICS)
   return __sync_bool_compare_and_swap(a, oldvalue, newvalue);
diff --git a/lib/TH/THDiskFile.c b/lib/TH/THDiskFile.c
index 7064b7f..50b006f 100644
--- a/lib/TH/THDiskFile.c
+++ b/lib/TH/THDiskFile.c
@@ -2,6 +2,10 @@
 #include "THDiskFile.h"
 #include "THFilePrivate.h"
 
+#ifdef _WIN64
+#include <stdint.h>
+#endif
+
 typedef struct THDiskFile__
 {
     THFile file;
@@ -170,7 +174,10 @@ static void THDiskFile_seek(THFile *self, size_t position)
 
   THArgCheck(dfself->handle != NULL, 1, "attempt to use a closed file");
 
-#ifdef _WIN32
+#if defined(_WIN64)
+  THArgCheck(position <= (size_t)INT64_MAX, 2, "position must be smaller than INT64_MAX");
+  if(_fseeki64(dfself->handle, (__int64)position, SEEK_SET) < 0)
+#elif defined(_WIN32)
   THArgCheck(position <= (size_t)LONG_MAX, 2, "position must be smaller than LONG_MAX");
   if(fseek(dfself->handle, (long)position, SEEK_SET) < 0)
 #else
@@ -190,7 +197,13 @@ static void THDiskFile_seekEnd(THFile *self)
 
   THArgCheck(dfself->handle != NULL, 1, "attempt to use a closed file");
 
-  if(fseek(dfself->handle, 0L, SEEK_END) < 0)
+#if defined(_WIN64)
+  if(_fseeki64(dfself->handle, 0, SEEK_END) < 0)
+#elif defined(_WIN32)
+  if(fseek(dfself->handle, 0, SEEK_END) < 0)
+#else
+  if(fseeko(dfself->handle, 0, SEEK_END) < 0)
+#endif
   {
     dfself->file.hasError = 1;
     if(!dfself->file.isQuiet)
@@ -203,7 +216,13 @@ static size_t THDiskFile_position(THFile *self)
   THDiskFile *dfself = (THDiskFile*)(self);
   THArgCheck(dfself->handle != NULL, 1, "attempt to use a closed file");
 
+#if defined(_WIN64)
+  __int64 offset = _ftelli64(dfself->handle);
+#elif defined(_WIN32)
   long offset = ftell(dfself->handle);
+#else
+  off_t offset = ftello(dfself->handle);
+#endif
   if (offset > -1)
       return (size_t)offset;
   else if(!dfself->file.isQuiet)
@@ -738,7 +757,7 @@ THFile *THPipeFile_new(const char *name, const char *mode, int isQuiet)
   THArgCheck(THPipeFile_mode(mode, &isReadable, &isWritable), 2, "file mode should be 'r','w'");
 
 #ifdef _WIN32
-  handle = popen(name, (isReadable ? "rb" : "wb"));
+  handle = _popen(name, (isReadable ? "rb" : "wb"));
 #else
   handle = popen(name, (isReadable ? "r" : "w"));
 #endif
diff --git a/lib/TH/THGeneral.c b/lib/TH/THGeneral.c
index 4bd4c67..d26af0a 100644
--- a/lib/TH/THGeneral.c
+++ b/lib/TH/THGeneral.c
@@ -3,6 +3,8 @@
 
 #ifndef TH_HAVE_THREAD
 #define __thread
+#elif _MSC_VER
+#define __thread __declspec( thread )
 #endif
 
 #if (defined(__unix) || defined(_WIN32))
@@ -16,14 +18,16 @@
 #endif
 
 /* Torch Error Handling */
-static void defaultTorchErrorHandlerFunction(const char *msg, void *data)
+static void defaultErrorHandlerFunction(const char *msg, void *data)
 {
   printf("$ Error: %s\n", msg);
   exit(-1);
 }
 
-static __thread void (*torchErrorHandlerFunction)(const char *msg, void *data) = defaultTorchErrorHandlerFunction;
-static __thread void *torchErrorHandlerData;
+static THErrorHandlerFunction defaultErrorHandler = defaultErrorHandlerFunction;
+static void *defaultErrorHandlerData;
+static __thread THErrorHandlerFunction threadErrorHandler = NULL;
+static __thread void *threadErrorHandlerData;
 
 void _THError(const char *file, const int line, const char *fmt, ...)
 {
@@ -40,7 +44,10 @@ void _THError(const char *file, const int line, const char *fmt, ...)
     snprintf(msg + n, 2048 - n, " at %s:%d", file, line);
   }
 
-  (*torchErrorHandlerFunction)(msg, torchErrorHandlerData);
+  if (threadErrorHandler)
+    (*threadErrorHandler)(msg, threadErrorHandlerData);
+  else
+    (*defaultErrorHandler)(msg, defaultErrorHandlerData);
 }
 
 void _THAssertionFailed(const char *file, const int line, const char *exp, const char *fmt, ...) {
@@ -52,17 +59,23 @@ void _THAssertionFailed(const char *file, const int line, const char *exp, const
   _THError(file, line, "Assertion `%s' failed. %s", exp, msg);
 }
 
-void THSetErrorHandler( void (*torchErrorHandlerFunction_)(const char *msg, void *data), void *data )
+void THSetErrorHandler(THErrorHandlerFunction new_handler, void *data)
+{
+  threadErrorHandler = new_handler;
+  threadErrorHandlerData = data;
+}
+
+void THSetDefaultErrorHandler(THErrorHandlerFunction new_handler, void *data)
 {
-  if(torchErrorHandlerFunction_)
-    torchErrorHandlerFunction = torchErrorHandlerFunction_;
+  if (new_handler)
+    defaultErrorHandler = new_handler;
   else
-    torchErrorHandlerFunction = defaultTorchErrorHandlerFunction;
-  torchErrorHandlerData = data;
+    defaultErrorHandler = defaultErrorHandlerFunction;
+  defaultErrorHandlerData = data;
 }
 
 /* Torch Arg Checking Handling */
-static void defaultTorchArgErrorHandlerFunction(int argNumber, const char *msg, void *data)
+static void defaultArgErrorHandlerFunction(int argNumber, const char *msg, void *data)
 {
   if(msg)
     printf("$ Invalid argument %d: %s\n", argNumber, msg);
@@ -71,8 +84,10 @@ static void defaultTorchArgErrorHandlerFunction(int argNumber, const char *msg,
   exit(-1);
 }
 
-static __thread void (*torchArgErrorHandlerFunction)(int argNumber, const char *msg, void *data) = defaultTorchArgErrorHandlerFunction;
-static __thread void *torchArgErrorHandlerData;
+static THArgErrorHandlerFunction defaultArgErrorHandler = defaultArgErrorHandlerFunction;
+static void *defaultArgErrorHandlerData;
+static __thread THArgErrorHandlerFunction threadArgErrorHandler = NULL;
+static __thread void *threadArgErrorHandlerData;
 
 void _THArgCheck(const char *file, int line, int condition, int argNumber, const char *fmt, ...)
 {
@@ -90,17 +105,26 @@ void _THArgCheck(const char *file, int line, int condition, int argNumber, const
       snprintf(msg + n, 2048 - n, " at %s:%d", file, line);
     }
 
-    (*torchArgErrorHandlerFunction)(argNumber, msg, torchArgErrorHandlerData);
+    if (threadArgErrorHandlerData)
+      (*threadArgErrorHandler)(argNumber, msg, threadArgErrorHandlerData);
+    else
+      (*defaultArgErrorHandler)(argNumber, msg, defaultArgErrorHandlerData);
   }
 }
 
-void THSetArgErrorHandler( void (*torchArgErrorHandlerFunction_)(int argNumber, const char *msg, void *data), void *data )
+void THSetArgErrorHandler(THArgErrorHandlerFunction new_handler, void *data)
 {
-  if(torchArgErrorHandlerFunction_)
-    torchArgErrorHandlerFunction = torchArgErrorHandlerFunction_;
+  threadArgErrorHandler = new_handler;
+  threadArgErrorHandlerData = data;
+}
+
+void THSetDefaultArgErrorHandler(THArgErrorHandlerFunction new_handler, void *data)
+{
+  if (new_handler)
+    defaultArgErrorHandler = new_handler;
   else
-    torchArgErrorHandlerFunction = defaultTorchArgErrorHandlerFunction;
-  torchArgErrorHandlerData = data;
+    defaultArgErrorHandler = defaultArgErrorHandlerFunction;
+  defaultArgErrorHandlerData = data;
 }
 
 static __thread void (*torchGCFunction)(void *data) = NULL;
@@ -232,7 +256,7 @@ void* THRealloc(void *ptr, long size)
 {
   if(!ptr)
     return(THAlloc(size));
-  
+
   if(size == 0)
   {
     THFree(ptr);
diff --git a/lib/TH/THGeneral.h.in b/lib/TH/THGeneral.h.in
index 1b68f5e..e52ba34 100644
--- a/lib/TH/THGeneral.h.in
+++ b/lib/TH/THGeneral.h.in
@@ -45,12 +45,18 @@
 #define TH_INDEX_BASE 1
 #endif
 
+typedef void (*THErrorHandlerFunction)(const char *msg, void *data);
+typedef void (*THArgErrorHandlerFunction)(int argNumber, const char *msg, void *data);
+
+
 TH_API double THLog1p(const double x);
 TH_API void _THError(const char *file, const int line, const char *fmt, ...);
 TH_API void _THAssertionFailed(const char *file, const int line, const char *exp, const char *fmt, ...);
-TH_API void THSetErrorHandler( void (*torchErrorHandlerFunction)(const char *msg, void *data), void *data );
+TH_API void THSetErrorHandler(THErrorHandlerFunction new_handler, void *data);
+TH_API void THSetDefaultErrorHandler(THErrorHandlerFunction new_handler, void *data);
 TH_API void _THArgCheck(const char *file, int line, int condition, int argNumber, const char *fmt, ...);
-TH_API void THSetArgErrorHandler( void (*torchArgErrorHandlerFunction)(int argNumber, const char *msg, void *data), void *data );
+TH_API void THSetArgErrorHandler(THArgErrorHandlerFunction new_handler, void *data);
+TH_API void THSetDefaultArgErrorHandler(THArgErrorHandlerFunction new_handler, void *data);
 TH_API void* THAlloc(long size);
 TH_API void* THRealloc(void *ptr, long size);
 TH_API void THFree(void *ptr);
diff --git a/lib/TH/THMemoryFile.c b/lib/TH/THMemoryFile.c
index d39b841..453e11e 100644
--- a/lib/TH/THMemoryFile.c
+++ b/lib/TH/THMemoryFile.c
@@ -1,5 +1,6 @@
 #include "THMemoryFile.h"
 #include "THFilePrivate.h"
+#include "stdint.h"
 
 typedef struct THMemoryFile__
 {
@@ -373,7 +374,7 @@ static size_t THMemoryFile_readLong(THFile *self, long *data, size_t n)
       size_t i;
       size_t nByte = 4*n;
       size_t nByteRemaining = (mfself->position + nByte <= mfself->size ? nByte : mfself->size-mfself->position);
-      int *storage = (int *)(mfself->storage->data + mfself->position);
+      int32_t *storage = (int32_t *)(mfself->storage->data + mfself->position);
       nread = nByteRemaining/4;
       for(i = 0; i < nread; i++)
         data[i] = storage[i];
@@ -383,12 +384,12 @@ static size_t THMemoryFile_readLong(THFile *self, long *data, size_t n)
     {
       int i, big_endian = !THDiskFile_isLittleEndianCPU();
       size_t nByte = 8*n;
-      long *storage = (long *)(mfself->storage->data + mfself->position);
+      int32_t *storage = (int32_t *)(mfself->storage->data + mfself->position);
       size_t nByteRemaining = (mfself->position + nByte <= mfself->size ? nByte : mfself->size-mfself->position);
       nread = nByteRemaining/8;
       for(i = 0; i < nread; i++)
         data[i] = storage[2*i + big_endian];
-      mfself->position += nread*4;
+      mfself->position += nread*8;
     }
   }
   else
@@ -449,8 +450,8 @@ static size_t THMemoryFile_writeLong(THFile *self, long *data, size_t n)
     {
       int i;
       size_t nByte = 4*n;
-      int *storage = (int *)(mfself->storage->data + mfself->position);
       THMemoryFile_grow(mfself, mfself->position+nByte);
+      int32_t *storage = (int32_t *)(mfself->storage->data + mfself->position);
       for(i = 0; i < n; i++)
         storage[i] = data[i];
       mfself->position += nByte;
@@ -459,8 +460,8 @@ static size_t THMemoryFile_writeLong(THFile *self, long *data, size_t n)
     {
       int i, big_endian = !THDiskFile_isLittleEndianCPU();
       size_t nByte = 8*n;
-      long *storage = (long *)(mfself->storage->data + mfself->position);
       THMemoryFile_grow(mfself, mfself->position+nByte);
+      int32_t *storage = (int32_t *)(mfself->storage->data + mfself->position);
       for(i = 0; i < n; i++)
       {
         storage[2*i + !big_endian] = 0;
diff --git a/lib/TH/cmake/FindMKL.cmake b/lib/TH/cmake/FindMKL.cmake
index 8dc3cde..e68ae6a 100644
--- a/lib/TH/cmake/FindMKL.cmake
+++ b/lib/TH/cmake/FindMKL.cmake
@@ -55,6 +55,9 @@ ELSE(CMAKE_COMPILER_IS_GNUCC)
   SET(mklthreads "mkl_intel_thread")
   SET(mklifaces  "intel")
   SET(mklrtls "iomp5" "guide")
+  IF (MSVC)
+    SET(mklrtls "libiomp5md")
+  ENDIF (MSVC)
 ENDIF (CMAKE_COMPILER_IS_GNUCC)
 
 # Kernel libraries dynamically loaded
@@ -80,6 +83,10 @@ IF (INTEL_MKL_DIR)
     "${INTEL_MKL_DIR}/include")
   SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH}
     "${INTEL_MKL_DIR}/lib/${mklvers}")
+  IF (MSVC)
+    SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH}
+      "${INTEL_MKL_DIR}/lib/${iccvers}")
+  ENDIF (MSVC)
 ENDIF (INTEL_MKL_DIR)
 
 # Try linking multiple libs
diff --git a/lib/TH/generic/THTensorMath.c b/lib/TH/generic/THTensorMath.c
index cae5959..a324191 100644
--- a/lib/TH/generic/THTensorMath.c
+++ b/lib/TH/generic/THTensorMath.c
@@ -1761,7 +1761,7 @@ void THTensor_(mode)(THTensor *values_, THLongTensor *indices_, THTensor *t, int
 
   TH_TENSOR_DIM_APPLY3(real, t, real, values_, long, indices_, dimension,
                        long i;
-                       long mode = 0;
+                       real mode = 0;
                        long modei = 0;
                        long temp_freq = 0;
                        long max_freq = 0;
diff --git a/lib/TH/generic/simd/simd.h b/lib/TH/generic/simd/simd.h
index e4660b1..d070450 100644
--- a/lib/TH/generic/simd/simd.h
+++ b/lib/TH/generic/simd/simd.h
@@ -2,6 +2,9 @@
 #define TH_SIMD_INC
 
 #include <stdint.h>
+#ifdef _MSC_VER
+#include <intrin.h>
+#endif
 
 // Can be found on Intel ISA Reference for CPUID
 #define CPUID_AVX2_BIT 0x10       // Bit 5 of EBX for EAX=0x7
@@ -56,13 +59,22 @@ static inline uint32_t detectHostSIMDExtensions()
 
 static inline void cpuid(uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
 {
+#ifndef _MSC_VER
   uint32_t a = *eax, b, c, d;
   asm volatile ( "cpuid\n\t"
-                 : "+a"(a), "=b"(b), "+c"(c), "=d"(d) );
+                 : "+a"(a), "=b"(b), "=c"(c), "=d"(d) );
   *eax = a;
   *ebx = b;
   *ecx = c;
   *edx = d;
+#else
+  uint32_t cpuInfo[4];
+  __cpuid(cpuInfo, *eax);
+  *eax = cpuInfo[0];
+  *ebx = cpuInfo[1];
+  *ecx = cpuInfo[2];
+  *edx = cpuInfo[3];
+#endif
 }
 
 static inline uint32_t detectHostSIMDExtensions()
diff --git a/lib/TH/vector/NEON.c b/lib/TH/vector/NEON.c
index 9d65550..ee4eb81 100644
--- a/lib/TH/vector/NEON.c
+++ b/lib/TH/vector/NEON.c
@@ -29,7 +29,7 @@ static void THFloatVector_fill_NEON(float *x, const float c, const long n) {
 }
 
 
-static void THFloatVector_diff_NEON(float *y, const float *x, const float c, const long n) {
+static void THFloatVector_diff_NEON(float *z, const float *x, const float *y, const long n) {
   __asm__ __volatile__ (
       "mov         r0, %2           @ \n\t"
       "mov         r1, %1           @ \n\t"
diff --git a/lib/TH/vector/SSE.c b/lib/TH/vector/SSE.c
index f909907..c47e28d 100644
--- a/lib/TH/vector/SSE.c
+++ b/lib/TH/vector/SSE.c
@@ -1,4 +1,8 @@
+#ifndef _MSC_VER
 #include <x86intrin.h>
+#else
+#include <intrin.h>
+#endif
 
 
 static void THDoubleVector_fill_SSE(double *x, const double c, const long n) {
diff --git a/test/longSize.lua b/test/longSize.lua
index 82eef04..f5eba86 100644
--- a/test/longSize.lua
+++ b/test/longSize.lua
@@ -1,42 +1,61 @@
-tensor = torch.rand(2,3)
-f = torch.DiskFile('tensor8.bin','w')
-f:binary()
-f:longSize(8)
-f:writeObject(tensor)
-f:close()
-f = torch.DiskFile('tensor8.bin','r')
-f:binary()
-f:longSize(8)
-tensor2 = f:readObject()
-f:close()
-print('Tensors are same: ',tensor:norm()==tensor2:norm())
+require 'torch'
 
-f = torch.DiskFile('tensor4.bin','w')
-f:binary()
-f:longSize(4)
-f:writeObject(tensor)
-f:close()
-f = torch.DiskFile('tensor4.bin','r')
-f:binary()
-f:longSize(4)
-tensor2 = f:readObject()
-f:close()
-print('Tensors are same: ',tensor:norm()==tensor2:norm())
+local tester = torch.Tester()
+local tests = torch.TestSuite()
 
-f = torch.MemoryFile()
-f:binary()
-f:longSize(8)
-f:writeObject(tensor)
-f:seek(1)
-tensor2 = f:readObject()
-f:close()
-print('Tensors are same: ',tensor:norm()==tensor2:norm())
+local tensor = torch.rand(2,3)
 
-f = torch.MemoryFile()
-f:binary()
-f:longSize(4)
-f:writeObject(tensor)
-f:seek(1)
-tensor2 = f:readObject()
-f:close()
-print('Tensors are same: ',tensor:norm()==tensor2:norm())
+function tests.diskFileLongSize8()
+  f = torch.DiskFile('tensor8.bin','w')
+  f:binary()
+  f:longSize(8)
+  f:writeObject(tensor)
+  f:close()
+  f = torch.DiskFile('tensor8.bin','r')
+  f:binary()
+  f:longSize(8)
+  tensor2 = f:readObject()
+  f:close()
+  tester:assert(tensor:norm()==tensor2:norm())
+  os.remove('tensor8.bin')
+end
+
+function tests.diskFileLongSize4()
+  f = torch.DiskFile('tensor4.bin','w')
+  f:binary()
+  f:longSize(4)
+  f:writeObject(tensor)
+  f:close()
+  f = torch.DiskFile('tensor4.bin','r')
+  f:binary()
+  f:longSize(4)
+  tensor2 = f:readObject()
+  f:close()
+  tester:assert(tensor:norm()==tensor2:norm())
+  os.remove('tensor4.bin')
+end
+
+function tests.memoryFileLongSize8()
+  f = torch.MemoryFile()
+  f:binary()
+  f:longSize(8)
+  f:writeObject(tensor)
+  f:seek(1)
+  tensor2 = f:readObject()
+  f:close()
+  tester:assert(tensor:norm()==tensor2:norm())
+end
+
+function tests.memoryFileLongSize4()
+  f = torch.MemoryFile()
+  f:binary()
+  f:longSize(4)
+  f:writeObject(tensor)
+  f:seek(1)
+  tensor2 = f:readObject()
+  f:close()
+  tester:assert(tensor:norm()==tensor2:norm())
+end
+
+tester:add(tests)
+tester:run()
diff --git a/test/test.lua b/test/test.lua
index 21df3b6..547a9f8 100644
--- a/test/test.lua
+++ b/test/test.lua
@@ -234,7 +234,7 @@ function torchtest.frac()
    end
 
    local f
-   local t = genericSingleOpTest:gsub('functionname', 'frac'):gsub('math.frac', 'TH_frac')   
+   local t = genericSingleOpTest:gsub('functionname', 'frac'):gsub('math.frac', 'TH_frac')
    local env = { TH_frac=TH_frac, torch=torch, math=math }
    if not setfenv then -- Lua 5.2
       f = load(t, 'test', 't', env)
@@ -1789,6 +1789,14 @@ function torchtest.mode()
    mytester:assertTensorEq(res:view(1, msize), mx, 0, 'torch.mode value')
    mytester:assertTensorEq(resix:view(1, msize), ix, 0, 'torch.mode index')
 
+   local input = torch.Tensor({
+       {1, 2, 2, 2, 3, 2},
+       {1.5, 2, 2, 1.5, 1.5, 5},
+   })
+   local value, index = torch.mode(input)
+   local expected_value = torch.Tensor({{2}, {1.5}})
+   mytester:assertTensorEq(value, expected_value)
+
    -- input unchanged
    mytester:assertTensorEq(x, x0, 0, 'torch.mode modified input')
 end
diff --git a/test/test_sharedmem.lua b/test/test_sharedmem.lua
index 14cdeaf..1230e59 100644
--- a/test/test_sharedmem.lua
+++ b/test/test_sharedmem.lua
@@ -1,4 +1,5 @@
 require 'torch'
+local ffi = require 'ffi'
 
 local tester = torch.Tester()
 local tests = torch.TestSuite()
@@ -13,38 +14,52 @@ local function createSharedMemStorage(name, size, storageType)
   return storage, shmName
 end
 
+local function shmFilePath(shmName)
+  return (ffi.os ~= 'Windows' and '/dev/shm/' or '') .. shmName
+end
+
+local function removeShmFile(shmFileName)
+  if ffi.os == 'Windows' then
+    os.remove(shmFileName)
+  end
+end
+
 function tests.createSharedMemFile()
   local storage, shmName = createSharedMemStorage()
+  local shmFileName = shmFilePath(shmName)
 
   -- check that file is at /dev/shm
-  tester:assert(paths.filep('/dev/shm/' .. shmName),
-                'Shared memory file does not exist')
+  tester:assert(paths.filep(shmFileName),
+                'Shared memory file exists')
 
   -- collect storage and make sure that file is gone
   storage = nil
   collectgarbage()
   collectgarbage()
-  tester:assert(not paths.filep('/dev/shm/' .. shmName),
-                'Shared memory file still exists')
+  removeShmFile(shmFileName)
+  tester:assert(not paths.filep(shmFileName),
+                'Shared memory file does not exists')
 end
 
 function tests.checkContents()
   local storage, shmName = createSharedMemStorage()
+  local shmFileName = shmFilePath(shmName)
   local tensor = torch.FloatTensor(storage, 1, torch.LongStorage{storage:size()})
   tensor:copy(torch.rand(storage:size()))
 
-  local sharedFile = torch.DiskFile('/dev/shm/'..shmName, 'r'):binary()
+  local sharedFile = torch.DiskFile(shmFileName, 'r'):binary()
   for i = 1, storage:size() do
     tester:assert(sharedFile:readFloat() == storage[i], 'value is not correct')
   end
   sharedFile:close()
+  removeShmFile(shmFileName)
 end
 
 function tests.testSharing()
   -- since we are going to cast numbers into double (lua default)
   -- we specifically generate double storage
   local storage, shmName = createSharedMemStorage(nil, nil, 'DoubleStorage')
-  local shmFileName = '/dev/shm/' .. shmName
+  local shmFileName = shmFilePath(shmName)
   local tensor = torch.DoubleTensor(storage, 1, torch.LongStorage{storage:size()})
   tensor:copy(torch.rand(storage:size()))
   local tensorCopy = tensor.new():resizeAs(tensor):copy(tensor)
@@ -70,6 +85,7 @@ function tests.testSharing()
   for i = 1, tensor:size(1) do
     tester:asserteq(tensor[i], rval, 'content is wrong')
   end
+  removeShmFile(shmFileName)
 end
 
 tester:add(tests)
diff --git a/test/test_timer.lua b/test/test_timer.lua
new file mode 100644
index 0000000..ecf576a
--- /dev/null
+++ b/test/test_timer.lua
@@ -0,0 +1,52 @@
+require 'torch'
+local ffi = require 'ffi'
+
+local tester = torch.Tester()
+local tests = torch.TestSuite()
+
+function tests.timerTime()
+  local timer = torch.Timer()
+
+  local function wait(seconds)
+    if ffi.os == 'Windows' then
+        os.execute(string.format('ping 127.0.0.1 -n %d > nul', seconds + 1))
+    else
+        os.execute(string.format('sleep %d > nul', seconds))
+    end
+  end
+
+  timer:reset()
+  wait(1)
+  local passed_time = timer:time().real
+  tester:assert(passed_time < 1.1,
+               ("Too long time passed: %.1f sec >= 1.1 sec"):format(passed_time))
+  tester:assert(passed_time > 0.9,
+               ("Too short time passed:  %.1f sec <= 0.9 sec"):format(passed_time))
+
+  timer:stop()
+  wait(1)
+  passed_time = timer:time().real
+  tester:assert(passed_time < 1.1,
+               ("Too long time passed: %.1f sec >= 1.1 sec"):format(passed_time))
+  tester:assert(passed_time > 0.9,
+               ("Too short time passed:  %.1f sec <= 0.9 sec"):format(passed_time))
+
+  timer:resume()
+  wait(1)
+  passed_time = timer:time().real
+  tester:assert(passed_time < 2.2,
+               ("Too long time passed: %.1f sec >= 2.2 sec"):format(passed_time))
+  tester:assert(passed_time > 1.8,
+               ("Too short time passed:  %.1f sec <= 1.8 sec"):format(passed_time))
+
+  timer:reset()
+  wait(1)
+  passed_time = timer:time().real
+  tester:assert(passed_time < 1.1,
+               ("Too long time passed: %.1f sec >= 1.1 sec"):format(passed_time))
+  tester:assert(passed_time > 0.9,
+               ("Too short time passed:  %.1f sec <= 0.9 sec"):format(passed_time))
+end
+
+tester:add(tests)
+tester:run()
diff --git a/utils.c b/utils.c
index 35fdae4..eb7ff53 100644
--- a/utils.c
+++ b/utils.c
@@ -61,12 +61,18 @@ int torch_islongargs(lua_State *L, int index)
   return 0;
 }
 
+#ifdef _WIN32
+#include <windows.h>
+#include <io.h>
+static __declspec( thread ) LARGE_INTEGER ticksPerSecond = { 0 };
+#endif
+
 static int torch_isatty(lua_State *L)
 {
+  FILE **fp = (FILE **) luaL_checkudata(L, -1, LUA_FILEHANDLE);
 #ifdef _WIN32
-  lua_pushboolean(L, 0);
+  lua_pushboolean(L, _isatty(_fileno(*fp)));
 #else
-  FILE **fp = (FILE **) luaL_checkudata(L, -1, LUA_FILEHANDLE);
   lua_pushboolean(L, isatty(fileno(*fp)));
 #endif
   return 1;
@@ -75,9 +81,13 @@ static int torch_isatty(lua_State *L)
 static double real_time()
 {
 #ifdef _WIN32
-  time_t ltime;
-  time(&ltime);
-  return (double)(ltime);
+  if (ticksPerSecond.QuadPart == 0)
+  {
+    QueryPerformanceFrequency(&ticksPerSecond);
+  }
+  LARGE_INTEGER current;
+  QueryPerformanceCounter(&current);
+  return (double)(current.QuadPart) / ticksPerSecond.QuadPart;
 #else
   struct timeval current;
   gettimeofday(&current, NULL);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/lua-torch-torch7.git



More information about the debian-science-commits mailing list