The body of a
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
not transferred to the workers. Only the character vector
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 client workspace.
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
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
and not to any functions called from there. The workaround for
to hide the calls to
You can run Simulink® models in parallel with the
instead of using
parfor-loops. For more information
and examples of using Simulink in parallel, see Run Multiple Simulations (Simulink).
If your Simulink model requires access to variables
contained in a
.mat file, you must load these parameters
in the workspace of each worker. You must do this before the
and after opening
parpool. To achieve this, you
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
evalin to move these variables to the
base workspace of each worker, in every