-
Notifications
You must be signed in to change notification settings - Fork 2
/
storage.lua
225 lines (197 loc) · 5.43 KB
/
storage.lua
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
-- todo:
-- RealRealStorage (plus pratique, ne serait-que pour THRealStorage, RealStorage...)
-- changer le script de template accordingly
-- make TH func return storage, tensors... this would avoid extra lua func redirections
-- prefixer les champs des structures (.data en .__data...) dans les declarations FFI
local display = require 'torch.display'
local argcheck = require 'argcheck'
local torch = require 'torch.env'
local class = require 'class'
local ffi = require 'ffi'
local C = require 'torch.TH'
local RealStorage = class('torch.RealStorage', nil, ffi.typeof('THRealStorage&'))
torch.RealStorage = RealStorage
RealStorage.__factory =
function()
local self = C.THRealStorage_new()[0]
ffi.gc(self, C.THRealStorage_free)
return self
end
RealStorage.new = argcheck{
{name="size", type="number", default=0},
nonamed = true,
call =
function(size)
local self = C.THRealStorage_newWithSize(size)[0]
ffi.gc(self, C.THRealStorage_free)
return self
end
}
argcheck{
{name="table", type="table"},
chain = RealStorage.new,
nonamed = true,
call =
function(tbl)
local size = #tbl
self = C.THRealStorage_newWithSize(size)[0]
ffi.gc(self, C.THRealStorage_free)
for i=1,size do
self.__data[i-1] = tbl[i]
end
return self
end
}
argcheck{
{name="filename", type="string"},
{name="shared", type="boolean", default=false},
{name="size", type="number", default=0},
chain = RealStorage.new,
nonamed = true,
call =
function(filename, shared, size)
local self = C.THRealStorage_newWithMapping(filename, size, shared and 1 or 0)[0]
ffi.gc(self, C.THRealStorage_free)
return self
end
}
RealStorage.fill = argcheck{
{name="self", type="torch.RealStorage"},
{name="value", type="number"},
call = C.THRealStorage_fill
}
RealStorage.size = argcheck{
{name="self", type="torch.RealStorage"},
call =
function(self)
return tonumber(self.__size)
end
}
RealStorage.resize = argcheck{
{name="self", type="torch.RealStorage"},
{name="size", type="number"},
call = C.THRealStorage_resize
}
RealStorage.rawCopy = argcheck{
{name="self", type="torch.RealStorage"},
{name="data", type="cdata"},
call =
function(self, data)
ffi.copy(self.__data, data, ffi.sizeof('real')*self.__size)
return self
end
}
RealStorage.totable = argcheck{
{name="self", type="torch.RealStorage"},
call =
function(self)
local tbl = {}
for i=1,self.__size do
tbl[i] = self.__data[i-1]
end
return tbl
end
}
if "RealStorage" == "CharRealStorage" or "RealStorage" == "ByteRealStorage" then
RealStorage.string = argcheck{
{name="self", type="torch.RealStorage"},
call =
function(self)
return ffi.string(self.__data, self.__size)
end
}
argcheck{
{name="self", type="torch.RealStorage"},
{name="data", type="string"},
chain = RealStorage.string,
call =
function(self, data)
self:resize(#data)
C.THRealStorage_rawCopy(self, ffi.cast('real*', data))
return self
end
}
end
RealStorage.copy = argcheck{
{name="self", type='torch.RealStorage'},
{name="src", type='torch.RealStorage'},
call = C.THRealStorage_copy
}
argcheck{
{name="self", type='torch.RealStorage'},
{name="src", type='torch.ByteStorage'},
chain = RealStorage.copy,
call = C.THRealStorage_copyByte
}
argcheck{
{name="self", type='torch.RealStorage'},
{name="src", type='torch.CharStorage'},
chain = RealStorage.copy,
call = C.THRealStorage_copyChar
}
argcheck{
{name="self", type='torch.RealStorage'},
{name="src", type='torch.ShortStorage'},
chain = RealStorage.copy,
call = C.THRealStorage_copyShort
}
argcheck{
{name="self", type='torch.RealStorage'},
{name="src", type='torch.IntStorage'},
chain = RealStorage.copy,
call = C.THRealStorage_copyInt
}
argcheck{
{name="self", type='torch.RealStorage'},
{name="src", type='torch.LongStorage'},
chain = RealStorage.copy,
call = C.THRealStorage_copyLong
}
argcheck{
{name="self", type='torch.RealStorage'},
{name="src", type='torch.FloatStorage'},
chain = RealStorage.copy,
call = C.THRealStorage_copyFloat
}
argcheck{
{name="self", type='torch.RealStorage'},
{name="src", type='torch.DoubleStorage'},
chain = RealStorage.copy,
call = C.THRealStorage_copyDouble
}
function RealStorage:__index(k)
if type(k) == 'number' then
if k > 0 and k <= tonumber(self.__size) then
return tonumber(self.__data[k-1])
else
error('index out of bounds')
end
else
return RealStorage[k]
end
end
function RealStorage:__newindex(k, v)
if type(k) == 'number' then
if k > 0 and k <= self.__size then
self.__data[k-1] = v
else
error('index out of bounds')
end
else
rawset(self, k, v)
end
end
function RealStorage:__len()
return self.__size
end
function RealStorage:write(file)
file:writeLong(self.__size)
file:writeRaw('real', self.__data, self.__size)
end
function RealStorage:read(file)
local size = file:readLong()
rawInitWithSize(self, size)
file:readRaw('real', self.__data, self.__size)
end
RealStorage.__tostring = display.storage
ffi.metatype('THRealStorage', getmetatable(RealStorage))