-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathengine.jl
353 lines (293 loc) · 10.3 KB
/
engine.jl
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# operation on MATLAB engine sessions
###########################################################
#
# Session open & close
#
###########################################################
const default_startflag = "-nodisplay"
const default_matlabcmd = matlab_cmd * " -nosplash -nodesktop"
# pass matlab flags directly or as a Vector of flags, i.e. "-a" or ["-a", "-b", "-c"]
startcmd(flag::AbstractString=default_startflag) =
isempty(flag) ? default_matlabcmd : default_matlabcmd * " " * flag
startcmd(flags::AbstractVector{<:AbstractString}) =
isempty(flags) ? default_matlabcmd : default_matlabcmd * " " * join(flags, " ")
# 64 K buffer should be sufficient to store the output text in most cases
const default_output_buffer_size = 64 * 1024
const windows_regserver_warning = """
Failed to start MATLAB engine. If you have/had multiple versions of MATLAB installed, this can happen if you
tried to start a different version of MATLAB in Julia compared to which MATLAB server is registered in Windows.
Steps to resolve this:
1. Register a specific MATLAB version manually as a server, open a MATLAB window as a user with administrator privileges.
In MATLAB, enter the command `!matlab -regserver`. Then close the MATLAB window. More details:
https://de.mathworks.com/help/matlab/matlab_external/registering-matlab-software-as-a-com-server.html
2. Ensure that the MATLAB.jl package is using the same MATLAB version that was registered in step 1. See the instructions on GitHub
on how to change the version that MATLAB.jl uses:
https://github.com/JuliaInterop/MATLAB.jl?tab=readme-ov-file#changing-matlab-version
"""
mutable struct MSession
ptr::Ptr{Cvoid}
buffer::Vector{UInt8}
bufptr::Ptr{UInt8}
function MSession(bufsize::Integer=default_output_buffer_size; flags=default_startflag)
if Sys.iswindows()
assign_persistent_msession()
end
ep = ccall(eng_open[], Ptr{Cvoid}, (Ptr{UInt8},), startcmd(flags))
if ep == C_NULL
@warn(
"Confirm MATLAB is installed and discoverable.",
matlab_libpath,
maxlog = 1
)
if Sys.iswindows()
@warn(windows_regserver_warning, maxlog = 1)
elseif Sys.islinux()
@warn(
"Ensure `csh` is installed; this may require running `sudo apt-get install csh`.",
maxlog = 1
)
end
throw(MEngineError("failed to open MATLAB engine session"))
end
if Sys.iswindows()
# hide the MATLAB command window on Windows and change to current directory
ccall(eng_set_visible[], Cint, (Ptr{Cvoid}, Cint), ep, 0)
ccall(eng_eval_string[], Cint, (Ptr{Cvoid}, Ptr{UInt8}),
ep, "try cd('$(escape_string(pwd()))'); end")
end
buf = Vector{UInt8}(undef, bufsize)
if bufsize > 0
bufptr = pointer(buf)
ccall(eng_output_buffer[], Cint, (Ptr{Cvoid}, Ptr{UInt8}, Cint),
ep, bufptr, bufsize)
else
bufptr = convert(Ptr{UInt8}, C_NULL)
end
self = new(ep, buf, bufptr)
finalizer(release, self)
return self
end
end
function unsafe_convert(::Type{Ptr{Cvoid}}, m::MSession)
ptr = m.ptr
ptr == C_NULL && throw(UndefRefError())
return ptr
end
function release(session::MSession)
ptr = session.ptr
if ptr != C_NULL
ccall(eng_close[], Cint, (Ptr{Cvoid},), ptr)
end
session.ptr = C_NULL
return nothing
end
function close(session::MSession)
# close a MATLAB Engine session
ret = ccall(eng_close[], Cint, (Ptr{Cvoid},), session)
ret != 0 && throw(MEngineError("failed to close MATLAB engine session (err = $ret)"))
session.ptr = C_NULL
return nothing
end
# default session
const default_msession_ref = Ref{MSession}()
# this function will start an MSession if default_msession_ref is undefined or if the
# MSession has been closed so that the engine ptr is void
function get_default_msession()
if !isassigned(default_msession_ref) || default_msession_ref[].ptr == C_NULL
default_msession_ref[] = MSession()
end
return default_msession_ref[]
end
function restart_default_msession(bufsize::Integer=default_output_buffer_size)
close_default_msession()
default_msession_ref[] = MSession(bufsize)
return nothing
end
function close_default_msession()
if isassigned(default_msession_ref) && default_msession_ref[].ptr !== C_NULL
close(default_msession_ref[])
end
return nothing
end
if Sys.iswindows()
function show_msession(m::MSession=get_default_msession())
ret = ccall(eng_set_visible[], Cint, (Ptr{Cvoid}, Cint), m, 1)
ret != 0 && throw(MEngineError("failed to show MATLAB engine session (err = $ret)"))
return nothing
end
function hide_msession(m::MSession=get_default_msession())
ret = ccall(eng_set_visible[], Cint, (Ptr{Cvoid}, Cint), m, 0)
ret != 0 && throw(MEngineError("failed to hide MATLAB engine session (err = $ret)"))
return nothing
end
function get_msession_visiblity(m::MSession=get_default_msession())
vis = Ref{Cint}(true)
ccall(eng_get_visible[], Int, (Ptr{Cvoid}, Ptr{Cint}), m, vis)
return vis[] == 1 ? true : false
end
end
###########################################################
#
# communication with MATLAB session
#
###########################################################
function eval_string(session::MSession, stmt::String)
# evaluate a MATLAB statement in a given MATLAB session
ret = ccall(eng_eval_string[], Cint, (Ptr{Cvoid}, Ptr{UInt8}), session, stmt)
ret != 0 && throw(MEngineError("invalid engine session (err = $ret)"))
bufptr = session.bufptr
if bufptr != C_NULL
bs = unsafe_string(bufptr)
if ~isempty(bs)
print(bs)
end
end
return nothing
end
eval_string(stmt::String) = eval_string(get_default_msession(), stmt)
function put_variable(session::MSession, name::Symbol, v::MxArray)
# put a variable into a MATLAB engine session
ret = ccall(
eng_put_variable[],
Cint,
(Ptr{Cvoid}, Ptr{UInt8}, Ptr{Cvoid}),
session,
string(name),
v,
)
ret != 0 && throw(
MEngineError("failed to put variable $(name) into MATLAB session (err = $ret)"),
)
return nothing
end
put_variable(session::MSession, name::Symbol, v) = put_variable(session, name, mxarray(v))
put_variable(name::Symbol, v) = put_variable(get_default_msession(), name, v)
function get_mvariable(session::MSession, name::Symbol)
pv = ccall(
eng_get_variable[],
Ptr{Cvoid},
(Ptr{Cvoid}, Ptr{UInt8}),
session,
string(name),
)
pv == C_NULL &&
throw(MEngineError("failed to get variable $(name) from MATLAB session"))
return MxArray(pv)
end
get_mvariable(name::Symbol) = get_mvariable(get_default_msession(), name)
get_variable(name::Symbol) = jvalue(get_mvariable(name))
get_variable(name::Symbol, kind) = jvalue(get_mvariable(name), kind)
###########################################################
#
# macro to simplify syntax
#
###########################################################
function _mput_multi(vs::Symbol...)
nv = length(vs)
if nv == 1
v = vs[1]
:(MATLAB.put_variable($(Meta.quot(v)), $(v)))
else
stmts = Vector{Expr}(undef, nv)
for i = 1:nv
v = vs[i]
stmts[i] = :(MATLAB.put_variable($(Meta.quot(v)), $(v)))
end
Expr(:block, stmts...)
end
end
macro mput(vs...)
esc(_mput_multi(vs...))
end
function make_getvar_statement(v::Symbol)
:($(v) = MATLAB.get_variable($(Meta.quot(v))))
end
function make_getvar_statement(ex::Expr)
if !(ex.head == :(::))
error("Invalid expression for @mget.")
end
v::Symbol = ex.args[1]
k::Symbol = ex.args[2]
:($(v) = MATLAB.get_variable($(Meta.quot(v)), $(k)))
end
function _mget_multi(vs::Union{Symbol,Expr}...)
nv = length(vs)
if nv == 1
make_getvar_statement(vs[1])
else
stmts = Vector{Expr}(undef, nv)
for i = 1:nv
stmts[i] = make_getvar_statement(vs[i])
end
Expr(:block, stmts...)
end
end
macro mget(vs...)
esc(_mget_multi(vs...))
end
###########################################################
#
# mxcall
#
###########################################################
# MATLAB does not allow underscore as prefix of a variable name
_gen_marg_name(mfun::Symbol, prefix::String, i::Int) = "jx_$(mfun)_arg_$(prefix)_$(i)"
function mxcall(session::MSession, mfun::Symbol, nout::Integer, in_args...)
nin = length(in_args)
# generate temporary variable names
in_arg_names = Vector{String}(undef, nin)
out_arg_names = Vector{String}(undef, nout)
for i = 1:nin
in_arg_names[i] = _gen_marg_name(mfun, "in", i)
end
for i = 1:nout
out_arg_names[i] = _gen_marg_name(mfun, "out", i)
end
# generate MATLAB statement
buf = IOBuffer()
if nout > 0
if nout > 1
print(buf, "[")
end
join(buf, out_arg_names, ", ")
if nout > 1
print(buf, "]")
end
print(buf, " = ")
end
print(buf, string(mfun))
print(buf, "(")
if nin > 0
join(buf, in_arg_names, ", ")
end
print(buf, ");")
stmt = String(take!(buf))
# put variables to MATLAB
for i = 1:nin
put_variable(session, Symbol(in_arg_names[i]), in_args[i])
end
# execute MATLAB statement
eval_string(session, stmt)
# get results from MATLAB
ret = if nout == 1
jvalue(get_mvariable(session, Symbol(out_arg_names[1])))
elseif nout >= 2
results = Vector{Any}(undef, nout)
for i = 1:nout
results[i] = jvalue(get_mvariable(session, Symbol(out_arg_names[i])))
end
tuple(results...)
else
nothing
end
# clear temporaries from MATLAB workspace
for i = 1:nin
eval_string(session, string("clear ", in_arg_names[i], ";"))
end
for i = 1:nout
eval_string(session, string("clear ", out_arg_names[i], ";"))
end
return ret
end
mxcall(mfun::Symbol, nout::Integer, in_args...) =
mxcall(get_default_msession(), mfun, nout, in_args...)