forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
THGeneral.cpp
124 lines (105 loc) · 3.77 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
#include <TH/THGeneral.h>
#ifdef __cplusplus
#include <c10/core/CPUAllocator.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
/* Torch Error Handling */
static void defaultErrorHandlerFunction(const char *msg, void *data)
{
throw std::runtime_error(msg);
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static THErrorHandlerFunction defaultErrorHandler = defaultErrorHandlerFunction;
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static void *defaultErrorHandlerData;
// NOLINTNEXTLINE(modernize-use-nullptr,cppcoreguidelines-avoid-non-const-global-variables)
static __thread THErrorHandlerFunction threadErrorHandler = NULL;
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static __thread void *threadErrorHandlerData;
void _THError(const char *file, const int line, const char *fmt, ...)
{
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-avoid-magic-numbers)
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, ...) {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-avoid-magic-numbers)
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);
}
/* Torch Arg Checking Handling */
static void defaultArgErrorHandlerFunction(int argNumber, const char *msg, void *data)
{
std::stringstream new_error;
new_error << "invalid argument " << argNumber << ": " << msg;
throw std::runtime_error(new_error.str());
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static THArgErrorHandlerFunction defaultArgErrorHandler = defaultArgErrorHandlerFunction;
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static void *defaultArgErrorHandlerData;
// NOLINTNEXTLINE(modernize-use-nullptr,cppcoreguidelines-avoid-non-const-global-variables)
static __thread THArgErrorHandlerFunction threadArgErrorHandler = NULL;
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static __thread void *threadArgErrorHandlerData;
void _THArgCheck(const char *file, int line, int condition, int argNumber, const char *fmt, ...)
{
if(!condition) {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-avoid-magic-numbers)
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* THAlloc(ptrdiff_t size)
{
if(size < 0)
THError("$ Torch: invalid memory size -- maybe an overflow?");
return c10::alloc_cpu(size);
}
void THFree(void *ptr)
{
c10::free_cpu(ptr);
}