Has anyone had success using Julia from Matlab?

Continuing the discussion from Embedding Julia in Matlab:

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.

Trying to run loadlibrary again resulted in an extensive failure: Julia loadlibrary - Pastebin.com

It looks like there are either more header files that need to be included, or this is just beyond loadlibrary’s capabilities. Does anyone have a working solution for 1.0.1?

None at all. I tried to do it in order to make a diffeqmat, a library for using DifferentialEquations.jl from Julia. However, I couldn’t get it to work.

Hi,

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?

Thank you in advance

mexjulia works on Windows with julia 0.6.4, in case that might suit your usage. I don’t know how hard it would be to get it to work with 1.0.

Is it too difficult for your use case to embed Julia in CMEX and call CMEX in MATLAB? I see no reason why this should not work.

1 Like

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.

1 Like

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.

1 Like

In this case, I thing CMEX can help, right?

So how do you create a cmex file with Julia?

IIRC, CMEX is a compiled C file. Hence, I do not see why you can’t embedded Julia code inside this C file as described here: Embedding Julia · The Julia Language

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.

Isn’t that what mexjulia is?

@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:

  1. 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.
  2. 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.

1 Like

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.

Maybe it is not that easy as I thought it would be. I tried to use a very simple example. It compiled fine, but when I execute MATLAB crashes with the following message:

fatal: error thrown and no exception handler available.
InitError(mod=:Sys, error=ErrorException("could not load symbol "jl_cpu_threads":

I think I managed to call Julia from MATLAB using CMEX, well kind of…

I create a file to compute the square root of a number using Julia. I called sqrt_julia.c:

#include <math.h>
#include <dlfcn.h>
#include "mex.h"
#include <julia.h>

#define r   prhs[0]
#define out plhs[0]

// Function: sqrt_julia(r)

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    double *d_out;
    double d_r;

    if (nrhs != 1)
        mexErrMsgTxt("The function must have only 1 argument.");

    if (nlhs > 1)
        mexErrMsgTxt("The function must have only 1 output argument.");

    if (mxGetNumberOfElements(r) != 1 || !mxIsDouble(r))
        mexErrMsgTxt("r must be a double scalar.");

    dlopen("libjulia.so", RTLD_GLOBAL | RTLD_LAZY);

    jl_init();

    d_r    = mxGetScalar(r);
    out    = mxCreateDoubleMatrix(1,1,mxREAL);
    d_out  = mxGetPr(out);

    jl_function_t* sqrt = jl_get_function(jl_base_module, "sqrt");
    jl_value_t* ret     = jl_call1(sqrt, jl_box_float64(d_r));
    *d_out              = jl_unbox_float64(ret);

    jl_atexit_hook(0);

    return;
}

which was compile by

mex -v CFLAGS='$CFLAGS -std=c99 -DJULIA_ENABLE_THREADING=1 -I/usr/include/julia' LDFLAGS='$LDFLAGS -ljulia -ldl' sqrt_julia.c

It works at the first time without problems. Then, it randomly crashes MATAB.

EDIT: Does anyone that have Julia built with JULIA_THREADS=0 can, please, test this code? By looking at the crash dump, the crase maybe related to threads.

1 Like

Works for me on Fedora 29, Julia 1.0.3, MATLAB R2018b and R2019a. It crashed the first time I built it, but after launching Matlab again and rebuilding I’m able to use it without any problems.

Never mind, it crashes if I leave it open long enough on any MATLAB version.

1 Like

Has anyone tried going from matlab to python to julia and back?
Might be another way around. Calling python from matlab should be fairly easy :

?
Just an idea

I am seeing the same behavior here! I do not remember the MATLAB version, but I was using openSUSE. The Julia version was not the official, but the one in openSUSE repositories.

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.

2 Likes