很难调试 mclapply 的错误代码,因为作业的所有值都会受到影响。
我准备了一个简单的例子。
library(parallel)
library(dplyr)
data(iris)
## Parallel Version
parFun <- function(i){
print(i)
## Generate a random subset of the iris data set
daf <- iris[sample(1:nrow(iris),10),]
## Bug in iteration number of 39, some internal function returned NULL
if(i == 39){
daf <- NULL
}
## Dplyr produces an error, needs an if test for NULL
res <- daf %>% group_by("Species") %>% slice_min(order_by = Petal.Width, n = 2)
return(res)
}
## Do the call which returns error code
## Scheduled core 3 encountered error in user code, all values of the job will be affected
resList <- mclapply(1:50,parFun,mc.cores=12)
idx <- sapply(resList,function(x){is.null(nrow(x))})
## Depending on the number of cores a sequence of jobs is affected
which(idx == TRUE)
如何调试这样的代码进行 1000 次迭代?如何找到导致错误的单个 i ?