Issue in using user-defined objects within parfor loop

3 次查看(过去 30 天)
I'm pretty new to parallel computation in MATLAB and I'm facing the following issue: I have the following script calling iteratively an optimization function
trials = cell2struct([ ...
{ 4 5 6 7 10 16 17 18 23 24}; ...
{ 0.5 1 0.5 0 0 0.5 0.5 0.5 0.5 0}; ...
{ 0 0 pi pi 0 pi-pi/6 -pi/6 pi/6 pi+pi/6 pi/6}; ...
{0.0458 0.0440 0.0510 0.0312 0.0464 0.0449 0.0461 0.0598 0.0598 0.0511} ], ...
{'idx', 'mounting', 'q5_0', 'dq4_squared'}, 1);
coeff = [2, 10.5, 20, 25, 2.5];
while < some_stop_condition >
[coeff, fval] = fminunc(@(x) EvaluateIndex(x, trials), coeff);
end
I have a working sequential version of the function EvaluateIndex, but since it takes very much time to perform the computation I would like to switch to a parallelized version. A simplified version of the "parallelized" objective function EvaluateIndex is as follows:
function MeanSquaredError = EvaluateIndex(coeff, configs)
% Simulation time
dT = 0.01; % Time step [s]
T_final = 60; % Duration [s]
% Wave characteristics
wave = SeaWave( < sea_wave_params > );
% Vehicle settings
for i = 1:length(configs)
vehicle(i) = PrototypeWAVE( < vehicle_i_params > );
end
dq4_squared = zeros(1, length(configs));
tic
parfor i = 1:length(configs)
% Initial conditions
q1_0 = 0;
q2_0 = 0;
q3_0 = 0;
q4_0 = pi/2;
q5_0 = configs(i).q5_0;
q_0 = [ q1_0+1.3*(1-cos(q3_0)), q2_0+1.3*sin(q3_0), q3_0, q4_0, q5_0 ]';
dq_0 = [ 0, 0, 0, 0, 0 ]';
% Run simulation
[~, Results] = Run(vehicle(i), wave, [q_0; dq_0], dT, T_final);
% Get results
dq4_squared(i) = mean(Results(:,9).^2);
end
toc
MeanSquaredError = sum( (dq4_squared - horzcat(configs.dq4_squared)).^2 );
end
Here, SeaWave and PrototypeWAVE are two classes, and the function Run is a method of the PrototypeWAVE class. According to some suggestions I have found, I have defined the objects of these classes outside the parfor loop; however, when I try to run the script, I get the following warnings:
Warning: Element(s) of class 'SeaWave' do not match the current constructor definition. The element(s) have been converted to structures.
Warning: Element(s) of class 'PrototypeWAVE' do not match the current constructor definition. The element(s) have been converted to structures.
and this error:
Error using EvaluateIndex (line 75)
Cannot find an exact (case-sensitive) match for 'Run'
The closest match is: run in /Applications/MATLAB_R2016b.app/toolbox/matlab/lang/run.m
It seems that MATLAB does not recognize the existence of the classes and their methods. I have already tried to add the line addAttachedFiles(gcp, {'./@SeaWave', './@PrototypeWAVE'}) before the parfor loop, but nothing changes.
I'm using MATLAB R2016b on MACOS.
Any help is very appreciated. Thanks in advance
  2 个评论
Adam
Adam 2017-12-19
may help. I don't know how your classes are defined so it is hard to say, but it appears you need to support the 0 input argument case in your constructor for use within a parfor loop.
Shouldn't this line:
q_0 = [ q1_0+vehicle.cog*(1-cos(q3_0)), q2_0+vehicle.cog*sin(q3_0), q3_0, q4_0, q5_0 ]';
refer to
vehicle(i)
instead?
Davide Fenucci
Davide Fenucci 2017-12-19
编辑:Davide Fenucci 2017-12-19
According to this https://it.mathworks.com/matlabcentral/answers/339474-constructing-class-objects-within-a-parfor-loop everything should work fine, if objects are constructed outside the parfor loop. Should the 0 input argument constructor have to be supported in any case?
I have edited the post to fix the issue with the parameter vehicle.cog (typo after the simplification of the function)

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by