For Each Subsystem - Apply algorithm to individual elements or subarrays of input signals or mask

        parameters - Simulink ([original](http://www.mathworks.com/help/simulink/slref/foreachsubsystem.html)) ([raw](?raw))

Apply algorithm to individual elements or subarrays of input signals or mask parameters

Libraries:
Simulink / Ports & Subsystems
HDL Coder / Ports & Subsystems

Description

The For Each Subsystem block is a Subsystem block preconfigured as a starting point for creating a subsystem that repeats execution during a simulation time step on each element or subarray of an input signal or mask parameter array.

For Each Subsystem block icon, displayed alongside contents of for-each subsystem, consisting of a For Each block, an Inport block, and an Outport block.

The set of blocks within the subsystem represents the algorithm applied to a single element or subarray of the original signal or mask parameter array. Inside the subsystem, each block that has states maintains separate sets of states for each element or subarray that it processes. Consequently, the operation of this subsystem is similar in behavior to copying the contents of the subsystem for each element in the original input signal or mask parameter array and then processing each element using its respective copy of the subsystem. As the set of blocks in the subsystem processes the elements or subarrays, the subsystem concatenates the results to form output signals.

Configure the Subsystem

The For Each Subsystem block contains a For Each block that acts as a control block for the subsystem. Specify the parameters of the For Each block to configure the decomposition of the subsystem inputs or mask parameters into elements or subarrays and to configure the concatenation of the individual results into output signals. The block parameters Partition Dimension and Partition Width specify the dimension through which to slice the input signal or mask parameter array and the width of each slice, respectively. To partition a row vector, specify the Partition Dimension as 2. To partition a column vector, specify the Partition Dimension as 1. Use the parameter Partition Offset to specify a gap or an overlap between partitions. Specify a Number of iterations to limit processing to a subset of the data. To learn more about the block parameters, seeFor Each.

Partition Input Signals to the Subsystem

To specify which input signals to partition for each iteration in a for-each subsystem, use the Input Partition tab in the dialog box of theFor Each block. When specifying a signal to be partitioned, specify thePartition Dimension, Partition Width, andPartition Offset parameters.

Partition Mask Parameters of the Subsystem

You can partition the mask parameters of a For Each Subsystem block. Partitioning is useful for systems that have identical structures in each iteration but different parameter values. In this case, changing the model to partition extra input signals for each parameter is cumbersome. Instead, add a mask parameter to a for-each subsystem. For more information, see Create a Simple Mask. To select the mask parameter for partitioning, use theParameter Partition tab of the For Each block dialog box. For more information, see Select Partition Parameters, below.

Concatenate Output

Define the dimension along which to concatenate the results by specifying theConcatenation Dimension in the Output Concatenation tab.

The results generated by the block for each subarray are stacked along the concatenation dimension. By default, dimension 1 (_y_-axis) is used, meaning that the results are stacked vertically. However, if you specify a concatenation dimension of 2, the results concatenate along the horizontal direction (_x_-axis). Thus, if the process generates row vectors, then the concatenated result is a matrix in the first case and a row vector in the second case. For an example, see Specifying the Concatenation Dimension in the For Each Block.

Select Partition Parameters

When selecting an input signal or subsystem mask parameter for partitioning, you must specify how to decompose it into elements or subarrays for each iteration. Set integer values for the Partition Dimension,Partition Width, and Partition Offset parameters.

As an illustration, consider an input signal matrix A of the form:

A 3-by-3 matrix A with all nine elements displayed, showing d1 as the vertical dimension and d2 as the horizontal dimension

The labels _d_1 and_d_2 define dimensions 1 and 2, respectively. If you retain the default setting of 1 for both the partition dimension and the partition width and 0 for the partition offset, then Simulink® slices perpendicular to partition dimension 1 at a width equal to the partition width, that is one element:

A 3-by-3 matrix A, with all nine elements showing, partitioned into rows

Matrix A decomposes into these three row vectors:

A 3-by-3 matrix A, decomposed into three 3-element row vectors

If you specify 2 as the partition dimension instead, Simulink slices perpendicular to dimension 2 to form three column vectors:

A 3-by-3 matrix A, decomposed into three 3-element column vectors

In addition to setting the Partition Dimension to2, if you set the Partition Width to2 and the Partition Offset to-1, Simulink uses two overlapping 3-by-2 partitions for processing.

A 3-by-3 matrix A, decomposed into two overlapping 3-by-2 matrices

For an example, see Partitioning an Input Signal with the For Each Block.

By default, all partitions of the input signal or mask parameter are processed. To process a subset of the partitions, enter the number of partitions to process as the Number of iterations. In the matrix examples above, if Partition Offset is set to0 (the default) and Number of iterations is set to 2, only the first 2 rows or columns of the input matrix A are processed.

Note

Only signals are considered one-dimensional in Simulink. Mask parameters are row or column vectors, according to their orientation. To partition a row vector, specify the partition dimension as 2, along the columns. To partition a column vector, specify the partition dimension as 1, along the rows.

Code Reuse Support

For certain models, the For Each Subsystem block improves code reuse in Simulink Coder™ generated code. Consider a model containing two reusable Atomic Subsystem blocks with the same scalar algorithm applied to each element of the signal. If the input signal dimensions of these subsystems are different,Simulink Coder generated code includes two distinct functions. You can replace these two subsystems with two identical For Each Subsystem blocks that are configured to process each element of their respective inputs using the same algorithm. In this case, Simulink Coder generated code consists of a single function parameterized by the number of input signal elements. This function is invoked twice, once for each unique instance of the For Each Subsystem block in the model. For each of these cases, the input signal elements have different values.

Multicore Execution Support

When you simulate your model in rapid accelerator mode, Simulink uses multicore execution for faster simulation of for-each subsystems. Simulink automatically profiles each eligible for-each subsystem the first two time steps it runs in rapid accelerator mode to compare parallel and serial execution times. Simulink then designates the for-each subsystem for parallel, multicore execution in subsequent time steps of the simulation run if doing so would speed up execution time. For nested for-each subsystems, multicore execution applies only to the top-level subsystem. Multicore execution does not apply to for-each subsystems containing continuous states orFunction Caller blocks.

To suppress multicore execution for a given for-each subsystem, set theMultithreadedSim parameter of the For Each block within the subsystem to'off'.

set_param(ForEachBlockName,'MultithreadedSim','off')

Note that this is a parameter of the For Each block within the subsystem, not the For Each Subsystem block itself. To suppress multicore execution for all for-each subsystems in a model, set theMultithreadedSim parameter of the model to'off'.

set_param(ModelName,'MultithreadedSim','off')

To re-enable multicore execution, set the relevantMultithreadedSim parameter to its default value of'auto'.

For an example, see Multithreaded Simulation Using For Each Subsystem.

Note

If you simulate your model in rapid accelerator mode or generate code from your model, and you partition mask parameters in a for-each subsystem, then any expression inside the for-each subsystem that references a partitioned parameter must be a tunable expression. See Tunable Expression Limitations (Simulink Coder).

S-Function Support

The For Each Subsystem block supports both C-MEX S-functions and Level-2 MATLAB® S-functions, provided that the S-function supports multiple execution instances using one of these techniques:

If you use these specifications:

Examples

Ports

Input

expand all

Signal input to a Subsystem block, specified as a scalar, vector, or matrix. Placing an Inport block in aSubsystem block adds an external input port to the block. The port label matches the name of the Inport block.

Use Inport blocks to receive signals from the local environment.

Data Types: half | single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | Boolean | fixed point | enumerated | bus

Output

expand all

Signal output from a Subsystem block, returned as a scalar, vector, or matrix. Placing an Outport block in aSubsystem block adds an external output port to the block. The port label matches the name of the Outport block.

Use Outport blocks to send signals to the local environment.

Data Types: half | single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | Boolean | fixed point | enumerated | bus

Parameters

expand all

To edit block parameters interactively, use theProperty Inspector. From the Simulink Toolstrip, on the Simulation tab, in thePrepare gallery, select Property Inspector.

Main

Select how to display port labels on the Subsystem block icon.

For port label editing on Subsystem blocks, see Edit Port Labels on Subsystem Blocks.

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: ShowPortLabels
Values: 'FromPortIcon' (default) | 'FromPortBlockName' 'SignalName' 'none'

Control user access to the contents of the subsystem.

You do not receive a response if you attempt to view the contents of a subsystem whose Read/Write permissions parameter is set to NoReadOrWrite. For example, when double-clicking such a subsystem, the software does not open the subsystem and does not display any messages.

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: Permissions
Values: 'ReadWrite' (default) | 'ReadOnly' 'NoReadOrWrite'

Enter the name of a function to be called if an error occurs while the software executes the subsystem.

The software passes two arguments to the function: the handle of the subsystem and a character vector that specifies the error type. If no function is specified, the software displays a generic error message if executing the subsystem causes an error.

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: ErrorFcn
Values: '' (default) | function name in quotes
Data Types: char | string

Select whether to resolve names of workspace variables referenced by this subsystem.

For more information, see Symbol Resolution and Symbol Resolution Process.

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: PermitHierarchicalResolution
Values: 'All' (default) | 'ExplicitOnly' 'None'

Try to eliminate any artificial algebraic loops that include the atomic subsystem

Dependencies

To enable this parameter, select Treat as atomic unit.

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: MinAlgLoopOccurrences
Values: 'off' (default) | 'on'

Code Generation

Parameters on the Code Generation tab require a Simulink Coder or Embedded Coder® license.

Select the code format to be generated for an atomic (nonvirtual) subsystem.

For more information, see:

The default value depends on the block configuration. For example, the default value for the Subsystem block is Auto. The default value for the CodeReuseSubsystem block isReusable function.

Tips

Dependencies

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: RTWSystemCode
Values: 'Auto' | 'Inline' 'Nonreusable function' 'Reusable function'

Select how the software names the function it generates for the subsystem.

If you have an Embedded Coder license, you can control function names with options on the Configuration Parameter > pane.

For more information, see Generate Subsystem Code as Separate Function and Files (Simulink Coder).

The default value depends on the block configuration. For example, the default value for the Subsystem block is Auto. The default value for the CodeReuseSubsystem block isUse subsystem name.

Dependencies

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: RTWFcnNameOpts
Values: 'Auto' | 'Use subsystem name' 'User specified'

Specify a unique, valid C or C++ function name for subsystem code.

Use this parameter if you want to give the function a specific name instead of allowing the Simulink Coder code generator to assign its own autogenerated name or use the subsystem name. For more information, see Generate Subsystem Code as Separate Function and Files (Simulink Coder).

Dependencies

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: RTWFcnName
Values: '' (default) | function name in quotes
Data Types: char | string

Select how the software names the separate file for the function it generates for the subsystem.

The default value depends on the block configuration. For example, the default value for the Subsystem block is Auto. The default value for the CodeReuseSubsystem block isUse function name.

Dependencies

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: RTWFileNameOpts
Values: 'Auto' | 'Use subsystem name' 'Use function name' 'User specified'

The filename that you specify does not have to be unique. However, avoid giving non-unique names that result in cyclic dependencies. For example,sys_a.h includes sys_b.h,sys_b.h includes sys_c.h, andsys_c.h includes sys_a.h.

For more information, see Generate Subsystem Code as Separate Function and Files (Simulink Coder).

Dependencies

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: RTWFileName
Values: '' (default) | filename in quotes
Data Types: char | string

Select how to use arguments with the generated function.

In some cases, when generating optimized code, the code generator might not generate a function that has arguments.

For more information, see:

Dependencies

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: FunctionInterfaceSpec
Values: 'void_void' (default) | 'Allow arguments (Optimized)' 'Allow arguments (Match graphical interface)'

Generate subsystem function code in which the internal data for an atomic subsystem is separated from its parent model and is owned by the subsystem.

For details on how to generate modular function code for an atomic subsystem, seeGenerate Modular Function Code for Nonvirtual Subsystems (Embedded Coder).

For details on how to apply memory sections to atomic subsystems, see Override Default Memory Placement for Subsystem Functions and Data (Embedded Coder).

Dependencies

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: FunctionWithSeparateData
Values: 'off' (default) | 'on'

Select how the software applies memory sections to the subsystem initialization and termination functions.

Tips

Dependencies

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: RTWMemSecFuncInitTerm
Values: 'Inherit from model' (default) | 'Default' model memory section in quotes

Select how Embedded Coder applies memory sections to the subsystem execution functions.

Tips

Dependencies

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: RTWMemSecFuncExecute
Values: 'Inherit from model' (default) | 'Default' model memory section in quotes

Select how the software applies memory sections to the subsystem constants.

Tips

Dependencies

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: RTWMemSecDataConstants
Values: 'Inherit from model' (default) | 'Default' model memory section in quotes

Select how the software applies memory sections to the subsystem internal data.

Tips

Dependencies

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: RTWMemSecDataInternal
Values: 'Inherit from model' (default) | 'Default' model memory section in quotes

Select how the software applies memory sections to the subsystem parameters.

Tips

Dependencies

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: RTWMemSecDataParameters
Values: 'Inherit from model' (default) | 'Default' model memory section in quotes

Block Characteristics

Data Types Booleana | busa doublea enumerateda fixed pointa halfa integera singlea
Direct Feedthrough no
Multidimensional Signals yesa
Variable-Size Signals no
Zero-Crossing Detection no
a Actual data type or capability support depends on block implementation.

Extended Capabilities

expand all

Actual code generation support depends on block implementation.

HDL Coder™ provides additional configuration options that affect HDL implementation and synthesized logic.

When you generate HDL code for the For Each Subsystem, the code generator attempts to use a for-generate loop that iterates through elements of the input and output signals. The for-generate loop improves readability and reduces the number of lines of code, which could otherwise be hundreds of lines of code for large vector signals. The subsystem supports vector and 3D matrix input signals. See Generate HDL Code for Blocks Inside For Each Subsystem (HDL Coder).

You can use a nonzero partition offset for HDL code generation. The code generator supports a scalar, vector, or matrix as mask parameter values. You can generate HDL code for element, 1D, and 2D partitions of mask parameters inside a For Each Subsystem block. Inside the For Each Subsystem block, use the mask parameter as the Constant value parameter in Constant blocks or as theGain parameter in Gain blocks.

Limitations

HDL Architecture

Architecture Description
Module (default) Generate code for the subsystem and the blocks within the subsystem.
BlackBox Generate a black box interface. The generated HDL code includes only the input/output port definitions for the subsystem. Therefore, you can use a subsystem in your model to generate an interface to existing, manually written HDL code. The black-box interface generation for subsystems is similar to the Model block interface generation without the clock signals.
No HDL Remove the subsystem from the generated code. You can use the subsystem in simulation, however, treat it as a “no-op” in the HDL code.

HDL Block Properties

General
AdaptivePipelining Automatic pipeline insertion based on the synthesis tool, target frequency, and multiplier word-lengths. The default is inherit. See alsoAdaptivePipelining (HDL Coder).
BalanceDelays Detects introduction of new delays along one path and inserts matching delays on the other paths. The default is inherit. See also BalanceDelays (HDL Coder).
ClockRatePipelining Insert pipeline registers at a faster clock rate instead of the slower data rate. The default is inherit. See also ClockRatePipelining (HDL Coder).
ConstrainedOutputPipeline Number of registers to place at the outputs by moving existing delays within your design. Distributed pipelining does not redistribute these registers. The default is0. For more details, see ConstrainedOutputPipeline (HDL Coder).
DistributedPipelining Pipeline register distribution, or register retiming. The default is inherit. See also DistributedPipelining (HDL Coder).
DSPStyle Synthesis attributes for multiplier mapping. The default is none. See also DSPStyle (HDL Coder).
FlattenHierarchy Remove subsystem hierarchy from generated HDL code. The default is inherit. See also FlattenHierarchy (HDL Coder).
InputPipeline Number of input pipeline stages to insert in the generated code. Distributed pipelining and constrained output pipelining can move these registers. The default is0. For more details, see InputPipeline (HDL Coder).
OutputPipeline Number of output pipeline stages to insert in the generated code. Distributed pipelining and constrained output pipelining can move these registers. The default is0. For more details, see OutputPipeline (HDL Coder).
SharingFactor Number of functionally equivalent resources to map to a single shared resource. The default is 0. See also Resource Sharing (HDL Coder).
StreamingFactor Number of parallel data paths, or vectors, that are time multiplexed to transform into serial, scalar data paths. The default is 0, which implements fully parallel data paths. See also Streaming (HDL Coder).

Target Specification

This block cannot be the DUT, so the block property settings in the Target Specification tab are ignored.

Complex Data Support

The block does not support complex data signals for HDL code generation. To input complex signals, convert the complex signal to an array of signals, and then input to the block. To learn more, see Generate HDL Code for Blocks Inside For Each Subsystem (HDL Coder).

Generate HDL Code with Algebraic Loops

To generate HDL code for models that contain For Each Subsystem blocks:

Actual data type support depends on block implementation.

Version History

Introduced in R2010a

expand all

You can now pause a simulation on a particular iteration of an iterator subsystem by adding signal breakpoints to signals produced by:

In previous releases, you could add a breakpoint in these locations, but the breakpoints did not pause the simulation and appeared as invalid in the block diagram.

Port value labels at the same locations now display the iteration number. In previous releases, port value labels at these locations showed inaccessible.

For an example, see Debug Simulation of Iterator Subsystem.