1
votes

I am using the Matlab parallel toolbox to do parallel computing. I've doubled check the variable definition of the code, and it follows the requirements of parfor command. However, Matlab still told me that "Transparency Violation Error," could you help me figure out this issue?

Here is the source code. Thank you so much.

load Wind80.mat
Wspeed_80 = Wind80;
TotalLoc = 4000;
Nloc = 5; % no of loc of each run
maxrun = ceil(TotalLoc/Nloc);
StrInfo.Pstr = 1; 
StrInfo.Tstr = 0:4:72;
tic
parfor run = 1:maxrun
   WT1 = [];
   WT2 = [];
   [WT1, WT2] = CompWTGenFun(run, Nloc, TotalLoc, StrInfo);
   filenm = ['ResultPara' num2str(StrInfo.Pstr) 'Run' num2str(run) '.mat' ];
   save(filenm, 'WT1', 'WT2', '-mat');
   clear WT1 WT2
end
toc
1
Not a solution, but some general improvements: Initializing WT1 and WT2 to empty matrices has no purpose, and clear WT1 WT2 is a pessimization (clear will typically slow down your code). - Cris Luengo
Regarding the problem: I think it might be because MATLAB cannot tell that filenm will be different for each loop iteration. Try generating the file names outside the loop, then in the loop save(filenm{run},...). - Cris Luengo
Thank you so much. The problem is solved by moving 'clear WT1 WT2' and save function in the subfunction of the parfor loop. - AIRicky
It seems Edric’s answer helped you then. If so, please click on the grey check mark to the left of the answer, o mark it as accepted. Thanks! - Cris Luengo

1 Answers

2
votes

You cannot use the save function within a parfor loop, as this will result in a transparency violation. Transparency violations occur when a function needs to look into (or modify) its calling workspace. Unfortunately, save does this - in your code, you give the save command the names of the variables you want to save, and the save implementation attempts to extract the values from its calling workspace (i.e. the body of the parfor loop).

The workaround is to hide the call to save inside a separate function, in other words, you need something like this:

parfor ...
    mySave(filenm, WT1, WT2);
end
# ...
function mySave(filenm, WT1, WT2)
    save(filenm, 'WT1', 'WT2', '-mat');
end

This works because the transparency constraint only applies to the code directly present in the body of the parfor loop.

As mentioned in the comments, the clear command you have is not necessary, and would again cause a transparency violation.