Embedding Julia in Matlab

embedding

#1

In need to port code from Matlab to Julia, and as intermediate step
it would e very useful to be able to call Julia functions from Matlab.
The project https://github.com/twadleigh/mexjulia has the aim to
make this possible, but it doesn’t work on Linux (and not sure if
it works with Julia 0.6 on any other platform).

Therefore I need help to debug and fix the following issue:

Is here someone around who could help to understand and fix
this issue?

Any help appreciated!

Uwe


#2

It works quite well with 0.6 on windows. I use it one a regular basis.
I use MATLAB r2015b and Windows 10.


#3

Apparently other people have problems on Windows (which I believe is the twadleigh’s primary platform). Given the unclear nature of that linux issue, and a number of conversations about memory handling and interrupts in the past when I have looked at this, I suspect there are problems here which are too deep to debug by proxy. I just looked through some of the code and took a few notes:

  • Very hard to understand the flow of control and design decisions here.
  • A number of suspicious (or at least odd) uses of unsafe_wrap in the array handling Julia code.
  • Only two of the rooted arguments here are used (but jl_call2 roots all of its arguments anyway) – maybe harmless, but a code smell.

Given the state of the code, if I was working on this, I would start from close to scratch (possibly using loadlibrary rather than mex) and do some minimal experiments first: basic code eval, wrapping arrays and type conversion for a small number of types, etc. That’s the only way I would feel sufficiently informed to judge whether and how to clean-up versus rebuild.


#4

The loadlibrary approach sound promising for me…
How do I convert a Julia function into a shared library (.so file)?


#5

I’m not sure how that would help with the interactive use case of mexjulia. For that I’m referring to calling julia embedding API via loadlibrary, which I’ve never used, but is apparently something more like an ffi (compared to mex).


#6

Well, while interactive use is nice, this is not my main requirement. I just want to be able to implement computational expensive flight control models in matlab/simulink, which then could be used to develop the controller. Ok, in the documentation it is explained how to do that for embedding Julia in a C program, but not how to embed it in a Matlab program.
Is there a tutorial out there how to do this, using loadlibrary on the Matlab side?


#7

I read this post – I would guess there is official documentation too. The idea is similar to python ctypes, Julia ccall, LuaJIT FFI, etc.: load a shared library (in this case libjulia.so) at runtime and call some functions, without having directly linked anything at (static) compile-time.

[edit: I misunderstood, and this interpretation was incorrect. It still requires a local C compiler, so not really any more helpful than mex here]


#8

Thanks for the hint!
On Linux, you always have a local C compiler available, so this is not an issue. It looks that this approach still might be easier than mex.