multi-core processing in R on windows XP - via doMC and foreach

╄→гoц情女王★ 提交于 2019-12-04 10:55:05

问题


I'm posting this question to ask for advice on how to optimize the use of multiple processors from R on a Windows XP machine.

At the moment I'm creating 4 scripts (each script with e.g. for (i in 1:100) and (i in 101:200), etc) which I run in 4 different R sessions at the same time. This seems to use all the available cpu.

I however would like to do this a bit more efficient. One solution could be to use the "doMC" and the "foreach" package but this is not possible in R on a Windows machine.

e.g.

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))

Any solutions or advice?


回答1:


For completeness, here is the requested answer to Tal's comment which provides a simple and portable alternative. The answer consists of running

 > library(snow)
 > help(makeCluster)

and running the first three lines of code from the top of the Examples: section:

> 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"
> 

Was that really that hard?

Add-on packages like doSNOW and thereafter foreach can make use of this in a portable way.




回答2:


Try the doSNOW parallel backend- it is supported out of the box on Windows. Use it with a snow socket cluster.




回答3:


You could try doSMP from REvolution Computing. For more information, see this blog posting: Parallel Multicore Processing with R (on Windows)



来源:https://stackoverflow.com/questions/2696341/multi-core-processing-in-r-on-windows-xp-via-domc-and-foreach

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!