From 511e50c19cb1c9466d3da94a832ab23970f56718 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Wed, 28 Jun 2017 21:35:10 +0000 Subject: [PATCH] dnn: cleanup torch integration code --- modules/dnn/src/torch/THDiskFile.cpp | 290 +---------------------- modules/dnn/src/torch/THDiskFile.h | 6 +- modules/dnn/src/torch/THFile.cpp | 48 +--- modules/dnn/src/torch/THFile.h | 41 +--- modules/dnn/src/torch/THFilePrivate.h | 14 +- modules/dnn/src/torch/THGeneral.cpp | 236 ------------------ modules/dnn/src/torch/THGeneral.h | 77 +----- modules/dnn/src/torch/torch_importer.cpp | 6 +- 8 files changed, 34 insertions(+), 684 deletions(-) diff --git a/modules/dnn/src/torch/THDiskFile.cpp b/modules/dnn/src/torch/THDiskFile.cpp index 5ba064b921..75e7d007e7 100644 --- a/modules/dnn/src/torch/THDiskFile.cpp +++ b/modules/dnn/src/torch/THDiskFile.cpp @@ -4,7 +4,7 @@ #include "THDiskFile.h" #include "THFilePrivate.h" -extern "C" +namespace TH { typedef struct THDiskFile__ @@ -36,7 +36,7 @@ static size_t fread__(void *ptr, size_t size, size_t nitems, FILE *stream) { size_t nread = 0; while(!feof(stream) && !ferror(stream) && (nread < nitems)) - nread += fread((char*)ptr+nread*size, size, THMin(2147483648UL/size, nitems-nread), stream); + nread += fread((char*)ptr+nread*size, size, std::min(2147483648UL/size, nitems-nread), stream); return nread; } #else @@ -81,57 +81,7 @@ static size_t fread__(void *ptr, size_t size, size_t nitems, FILE *stream) } \ \ return nread; \ - } \ - \ - static long THDiskFile_write##TYPEC(THFile *self, TYPE *data, long n) \ - { \ - THDiskFile *dfself = (THDiskFile*)(self); \ - long nwrite = 0L; \ - \ - THArgCheck(dfself->handle != NULL, 1, "attempt to use a closed file"); \ - THArgCheck(dfself->file.isWritable, 1, "attempt to write in a read-only file"); \ - \ - if(dfself->file.isBinary) \ - { \ - if(dfself->isNativeEncoding) \ - { \ - nwrite = fwrite(data, sizeof(TYPE), n, dfself->handle); \ - } \ - else \ - { \ - if(sizeof(TYPE) > 1) \ - { \ - char *buffer = (char*)THAlloc(sizeof(TYPE)*n); \ - THDiskFile_reverseMemory(buffer, data, sizeof(TYPE), n); \ - nwrite = fwrite(buffer, sizeof(TYPE), n, dfself->handle); \ - THFree(buffer); \ - } \ - else \ - nwrite = fwrite(data, sizeof(TYPE), n, dfself->handle); \ - } \ - } \ - else \ - { \ - long i; \ - for(i = 0; i < n; i++) \ - { \ - ASCII_WRITE_ELEM; \ - if( dfself->file.isAutoSpacing && (i < n-1) ) \ - fprintf(dfself->handle, " "); \ - } \ - if(dfself->file.isAutoSpacing && (n > 0)) \ - fprintf(dfself->handle, "\n"); \ - } \ - \ - if(nwrite != n) \ - { \ - dfself->file.hasError = 1; \ - if(!dfself->file.isQuiet) \ - THError("write error: wrote %d blocks instead of %d", nwrite, n); \ - } \ - \ - return nwrite; \ -} + } static int THDiskFile_mode(const char *mode, int *isReadable, int *isWritable) { @@ -162,13 +112,6 @@ static int THDiskFile_mode(const char *mode, int *isReadable, int *isWritable) return 0; } -static void THDiskFile_synchronize(THFile *self) -{ - THDiskFile *dfself = (THDiskFile*)(self); - THArgCheck(dfself->handle != NULL, 1, "attempt to use a closed file"); - fflush(dfself->handle); -} - static void THDiskFile_seek(THFile *self, long position) { THDiskFile *dfself = (THDiskFile*)(self); @@ -326,11 +269,6 @@ static void THDiskFile_free(THFile *self) THFree(dfself); } -/* READ_WRITE_METHODS(int, Bool, */ -/* int value = 0; int ret = fscanf(file->handle, "%d", &value); array[i] = (value ? 1 : 0); if(ret <= 0) break; else result++, */ -/* int value = (array[i] ? 1 : 0); nElemWritten = fprintf(file->handle, "%d", value), */ -/* true) */ - /* Note that we do a trick */ READ_WRITE_METHODS(unsigned char, Byte, nread = fread(data, 1, n, dfself->handle); break, @@ -426,79 +364,6 @@ static long THDiskFile_readLong(THFile *self, int64 *data, long n) return nread; } -static long THDiskFile_writeLong(THFile *self, int64 *data, long n) -{ - THDiskFile *dfself = (THDiskFile*)(self); - long nwrite = 0L; - - THArgCheck(dfself->handle != NULL, 1, "attempt to use a closed file"); - THArgCheck(dfself->file.isWritable, 1, "attempt to write in a read-only file"); - - if(dfself->file.isBinary) - { - if(dfself->longSize == 0 || dfself->longSize == sizeof(long)) - { - if(dfself->isNativeEncoding) - { - nwrite = fwrite(data, sizeof(long), n, dfself->handle); - } - else - { - char *buffer = (char*)THAlloc(sizeof(long)*n); - THDiskFile_reverseMemory(buffer, data, sizeof(long), n); - nwrite = fwrite(buffer, sizeof(long), n, dfself->handle); - THFree(buffer); - } - } else if(dfself->longSize == 4) - { - int32_t *buffer = (int32_t *)THAlloc(4*n); - long i; - for(i = 0; i < n; i++) - buffer[i] = data[i]; - if(!dfself->isNativeEncoding) - THDiskFile_reverseMemory(buffer, buffer, 4, n); - nwrite = fwrite(buffer, 4, n, dfself->handle); - THFree(buffer); - } - else /* if(dfself->longSize == 8) */ - { - int big_endian = !THDiskFile_isLittleEndianCPU(); - int32_t *buffer = (int32_t*)THAlloc(8*n); - long i; - for(i = 0; i < n; i++) - { - buffer[2*i + !big_endian] = 0; - buffer[2*i + big_endian] = data[i]; - } - if(!dfself->isNativeEncoding) - THDiskFile_reverseMemory(buffer, buffer, 8, n); - nwrite = fwrite(buffer, 8, n, dfself->handle); - THFree(buffer); - } - } - else - { - long i; - for(i = 0; i < n; i++) - { - long res = 0; - int ret = fprintf(dfself->handle, "%ld", res); data[i] = res; if(ret <= 0) break; else nwrite++; - if( dfself->file.isAutoSpacing && (i < n-1) ) - fprintf(dfself->handle, " "); - } - if(dfself->file.isAutoSpacing && (n > 0)) - fprintf(dfself->handle, "\n"); - } - - if(nwrite != n) - { - dfself->file.hasError = 1; - if(!dfself->file.isQuiet) - THError("write error: wrote %d blocks instead of %d", nwrite, n); - } - - return nwrite; -} static long THDiskFile_readString(THFile *self, const char *format, char **str_) { @@ -592,25 +457,6 @@ static long THDiskFile_readString(THFile *self, const char *format, char **str_) } -static long THDiskFile_writeString(THFile *self, const char *str, long size) -{ - THDiskFile *dfself = (THDiskFile*)(self); - long nwrite; - - THArgCheck(dfself->handle != NULL, 1, "attempt to use a closed file"); - THArgCheck(dfself->file.isWritable, 1, "attempt to write in a read-only file"); - - nwrite = fwrite(str, 1, size, dfself->handle); - if(nwrite != size) - { - dfself->file.hasError = 1; - if(!dfself->file.isQuiet) - THError("write error: wrote %ld blocks instead of %ld", nwrite, size); - } - - return nwrite; -} - THFile *THDiskFile_new(const char *name, const char *mode, int isQuiet) { static struct THFileVTable vtable = { @@ -625,16 +471,6 @@ THFile *THDiskFile_new(const char *name, const char *mode, int isQuiet) THDiskFile_readDouble, THDiskFile_readString, - THDiskFile_writeByte, - THDiskFile_writeChar, - THDiskFile_writeShort, - THDiskFile_writeInt, - THDiskFile_writeLong, - THDiskFile_writeFloat, - THDiskFile_writeDouble, - THDiskFile_writeString, - - THDiskFile_synchronize, THDiskFile_seek, THDiskFile_seekEnd, THDiskFile_position, @@ -649,122 +485,12 @@ THFile *THDiskFile_new(const char *name, const char *mode, int isQuiet) THArgCheck(THDiskFile_mode(mode, &isReadable, &isWritable), 2, "file mode should be 'r','w' or 'rw'"); - if( isReadable && isWritable ) - { - handle = fopen(name, "r+b"); - if(!handle) - { - handle = fopen(name, "wb"); - if(handle) - { - fclose(handle); - handle = fopen(name, "r+b"); - } - } - } - else - handle = fopen(name, (isReadable ? "rb" : "wb")); - - if(!handle) - { - if(isQuiet) - return 0; - else - THError("cannot open <%s> in mode %c%c", name, (isReadable ? 'r' : ' '), (isWritable ? 'w' : ' ')); - } - - self = (THDiskFile*)THAlloc(sizeof(THDiskFile)); - - self->handle = handle; - self->name = (char*)THAlloc(strlen(name)+1); - strcpy(self->name, name); - self->isNativeEncoding = 1; - self->longSize = 0; - - self->file.vtable = &vtable; - self->file.isQuiet = isQuiet; - self->file.isReadable = isReadable; - self->file.isWritable = isWritable; - self->file.isBinary = 0; - self->file.isAutoSpacing = 1; - self->file.hasError = 0; - - return (THFile*)self; -} - -/* PipeFile */ - -static int THPipeFile_mode(const char *mode, int *isReadable, int *isWritable) -{ - *isReadable = 0; - *isWritable = 0; - if(strlen(mode) == 1) - { - if(*mode == 'r') - { - *isReadable = 1; - return 1; - } - else if(*mode == 'w') - { - *isWritable = 1; - return 1; - } - } - return 0; -} - -static void THPipeFile_free(THFile *self) -{ - THDiskFile *dfself = (THDiskFile*)(self); - if(dfself->handle) - pclose(dfself->handle); - THFree(dfself->name); - THFree(dfself); -} - -THFile *THPipeFile_new(const char *name, const char *mode, int isQuiet) -{ - static struct THFileVTable vtable = { - THDiskFile_isOpened, - - THDiskFile_readByte, - THDiskFile_readChar, - THDiskFile_readShort, - THDiskFile_readInt, - THDiskFile_readLong, - THDiskFile_readFloat, - THDiskFile_readDouble, - THDiskFile_readString, - - THDiskFile_writeByte, - THDiskFile_writeChar, - THDiskFile_writeShort, - THDiskFile_writeInt, - THDiskFile_writeLong, - THDiskFile_writeFloat, - THDiskFile_writeDouble, - THDiskFile_writeString, - - THDiskFile_synchronize, - THDiskFile_seek, - THDiskFile_seekEnd, - THDiskFile_position, - THDiskFile_close, - THPipeFile_free - }; - - int isReadable; - int isWritable; - FILE *handle; - THDiskFile *self; - - THArgCheck(THPipeFile_mode(mode, &isReadable, &isWritable), 2, "file mode should be 'r','w'"); - -#ifdef _WIN32 - handle = popen(name, (isReadable ? "rb" : "wb")); + CV_Assert(isReadable && !isWritable); +#ifdef _MSC_VER + if (fopen_s(&handle, name, "rb") != 0) + handle = NULL; #else - handle = popen(name, (isReadable ? "r" : "w")); + handle = fopen(name,"rb"); #endif if(!handle) diff --git a/modules/dnn/src/torch/THDiskFile.h b/modules/dnn/src/torch/THDiskFile.h index bc5c001c74..112d77fe83 100644 --- a/modules/dnn/src/torch/THDiskFile.h +++ b/modules/dnn/src/torch/THDiskFile.h @@ -3,8 +3,10 @@ #include "THFile.h" +namespace TH +{ + TH_API THFile *THDiskFile_new(const char *name, const char *mode, int isQuiet); -TH_API THFile *THPipeFile_new(const char *name, const char *mode, int isQuiet); TH_API const char *THDiskFile_name(THFile *self); @@ -16,4 +18,6 @@ TH_API void THDiskFile_bigEndianEncoding(THFile *self); TH_API void THDiskFile_longSize(THFile *self, int size); TH_API void THDiskFile_noBuffer(THFile *self); +} // namespace + #endif diff --git a/modules/dnn/src/torch/THFile.cpp b/modules/dnn/src/torch/THFile.cpp index 4be78ed5b8..081873f722 100644 --- a/modules/dnn/src/torch/THFile.cpp +++ b/modules/dnn/src/torch/THFile.cpp @@ -2,19 +2,13 @@ #if defined(ENABLE_TORCH_IMPORTER) && ENABLE_TORCH_IMPORTER #include "THFile.h" #include "THFilePrivate.h" -#include -extern "C" -{ + +namespace TH { #define IMPLEMENT_THFILE_RW(TYPEC, TYPE) \ long THFile_read##TYPEC##Raw(THFile *self, TYPE *data, long n) \ { \ return (*self->vtable->read##TYPEC)(self, data, n); \ - } \ - \ - long THFile_write##TYPEC##Raw(THFile *self, TYPE *data, long n) \ - { \ - return (*self->vtable->write##TYPEC)(self, data, n); \ } IMPLEMENT_THFILE_RW(Byte, unsigned char) @@ -30,16 +24,6 @@ long THFile_readStringRaw(THFile *self, const char *format, char **str_) return self->vtable->readString(self, format, str_); } -long THFile_writeStringRaw(THFile *self, const char *str, long size) -{ - return self->vtable->writeString(self, str, size); -} - -void THFile_synchronize(THFile *self) -{ - self->vtable->synchronize(self); -} - void THFile_seek(THFile *self, long position) { self->vtable->seek(self, position); @@ -124,11 +108,6 @@ void THFile_clearError(THFile *self) TYPE scalar; \ THFile_read##TYPEC##Raw(self, &scalar, 1); \ return scalar; \ - } \ - \ - void THFile_write##TYPEC##Scalar(THFile *self, TYPE scalar) \ - { \ - THFile_write##TYPEC##Raw(self, &scalar, 1); \ } IMPLEMENT_THFILE_SCALAR(Byte, unsigned char) @@ -139,26 +118,5 @@ IMPLEMENT_THFILE_SCALAR(Long, int64) IMPLEMENT_THFILE_SCALAR(Float, float) IMPLEMENT_THFILE_SCALAR(Double, double) -/* -#define IMPLEMENT_THFILE_STORAGE(TYPEC, TYPE) \ - long THFile_read##TYPEC(THFile *self, TH##TYPEC##Storage *storage) \ - { \ - return THFile_read##TYPEC##Raw(self, storage->data, storage->size); \ - } \ - \ - long THFile_write##TYPEC(THFile *self, TH##TYPEC##Storage *storage) \ - { \ - return THFile_write##TYPEC##Raw(self, storage->data, storage->size); \ - } - -IMPLEMENT_THFILE_STORAGE(Byte, unsigned char) -IMPLEMENT_THFILE_STORAGE(Char, char) -IMPLEMENT_THFILE_STORAGE(Short, short) -IMPLEMENT_THFILE_STORAGE(Int, int) -IMPLEMENT_THFILE_STORAGE(Long, long) -IMPLEMENT_THFILE_STORAGE(Float, float) -IMPLEMENT_THFILE_STORAGE(Double, double) -*/ - -} +} // namespace #endif diff --git a/modules/dnn/src/torch/THFile.h b/modules/dnn/src/torch/THFile.h index a496c96c0c..5ac3af2bed 100644 --- a/modules/dnn/src/torch/THFile.h +++ b/modules/dnn/src/torch/THFile.h @@ -6,6 +6,8 @@ #include "opencv2/core/hal/interface.h" #include "THGeneral.h" +namespace TH +{ typedef struct THFile__ THFile; TH_API int THFile_isOpened(THFile *self); @@ -33,33 +35,6 @@ TH_API int64 THFile_readLongScalar(THFile *self); TH_API float THFile_readFloatScalar(THFile *self); TH_API double THFile_readDoubleScalar(THFile *self); -TH_API void THFile_writeByteScalar(THFile *self, unsigned char scalar); -TH_API void THFile_writeCharScalar(THFile *self, char scalar); -TH_API void THFile_writeShortScalar(THFile *self, short scalar); -TH_API void THFile_writeIntScalar(THFile *self, int scalar); -TH_API void THFile_writeLongScalar(THFile *self, int64 scalar); -TH_API void THFile_writeFloatScalar(THFile *self, float scalar); -TH_API void THFile_writeDoubleScalar(THFile *self, double scalar); - -/* storage */ -/* -TH_API long THFile_readByte(THFile *self, THByteStorage *storage); -TH_API long THFile_readChar(THFile *self, THCharStorage *storage); -TH_API long THFile_readShort(THFile *self, THShortStorage *storage); -TH_API long THFile_readInt(THFile *self, THIntStorage *storage); -TH_API long THFile_readLong(THFile *self, THLongStorage *storage); -TH_API long THFile_readFloat(THFile *self, THFloatStorage *storage); -TH_API long THFile_readDouble(THFile *self, THDoubleStorage *storage); - -TH_API long THFile_writeByte(THFile *self, THByteStorage *storage); -TH_API long THFile_writeChar(THFile *self, THCharStorage *storage); -TH_API long THFile_writeShort(THFile *self, THShortStorage *storage); -TH_API long THFile_writeInt(THFile *self, THIntStorage *storage); -TH_API long THFile_writeLong(THFile *self, THLongStorage *storage); -TH_API long THFile_writeFloat(THFile *self, THFloatStorage *storage); -TH_API long THFile_writeDouble(THFile *self, THDoubleStorage *storage); -*/ - /* raw */ TH_API long THFile_readByteRaw(THFile *self, unsigned char *data, long n); TH_API long THFile_readCharRaw(THFile *self, char *data, long n); @@ -70,21 +45,11 @@ TH_API long THFile_readFloatRaw(THFile *self, float *data, long n); TH_API long THFile_readDoubleRaw(THFile *self, double *data, long n); TH_API long THFile_readStringRaw(THFile *self, const char *format, char **str_); /* you must deallocate str_ */ -TH_API long THFile_writeByteRaw(THFile *self, unsigned char *data, long n); -TH_API long THFile_writeCharRaw(THFile *self, char *data, long n); -TH_API long THFile_writeShortRaw(THFile *self, short *data, long n); -TH_API long THFile_writeIntRaw(THFile *self, int *data, long n); -TH_API long THFile_writeLongRaw(THFile *self, int64 *data, long n); -TH_API long THFile_writeFloatRaw(THFile *self, float *data, long n); -TH_API long THFile_writeDoubleRaw(THFile *self, double *data, long n); -TH_API long THFile_writeStringRaw(THFile *self, const char *str, long size); - -TH_API void THFile_synchronize(THFile *self); TH_API void THFile_seek(THFile *self, long position); TH_API void THFile_seekEnd(THFile *self); TH_API long THFile_position(THFile *self); TH_API void THFile_close(THFile *self); TH_API void THFile_free(THFile *self); - +} // namespace #endif //defined(ENABLE_TORCH_IMPORTER) && ENABLE_TORCH_IMPORTER #endif //TH_FILE_INC diff --git a/modules/dnn/src/torch/THFilePrivate.h b/modules/dnn/src/torch/THFilePrivate.h index dbef9f7459..af3890d8e6 100644 --- a/modules/dnn/src/torch/THFilePrivate.h +++ b/modules/dnn/src/torch/THFilePrivate.h @@ -1,3 +1,5 @@ +namespace TH { + struct THFile__ { struct THFileVTable *vtable; @@ -25,19 +27,11 @@ struct THFileVTable long (*readDouble)(THFile *self, double *data, long n); long (*readString)(THFile *self, const char *format, char **str_); - long (*writeByte)(THFile *self, unsigned char *data, long n); - long (*writeChar)(THFile *self, char *data, long n); - long (*writeShort)(THFile *self, short *data, long n); - long (*writeInt)(THFile *self, int *data, long n); - long (*writeLong)(THFile *self, int64 *data, long n); - long (*writeFloat)(THFile *self, float *data, long n); - long (*writeDouble)(THFile *self, double *data, long n); - long (*writeString)(THFile *self, const char *str, long size); - - void (*synchronize)(THFile *self); void (*seek)(THFile *self, long position); void (*seekEnd)(THFile *self); long (*position)(THFile *self); void (*close)(THFile *self); void (*free)(THFile *self); }; + +} // namespace diff --git a/modules/dnn/src/torch/THGeneral.cpp b/modules/dnn/src/torch/THGeneral.cpp index 57bd8b99f5..b0f38e0aa7 100644 --- a/modules/dnn/src/torch/THGeneral.cpp +++ b/modules/dnn/src/torch/THGeneral.cpp @@ -1,6 +1,5 @@ #include "../precomp.hpp" #if defined(ENABLE_TORCH_IMPORTER) && ENABLE_TORCH_IMPORTER -#include #if defined(TH_DISABLE_HEAP_TRACKING) #elif (defined(__unix) || defined(_WIN32)) @@ -11,239 +10,4 @@ #include "THGeneral.h" -extern "C" -{ - -#ifndef TH_HAVE_THREAD -#define TH_THREAD -#else -#define TH_THREAD __thread -#endif - -/* Torch Error Handling */ -static void defaultTorchErrorHandlerFunction(const char *msg, void*) -{ - CV_Error(cv::Error::StsError, cv::String("Torch Error: ") + msg); -} - -static TH_THREAD void (*torchErrorHandlerFunction)(const char *msg, void *data) = defaultTorchErrorHandlerFunction; -static TH_THREAD void *torchErrorHandlerData; - -void _THError(const char *file, const int line, const char *fmt, ...) -{ - char msg[2048]; - va_list args; - - /* vasprintf not standard */ - /* vsnprintf: how to handle if does not exists? */ - va_start(args, fmt); - int n = vsnprintf(msg, 2048, fmt, args); - va_end(args); - - if(n < 2048) { - snprintf(msg + n, 2048 - n, " at %s:%d", file, line); - } - - (*torchErrorHandlerFunction)(msg, torchErrorHandlerData); -} - -void _THAssertionFailed(const char *file, const int line, const char *exp, const char *fmt, ...) { - char msg[1024]; - va_list args; - va_start(args, fmt); - vsnprintf(msg, 1024, fmt, args); - va_end(args); - _THError(file, line, "Assertion `%s' failed. %s", exp, msg); -} - -void THSetErrorHandler( void (*torchErrorHandlerFunction_)(const char *msg, void *data), void *data ) -{ - if(torchErrorHandlerFunction_) - torchErrorHandlerFunction = torchErrorHandlerFunction_; - else - torchErrorHandlerFunction = defaultTorchErrorHandlerFunction; - torchErrorHandlerData = data; -} - -/* Torch Arg Checking Handling */ -static void defaultTorchArgErrorHandlerFunction(int argNumber, const char *msg, void*) -{ - if(msg) - CV_Error(cv::Error::StsError, cv::format("Torch invalid argument %d: %s", argNumber, msg)); - else - CV_Error(cv::Error::StsError, cv::format("Invalid argument %d", argNumber)); -} - -static TH_THREAD void (*torchArgErrorHandlerFunction)(int argNumber, const char *msg, void *data) = defaultTorchArgErrorHandlerFunction; -static TH_THREAD void *torchArgErrorHandlerData; - -void _THArgCheck(const char *file, int line, int condition, int argNumber, const char *fmt, ...) -{ - if(!condition) { - char msg[2048]; - va_list args; - - /* vasprintf not standard */ - /* vsnprintf: how to handle if does not exists? */ - va_start(args, fmt); - int n = vsnprintf(msg, 2048, fmt, args); - va_end(args); - - if(n < 2048) { - snprintf(msg + n, 2048 - n, " at %s:%d", file, line); - } - - (*torchArgErrorHandlerFunction)(argNumber, msg, torchArgErrorHandlerData); - } -} - -void THSetArgErrorHandler( void (*torchArgErrorHandlerFunction_)(int argNumber, const char *msg, void *data), void *data ) -{ - if(torchArgErrorHandlerFunction_) - torchArgErrorHandlerFunction = torchArgErrorHandlerFunction_; - else - torchArgErrorHandlerFunction = defaultTorchArgErrorHandlerFunction; - torchArgErrorHandlerData = data; -} - -static TH_THREAD void (*torchGCFunction)(void *data) = NULL; -static TH_THREAD void *torchGCData; -static TH_THREAD long torchHeapSize = 0; -static TH_THREAD long torchHeapSizeSoftMax = 300000000; // 300MB, adjusted upward dynamically - -/* Optional hook for integrating with a garbage-collected frontend. - * - * If torch is running with a garbage-collected frontend (e.g. Lua), - * the GC isn't aware of TH-allocated memory so may not know when it - * needs to run. These hooks trigger the GC to run in two cases: - * - * (1) When a memory allocation (malloc, realloc, ...) fails - * (2) When the total TH-allocated memory hits a dynamically-adjusted - * soft maximum. - */ -void THSetGCHandler( void (*torchGCFunction_)(void *data), void *data ) -{ - torchGCFunction = torchGCFunction_; - torchGCData = data; -} - -static long getAllocSize(void *ptr) { -#if defined(TH_DISABLE_HEAP_TRACKING) - return 0; -#elif defined(__unix) - return malloc_usable_size(ptr); -#elif defined(__APPLE__) - return malloc_size(ptr); -#elif defined(_WIN32) - return _msize(ptr); -#else - return 0; -#endif -} - -/* (1) if the torch-allocated heap size exceeds the soft max, run GC - * (2) if post-GC heap size exceeds 80% of the soft max, increase the - * soft max by 40% - */ -static void maybeTriggerGC() { - if(torchGCFunction && torchHeapSize > torchHeapSizeSoftMax) { - torchGCFunction(torchGCData); - if(torchHeapSize > torchHeapSizeSoftMax * 0.8) { - torchHeapSizeSoftMax = torchHeapSizeSoftMax * 1.4; - } - } -} - -// hooks into the TH heap tracking -void THHeapUpdate(long size) { - torchHeapSize += size; - if (size > 0) - maybeTriggerGC(); -} - -static void* THAllocInternal(long size) -{ - void *ptr; - - if (size > 5120) - { -#if (defined(__unix) || defined(__APPLE__)) && (!defined(DISABLE_POSIX_MEMALIGN)) - if (posix_memalign(&ptr, 64, size) != 0) - ptr = NULL; -/* -#elif defined(_WIN32) - ptr = _aligned_malloc(size, 64); -*/ -#else - ptr = malloc(size); -#endif - } - else - { - ptr = malloc(size); - } - - THHeapUpdate(getAllocSize(ptr)); - return ptr; -} - -void* THAlloc(long size) -{ - void *ptr; - - if(size < 0) - THError("$ Torch: invalid memory size -- maybe an overflow?"); - - if(size == 0) - return NULL; - - ptr = THAllocInternal(size); - - if(!ptr && torchGCFunction) { - torchGCFunction(torchGCData); - ptr = THAllocInternal(size); - } - - if(!ptr) - THError("$ Torch: not enough memory: you tried to allocate %dGB. Buy new RAM!", size/1073741824); - - return ptr; -} - -void* THRealloc(void *ptr, long size) -{ - if(!ptr) - return(THAlloc(size)); - - if(size == 0) - { - THFree(ptr); - return NULL; - } - - if(size < 0) - THError("$ Torch: invalid memory size -- maybe an overflow?"); - - THHeapUpdate(-getAllocSize(ptr)); - void *newptr = realloc(ptr, size); - - if(!newptr && torchGCFunction) { - torchGCFunction(torchGCData); - newptr = realloc(ptr, size); - } - THHeapUpdate(getAllocSize(newptr ? newptr : ptr)); - - if(!newptr) - THError("$ Torch: not enough memory: you tried to reallocate %dGB. Buy new RAM!", size/1073741824); - - return newptr; -} - -void THFree(void *ptr) -{ - THHeapUpdate(-getAllocSize(ptr)); - free(ptr); -} - -} #endif diff --git a/modules/dnn/src/torch/THGeneral.h b/modules/dnn/src/torch/THGeneral.h index efde1c1fa4..cdcbe3d905 100644 --- a/modules/dnn/src/torch/THGeneral.h +++ b/modules/dnn/src/torch/THGeneral.h @@ -10,78 +10,13 @@ #include #include -#ifdef __cplusplus -# define TH_EXTERNC extern "C" -#else -# define TH_EXTERNC extern -#endif +#define TH_API -#define TH_API TH_EXTERNC +#define THError(...) CV_Error(cv::Error::StsError, cv::format(__VA_ARGS__)) +#define THArgCheck(cond, ...) CV_Assert(cond) -#define THInf DBL_MAX - -//#define TH_INLINE @TH_INLINE@ - -#ifndef __cplusplus -//#define inline @TH_INLINE@ -#endif - -#ifndef M_PI -# define M_PI 3.14159265358979323846 -#endif - -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 _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* THAlloc(long size); -TH_API void* THRealloc(void *ptr, long size); -TH_API void THFree(void *ptr); -TH_API void THSetGCHandler( void (*torchGCHandlerFunction)(void *data), void *data ); -// this hook should only be called by custom allocator functions -TH_API void THHeapUpdate(long size); - -#define THError(...) _THError(__FILE__, __LINE__, __VA_ARGS__) -#define THArgCheck(...) _THArgCheck(__FILE__, __LINE__, __VA_ARGS__) -#define THAssert(exp) \ -do { \ - if (!(exp)) { \ - _THAssertionFailed(__FILE__, __LINE__, #exp, ""); \ - } \ -} while(0) -#define THAssertMsg(exp, ...) \ -do { \ - if (!(exp)) { \ - _THAssertionFailed(__FILE__, __LINE__, #exp, __VA_ARGS__); \ - } \ -} while(0) - -#define TH_CONCAT_STRING_2(x,y) TH_CONCAT_STRING_2_EXPAND(x,y) -#define TH_CONCAT_STRING_2_EXPAND(x,y) #x #y - -#define TH_CONCAT_STRING_3(x,y,z) TH_CONCAT_STRING_3_EXPAND(x,y,z) -#define TH_CONCAT_STRING_3_EXPAND(x,y,z) #x #y #z - -#define TH_CONCAT_STRING_4(x,y,z,w) TH_CONCAT_STRING_4_EXPAND(x,y,z,w) -#define TH_CONCAT_STRING_4_EXPAND(x,y,z,w) #x #y #z #w - -#define TH_CONCAT_2(x,y) TH_CONCAT_2_EXPAND(x,y) -#define TH_CONCAT_2_EXPAND(x,y) x ## y - -#define TH_CONCAT_3(x,y,z) TH_CONCAT_3_EXPAND(x,y,z) -#define TH_CONCAT_3_EXPAND(x,y,z) x ## y ## z - -#define TH_CONCAT_4_EXPAND(x,y,z,w) x ## y ## z ## w -#define TH_CONCAT_4(x,y,z,w) TH_CONCAT_4_EXPAND(x,y,z,w) - -#define THMin(X, Y) ((X) < (Y) ? (X) : (Y)) -#define THMax(X, Y) ((X) > (Y) ? (X) : (Y)) - -#if (defined(_MSC_VER) || defined(__MINGW32__)) -#define snprintf _snprintf -#define popen _popen -#define pclose _pclose -#endif +#define THAlloc malloc +#define THRealloc realloc +#define THFree free #endif diff --git a/modules/dnn/src/torch/torch_importer.cpp b/modules/dnn/src/torch/torch_importer.cpp index 98c37dbde0..d813b13559 100644 --- a/modules/dnn/src/torch/torch_importer.cpp +++ b/modules/dnn/src/torch/torch_importer.cpp @@ -47,10 +47,14 @@ #include #include +#if defined(ENABLE_TORCH_IMPORTER) && ENABLE_TORCH_IMPORTER +#include "THDiskFile.h" +#endif + namespace cv { namespace dnn { #if defined(ENABLE_TORCH_IMPORTER) && ENABLE_TORCH_IMPORTER -#include "THDiskFile.h" +using namespace TH; //#ifdef NDEBUG static bool dbgPrint = false;