I’m trying to use Matlab’s foreign function interface (see the MATLAB loadlibrary function) to call Julia code from MATLAB. Has anyone been successful in doing this? All of the projects I have found (mexjulia being the most successful) have been abandoned.
To test the FFI, I copied all of the files from /usr/include/julia to my home directory, as well as /usr/lib64/libjulia.so. The loadlibrary function had trouble with some header files in the uv directory within ~/julia not being found, so I copied them all up into ~/julia and modified the header files accordingly to refer to foo.h instead of uv/foo.h.
I didn’t try myself yet but I will probably need to call Julia from Matlab very soon… but since nobody seems to be able to do it properly, it is a bit annoying…
I was thinking to one thing, I don’t know if it is an acceptable solution, I’m not an expert programmer.
Since calling Julia from Python and Python from Matlab both work (at least I think it is), is it not possible to create a function in Matlab which call Python, which itself call Julia?
Is is a bit ugly but it can work, does anybody try something equivalent?
When I was faced with a similar situation, I found it significantly easier to go the other way — embed Matlab in Julia using MATLAB.jl. The interoperability and extended quoting mechanisms that Julia provides makes it quite nice to have Matlab code — even a non-trivial script — inserted immediately into a .jl file.
But if you have a Simulink model and need one non-trivial time critical component written in Julia, than using Julia from Matlab/Simulink is the only way to go… Not easy yet, but at some point in time it should become feasible. For my usecase copying input and output parameters would not be a problem, because the Julia function would be computational intensive.
You’d just use Julia’s C embedding API with Matlab’s C mex interface. Writing and compiling custom mex files is for the birds, but once you have one that can call an arbitrary Julia code, I suppose that’s the last one you’d need to write.
@jebej I’m trying to get it to work on Linux on 1.0, and it looks like mexjulia never got full Linux functionality working.
The ideal solution would be to write a mex file that can call an arbitrary Julia code like @mbauman suggests. This is what mexjulia attempted to do, but there’s so much work to be done to bring that project back up to speed that it would probably be best to write a whole new piece of software to handle calling Julia.
There are two problems that I see complicating this task:
The mex file will have to be able to call arbitrary Julia code and return useful data. Calling it isn’t the hard part, returning data is. You either have to detect the outputs of the function and account for that within one mex file, or you have to have multiple mex files for each type of data that can be returned.
You need to have a Julia process running in the background that can be called by several Matlab calls successively. If Julia has to start and stop on each function call, you lose any performance gains that you would expect from using Julia.
I may have this wrong since I’m not very experienced in C (or Julia), but that was the conclusion I came to when I first looked at this problem a couple of months ago.
You are right. When I mentioned CMEX, I was thinking about a scenario in which a very long and complicated computation is happening in Julia so that the startup time is negligible. I am not sure if there is a good method to keep Julia alive between calls of the CMEX.
If you’re going to link with the library explicitly using -ljulia, you shouldn’t need to dlopen it. If you do need to dlopen it for some reason, you should also be using dlsym to get access to all the julia runtime functions explicitly.
You may also have problems using jl_init and jl_atexit_hook multiple times within the same session. That shouldn’t be a problem, but I’m not sure whether it’s been properly tested. Regardless of whether it does work, it will completely kill performance so I’d suggest calling jl_init() only a single time within a given matlab session.