8

Suppose that I want to do something in R that would normally (in one process/thread) look like this:

for(i in 1:2) {
    for(j in 1:2) {
        #Do some stuff here
    }
}

Using R's new package parallel, on a quad core machine, can I do the following?

cluster<-makeCluster(4)

innerLoop<-function() {
   #Do some stuff here
}

outerLoop<-function() { 
   result<-do.call(, parLapply(cluster, c(1:2), innerLoop))
}

final.result<-do.call(, parLapply(cluster, c(1:2), outerLoop))

Is this possible with the parallel package that comes with R-2.14.0?

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
power
  • 1,680
  • 3
  • 18
  • 30

1 Answers1

12

Yes, you can do that. For the first level of parallelization you have to use distributed memory technology (as makeCluster() from the snow package) and in the second level of parallelization you have to use shared memory technology (multicore package, mclapply()).

Here is a simple code example:

library(parallel)

cl <- makeCluster(2)

inner <- function(x){
    pid <- Sys.getpid()
    name <- Sys.info()["nodename"]
    str <- paste("This is R running on", name, "with PID", pid, "!")
    return(str)
}

outer <- function(x, cores, funceval){
    require(parallel)
    mclapply(1:cores, funceval)
}

parLapply(cl, 1:length(cl), outer, 2, inner)

In the output you should see different machine names and different PIDs!

  • 1
    Thanks Markus. Can I avoid using mutlicore/mclapply() to make my code portable to Windows as well? – power Dec 08 '11 at 02:49
  • I think no. Probably you can use SOCK and MPI in combination! – Markus Schmidberger Dec 08 '11 at 09:23
  • Some quick test code shows that your answer is correct for SOCK clusters, but I'm not clear on why that might be (especially if outer is making its own cluster object to work with). Do you know? Are 'they' just protecting us from ourselves? – russellpierce May 27 '15 at 11:53