Skip to content

Instantly share code, notes, and snippets.

@chochkov
Created October 30, 2012 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chochkov/3982033 to your computer and use it in GitHub Desktop.
Save chochkov/3982033 to your computer and use it in GitHub Desktop.
Rscript
#!/usr/bin/Rscript
library(RPostgreSQL)
library(data.table)
library(optparse)
opts.list <- list(
make_option(c('-d', '--day'), help='Global Rank for day')
# ... further options
)
opts <- OptionParser(option_list=opts.list)
source('db_connect.r')
tryCatch({
# connect to database, redirect output streams, etc
source('global_rank.r')
calculate_global_rank(opts$options$day)
}
, finally={
# close connections
}
})
# this is part of R, not an external package
library(parallel)
# optional: impose reproducibility of the uniform random draws below
set.seed(11)
# take advantage of all cores on my computer
nodes = 8
# wrap longer tasks in a function
compute <- function(arg) {
computing.times <- runif(1) * arg
Sys.sleep(computing.times) # compute heavily!
computing.times
}
# start an 8 workers parallel cluster
cluster <- makeCluster(nodes)
print(system.time({
tryCatch(
{
args.range <- 1:nodes
print(parLapply(cluster, args.range, compute))
},
finally=stopCluster(cluster)
)
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment