Is parallel processing possible in optimization with App Designer

4 views (last 30 days)
I have an app that I developed in App Designer which performs optimization with the aid of the Matlab function lsqnonlin. A snippet:
methods (access = private)
function x=doOptimize(app,x0,lb,ub,options)
f=@(xx)Fun(app,xx);
x=lsqnonlin(f,x0,lb,ub,options);
end
function ff=Fun(app,x)
ff=someFunction(app,x);
end
end
The “someFunction” is a very complex function which utilizes many app properties and methods. The app works fine. However, I would like to take advantage of parallel processing by setting options.UseParallel = true. Doing this causes all kinds of warnings and errors, evidently in accordance with previous posts that stated that the save and load performed by the parallel workers are incompatible with app objects. My question is this: Is there any work-around for this problem? Of course, if app could be excluded altogether from the function Fun, then access=Static can be used. But in this case app is completely intertwined in the calculations. On the other hand, for the parallel processing, why should it be necessary to save the app variable? Since app-related variables only appear in someFunction, would it be possible to remove all references to app in the snippet, define access as Static, and pass app to someFunction as a global variable? Could this trick the parallel workers into thinking that there are no app objects around? (I’m willing to try anything.)
  2 Comments
Karan Singh
Karan Singh on 25 Jul 2024
@Sherman Marcus I belive the approach you are suggesting is possible have you tried it out? One approach is to pass the necessary data from the app object as separate variables to the someFunction. Instead of passing the app object, pass the necessary data as separate arguments to someFunction.
methods (Access = private, Static)
function x = doOptimize(x0, lb, ub, options, appData)
f = @(xx) Fun(xx, appData);
x = lsqnonlin(f, x0, lb, ub, options);
end
function ff = Fun(x, appData)
ff = someFunction(x, appData);
end
function ff = someFunction(x, appData)
end
end
% Call doOptimize with additional appData argument
appData = struct('data1', app.data1, 'data2', app.data2, ...); % Add all necessary data
x = doOptimize(x0, lb, ub, options, appData);
Sherman Marcus
Sherman Marcus on 25 Jul 2024
Thank you Karan. I assume you are absolutely correct. The problem in my case is that I have many many properties and methods that are involved, and separating them out I believe would be impractical. I therefore had hoped there might be another way to transfer the app object itself. Of course, global statements should be avoided, but if it would work it might be worthwhile. I'm in the process of trying.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 25 Jul 2024
Edited: Walter Roberson on 25 Jul 2024
My question is this: Is there any work-around for this problem?
No, there is no workaround.
Since app-related variables only appear in someFunction, would it be possible to remove all references to app in the snippet, define access as Static, and pass app to someFunction as a global variable?
No, the value of global variables is not transfered to parallel workers.
The closest approach to that would be parallel.pool.Constant

More Answers (0)

Categories

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

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!