Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sharedserialize.lua - github.com/torch/threads-ffi.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fbd789a9432704a0ede17be12ddd7c479bb2fcd8 (plain)
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
require 'torch'
local ffi = require 'ffi'

local serialize = {}
local typenames = {}

local function serializePointer(obj, f)
   -- on 32-bit systems double can represent all possible
   -- pointer values, but signed long can't
   if ffi.sizeof('long') == 4 then
      f:writeDouble(torch.pointer(obj))
   -- on 64-bit systems, long can represent a larger
   -- range of integers than double, so it's safer to use this
   else
      f:writeLong(torch.pointer(obj))
   end
end

local function deserializePointer(f)
   if ffi.sizeof('long') == 4 then
      return f:readDouble()
   else
      return f:readLong()
   end
end

-- tds support
local _, tds = pcall(require, 'tds') -- for the free/retain functions
if tds then

   -- hash
   local mt = {}
   function mt.__factory(f)
      local self = deserializePointer(f)
      self = ffi.cast('tds_hash&', self)
      ffi.gc(self, tds.C.tds_hash_free)
      return self
   end
   function mt.__write(self, f)
      serializePointer(self, f)
      tds.C.tds_hash_retain(self)
   end
   function mt.__read(self, f)
   end
   typenames['tds.Hash'] = mt

   -- vec
   local mt = {}
   function mt.__factory(f)
      local self = deserializePointer(f)
      self = ffi.cast('tds_vec&', self)
      ffi.gc(self, tds.C.tds_vec_free)
      return self
   end
   function mt.__write(self, f)
      serializePointer(self, f)
      tds.C.tds_vec_retain(self)
   end
   function mt.__read(self, f)
   end
   typenames['tds.Vec'] = mt

   -- atomic
   local mt = {}
   function mt.__factory(f)
      local self = deserializePointer(f)
      self = ffi.cast('tds_atomic_counter&', self)
      ffi.gc(self, tds.C.tds_atomic_free)
      return self
   end
   function mt.__write(self, f)
      serializePointer(self, f)
      tds.C.tds_atomic_retain(self)
   end
   function mt.__read(self, f)
   end
   typenames['tds.AtomicCounter'] = mt
end

-- tensor support
for _, typename in ipairs{
   'torch.ByteTensor',
   'torch.CharTensor',
   'torch.ShortTensor',
   'torch.IntTensor',
   'torch.LongTensor',
   'torch.FloatTensor',
   'torch.DoubleTensor',
   'torch.HalfTensor',
   'torch.CudaTensor',
   'torch.CudaByteTensor',
   'torch.CudaCharTensor',
   'torch.CudaShortTensor',
   'torch.CudaIntTensor',
   'torch.CudaLongTensor',
   'torch.CudaDoubleTensor',
   'torch.CudaHalfTensor',
   'torch.ByteStorage',
   'torch.CharStorage',
   'torch.ShortStorage',
   'torch.IntStorage',
   'torch.LongStorage',
   'torch.FloatStorage',
   'torch.DoubleStorage',
   'torch.HalfStorage',
   'torch.CudaStorage',
   'torch.CudaByteStorage',
   'torch.CudaCharStorage',
   'torch.CudaShortStorage',
   'torch.CudaIntStorage',
   'torch.CudaLongStorage',
   'torch.CudaDoubleStorage',
   'torch.CudaHalfStorage',
   'torch.Allocator'} do

   local mt = {}

   function mt.__factory(f)
      local self = deserializePointer(f)
      self = torch.pushudata(self, typename)
      return self
   end

   function mt.write(self, f)
      serializePointer(self, f)
      if typename ~= 'torch.Allocator' then
         self:retain()
      end
   end

   function mt.read(self, f)
   end

   typenames[typename] = mt
end

local function swapwrite()
   for typename, mt in pairs(typenames) do
      local mts = torch.getmetatable(typename)
      if mts then
         mts.__factory, mt.__factory = mt.__factory, mts.__factory
         mts.__write, mt.__write = mt.__write, mts.__write
         mts.write, mt.write = mt.write, mts.write
      end
   end
end

local function swapread()
   for typename, mt in pairs(typenames) do
      local mts = torch.getmetatable(typename)
      if mts then
         mts.__factory, mt.__factory = mt.__factory, mts.__factory
         mts.__read, mt.__read = mt.__read, mts.__read
         mts.read, mt.read = mt.read, mts.read
      end
   end
end

function serialize.save(func)
   local status, msg = pcall(swapwrite)
   if not status then
      print(string.format('FATAL THREAD PANIC: (write) %s', msg))
      os.exit(-1)
   end

   local status, storage = pcall(
      function()
         local f = torch.MemoryFile()
         f:binary()
         f:writeObject(func)
         local storage = f:storage()
         f:close()
         return storage
      end
   )
   if not status then
      print(string.format('FATAL THREAD PANIC: (write) %s', storage))
      os.exit(-1)
   end

   local status, msg = pcall(swapwrite)
   if not status then
      print(string.format('FATAL THREAD PANIC: (write) %s', msg))
      os.exit(-1)
   end

   return storage
end

function serialize.load(storage)
   local status, msg = pcall(swapread)
   if not status then
      print(string.format('FATAL THREAD PANIC: (read) %s', msg))
      os.exit(-1)
   end

   local status, func = pcall(
      function()
         local f = torch.MemoryFile(storage)
         f:binary()
         local func = f:readObject()
         f:close()
         return func
      end
   )
   if not status then
      print(string.format('FATAL THREAD PANIC: (read) %s', func))
      os.exit(-1)
   end

   local status, msg = pcall(swapread)
   if not status then
      print(string.format('FATAL THREAD PANIC: (read) %s', msg))
      os.exit(-1)
   end

   return func
end

return serialize