Parfor loop in Matlab class issue
Mostrar comentarios más antiguos
I have written my code in App designer and I have used parfor to speed up my run. However the run is very slow. I will explain the issue with an example. Functions called within parfor loop will only work if those functions are defined under methods static and the function calling is done by className.functionName(argument).
Incase I define the function with just methods (Access= private), function calling is done by functionName(app,argument), but these types of function calling will not work when used with parfor and will throw an error. This topic was previously discussed in the subcomments of this post --> here .
It takes 10 times more time with 'parfor along with static method functions' than the usual 'for loop with methods (Access= private) function'.
What can be done to speed up the parfor?
The code below is for representative purposes only.
classdef app1 < matlab.apps.AppBase
properties (Access = public)
MaximumDistancebetween2cellsEditField matlab.ui.control.NumericEditField
end
properties (Access = public)
postprocess
end
methods (Access = private, Static)
function [c,ceq] = nlincon(p,coord)
ceq = [];
for i=1:2
for j= 1:250
c(n) = 32^2-((p(i,1)-coords(j,1))^2 + (p(i,2)-coords(j,2))^2);
end
end
end
function out = coordsAction(coords)
% some operation
out = coords;
end
function value = optimization(xval,yval,xmin,ymin,coords,cdf,xq,yq)
initialVal{1,1}(1,1) = xval + xmin;
initialVal{1,1}(1,2) = xval - ymin;
initialVal{1,1}(2,1) = xval + xmin;
initialVal{1,1}(2,2) = xval + ymin;
p1 = @(Point) interp2(xq,yq,cdf,Point(1,1),Point(1,2),'nearest');
p2 = @(Point) interp2(xq,yq,cdf,Point(2,1),Point(2,2),'nearest');
newCoords = app1.coordsAction(coords)
nonlcon = @(p) app1.nlincon(p,newCoords);
lb1(1,1) = initialVal{1,1}(1,1) ;
lb1(1,2) = initialVal{1,1}(1,2) - ymin*2;
ub1(1,1) = initialVal{1,1}(1,1) ;
ub1(1,2) = initialVal{1,1}(1,2) ;
lb1(2,1) = initialVal{1,1}(2,1) ;
lb1(2,2) = initialVal{1,1}(2,2) ;
ub1(2,1) = initialVal{1,1}(2,1) ;
ub1(2,2) = initialVal{1,1}(2,2) + ymin*2;
W1 = @(X) -1+(1-p1(X))*(1-p2(X));
options = optimoptions('fmincon','Algorithm','sqp');
[value,~,exitflag] = fmincon(W1,initialVal{1,1},[],[],[],[], lb1, ub1, nonlcon, options);
end
end
methods (Access = private)
function Sensor(app)
%these values have been defined in other functions are saved
%there as app.postprocess.variableName
xmin = app.postprocess.xmin;
ymin = app.postprocess.ymin;
coords = app.postprocess.coords;
cdf = app.postprocess.cdf;
xq = app.postprocess.xq;
yq = app.postprocess.yq;
cellX = -2000:5:2000;
cellY = -500:5:500;
nx = numel(cellX);
ny = numel(cellY);
completeCellPositions={};
parfor idx = 1:(nX*nY)
[i, j] = ind2sub([nX, nY], idx);
xVal = cellX(i);
yVal = cellY(j);
temp1 = app1.optimization(xVal,yVal,xmin,ymin,coords,cdf,xq,yq);
completeCellPositions = [completeCellPositions, temp1];
end
app.postprocess.cellPos = completeCellPositions
end
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: PostprocessButton
function PostprocessButtonPushed(app, event)
Sensor(app);
cellPos = app.postprocess.cellPos;
%further actions
end
end
end
--
Respuestas (1)
Arnab Samaddar-Chaudhuri
el 23 de Nov. de 2022
0 votos
Categorías
Más información sobre Construct and Work with Object Arrays en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!