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
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);
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop Apps Using App Designer 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!