...or put another way:
- How to make a parallel computation using third-party functions (e.g.
plyr
) until user interrupts, - and delay the interrupt handling so I can notify the working threads to stop execution after the current iteration?
I do some bootstrap simulations, which constitute an embarrassingly parallel problem: Each simulation run is independent from other simulations, and is single-threaded.
But if we don't want to run a fixed number of simulations, but run them as long as user has patience to wait?
There are many angles from which the problem could be solved; e.g. I could solve the problem if it was possible to stop the "interrupting" part of the interrupt handling, and get the information that "user requested an interrupt" from some function.
Or maybe there is a more advanced parallel library than plyr
+doMC
, that has provisions for this case.
As an illustration I put a sketch of my current implementation, that simply discards half-finished work on current iteration, when user interrupts.
library(plyr)
library(doMC)
registerDoMC()
do.one.run.of.some.lengthy.simulation<-function(i,args)
{
...
}
i<-1:10000
tryCatch({
a_ply(i,1,do.one.run.of.some.lengthy.simulation)
}, interrupt = function(ex) {
cat("An interrupt was detected, and all currently running simulations were aborted.\n");
print(ex);
})
(The results are accumulated on hard drive as a side effect of running each function)