Problem working with parfor

Dear all,
I am working with the parallel tool in order to optimize the run time of my simulations. I am using a resevoir simulator (MATLAB Reservoir Simulation Toolbox). I am adding a function after each time convergence, to do a parallel calculations on each cell. However, it seems there is a problem with the parallel tool. It stuck in a cetrain function as found in the attached image.
The code stucks here does not proceed anymore. Do you have any idea why it is happening?

 Accepted Answer

Edric Ellis
Edric Ellis on 18 Apr 2024
That stack frame in the profiler is simply where your MATLAB client is waiting for the workers to complete their work. You can get profiling information from the workers using mpiprofile.

3 Comments

I found that the code is stucked in below function:
function [tags, results] = getCompleteIntervals(obj, numIntervals)
tags = nan(numIntervals, 1);
results = cell(numIntervals, 2);
for i = 1:numIntervals
err = [];
r = parallel.internal.pool.ParforIntervalResult();
while r.isEmpty()
assert(obj.NumIntervalsInController > 0, ...
'Internal error in PARFOR - no intervals to retrieve.');
r = obj.ParforController.waitForNextCompletedInterval(obj.AwaitIntervalPollPeriod);
% In each poll tick of the polling wait, yield to any
% events allowed to interrupt the parallel language.
parallel.internal.pool.yield();
if r.isEmpty()
% Only test to see if the session is failing if we didn't get a
% results from the queue
if ~obj.isSessionValidAndRunning()
errorMessageInput = iGetParpoolLinkForError();
error(message('parallel:lang:parfor:SessionShutDown', errorMessageInput));
end
else
obj.NumIntervalsInController = obj.NumIntervalsInController - 1;
if r.hasError()
% Maybe try again
[r, err] = obj.handleIntervalErrorResult(r);
end
end
end
% Check to see if the interval result has an error
if r.hasError()
throw(err);
else
tags(i) = r.getTag();
data = parallel.internal.pool.optionallyDeserialize(r.getResult());
assert(numel(data) == 2, ...
'Unexpectedly received the incorrect number of outputs.');
results(i,:) = data;
obj.IncompleteIntervalsCell{tags(i)} = [];
end
end
% Yield one final time to ensure all events directly generated by
% code in the PARFOR loop are executed before we give control back
% to the user.
parallel.internal.pool.yield();
end
It is stucked in the while loop, and it does not go further as r is empty (r.isEmpty()).
That means the workers are still working on your loop. The simplest way to check that things are still proceeding on the workers is to add a disp statement to the body of your parfor loop to see where things are getting to.
Thanks for your help. As a matter of fact you are right. There was a problem inside the third party software that I fixed it. I MUST NEVER LOSE MY FAITH IN MATLAB!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 17 Apr 2024

Commented:

on 18 Apr 2024

Community Treasure Hunt

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

Start Hunting!