2

As a part of my masters thesis I am programming an external C-Subroutine for a MBS-Software in order to work as a force element like e.g.:

force=subfunction(displacement)

There is a Matlab code doing this and very time-consuming to implement it in C.

So the following options I considered to overcome this the most efficient way:

1. Using Matlab-Compiler to create a .exe and start it from C

Very nice and easy solution. However the problem is that the mbs-solver calls the function >10000 times and the compiled Matlab executable has to load the MCR (Matlab Compiler Runtime) every single time! The processing time is very short (~0.001 seconds) but the loading of MCR takes about 5 seconds (!) for each iteration step. This makes this option unfortunately useless for me.

http://www.mathworks.de/support/solutions/en/data/1-1ARNS/

"Every time you start a compiled executable, you essentially start another instance of MATLAB."

2. Using Matlab-Compiler to create a shared library etc.

I haven't tried this. As far as I researched I understood that also for these options the MCR has to be started every single step like the option 1.

3. Using the Matlab-Coder to generate a C-Code

It's not an easy option as far as I tried sometimes. The generated code should be optimized. Would it really be more efficient?

4. Starting Matlab-Executable only the first time without closing it and communicating the C-Code over any files

Just a conception: I would like to start Matlab-Exe. in the first iteration step and let write a file which contents "displacement". Then I would scan every milisecond the file from Matlab if any new value came. I would process it and write in another file and the C-Routine that scans the result file every milisecond would find the new value and keep working.

I can imagine that this works faster but surely it's bit tricky to implement until it works bugfree.


I am thankful for any reports about your experiences or ideas.

3
  • in the second option, you don't have to initialize the MCR every iteration; you would build your C program linked against the generated shared library, initialize it once at the beginning, and then call the function inside a loop. Commented Sep 5, 2013 at 19:06
  • Provided that your MATLAB code is compatible (or could be made to be compatible) with MATLAB Coder then I would really think option 3 is the way to go. In general, a standalone executable generated using Coder executes faster than the equivalent generated with Compiler. As you mentioned, you avoid the need to start the MCR, but also you avoid the overhead associated with executing code that is interpreted through MATLAB. This can be pretty significant (as an example, consider that "Accelerator Mode" in simulink basically works just by generating/building/executing code behind the scenes) Commented Sep 5, 2013 at 20:45
  • You had commented that option 3 was not an easy option. Were there particular problems that you ran into? Commented Sep 5, 2013 at 20:48

1 Answer 1

1

To offer a couple options not yet listed:

  • If you use SAGE, you can call both MATLAB and C code from a single script. You may have to wrap the C code in Cython, but this is quite easy. You technically wouldn't be calling the MATLAB from the C, but rather the MATLAB and C both from a third script, but the result would be the same.

  • If you're willing to translate the MATLAB into a very-similar Numpy script, you could use Cython to compile the complex code and integrate it all very cleanly. I've done these translations several times and it's a pretty painless process.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.