Search code examples
windowsrparallel-processingmulticore

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


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?


Solution

  • 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.