Generate Code for Global Variables
You can generate C/C++ code and MEX functions for MATLAB® functions that use global variables. When you run a MEX function for MATLAB code that uses a global variable, the generated function and MATLAB each have their own copy of the variable. You must decide how you want to synchronize data between these copies. In addition, certain limitations apply when you use global data in MATLAB code for code generation.
Generate Code for a MATLAB Function That Uses Global Data
When you generate code for a MATLAB function that uses global variables, you must define and initialize the variables before you generate code or at the time of code generation. If you do not define global variables at the time of code generation, the code generator looks for the global variable in the MATLAB global workspace. If the global variable does not exist, code generation fails.
To generate code for a MATLAB function that uses global variables, use one of these approaches:
Define and initialize the global variables in the MATLAB global workspace before you generate code by using the MATLAB Coder™ app or the
codegen
command.In the MATLAB Coder app, define and initialize the global variables on the Entry Points pane before you generate code.
At the command line, define and initialize global variables by using the
codegen
command with the-globals
option.
Define and Initialize Global Variables in the Global Workspace
To define and initialize the global variables in the MATLAB global workspace before code generation, use the
global
function at the command line. For
example:
global A B;
A = ones(4);
B = [1 2 3];
Then, generate code by using the MATLAB
Coder app or the codegen
command. You do not have to
further specify the global variables.
Define and Initialize Global Variables in the MATLAB Coder App
To define and initialize global variables in the MATLAB Coder app, click New entry point > New global. Specify the name, type, and initial value of each global variable. See Define Global Variables in the MATLAB Coder App. Then, generate code by using the app.
Define and Initialize Global Variables in the codegen
command
You can define and initialize global variables at the time of code generation when
you generate code at the command line by using the codegen
command. Use the -globals
option to pass a cell array of global
variable names and initial values to the codegen
command. This
cell array has the format
{
.
For
example:global1
,value1
,global2
,value2
,...}
codegen use_globals -globals {"A",ones(4),"B",[1 2 3]} -args {0}
Alternatively, you can specify global variable types by using the coder.typeof
function. In this case, the cell array you pass to the
-globals
option has the format
{
.
For
example:global1
,{type1
,value1
},global2
,{type2
,value2
},...}
codegen use_globals -globals {"A",{coder.typeof(0,[4 4]),ones(4)},"B",{coder.typeof(0,[1 3]),[1 2 3]}} -args {0}
If the global variable is a cell array, you must use
coder.typeof
. See Specify Global Cell Arrays at the Command Line.
Define Constant Global Data
If you know that the value of a global variable does not change at run time, you can reduce overhead in the generated code by specifying that the global variable has a constant value. You cannot write to a constant global variable. You also cannot synchronize the value of a constant global variable with MATLAB.
To define a constant global variable, use on of these approaches:
At the command line, use the
coder.Constant
class in the cell array that you pass to the-globals
option of thecodegen
command. For example:codegen use_globals -globals {"A",ones(4),"B",coder.Constant([1 2 3])} -args {0}
In the MATLAB Coder app, in the Globals section of the Entry Points pane, point to a variable and then click the Modes and Actions button . In the Modes section, select Use Constant.
Synchronizing Global Data Between Generated MEX Function and MATLAB
When you generate and run a MEX function, the generated function and MATLAB each have copies of the global data. To make these copies consistent, you must synchronize global data when the MEX function and MATLAB interact. If you do not synchronize the data, their global variables might differ. The degree to which the generated function modifies global data determines how frequently to synchronize global data.
When you define a global variable as a constant, you cannot synchronize the global data used by the generated function with MATLAB. Instead, the MEX function generates an error if the value of a constant global variable in the generated function code differs from the value of the variable in the MATLAB global workspace.
When to Synchronize Global Data
By default, synchronization of global data occurs at when MEX function execution begins, when MEX function execution ends, and after each extrinsic function call. This behavior results in maximum consistency between the generated function and MATLAB, but can decrease the performance of the generated code function.
You can control the synchronization of global data between the generated function
and MATLAB by using the configuration parameter Global data
synchronization mode. For individual extrinsic functions, you can
control global data synchronization by using the "-sync"
argument
to the coder.extrinsic
function.
This table summarizes how to set these parameters for different goals when you generate a MEX function.
Goal | Setting of the parameter Global data synchronization mode | Function calls that use
coder.extrinsic | Generated Code Behavior |
---|---|---|---|
Maximum consistency. | At MEX-function entry, exit and extrinsic
calls | Unused |
This behavior is the default. |
Optimize synchronization behavior when most extrinsic function calls modify global data, but certain extrinsic functions do not. | At MEX-function entry, exit and extrinsic
calls | Disable synchronization and constant value checking for certain
extrinsic functions by using the argument
"-sync:off" |
|
Optimize synchronization behavior when only a few extrinsic function calls modify global data. | At MEX-function entry and exit | Enable synchronization and constant value checking for certain
extrinsic functions by using the argument
"-sync:on" |
|
Optimize synchronization behavior when extrinsic calls do not modify global data. | At MEX-function entry and exit | Unused |
|
Potentially optimize performance by disabling synchronization. Do not use this mode unless your code does not modify global data. | Disabled | Not available |
|
Clear Global Data
Because MEX functions and MATLAB each have their own copy of global data, you must clear both copies to ensure that consecutive MEX runs produce the same results.
To clear the copy of the global data in the MATLAB workspace, use this command:
clear global
To clear the copy of the global data in the MEX workspace, use this command:
clear MEX
Alternatively, use this command to remove both copies of the global data:
clear all
Global Data Limitations for Generated Code
Global structure variables cannot contain handle objects or sparse arrays.
You cannot apply
coder.cstructname
directly to a global variable. To name the structure type to use with a global variable, usecoder.cstructname
to create a type object that names the structure type. Then, when you runcodegen
, specify that the global variable has that type. See Name C Structure Type to Use with Global Structure Variable.Global variables do not synchronize when there is a run-time error during SIL or PIL execution. Consider the function
SILtest
, which increments global variableg
and then produces a run-time error.In MATLAB and MEX execution,function SILtest global g; g = g + 1; error('MyError'); end
SILtest
increments and synchronizes global variableg
in the workspace. However, in SIL execution, the global variableg
does not increment in the workspace due to the run-time error. For more information on using SIL and PIL execution with global variables, see Speed Up SIL/PIL Execution by Disabling Constant Input Checking and Global Data Synchronization (Embedded Coder).
See Also
MATLAB Coder | global
| codegen