2

I've read that I can generate code from Simulink models/block diagrams. I am curious whether Simulink always converts a model to (c/c++/java) code prior to running a simulation in the Simulink software, and then execute that code? I mean, whenever I'm running a simulation is Matlab converting the block diagram to (c/c++/java) code and running that code behind the scene. In this case, simulation in Simulink directly depends on running some code; this information is important to me in some way.

Generating and running code for a complete model seems plausible, as we can write s-functions using C/Matlab code and use them as custom blocks. So simulating a model involves running code in some degree. Again, since we can write Matlab code as well, simulation may involve interpreting Matlab code in some environment. It makes me curious whether these information are available - how tightly running a simulation in Simulink depends on executing native code in user's machine.

I did some search before posting and found this SO question: How does simulation engine work? Discussion in this question does not answer my question directly.

Community
  • 1
  • 1
Shafiul
  • 2,832
  • 9
  • 37
  • 55
  • http://www.mathworks.com/help/simulink/ug/how-the-acceleration-modes-work.html – Daniel Mar 01 '16 at 00:00
  • Is your concern on simulation speeds ? – PagMax Mar 01 '16 at 01:04
  • Simulation is basically a huge numerical integration problem. S-functions etc. only compute some entries on the left and the right side of the equation at each time step. It is converted to a compacted code when you Build your model and use things like XPC Target etc. – percusse Mar 01 '16 at 11:16
  • @PagMax No I'm not concerned on speeds. Just curious whether native code gets generated and run behind the scene during simulation. – Shafiul Mar 01 '16 at 23:59
  • Question fully answered? – Daniel Mar 08 '16 at 15:14
  • Thanks @Daniel! I've got some relevant questions which I commented in your answer, as I'm not sure whether I will ask them as separate questions! – Shafiul Mar 08 '16 at 21:42

1 Answers1

4

The answer depens on which mode you chose.

In the normal mode Simulink will run the model as it is primarily using the MATLAB execution engine. No code is generated. Native implemented parts (e.g. S-Functions) are used as individual binaries called by the MATLAB interpreter.

In the accelerator mode Simulink generates model code. This means your full model (except parts where code generation is impossible) is generated and compiled into one binary.

In the rapid accelerator mode not only your model but also the solver is generated and compiled into one binary, now running in a separate process.

For more details refer to the official documentation

Daniel
  • 36,610
  • 3
  • 36
  • 69
  • Thanks Daniel. I've got a few side questions: (1) How non-S-function blocks are implemented? If they are implemented using native C code then is Matlab executing them as binaries? (2) What about the blocks which are written using Matlab code? Is Matlab code interpreted or compiled and then run? Or is Matlab code getting translated to C code and then being run? (3) For the s-function blocks having C code how can we change compiler parameters (e.g. -O, -std flags etc.) while running a simulation? – Shafiul Mar 08 '16 at 21:41
  • 1
    @Shafiul: (1) There are some built-in blocks which are definitely not s-functions. These are for example subsystems, mux, demux, probably most of the signal attribute blocks and some other blocks. When reading generated code, they look totally different. Simulink is closed source, you probably won't get a detailed answer. (2) That is the difference between a MATLAB Function and an interpreted MATLAB Function. (3) No idea. – Daniel Mar 08 '16 at 22:02