Skip to content

Instantly share code, notes, and snippets.

@cboettig
Created November 11, 2011 00:00
Show Gist options
  • Save cboettig/1356682 to your computer and use it in GitHub Desktop.
Save cboettig/1356682 to your computer and use it in GitHub Desktop.
cluster parallelization for R scripts
library(Rmpi)
## load the packages we'll need
RLIBS="~/R/x86_64-redhat-linux-gnu-library/2.13"
.libPaths(c(RLIBS, .libPaths()))
### Direct RMPI way:
mpi.spawn.Rslaves(nslaves=15)
slavefn <- function() { print(paste("Hello from", foldNumber)) }
mpi.bcast.cmd(foldNumber <- mpi.comm.rank())
mpi.bcast.Robj2slave(slavefn)
result <- mpi.remote.exec(slavefn())
print(result)
mpi.close.Rslaves()
library(snow)
## snow method
cluster <- makeCluster(16, type="MPI")
clusterEvalQ(cluster, library(utils)) # load a library
clusterExport(cluster, ls()) # export everything
out <- parSapply(cluster, 1:16, function(x) print(paste("snow hello from ", x)))
print(out)
stopCluster(cluster)
## SNOWFALL method
library(snowfall)
# default
sfInit( parallel=TRUE, cpus=16)
sfExportAll()
sfLibrary(utils)
out <- sfSapply(1:16, function(x) print(paste("snow hello from ", x)))
print(out)
sfStop()
# snowfall MPI
sfInit( parallel=TRUE, cpus=16, type="MPI" )
sfExportAll()
sfLibrary(utils)
out <- sfSapply(1:16, function(x) print(paste("snow hello from ", x)))
print(out)
sfStop()
# snow's close command, shuts down and quits from script
mpi.quit(save = "no")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment