Transparency issue with Simulink in PARFOR loop

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);

回答(1 个)

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 个评论

Thanks for the reply! I've pushed the lines into a function, as well as all the variables used by the model. It seems to have corrected the transparency issue. However, I now get this error
### Building the Accelerator target for model: OS_Model_V2
### Building the Accelerator target for model: OS_Model_V2
### Generating code into build folder: E:\Google Drive\RESTOFPATH\OS_Model_V2
Warning: Control Character '\G' is not valid. See 'doc sprintf' for control characters valid in the format string.
### Build procedure for model: 'OS_Model_V2' aborted due to an error.
Error using simInPARFOR (line 59)
Problem creating Accelerator MEX file for model 'OS_Model_V2'.
which, from what I understand, seems to be an error with the notation used to save the MEX file. However, I have no issue running the simulation via SIMULINK and I previously had no issue before using the PARFOR loop either.
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.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Manual Performance Optimization 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by