Issue in using user-defined objects within parfor loop
9 views (last 30 days)
Show older comments
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 Comments
Adam
on 19 Dec 2017
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?
Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements 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!