在Windows XP上的R中进行多核处理 - 通过doMC和foreach

Jan*_*nvb 8 windows parallel-processing multicore r

我发布这个问题是为了询问如何在Windows XP机器上优化R中多个处理器的使用.

目前我正在创建4个脚本(每个脚本用例如for(i in 1:100)和(i in 101:200)等),我同时在4个不同的R会话中运行.这似乎使用了所有可用的cpu.

但是,我希望这样做更有效率.一种解决方案可能是使用"doMC"和"foreach"软件包,但这在Windows机器上的R中是不可能的.

例如

library("foreach")
library("strucchange")
library("doMC") # would this be possible on a windows machine?
registerDoMC(2)  # for a computer with two cores (processors)
## Nile data with one breakpoint: the annual flows drop in 1898
## because the first Ashwan dam was built
data("Nile")
plot(Nile)

## F statistics indicate one breakpoint
fs.nile <- Fstats(Nile ~ 1)
plot(fs.nile)
breakpoints(fs.nile)     # , hpc = "foreach" --> It would be great to test this.
lines(breakpoints(fs.nile))
Run Code Online (Sandbox Code Playgroud)

任何解决方案或建议?

Dir*_*tel 4

为了完整起见,这里是对 Tal 评论的要求答案,它提供了一个简单且可移植的替代方案。答案包括跑步

 > library(snow)
 > help(makeCluster)
Run Code Online (Sandbox Code Playgroud)

并运行示例:部分顶部的前三行代码:

> cl <- makeCluster(c("localhost","localhost"), type = "SOCK")
> clusterApply(cl, 1:2, get("+"), 3)
[[1]]
[1] 4

[[2]]
[1] 5

> stopCluster(cl)
> .Platform$OS.type
[1] "windows"
> 
Run Code Online (Sandbox Code Playgroud)

真的有那么难吗?

像doSNOW和之后的 foreach这样的附加包可以以可移植的方式利用它。