1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
## ----loadLibs, echo=FALSE, results="hide"-------------------------------------
library(foreach)
registerDoSEQ()
## ----init1,echo=FALSE,results="hide"------------------------------------------
sim <- function(a, b) 10 * a + b
avec <- 1:2
bvec <- 1:4
## ----for1---------------------------------------------------------------------
x <- matrix(0, length(avec), length(bvec))
for (j in 1:length(bvec)) {
for (i in 1:length(avec)) {
x[i,j] <- sim(avec[i], bvec[j])
}
}
x
## ----foreach1-----------------------------------------------------------------
x <-
foreach(b=bvec, .combine='cbind') %:%
foreach(a=avec, .combine='c') %do% {
sim(a, b)
}
x
## ----foreach2-----------------------------------------------------------------
x <-
foreach(b=bvec, .combine='cbind') %:%
foreach(a=avec, .combine='c') %dopar% {
sim(a, b)
}
x
## ----foreach3-----------------------------------------------------------------
opts <- list(chunkSize=2)
x <-
foreach(b=bvec, .combine='cbind', .options.nws=opts) %:%
foreach(a=avec, .combine='c') %dopar% {
sim(a, b)
}
x
## ----init2, echo=FALSE, results="hide"----------------------------------------
sim <- function(a, b) {
x <- 10 * a + b
err <- abs(a - b)
list(x=x, err=err)
}
## ----for2---------------------------------------------------------------------
n <- length(bvec)
d <- data.frame(x=numeric(n), a=numeric(n), b=numeric(n), err=numeric(n))
for (j in 1:n) {
err <- Inf
best <- NULL
for (i in 1:length(avec)) {
obj <- sim(avec[i], bvec[j])
if (obj$err < err) {
err <- obj$err
best <- data.frame(x=obj$x, a=avec[i], b=bvec[j], err=obj$err)
}
}
d[j,] <- best
}
d
## ----innercombine-------------------------------------------------------------
comb <- function(d1, d2) if (d1$err < d2$err) d1 else d2
## ----foreach4-----------------------------------------------------------------
opts <- list(chunkSize=2)
d <-
foreach(b=bvec, .combine='rbind', .options.nws=opts) %:%
foreach(a=avec, .combine='comb', .inorder=FALSE) %dopar% {
obj <- sim(a, b)
data.frame(x=obj$x, a=a, b=b, err=obj$err)
}
d
## ----foreach5-----------------------------------------------------------------
library(iterators)
opts <- list(chunkSize=2)
d <-
foreach(b=bvec, j=icount(), .combine='rbind', .options.nws=opts) %:%
foreach(a=avec, i=icount(), .combine='comb', .inorder=FALSE) %dopar% {
obj <- sim(a, b)
data.frame(x=obj$x, i=i, j=j, err=obj$err)
}
d
|