DWork Vector Basics

What is a DWork Vector?

DWork vectors are blocks of memory that an S-function asks the Simulink® engine to allocate to each instance of the S-function in a model. If multiple instances of your S-function can occur in a model, your S-function must use DWork vectors instead of global or static memory to store instance-specific values of S-function variables. Otherwise, your S-function runs the risk of one instance overwriting data needed by another instance, causing a simulation to fail or produce incorrect results. The ability to keep track of multiple instances of an S-function is called reentrancy.

You can create an S-function that is reentrant by using DWork vectors that the engine manages for each particular instance of the S-function.

Advantages of DWork Vectors

DWork vectors have several advantages:

  • Provide instance-specific storage for block variables

  • Support floating-point, integer, pointer, and general data types

  • Eliminate static and global variables

  • Interact directly with the Simulink engine to perform memory allocation, initialization, and deallocation

  • Facilitate inlining the S-function during code generation

  • Provide more control over how data appears in the generated code

    Note   DWork vectors are the most generalized and versatile type of work vector and the following sections focus on their use. The Simulink product provides additional elementary types of work vectors that support floating-point, integer, pointer, and mode data. You can find a discussion of these work vectors in Elementary Work Vectors.

DWork vectors provide the most flexibility for setting data types, names, etc., of the data in the simulation and during code generation. The following list describes all the properties that you can set on a DWork vector:

  • Data type

  • Size

  • Numeric type, either real or complex

  • Name

  • Usage type (see Types of DWork Vectors)

  • Simulink Coder™ identifier

  • Simulink Coder storage class

  • Simulink Coder C type qualifier

See How to Use DWork Vectors for instructions on how to set these properties. The three Simulink Coder properties pertain only to code generation and have no effect during simulation.

DWork Vectors and the Simulink Engine

A key advantage of DWork vectors is their connection to the Simulink engine. Over the course of the simulation, the engine relieves the S-function of all memory management tasks related to DWork vectors.

To see how this connection is useful, consider an S-function that uses a global variable to store data. If more than one copy of the S-function exists in a model, each instance of the S-function must carefully allocate, manipulate, and deallocate each piece of memory it uses.

In an S-function that uses DWork vectors, the engine, not the S-function, manages the memory for the DWork vector. At the start of a simulation, the engine allocates the memory required for each instance of the S-function based on the size and the data type of the DWork vector contents. At the end of the simulation, the engine automatically deallocates the memory.

    Note   You have no control over how the engine allocates memory for DWork vectors during simulation. When using the Simulink Coder software, you can use storage classes to customize the memory allocation during code generation. See the ssSetDWorkRTWStorageClass reference page for more information on using storage classes.

The engine also performs special tasks based on the type of DWork vector used in the S-function. For example, it includes DWork vectors that store discrete state information in the model-wide state vector and makes them available during state logging.

DWork Vectors and the Simulink Coder Product

DWork vectors allow you to customize how data appears in the generated code. When code is generated, the Simulink Coder code generator includes the DWork vector in the data structure for the model. The DWork vector controls the field name used in the structure. DWork vectors also control the storage class and C type qualifier used in the generated code. See sfun_rtwdwork.c for an example.

Was this topic helpful?