-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtorch_server.lua
50 lines (44 loc) · 1.18 KB
/
torch_server.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
-- Torch interface to Julia (server)
local json = require ("dkjson")
local zmq = require "lzmq"
local context = zmq.init(1)
local socket = context:socket(zmq.REP)
socket:bind("tcp://*:7000")
function serialize(data)
if torch.type(1) == torch.type(data) then
return {data}
end
--for now it assumes data is a 1-D array
local ret = {}
for i=1,data:size(1) do
ret[i] = data[i]
end
return ret
end
while true do
-- Wait for next request from client
local request = socket:recv()
-- print("Received Hello [" .. request .. "]")
-- print(request)
if request ~= nil then
request = json.decode(request, 1, nil)
-- print('req:',request, ' | cmd:', request.cmd)
if request.cmd == "load" then
require(request.name)
ret = {1}
ret = json.encode (ret, { indent = true })
socket:send(ret)
elseif request.cmd == "call" then
func_name = request.msg.func
args = request.msg.args
ret = _G[func_name](args)
-- ret = torch.rand(10):float()
ret = serialize(ret)
ret = json.encode (ret,{ indent = true })
socket:send(ret)
end
end
end
-- We never get here but if we did, this would be how we end
socket:close()
context:term()