我注意到我的环境中的并行处理存在一些问题,我的列表中出现了无法解释的 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,但有时会产生。