|On this page…|
Simulink® provides various run-time and compile-time diagnostics that you can use to help avoid problems with data stores. Diagnostics are available in the Model Configuration Parameters dialog box and the Data Store Memory block's parameters dialog box. The Simulink Model Advisor provides support by listing cases where data store errors are more likely because diagnostics are disabled.
You can use data store run-time diagnostics to detect unintended sequences of data store reads and writes that occur during simulation. You can apply these diagnostics to all data stores, or allow each Data Store Memory block to set its own value. The diagnostics are:
Detect read before write: Detect when a data store is read from before written to within a given time step
Detect write after read: Detect when a data store is written to after being read from within a given time step
Detect write after write: Detect when a data store is written to multiple times within a given time step
These diagnostics appear in the Model Configuration Parameters > Diagnostics > Data Validity > Data Store Memory Block pane, where each can have one of the following values:
Disable all — Disables this diagnostic for all data stores accessed by the model.
Enable all as warnings — Displays the diagnostic as a warning in the MATLAB® Command Window.
Enable all as errors — Halts the simulation and displays the diagnostic in an error dialog box.
Use local settings — Allow each Data Store Memory block to set its own value for this diagnostic (default).
The same diagnostics also appear in each Data Store Memory block parameters dialog box Diagnostics tab. You can set each diagnostic to none, warning, or error. The value specified by an individual block takes effect only if the corresponding configuration parameter is Use local settings. See Diagnostics Pane: Data Validity and the Data Store Memory documentation for more information.
The most conservative technique is to set all data store diagnostics to Enable all as errors in Model Configuration Parameters > Diagnostics > Data Validity > Data Store Memory Block. However, this setting is not best in all cases, because it can flag intended behavior as erroneous. For example, the next figure shows a model that uses block priorities to force the Data Store Read block to execute before the Data Store Write block:
An error occurred during simulation because the data store A is read from the Stateflow® chart before the Data Store Write updates it. If the associated delay is intended, you can suppress the error by setting the global parameter Detect read before write to Use local settings, then setting that parameter to disable in the Data Store Write block. If you use this technique, be sure to set the parameter to error in all other Data Store Write blocks aside from those which are to be intentionally excluded from the diagnostic.
Diagnostics might be more conservative for data store memory used by MATLAB Function blocks. For example, if you pass arrays of data store memory to MATLAB functions, optimizations such as A=foo(A) might result in MATLAB marking the entire contents of the array as read or written, even though only some elements were accessed.
Data integrity may be compromised if a data store is read from in one task and written to in another task. For example, suppose that:
A task is writing to a data store.
A second task interrupts the first task.
The second task reads from that data store.
If the first task had only partly updated the data store when the second task interrupted, the resulting data in the data store is inconsistent. For example, if the value is a vector, some of its elements may have been written in the current time step, while the rest were written in the previous step. If the value is a multi-word, it may be left in an inconsistent state that is not even partly correct.
Unless you are certain that task preemption cannot cause data integrity problems, set the compile-time diagnostic Model Configuration Parameters > Diagnostics > Data Validity > Data Store Memory Block > Multitask Data Store to warning (the default) or error. This diagnostic flags any case of a data store that is read from and written to in different tasks. The next figure illustrates a problem detected by setting Multitask Data Store to error:
Since the data store A is written to in the fast task and read from in the slow task, an error is reported, with suggested remedy. This diagnostic is applicable even in the case that a data store read or write is inside of a conditional subsystem. Simulink correctly identifies the task that the block is executing within, and uses that task for the purpose of evaluating the diagnostic.
The next figure shows one solution to the problem shown above: place a rate transition block after the data store read, which previously accessed the data store at the slower rate.
With this change, the data store write can continue to occur at the faster rate. This may be important if that data store must be read at that faster rate elsewhere in the model.
The Multitask Data Store diagnostic also applies to data store reads and writes in referenced models. If two different child models execute a data store's reads and writes in differing tasks, the error will be detected when Simulink compiles their common parent model.
Data store errors can occur due to duplicate uses of a data store name within a model. For instance, data store shadowing occurs when two or mote data store memories in different nested scopes have the same data store name. In this situation, the data store memory referenced by a data store read or write block at a low level may not be the intended store.
To prevent errors caused by duplicate data store names, set the compile-time diagnostic Model Configuration Parameters > Diagnostics > Data Validity > Data Store Memory Block > Duplicate Data Store Names to warning or error. By default, the value of the diagnostic is none, suppressing duplicate name detection. The next figure shows a problem detected by setting Duplicate Data Store Names to error:
The data store read at the bottom level of a subsystem hierarchy refers to a data store named A, and two Data Store Memory blocks in the same model have that name, so an error is reported. This diagnostic guards against assuming that the data store read refers to the Data Store Memory block in the top level of the model. The read actually refers to the Data Store Memory block at the intermediate level, which is closer in scope to the Data Store Read block.
The Model Advisor provides several diagnostics that you can use with data stores. See these sections for information about Model Advisor diagnostics for data stores: