MATLAB: Does SIM not work with PARFOR in Distributed Computing Toolbox 3.2 (R2007b)

commandMATLAB Parallel ServerParallel Computing Toolboxparsimsavesimtransperancy

When I execute the following commands:
parfor (i=1:4)
sim('vdp');
end
I receive the following error: ERROR: ??? Error using ==> parallel_function>make_general_channel/channel_general at 844 Error using ==> sim Transparency violation error. See Parallel for Loops in MATLAB, "Transparency". Error in ==> parallel_function>distributed_execution at 741 [tags, out] = P.getCompleteIntervals(chunkSize); Error in ==> parallel_function at 553 R = distributed_execution(…

Best Answer

This is the expected functionality. To use SIM in PARFOR, you must use a wrapper to make the SIM command "Transparent."
A program is not transparent if it assigns to variables in the workspace. SIM('vdp') is not transparent because it logs to variables in the workspace. Therefore, it needs to be made transparent. One way to do this is to encapsulate the SIM call into a local function.
To work around this issue, use the following commands:
parfor (i=1:4)
x{i} = parallel_sim('vdp');
end
where PARALLEL_SIM is the following function:
function x = parallel_sim(mdl)
[t,x,u] = sim(mdl)
end
In general, PARFOR runtime rules specify the runtime transparency requirement. It places a restriction on the use of MATLAB functions within the PARFOR loop to those that are statically analyzable. Certain constructs in MATLAB, such as EVAL, EVALIN, LOAD, and SAVE, modify their workspaces in a non-statically analyzable way. Thus, eval('string') or evalin('string', 'workspace') will modify respective workspaces as defined by the variable 'string', which can be an arbitrary piece of MATLAB code (and in a particularly malicious case even a call to exit). Similarly, LOAD, which loads a MATLAB data file into memory with an arbitrary set of variables, modifies the 'workspace' in a way that cannot be determined by static analysis alone. There is a single 'workspace' before and after the PARFOR loop. When multiple workers are trying to operate on this 'workspace', using such functions will destroy its integrity – that is, there will be no deterministic way to find out what data must be transferred to and from the workers. Attempts to execute a PARFOR loop containing such constructs, causes MATLAB to throw an error.