R with parallel & pls - 如何处理 Windows 中的非终止 Rscript 进程

标签 r parallel-processing pls

我想在 R 中运行多个偏最小二乘模型,并试图利用并行包。但是,在运行我的代码后,我可以在我的任务管理器中看到 Rscript 实例,除非我关闭 RStudio,否则它们不会终止。这些 Rscipts 是个问题,因为如果我执行过多的迭代,它们会耗尽我计算机上的所有可用内存并基本上让我的计算机停止运转。

有谁知道如何处理这些徘徊的 Rscript(或者可以指出我的代码中的错误,我是 R 的新手)?

下面是我的示例代码:

library(pls)       #Package for PLS regression and MSC
library(parallel)  #Allows for multi-core computations for cross-validation calculations

data(gasoline)

#Parallel Computing setup
num_cores <- 2
Made_Cluster = makeCluster(num_cores, type = "PSOCK")

num_iterations <- 10
for (i in 1:num_iterations) {
  pls.options(parallel = makeCluster(num_cores, type = "PSOCK"))
  gas1 <- plsr(octane ~ NIR, data = gasoline, validation = "LOO")
}
stopCluster(Made_Cluster)

我已确认在循环内放置 makeCluster 和 StopCluster 命令会产生相同的 Rscripts,但不会终止。它也会发生,即使 num_cores <- 1

library(pls)       #Package for PLS regression and MSC
library(parallel)  #Allows for multi-core computations for cross-validation calculations

data(gasoline)

#Parallel Computing setup
num_cores <- 1

num_iterations <- 10
for (i in 1:num_iterations) {
Made_Cluster = makeCluster(num_cores, type = "PSOCK")
  pls.options(parallel = makeCluster(num_cores, type = "PSOCK"))
  gas1 <- plsr(octane ~ NIR, data = gasoline, validation = "LOO")
stopCluster(Made_Cluster)
}

最后,终端显示有关未使用连接的奇怪消息。这些警告表现出不同的语法,我无法始终如一地重现它们。这里有几个例子:

Warning messages:
1: In if (!is.vector(X) || is.object(X)) X <- as.list(X) :
      closing unused connection 4 (<-mycomputer:port#)
2: In is.data.frame(x) :
  closing unused connection 13 (<-mycomputer:port#)
3: In crossprod(q.a) :
  closing unused connection 17 (<-mycomputer:port#)

这是我的 session 信息()

Rstudio 
$version
[1] ‘1.1.456’

R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

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] pls_2.6-0

loaded via a namespace (and not attached):
[1] compiler_3.5.1 tools_3.5.1  

最佳答案

您只想创建一个“集群”对象(= 一次调用 makeCluster(),而不是多次)。像这样的东西:

cl <- makeCluster(num_cores, type = "PSOCK")
pls.options(parallel = cl)

[...]
for (i in 1:num_iterations) {
   gas1 <- plsr(octane ~ NIR, data = gasoline, validation = "LOO")
}

stopCluster(cl)

对您的观察的解释:如果您使用 pls.options(parallel = makeCluster(...)),您最终会在该调用中创建另一个集群,这将不会被显式停止,因为您没有它的句柄。当 R 的垃圾收集器发现这样一个“杂散”集群时,它的底层连接最终将被关闭——这就是你收到这些警告的原因/时间。如果将 pls.options(parallel = makeCluster(...)) 放入循环中,每次迭代都会创建一个杂散簇,并且会收到更多警告。垃圾收集器在“随机”时间运行,这就是为什么这些警告的回溯看起来是随机的/不可重现的。

关于R with parallel & pls - 如何处理 Windows 中的非终止 Rscript 进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51868082/

相关文章:

r - 列与 ggplot geom_col 中的数据不对齐

R:将数据从宽数据转换为长数据 - 多个条件 - 出现错误

parallel-processing - MPI_Bcast() 是否阻塞?

c - undefined reference (readline,pthread_create,pthread_detach),makefile不包含库

r - R2和RMSE在pls R的交叉验证中如何计算

R:是否有与 Stata 的 codebookout 命令等效的命令?

r - ggplot : Save multiple plots in one pdf with the same plot proportions

parallel-processing - 在 Perl 6 中需要简单的并行示例

c - if 语句中的 MPI_Bcast?