Ensure Transparency in
The body of a
spmd block must
be transparent. Transparency means that all references to variables
must be visible in the text of the code.
In the following examples, the variable
X is not transferred to the
workers. Only the character vector
'X' is passed to
X is not visible as an input
variable in the loop or block body. As a result, MATLAB® issues an error at run time.
X = 5; parfor ii = 1:4 eval('X'); end
X = 5; spmd eval('X'); end
Similarly, you cannot clear variables from a workspace by executing
clear inside a
parfor ii = 1:4 <statements...> clear('X') % cannot clear: transparency violation <statements...> end
spmd; clear('X'); end
Alternatively, you can free up memory used by a variable by setting its value to empty when it is no longer needed.
parfor ii = 1:4 <statements...> X = ; <statements...> end
In the case of
spmd blocks, you can clear its Composite from the
In general, the requirement for transparency restricts all dynamic access to variables, because the entire variable might not be present in any given worker. In a transparent workspace, you cannot create, delete, modify, access, or query variables if you do not explicitly specify these variables in the code.
Examples of other actions or functions that violate transparency in a
If a script attempts to read or write variables of the parent workspace, then running this script can cause a transparency violation. To avoid this issue, convert the script to a function, and call it with the necessary variables as input or output arguments.
Transparency applies only to the direct body of the
spmd construct, and not to any functions called from there.
The workaround for
load is to
hide the calls to
load inside a
Parallel Simulink Simulations
You can run Simulink® models in parallel with the
parsim command instead
parfor-loops. For more information and examples of
using Simulink in parallel, see Running Multiple Simulations (Simulink).
If your Simulink model requires access to variables contained in a
.matfile, you must load these parameters in the workspace of each worker. You must do this before the
parfor-loop, and after opening
parpool. To achieve this, you can use
parfevalOnAll, as shown in the examples.
spmd evalin('base', 'load(''path/to/file'')') end
parfevalOnAll(@evalin, 0, 'base', 'load(''path/to/file'')')
If your model also requires variables defined in the body of your MATLAB script, you must use
evalinto move these variables to the base workspace of each worker, in every