Is parallel processing possible in optimization with App Designer

4 次查看(过去 30 天)
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 个评论
Karan Singh
Karan Singh 2024-7-25
@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 2024-7-25
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.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2024-7-25
编辑:Walter Roberson 2024-7-25
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by