File: bootParallel.R

package info (click to toggle)
r-cran-doparallel 1.0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 320 kB
  • ctags: 2
  • sloc: sh: 41; makefile: 3
file content (83 lines) | stat: -rw-r--r-- 2,511 bytes parent folder | download | duplicates (4)
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
suppressMessages(library(doParallel))
cl <- makePSOCKcluster(4)
registerDoParallel(cl)

cat(sprintf('doParallel %s\n', packageVersion('doParallel')))
junk <- matrix(0, 1000000, 8)
cat(sprintf('Size of extra junk data: %d bytes\n', object.size(junk)))

x <- iris[which(iris[,5] != "setosa"), c(1,5)]

trials <- 10000

ptime <- system.time({
  r <- foreach(icount(trials), .combine=cbind,
               .export='junk') %dopar% {
    ind <- sample(100, 100, replace=TRUE)
    result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit))
    coefficients(result1)
  }
})[3]
cat(sprintf('parallel foreach:                    %6.1f sec\n', ptime))

ptime2 <- system.time({
  snowopts <- list(preschedule=TRUE)
  r <- foreach(icount(trials), .combine=cbind,
               .export='junk', .options.snow=snowopts) %dopar% {
    ind <- sample(100, 100, replace=TRUE)
    result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit))
    coefficients(result1)
  }
})[3]
cat(sprintf('parallel foreach with prescheduling: %6.1f sec\n', ptime2))


ptime3 <- system.time({
  chunks <- getDoParWorkers()
  r <- foreach(n=idiv(trials, chunks=chunks), .combine=cbind,
               .export='junk') %dopar% {
    y <- lapply(seq_len(n), function(i) {
      ind <- sample(100, 100, replace=TRUE)
      result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit))
      coefficients(result1)
    })
    do.call('cbind', y)
  }
})[3]
cat(sprintf('chunked parallel foreach:            %6.1f sec\n', ptime3))

ptime4 <- system.time({
  mkworker <- function(x, junk) {
    force(x)
    force(junk)
    function(i) {
      ind <- sample(100, 100, replace=TRUE)
      result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit))
      coefficients(result1)
    }
  }
  y <- parLapply(cl, seq_len(trials), mkworker(x, junk))
  r <- do.call('cbind', y)
})[3]
cat(sprintf('parLapply:                           %6.1f sec\n', ptime4))

stime <- system.time({
  y <- lapply(seq_len(trials), function(i) {
    ind <- sample(100, 100, replace=TRUE)
    result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit))
    coefficients(result1)
  })
  r <- do.call('cbind', y)
})[3]
cat(sprintf('sequential lapply:                   %6.1f sec\n', stime))

stime2 <- system.time({
  r <- foreach(icount(trials), .combine=cbind) %do% {
    ind <- sample(100, 100, replace=TRUE)
    result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit))
    coefficients(result1)
  }
})[3]
cat(sprintf('sequential foreach:                  %6.1f sec\n', stime2))

stopCluster(cl)