Parallel Workers Sometimes Can't Access Function
10 次查看(过去 30 天)
显示 更早的评论
I'm building an app that uses the Parallel Processing toolbox to run Simulink models in parallel. I create a list of FevalFuture objects for the workers, and assign their tasks with parfeval on a function I wrote.
It was working at one point, but now it takes 3 tries to successfully run.
After the pool is first opened, none of the tasks run at all. They all immediately return a MATLAB:class:MethodRestricted error saying they cannot access the function I'm sending them.
The second time I run my app, with the pool still open, only some of them fail with the same error. The rest run successfully.
The third time and then on, everything runs perfectly with no errors.
Does anyone have any idea why parallel workers would sometimes be unable to access a function?
EDIT
Here is a partial transcription of the code. I am unable to share portions of it, but I don't think that those parts contribute to the problem anyway. The abbreviated parts are marked by '%%' and the relevent variable described.
% start the pool
pool = parpool('Processes');
% pre-load Simulink on the workers
parfevalOnAll(@start_simulink, 0);
% build a list of Simulink models to load on the workers, and transfer any
% existing cache files for those models to them
%% cacheFiles = vector of cache file .slxc filepaths
addAttachedFiles(pool, cacheFiles);
% build a list of tasks & divide them up evenly among workers
%% workerTasks = a cell array of table objects, which contain rows for each simulation task
% create a dataQueue to update UI components
dq = parallel.pool.Dataqueue;
afterEach(dq, @progressFunc); % progressFunc simply updates a label in the app
% suppress warnings from workers
parfevalOnAll(@warning, 0, 'off', 'all');
numIterations = numel(workerTasks);
% create global futures object for collecting intermediate results
app.futures(1:numIterations) = parallel.FevalFuture;
% assign tasks to workers
for p = 1:numIterations
app.futures(p) = parfeval(@myApp.parallelSimulationTask, 1, ...
workerTasks{p}, dq);
end
% collect results as they come in
numCompleted = 0;
while numCompleted < numIterations
try
[completedIdx, results] = fetchNext(app.futures);
if ~isempty(completedIdx)
workerTasks{completedIdx} = results;
end
catch ex
% this is where MATLAB:class:MethodRestricted error occurs, at the
% fetchNext command
end
numCompleted = numCompleted + 1;
end
The full error text from ex is:
ex.identifier = 'MATLAB:parallel:future:FetchNextFutureErrored';
ex.message = 'The function evaluation completed with an error.';
ex.cause.identifier = 'MATLAB:class:MethodRestricted';
ex.cause.message = 'Cannot access method parallelSimulationTask in class myApp';
5 个评论
Madheswaran
2024-8-9
Hi Daniel,
This error occurs when the method you are trying to access (parallelSimulationTask) is not accessible, likely because the method is private or protected. Additionally, you mentioned that it was working at one point, but now it takes three tries to successfully run, I suspect there might be a race condition happening in the method parallelSimulationTask. Posting further information on the method would be helpful to look into the issue.
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!