forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
THGeneral.cpp
328 lines (274 loc) · 7.25 KB
/
THGeneral.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include <TH/THGeneral.h>
#ifdef _OPENMP
#include <omp.h>
#endif
#ifndef TH_HAVE_THREAD
#define __thread
#elif _MSC_VER
#define __thread __declspec( thread )
#endif
#if (defined(__unix) || defined(_WIN32))
#if defined(__FreeBSD__)
#include <malloc_np.h>
#else
#include <malloc.h>
#endif
#elif defined(__APPLE__)
#include <malloc/malloc.h>
#endif
#ifdef TH_BLAS_MKL
// this is the C prototype, while mkl_set_num_threads is the fortran prototype
TH_EXTERNC void MKL_Set_Num_Threads(int);
// this is the C prototype, while mkl_get_max_threads is the fortran prototype
TH_EXTERNC int MKL_Get_Max_Threads(void);
#endif
/* Torch Error Handling */
static void defaultErrorHandlerFunction(const char *msg, void *data)
{
printf("$ Error: %s\n", msg);
exit(-1);
}
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, ...)
{
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);
}
if (threadErrorHandler)
(*threadErrorHandler)(msg, threadErrorHandlerData);
else
(*defaultErrorHandler)(msg, defaultErrorHandlerData);
TH_UNREACHABLE;
}
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(THErrorHandlerFunction new_handler, void *data)
{
threadErrorHandler = new_handler;
threadErrorHandlerData = data;
}
void THSetDefaultErrorHandler(THErrorHandlerFunction new_handler, void *data)
{
if (new_handler)
defaultErrorHandler = new_handler;
else
defaultErrorHandler = defaultErrorHandlerFunction;
defaultErrorHandlerData = data;
}
/* Torch Arg Checking Handling */
static void defaultArgErrorHandlerFunction(int argNumber, const char *msg, void *data)
{
if(msg)
printf("$ Invalid argument %d: %s\n", argNumber, msg);
else
printf("$ Invalid argument %d\n", argNumber);
exit(-1);
}
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, ...)
{
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);
}
if (threadArgErrorHandler)
(*threadArgErrorHandler)(argNumber, msg, threadArgErrorHandlerData);
else
(*defaultArgErrorHandler)(argNumber, msg, defaultArgErrorHandlerData);
TH_UNREACHABLE;
}
}
void THSetArgErrorHandler(THArgErrorHandlerFunction new_handler, void *data)
{
threadArgErrorHandler = new_handler;
threadArgErrorHandlerData = data;
}
void THSetDefaultArgErrorHandler(THArgErrorHandlerFunction new_handler, void *data)
{
if (new_handler)
defaultArgErrorHandler = new_handler;
else
defaultArgErrorHandler = defaultArgErrorHandlerFunction;
defaultArgErrorHandlerData = data;
}
static __thread void (*torchGCFunction)(void *data) = NULL;
static __thread void *torchGCData;
/* 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 void* THAllocInternal(ptrdiff_t 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);
}
return ptr;
}
void* THAlloc(ptrdiff_t 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, ptrdiff_t size)
{
if(!ptr)
return(THAlloc(size));
if(size == 0)
{
THFree(ptr);
return NULL;
}
if(size < 0)
THError("$ Torch: invalid memory size -- maybe an overflow?");
void *newptr = realloc(ptr, size);
if(!newptr && torchGCFunction) {
torchGCFunction(torchGCData);
newptr = realloc(ptr, size);
}
if(!newptr)
THError("$ Torch: not enough memory: you tried to reallocate %dGB. Buy new RAM!", size/1073741824);
return newptr;
}
void THFree(void *ptr)
{
free(ptr);
}
double THLog10(const double x)
{
return log10(x);
}
double THLog1p(const double x)
{
#if (defined(_MSC_VER) || defined(__MINGW32__))
volatile double y = 1 + x;
return log(y) - ((y-1)-x)/y ; /* cancels errors with IEEE arithmetic */
#else
return log1p(x);
#endif
}
double THLog2(const double x)
{
return log2(x);
}
double THExpm1(const double x)
{
return expm1(x);
}
void THSetNumThreads(int num_threads)
{
#ifdef _OPENMP
omp_set_num_threads(num_threads);
#endif
#ifdef TH_BLAS_MKL
MKL_Set_Num_Threads(num_threads);
#endif
}
int THGetNumThreads(void)
{
#ifdef _OPENMP
return omp_get_max_threads();
#else
return 1;
#endif
}
int THGetNumCores(void)
{
#ifdef _OPENMP
return omp_get_num_procs();
#else
return 1;
#endif
}
TH_API void THInferNumThreads(void)
{
#if defined(_OPENMP) && defined(TH_BLAS_MKL)
// If we are using MKL an OpenMP make sure the number of threads match.
// Otherwise, MKL and our OpenMP-enabled functions will keep changing the
// size of the OpenMP thread pool, resulting in worse performance (and memory
// leaks in GCC 5.4)
omp_set_num_threads(MKL_Get_Max_Threads());
#endif
}
THDescBuff _THSizeDesc(const int64_t *size, const int64_t ndim) {
const int L = TH_DESC_BUFF_LEN;
THDescBuff buf;
char *str = buf.str;
int i, n = 0;
n += snprintf(str, L-n, "[");
for (i = 0; i < ndim; i++) {
if (n >= L) break;
n += snprintf(str+n, L-n, "%" PRId64, size[i]);
if (i < ndim-1) {
n += snprintf(str+n, L-n, " x ");
}
}
if (n < L - 2) {
snprintf(str+n, L-n, "]");
} else {
snprintf(str+L-5, 5, "...]");
}
return buf;
}