R with parallel & pls - 如何处理 Windows 中的非终止 Rscript 进程
R with parallel & pls - How to handle non-terminating Rscript processes in Windows
我想在 R 中 运行 多个偏最小二乘模型,并且正在尝试利用并行包。然而,在我的代码 运行 之后,我可以在我的任务管理器中看到 Rscript 的实例,除非我关闭 RStudio,否则它们不会终止。这些 Rscipts 是个问题,因为如果我执行过多的迭代,它们会耗尽我计算机上的所有可用内存并基本上让我的计算机停止运行。
有谁知道如何处理这些徘徊的 Rscripts(或者可以指出我的代码中的错误,我是 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#)
这是我的会话信息()
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
您只想创建一个 "cluster" 对象(= 对 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 的垃圾收集器找到这样的 "stray" 集群时,它的底层连接最终将被关闭 - 这是 why/when 你会收到那些警告。如果将 pls.options(parallel = makeCluster(...))
放入循环中,每次迭代都会创建一个杂散簇,并且会收到更多警告。垃圾收集器运行 "random" 次,这就是为什么这些警告的回溯出现 random/non-reproducible.
我想在 R 中 运行 多个偏最小二乘模型,并且正在尝试利用并行包。然而,在我的代码 运行 之后,我可以在我的任务管理器中看到 Rscript 的实例,除非我关闭 RStudio,否则它们不会终止。这些 Rscipts 是个问题,因为如果我执行过多的迭代,它们会耗尽我计算机上的所有可用内存并基本上让我的计算机停止运行。
有谁知道如何处理这些徘徊的 Rscripts(或者可以指出我的代码中的错误,我是 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#)
这是我的会话信息()
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
您只想创建一个 "cluster" 对象(= 对 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 的垃圾收集器找到这样的 "stray" 集群时,它的底层连接最终将被关闭 - 这是 why/when 你会收到那些警告。如果将 pls.options(parallel = makeCluster(...))
放入循环中,每次迭代都会创建一个杂散簇,并且会收到更多警告。垃圾收集器运行 "random" 次,这就是为什么这些警告的回溯出现 random/non-reproducible.