Transparency issue with Simulink in PARFOR loop
Show older comments
Hi,
I am currently trying to optimize parameters for a system by comparing various MATLAB optimization solvers. The function I am optimizing runs a Monte Carlo simulation by running a Simulink Model. Everything works correctly in serial computing. However, considering the optimization with GlobalSearch takes around 24hours to run, I am trying to make it faster by changing my code to Parallel Computing.
I am currently running on MATLAB R2015a, meaning I do not have access to parsim, parallel.pool.Constant or anything like this.
Here's a sample of code (simplified to make it easier to read):
function f = energy(param)
nbr = 5000;
parpool('local', 4)
parfor i = 1:nbr
Ixv = normrnd(param(1),7.5e-3/10,1,1); %I have 7 other parameters like this
Omega_2 = [];
t = [];
simOut = sim('OS_Model_V2','SrcWorkspace','current');
Omega_2 = simOut.get('Omega_2');
t = simOut.get('t');
energyused(i) = trapz(t,Omega_2.data);
end
parpool close
moyenergy = 1/nbr*sum(energyused);
sigmaenergy = 1/(nbr-1) *sum((energyused - moyenergy).^2);
f= 0.5*moyenergy+0.5*sigmaenergy;
MATLAB gives me the following error message:
Error using energy>(parfor body) (line 274)
Invalid setting in 'OS_Model_V2/Controller/Ixx' for parameter 'Gain'.
Error using energy>(parfor body) (line 274)
Transparency violation error.
See Parallel Computing Toolbox documentation about Transparency
Line 274 is the line calling SimOut = sim(...). I get a similar error for pretty much every other parameter.
I simply do not understand where the error is coming from. I understand PARFOR requires every call to a variable to be visible in the PARFOR body. But except for the param(1) call, every other variable is declared inside the body.
I have tried using assignin(), using a function in the PARFOR body to assign the value of param, etc. and nothing seems to work. Any help would be greatly appreciated.
Also, would it possibly be faster to simply use parallel computing when calling my optimization function rather than using it inside the function in the Monte Carlo simulation?
options = optimoptions('solvername','UseParallel',true);
Answers (1)
Walter Roberson
on 16 Jun 2017
You need to push the
simOut = sim('OS_Model_V2','SrcWorkspace','current');
Omega_2 = simOut.get('Omega_2');
t = simOut.get('t');
portion into a function call, so that the "current" workspace is well separated from the workspace that the parfor is executing in. Note in this regard that you will need to copy into that function's workspace all variables that the model reads using From Workspace blocks, and all global variables that might be handled by the Data Store
2 Comments
Gabriel Descôteaux
on 19 Jun 2017
Walter Roberson
on 19 Jun 2017
I would suggest changing your build folder to something local. Besides the problem that someone has used sprintf(VARIABLE) instead of sprintf("%s", VARIABLE), there can be strange interactions and performance issues when trying to build into a drive that is being handled by an application such as Google Drive or iCloud.
Categories
Find more on Manual Performance Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!