Parfor loop in Matlab class issue

2 ビュー (過去 30 日間)
Arnab Samaddar-Chaudhuri
Arnab Samaddar-Chaudhuri 2022 年 11 月 22 日
回答済み: Arnab Samaddar-Chaudhuri 2022 年 11 月 23 日
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
--

回答 (1 件)

Arnab Samaddar-Chaudhuri
Arnab Samaddar-Chaudhuri 2022 年 11 月 23 日
I have tried out various testcases. The problem doesnt lie with using static method. Even when I create seperate functions in .m files, instead of using private in-app functions, the runtime is similar and still very slow.
From what I can deduce, this high runtime is due to function overhead

カテゴリ

Help Center および File ExchangeParallel for-Loops (parfor) についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by