R 崩溃访问并行创建的对象,foreach()

问题描述

我正在迁移到一个新的 Azure VM,突然在我以前从未有过的疯狂地方出现崩溃和错误。 (新的 VM 是从 Windows Server 2016 到 2019 的切换,但这可能是一个完全的红鲱鱼。)我已经找到了一个可以使用以下代码重现问题的地方

# load packages
library(foreach)
library(randomForest)
library(iterators)
library(parallel)
library(doParallel)

numCores <- detectCores() - 1
ntrees <- 8000
treeSubs <- ntrees/numCores
# initialize
cl <- makeCluster(numCores)
registerDoParallel(cl)
# dummy datasets
x <- as.data.frame(matrix(runif(100000),20000))
y <- gl(2,10000)

parRf <- foreach(ntree = rep(treeSubs,numCores),.combine = randomForest::combine,.packages = 'randomForest',.multicombine = TRUE) %dopar%
                                randomForest(x=x,y=y,importance=TRUE,mtry=2,ntree = ntree,replace = TRUE
  )

z <- matrix(runif(1000),200)

pred <- predict(parRf,z,type = "prob")

请注意,导致失败的是预测步骤,但是当我不并行进行 randomForest 调用时,预测步骤工作正常。或者,如果我使数据集更小,它也可以工作。在 RStudio 中,我得到了灰色的“炸弹”,而在 RGui 中它就消失了。

以下是 Windows 事件日志中崩溃报告的一些详细信息:

Faulting application name: rsession.exe,version: 1.1.463.0,time stamp: 0x5bd11fb5
Faulting module name: randomForest.dll,version: 0.0.0.0,time stamp: 0x609f54bd
Exception code: 0xc0000005
Fault offset: 0x0000000000001b42
Faulting process id: 0x1e48
Faulting application start time: 0x01d752f21b6d7a79
Faulting application path: C:\Program Files\RStudio\bin\x64\rsession.exe

我想知道这是否可能与这个问题有关: R Crashes when training using caret and method = gamLoess 但我没有看到任何解决方案...

这是会话信息:

> sessionInfo()
R version 4.0.5 (2021-03-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server >= 2012 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] parallel  stats     graphics  Grdevices utils     datasets  methods   base     

other attached packages:
[1] doParallel_1.0.16   iterators_1.0.13    randomForest_4.6-14 foreach_1.5.1      

loaded via a namespace (and not attached):
[1] compiler_4.0.5   tools_4.0.5      codetools_0.2-18
> 

预先感谢您提供任何提示

解决方法

代码并行工作。 尝试在项目空间中运行代码......(创建一个新项目并在其中运行)并检查。 (在项目空间外运行时,我收到了其他以内存为中心的代码的错误。)

头(预) 1 2 1 0.553750 0.446250 2 0.533750 0.466250 3 0.367750 0.632250 4 0.578625 0.421375 5 0.487125 0.512875 6 0.423375 0.576625

,

这似乎是 randomForest 包中的一个错误,基于我在此处报告的侦查:

https://github.com/RevolutionAnalytics/foreach/issues/30

简而言之,不是整数的 ntree 设置会导致段错误。我不认为这曾经是一个问题(多年来一直使用此代码),所以我不知道发生了什么变化以将其带到前台。

解决方案:确保 ntree 是一个整数。