Target Language Compiler    

Inlining C MEX S-Functions

When a Simulink model contains an S-function and a corresponding TLC block target file exists for that S-function, Real-Time Workshop inlines the S-function. Inlining an S-function can produce more efficient code by eliminating the S-function Application Program Interface (API) layer from the generated code.

For S-functions that can perform a variety of tasks, inlining them gives you the opportunity to generate code only for the current mode of operation set for each instance of the block. As an example of this, if an S-function accepts an arbitrary signal width and loops through each element of the signal, you would want to generate inlined code that has loops when the signal has two or more elements, but generates a simple nonlooped calculation when the signal has just one element.

Level 1 C MEX S-functions (written to an older form of the S-function API) that are not inlined will cause the generated code to make calls to all of these seven functions, even if the routine is empty for the particular S-function.

Function
Purpose
mdlInitializeSizes
Initialize the sizes array.
mdlInitializeSampleTimes
Initialize the sample times array.
mdlInitializeConditions
Initialize the states.
mdlOutputs
Compute the outputs.
mdlUpdate
Update discrete states.
mdlDerivatives
Compute the derivatives of continuous states.
mdlTerminate
Clean up when the simulation terminates.

Level 2 C MEX S-functions (i.e., those written to the current S-function API) that are not inlined make calls to the above functions with the following exceptions:

By inlining an S-function, you can eliminate the calls to these possibly empty functions in the simulation loop. This can greatly improve the efficiency of the generated code. To inline an S-function called sfunc_name, you create a custom S-function block target file called sfunc_name.tlc and place it in the same directory as the S-function's MEX-file. Then, at build time, the target file is executed instead of setting up function calls into the S-function's .c file. The S-function target file "inlines" the S-function by directing the Target Language Compiler to insert only the statements defined in the target file.

In general, inlining an S-function is especially useful when


  Writing Block Target Files to Inline S-Functions S-Function Parameters