我注意到环境中的并行处理存在一些问题,列表中出现无法解释的NULL值。下面是一个生成NULL的简单示例。
library(parallel)
print(sessionInfo())
print(paste("Number of cores:", detectCores()))
list_a <- list()
# Assing values from 1 to 100 to the list
for (i in 1:100) {
list_a[i] <- i
}
res <- mclapply(list_a, function(x) {x*x}, mc.cores = 28)
# Print length of list_a, unlist(res) and number of nulls in res
print(paste("Length of the list_a is", length(list_a)))
print(paste("Length of the unlist(res) is", length(unlist(res))))
print(paste("Number of nulls in res is",
sum(unlist(lapply(res, is.null)))))
下面是我使用Rscript运行脚本时的打印内容
pietvil@90113001SR001 $ Rscript --no-init-file mclapplydebug3.R
R version 3.5.1 (2018-07-02)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux Server release 6.10 (Santiago)
Matrix products: default
BLAS: /usr/lib64/R/lib/libRblas.so
LAPACK: /usr/lib64/R/lib/libRlapack.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] parallel stats graphics grDevices utils datasets methods
[8] base
loaded via a namespace (and not attached):
[1] compiler_3.5.1
[1] "Number of cores: 56"
[1] "Length of the list_a is 100"
[1] "Length of the unlist(res) is 97"
[1] "Number of nulls in res is 3"
如果我使用29个内核运行mclapply,也会出现以下错误
Error in sendMaster(try(lapply(X = S, FUN = FUN, ...), silent = TRUE)) :
write error, closing pipe to the master
Calls: mclapply -> lapply -> FUN -> sendMaster
如果我使用少于28个内核,则不会获得任何NULL值。但是,我正在运行非常耗时的脚本,并且我想利用尽可能多的内核。知道该怎么办吗?
Edit1:在我们的服务器上进行重大操作系统更新后,我注意到了这个问题。我有一个复杂的mclapply和一个foreach%dopar%循环,两者都开始返回意外的空值。在研究此问题时,我注意到即使是这个简单的示例也返回了空值,这就是为什么我发布了这个值。甚至在这个示例中,foreach有时在我的环境中也会返回一些空值。
Edit2:我在另一台服务器(RHEL 7.6)中尝试了此示例,但在该环境中没有任何空值。
Edit3:如果稍后在脚本中再次运行res <- mclapply(list_a, function(x) {x*x}, mc.cores = 28)
,它并不总是产生NULL,但有时会产生NULL。