parfeval performance inside appdesigner apps

45 views (last 30 days)
cr
cr on 20 Sep 2024
Commented: Rik on 1 Nov 2024 at 7:41
I have an image processing routine that takes 4-5s when run on a single thread. If parallelized using parfeval() inside an m script and function file the time elapsed is ~2s. When I use the same methods inside an appdesigner (where the structure of functions has to be adapted such that the function takes in 'app') I see weird things. E.g. if the funtion is structured in a manner as below I see warning message Warning: Unable to save App Designer app object. Save notsupported for matlab.apps.AppBase objects.
% Style-1 %
function result = somePrivateFcn(app,img,a,b)
%
F(1) = parfeval(Pool,@processImg,1,img(1:500,1:500),Type,1);
F(2) = parfeval(Pool,@processImg,1,img(1:500,501:end),Type,1);
% some processing %
function out = processImg(img,Type,N)
%some processing
out = x.centroid;
end
end
I'm confused by the warning since I'm not passing in the app object to the processImg subfunction. Perhaps all the workspace of parent function needs to be available and hence app object from parent function needs to be copied to workers. I don't know if this is the reason but the execution takes 6s instead of expected 2s.
I tried taking the processImg() out of the somePrivateFcn() and made it just another private function with definitions like below that would ensure app object is not copied to workers (just using 0 instead of app).
% Style-2 %
function result = somePrivateFcn(app,img,a,b)
F(1) = parfeval(Pool,@processImg,1,0,img(1:500,1:500),Type,1);
F(2) = parfeval(Pool,@processImg,1,0,img(1:500,501:end),Type,1);
% some processing %
end
function out = processImg(~,img,Type,N)
%some processing
out = x.centroid;
end
This however showed new messages in cmd window (not warning, just info) Analyzing and transferring files to the workers ...done. The execution time now shot to ~15s.
I tried creating a separate mfile for procesImg function to get rid of the need to pass app obj. This time there were no warnings or messages but the processing time rose to 12s.
Can someone help understand what's going on. What should I do to get the performance of parfeval in appdesigner on par with that in script implementation?
Last year in a different project (using R2023a) I recall doing a task like in style-2 above but with app object sent to processImg() when called from parfeval and also app mentioned in the function definition of processImg() (instead of being skipped with ~). The parallel exection worked fine and I never saw save App warnings or the "... transferring data..." message ever. Has the behaviour changed between R2023a and R2023b?
Thanks.
  3 Comments
cr
cr on 31 Oct 2024 at 10:16
Edited: cr on 31 Oct 2024 at 10:19
I have been seeing weird things while digging deep into the cause. So there is a thread here that tells how to avoid passing app object to workers. Use "static", fair enough!
Now, before I found the other thread I tried making the function a standalone function file and the warning messages went away but there was no time advantage of using parallel processing that I was getting like I did when I ran the equivalent code outside the app in a script. I implemented static function in the manner below.
classdef MyApp < matlab.apps.AppBase
properties (Access = public)...
end
methods (Access = private, Static)
function out = processImg(img,Type,N)
%some processing
out = x.centroid;
end
end
methods (Access = private)
function result = somePrivateFcn(app,img,a,b)
% some param definitions and variable initializations
F(1) = parfeval(Pool,@MyApp.processImg,1,0,img(1:500,1:500),Type,1);
F(2) = parfeval(Pool,@MyApp.processImg,1,0,img(1:500,501:end),Type,1);
% some processing %
end
end
end
If I put app.processImg() rather than MyApp.processImg() in parfeval it doesn't behave the same and I get the warning messages back and time taken to execute shoots up.
Rik
Rik on 1 Nov 2024 at 7:41
If your license allows, you might want to create a support ticket. This sounds like an issue with the Matlab internals to me.

Sign in to comment.

Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!