| ## not necessarily reproducible examples. |
| |
| library(parallel) |
| |
| |
| cl <- makeCluster(getOption("cl.cores", 2)) |
| |
| clusterApply(cl, 1:2, get("+"), 3) |
| xx <- 1 |
| clusterExport(cl, "xx") |
| clusterCall(cl, function(y) xx + y, 2) |
| |
| ## Use clusterMap like an mapply example |
| clusterMap(cl, function(x,y) seq_len(x) + y, |
| c(a = 1, b = 2, c = 3), c(A = 10, B = 0, C = -10)) |
| |
| |
| parSapply(cl, 1:20, get("+"), 3) |
| |
| ## PR14898 |
| parSapply(cl, 1, identity) |
| |
| if(require(boot, quietly = TRUE)) { |
| set.seed(11) |
| ## A bootstrapping example, which can be done in many ways: |
| clusterEvalQ(cl, { |
| ## set up each worker. Could also use clusterExport() |
| library(boot) |
| cd4.rg <- function(data, mle) MASS::mvrnorm(nrow(data), mle$m, mle$v) |
| cd4.mle <- list(m = colMeans(cd4), v = var(cd4)) |
| NULL |
| }) |
| res <- clusterEvalQ(cl, boot(cd4, corr, R = 100, |
| sim = "parametric", ran.gen = cd4.rg, mle = cd4.mle)) |
| cd4.boot <- do.call(c, res) |
| print(boot.ci(cd4.boot, type = c("norm", "basic", "perc"), |
| conf = 0.9, h = atanh, hinv = tanh)) |
| } |
| stopCluster(cl) |
| |
| |