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.
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:
mdlInitializeConditions
is only called if MDL_INITIALIZE_CONDITIONS
is declared with #define
.
mdlStart
is called only if MDL_START
is declared with #define
.
mdlUpdate
is called only if MDL_UPDATE
is declared with #define
.
mdlDerivatives
is called only if MDL_DERIVATIVES
is declared with #define
.
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
mdlUpdate
).
![]() | Writing Block Target Files to Inline S-Functions | S-Function Parameters | ![]() |