inst/examples/run.R

library(Rmpi)
library(snow)

loadPackagesOnCluster <- function(cl, packages){
  snow::clusterExport(cl, "packages", envir = environment())
  res <- snow::clusterEvalQ(cl, invisible(lapply(packages, library, character.only = TRUE, logical.return = TRUE, quietly = TRUE)))
  all(unlist(res))
}

logger <- function(...){
  cat("[", format(Sys.time()), "]", ..., "\n")
}

logger("initialize cluster")

jobname <- acommandArgs(trailingOnly = TRUE)[1]

l <- readRDS(paste0("./", jobname, "/run.rds"))

cl <- snow::makeMPIcluster(Rmpi::mpi.universe.size()-1)
loadPackagesOnCluster(cl, l$packages)

logger("start calculation")
res <- do.call(l$fun, c(list(cl = cl), l$args))

logger("save results")
out <- paste0("./", l$jobname, "/res.rds")
saveRDS(res, out)

logger("stop cluster")
stopCluster(cl)
mpi.exit()
logger("end script")
bonartm/cheopsr documentation built on May 3, 2019, 9:37 p.m.