With the parallel
R package, I can run things in parallel like this:
library(parallel)
cl <- makeCluster(2) # Create a cluster with 2 workers
... # do some parallel stuff
stopCluster(cl)
However the cl
variable referring to the cluster can get lost, for instance when running from a function that fails:
do.something <- function() {
library(parallel)
cl <- makeCluster(detectCores())
parLapply(cl, 1:10, function(x) {
stop("An error occured")
})
stopCluster(cl)
}
do.something()
here, stopCluster
has not been executed. When this happens, I am left with the workers running, as shown by ps
:
501 53300 9225 0 2:16PM ttys003 0:00.27 /opt/local/Library/Frameworks/R.framework/Resources/bin/exec/R
501 53390 1 0 2:19PM ttys003 0:00.16 /opt/local/Library/Frameworks/R.framework/Resources/bin/exec/R --slave --no-restore -e parallel:::.slaveRSOCK() --args MASTER=localhost PORT=11099 OUT=/dev/null TIMEOUT=2592000 XDR=TRUE
501 53399 1 0 2:19PM ttys003 0:00.16 /opt/local/Library/Frameworks/R.framework/Resources/bin/exec/R --slave --no-restore -e parallel:::.slaveRSOCK() --args MASTER=localhost PORT=11099 OUT=/dev/null TIMEOUT=2592000 XDR=TRUE
501 53408 1 0 2:19PM ttys003 0:00.16 /opt/local/Library/Frameworks/R.framework/Resources/bin/exec/R --slave --no-restore -e parallel:::.slaveRSOCK() --args MASTER=localhost PORT=11099 OUT=/dev/null TIMEOUT=2592000 XDR=TRUE
501 53417 1 0 2:19PM ttys003 0:00.16 /opt/local/Library/Frameworks/R.framework/Resources/bin/exec/R --slave --no-restore -e parallel:::.slaveRSOCK() --args MASTER=localhost PORT=11099 OUT=/dev/null TIMEOUT=2592000 XDR=TRUE
Of course I could manually kill
the slaves one by one, or restart R. However at times it may not be practical, for instance if multiple instances of R are running their own pools. Is there a way to stop them from within R when cl
has been lost? How do people normally handle this scenario?
Close the clusterIf you exit R, it should automatically close all processes also. This does not delete the cl object, just the cluster it refers to in the background. Keep in mind that closing a cluster is equivalent to quitting R in each; anything saved there is lost and packages will need to be re-loaded.
makeCluster creates a cluster of one of the supported types. The default type, "PSOCK" , calls makePSOCKcluster . Type "FORK" calls makeForkCluster . Other types are passed to package snow.
There are various packages in R which allow parallelization. “parallel” Package The parallel package in R can perform tasks in parallel by providing the ability to allocate cores to R. The working involves finding the number of cores in the system and allocating all of them or a subset to make a cluster.
There are a couple of mechanisms to have code always run, even if there is an error:
try
Wrap the error-prone section inside a try
or tryCatch
block. You can then examine the result to see if there was an error.
do.something <- function() {
library(parallel)
cl <- makeCluster(detectCores())
result <- try({
parLapply(cl, 1:10, function(x) {
stop("An error occured")
})
})
if(inherits(result, "try-error"))
print("there was an error!")
stopCluster(cl)
result
}
on.exit
The code inside an on.exit
call will always run when the function ends, whether cleanly or due to an error.
do.something <- function() {
library(parallel)
cl <- makeCluster(detectCores())
on.exit(stopCluster(cl))
parLapply(cl, 1:10, function(x) {
stop("An error occured")
})
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With