一个作业中的错误会污染mclapply [英] An error in one job contaminates others with mclapply
问题描述
当mclapply(X, FUN)
遇到某些X
值的错误时,该错误会传播到X
的某些(但不是全部)其他值:
When mclapply(X, FUN)
encounters errors for some of the values of X
, the errors propagate to some (but not all) of the other values of X
:
require(parallel)
test <- function(x) if(x == 3) stop() else x
mclapply(1:3, test, mc.cores = 2)
#[[1]]
#[1] "Error in FUN(c(1L, 3L)[[2L]], ...[cut]
#
#[[2]]
#[1] 2
#
#[[3]]
#[1] "Error in FUN(c(1L, 3L)[[2L]], ... [cut]
#Warning message:
#In mclapply(1:3, test, mc.cores = 2) :
# scheduled core 1 encountered error in user code, all values of the job will be affected
如何阻止这种情况发生?
How can I stop this happening?
推荐答案
诀窍是设置mc.preschedule = FALSE
mclapply(1:3, test, mc.cores = 2, mc.preschedule = FALSE)
#[[1]]
#[1] 1
#[[2]]
#[1] 2
#[[3]]
#[1] "Error in FUN(X[[nexti]], ...[cut]
#Warning message:
#In mclapply(1:3, test, mc.cores = 2, mc.preschedule = FALSE) :
# 1 function calls resulted in an error
之所以起作用,是因为默认情况下,mclapply
似乎将X划分为mc.cores
个组,并将矢量化版本的FUN
应用于每个组.结果,如果组中的任何成员产生错误,则该组中的所有值都将产生相同的错误(但其他组中的值不受影响).
This works because by default mclapply
seems to divide X into mc.cores
groups and applies a vectorized version of FUN
to each group. As a result if any member of the group yields an error, all values in that group will yield the same error (but values in other groups are unaffected).
设置mc.preschedule = FALSE
有不利影响,并且可能无法重现伪随机数序列,其中相同作业始终按顺序接收相同的数字,请参见标题随机数<下的?mcparallel
. /em>.
Setting mc.preschedule = FALSE
has adverse effects and may make it impossible to reproduce a sequence of pseudo-random numbers where the same job always receives the same number in the sequence, see ?mcparallel
under the heading Random numbers.
这篇关于一个作业中的错误会污染mclapply的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!