MATLAB Answers

Interrupt parfor from uiprogressdlg CancelRequested

40 views (last 30 days)
Mark
Mark on 1 Feb 2021
Commented: Mark on 3 Feb 2021
I have a parfor which processes a large number of files, and I use a cancelable uiprogressdlg with a parallel.pool.DataQueue to update the progress percentage as the files are processed. As expected, I cannot read the status of progress.CancelRequested inside the parfor, but can read it inside of my DataQueue afterEach called function (updateProgress).
I am not very particular on how the cancel operation is executed, so I tried throwing an error when CancelRequested was true, but DataQueue catches this, and converts it to a warning.
I would also be fine with replicating an interrupt like Ctrl-C, as that seems to be able to break out of a parfor without issue, but there does not seem to be a way to send this type of interrupt programmatically.
The only other solution I have found is to delete the parallel pool, which is rather crude (but it does work).
Is there a different way to throw an error that would not be caught? Or some way to accomplish this cancel operation without killing my parallel pool (so the program could be started again without waiting for the pool to be restarted)?

Accepted Answer

Raymond Norris
Raymond Norris on 3 Feb 2021
I'm not entirely sure what you're hoping to have happen with the cancel, but I'll say that parfor can only be cancelled by Ctrl-C or killing the parallel pool. Perhaps a more graceful way is to use parfeval. This allows you the ability to update (i.e. DataQueue) and early termination (by cancelling the "futures").
Take a look at the "Cancel Array of Futures" example
In the second for loop, this could be updating your progress bar but also canceling your futures. It'd probably be best to post what you currently have to see if this addresses what you're asking for.
  1 Comment
Mark
Mark on 3 Feb 2021
I just want execution of my overall script to stop (ideally via an error).
I agree that Ctrl-C and killing the pool will stop the parfor, but so will an error. Outisde of killing the pool though, I don't see any way to give a programmatic Ctrl-C or trigger an error in the parfor when CancelRequested is true (and errors in my DataQueue are caught when I don't want them to be).
I guess another crude alternative would be to create a blank "cancel" file in a known location when the cancel is requested (found by my DataQueue's afterEach function), and check for the presence of that file within the parfor. As I understand it, there is no way to communicate with the parallel workers to give them inputs, other than taking inputs from files, right?
Thanks for the info on parfevel and parallel futures, I think I can make that work in my code. I have several parfor operations that would need to be replaced, but this seems like the proper solution.

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!