gpt4 book ai didi

r - parallel::mclapply() 添加或删除对全局环境的绑定(bind)。哪个?

转载 作者:行者123 更新时间:2023-12-02 14:53:34 24 4
gpt4 key购买 nike

为什么这很重要

对于 drake ,我希望用户能够在锁定的全局环境中执行 mclapply() 调用。为了再现性,环境被锁定。 Without locking, data analysis pipelines could invalidate themselves .

mclapply() 添加或删除全局绑定(bind)的证据

set.seed(0)
a <- 1

# Works as expected.
rnorm(1)
#> [1] 1.262954
tmp <- parallel::mclapply(1:2, identity, mc.cores = 2)

# No new bindings allowed.
lockEnvironment(globalenv())

# With a locked environment
a <- 2 # Existing bindings are not locked.
b <- 2 # As expected, we cannot create new bindings.
#> Error in eval(expr, envir, enclos): cannot add bindings to a locked environment
tmp <- parallel::mclapply(1:2, identity, mc.cores = 2) # Unexpected error.
#> Warning in parallel::mclapply(1:2, identity, mc.cores = 2): all scheduled
#> cores encountered errors in user code

reprex package 创建于 2019-01-16 (v0.2.1)

编辑

对于最初的激励问题,请参见 https://github.com/ropensci/drake/issues/675https://ropenscilabs.github.io/drake-manual/hpc.html#parallel-computing-within-targets .

最佳答案

我认为 parallel:::mc.set.stream() 有答案。显然,mclapply() 默认尝试从全局环境中删除 .Random.seed。由于默认的 RNG 算法是 Mersenne Twister,我们深入研究下面的 else block 。

> parallel:::mc.set.stream
function ()
{
if (RNGkind()[1L] == "L'Ecuyer-CMRG") {
assign(".Random.seed", get("LEcuyer.seed", envir = RNGenv),
envir = .GlobalEnv)
}
else {
if (exists(".Random.seed", envir = .GlobalEnv, inherits = FALSE))
rm(".Random.seed", envir = .GlobalEnv, inherits = FALSE)
}
}
<bytecode: 0x4709808>
<environment: namespace:parallel>

我们可以使用 mc.set.seed = FALSE 使下面的代码工作,但这在实践中可能不是一个好主意。

set.seed(0)
lockEnvironment(globalenv())
parallel::mclapply(1:2, identity, mc.cores = 2, mc.set.seed = FALSE)

我想知道是否有一种方法可以锁定环境,同时仍然允许我们删除 .Random.seed

关于r - parallel::mclapply() 添加或删除对全局环境的绑定(bind)。哪个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54229295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com