Created
October 30, 2012 18:23
-
-
Save chochkov/3982033 to your computer and use it in GitHub Desktop.
Rscript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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