forked from torch/torch7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
255 lines (232 loc) · 5.29 KB
/
utils.c
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
#include "general.h"
#include "utils.h"
#ifdef WIN32
# include <time.h>
#else
# include <sys/time.h>
#endif
THLongStorage* torch_checklongargs(lua_State *L, int index)
{
THLongStorage *storage;
int i;
int narg = lua_gettop(L)-index+1;
if(narg == 1 && luaT_toudata(L, index, "torch.LongStorage"))
{
THLongStorage *storagesrc = luaT_toudata(L, index, "torch.LongStorage");
storage = THLongStorage_newWithSize(storagesrc->size);
THLongStorage_copy(storage, storagesrc);
}
else
{
storage = THLongStorage_newWithSize(narg);
for(i = index; i < index+narg; i++)
{
if(!lua_isnumber(L, i))
{
THLongStorage_free(storage);
luaL_argerror(L, i, "number expected");
}
THLongStorage_set(storage, i-index, lua_tonumber(L, i));
}
}
return storage;
}
int torch_islongargs(lua_State *L, int index)
{
int narg = lua_gettop(L)-index+1;
if(narg == 1 && luaT_toudata(L, index, "torch.LongStorage"))
{
return 1;
}
else
{
int i;
for(i = index; i < index+narg; i++)
{
if(!lua_isnumber(L, i))
return 0;
}
return 1;
}
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, _isatty(_fileno(*fp)));
#else
lua_pushboolean(L, isatty(fileno(*fp)));
#endif
return 1;
}
static double real_time()
{
#ifdef _WIN32
if (ticksPerSecond.QuadPart == 0)
{
QueryPerformanceFrequency(&ticksPerSecond);
}
LARGE_INTEGER current;
QueryPerformanceCounter(¤t);
return (double)(current.QuadPart) / ticksPerSecond.QuadPart;
#else
struct timeval current;
gettimeofday(¤t, NULL);
return (current.tv_sec + current.tv_usec/1000000.0);
#endif
}
static int torch_lua_tic(lua_State* L)
{
double ttime = real_time();
lua_pushnumber(L,ttime);
return 1;
}
static int torch_lua_toc(lua_State* L)
{
double toctime = real_time();
lua_Number tictime = luaL_checknumber(L,1);
lua_pushnumber(L,toctime-tictime);
return 1;
}
static int torch_lua_getdefaulttensortype(lua_State *L)
{
const char* tname = torch_getdefaulttensortype(L);
if(tname)
{
lua_pushstring(L, tname);
return 1;
}
return 0;
}
const char* torch_getdefaulttensortype(lua_State *L)
{
lua_getglobal(L, "torch");
if(lua_istable(L, -1))
{
lua_getfield(L, -1, "Tensor");
if(lua_istable(L, -1))
{
if(lua_getmetatable(L, -1))
{
lua_pushstring(L, "__index");
lua_rawget(L, -2);
if(lua_istable(L, -1))
{
lua_rawget(L, LUA_REGISTRYINDEX);
if(lua_isstring(L, -1))
{
const char *tname = lua_tostring(L, -1);
lua_pop(L, 4);
return tname;
}
}
else
{
lua_pop(L, 4);
return NULL;
}
}
else
{
lua_pop(L, 2);
return NULL;
}
}
else
{
lua_pop(L, 2);
return NULL;
}
}
else
{
lua_pop(L, 1);
return NULL;
}
return NULL;
}
static int torch_getnumthreads(lua_State *L)
{
lua_pushinteger(L, THGetNumThreads());
return 1;
}
static int torch_setnumthreads(lua_State *L)
{
THSetNumThreads(luaL_checkint(L, 1));
return 0;
}
static int torch_getnumcores(lua_State *L)
{
lua_pushinteger(L, THGetNumCores());
return 1;
}
static void luaTorchGCFunction(void *data)
{
lua_State *L = data;
lua_gc(L, LUA_GCCOLLECT, 0);
}
static int torch_setheaptracking(lua_State *L)
{
int enabled = luaT_checkboolean(L,1);
lua_getglobal(L, "torch");
lua_pushboolean(L, enabled);
lua_setfield(L, -2, "_heaptracking");
if(enabled) {
THSetGCHandler(luaTorchGCFunction, L);
} else {
THSetGCHandler(NULL, NULL);
}
return 0;
}
static void luaTorchErrorHandlerFunction(const char *msg, void *data)
{
lua_State *L = data;
luaL_error(L, msg);
}
static void luaTorchArgErrorHandlerFunction(int argNumber, const char *msg, void *data)
{
lua_State *L = data;
luaL_argcheck(L, 0, argNumber, msg);
}
static int torch_updateerrorhandlers(lua_State *L)
{
THSetErrorHandler(luaTorchErrorHandlerFunction, L);
THSetArgErrorHandler(luaTorchArgErrorHandlerFunction, L);
return 0;
}
static const struct luaL_Reg torch_utils__ [] = {
{"getdefaulttensortype", torch_lua_getdefaulttensortype},
{"isatty", torch_isatty},
{"tic", torch_lua_tic},
{"toc", torch_lua_toc},
{"setnumthreads", torch_setnumthreads},
{"getnumthreads", torch_getnumthreads},
{"getnumcores", torch_getnumcores},
{"factory", luaT_lua_factory},
{"getconstructortable", luaT_lua_getconstructortable},
{"typename", luaT_lua_typename},
{"isequal", luaT_lua_isequal},
{"getenv", luaT_lua_getenv},
{"setenv", luaT_lua_setenv},
{"newmetatable", luaT_lua_newmetatable},
{"setmetatable", luaT_lua_setmetatable},
{"getmetatable", luaT_lua_getmetatable},
{"metatype", luaT_lua_metatype},
{"pushudata", luaT_lua_pushudata},
{"version", luaT_lua_version},
{"pointer", luaT_lua_pointer},
{"setheaptracking", torch_setheaptracking},
{"updateerrorhandlers", torch_updateerrorhandlers},
{NULL, NULL}
};
void torch_utils_init(lua_State *L)
{
torch_updateerrorhandlers(L);
luaT_setfuncs(L, torch_utils__, 0);
}