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?

0

1 Answer 1

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!

3
  • 1
    Thanks Markus. Can I avoid using mutlicore/mclapply() to make my code portable to Windows as well?
    – power
    Dec 8, 2011 at 2:49
  • I think no. Probably you can use SOCK and MPI in combination! Dec 8, 2011 at 9: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? May 27, 2015 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.