celestialgod

R parallel pkgs test

Nov 26th, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.11 KB | None | 0 0
  1. require(doParallel)
  2. library(foreach)
  3. require(snowfall)
  4. library(microbenchmark)
  5.  
  6. sfInit(parallel = TRUE, cpus = 6L)
  7. cl <- sfGetCluster()
  8. registerDoParallel(cl)
  9.  
  10. a <- rnorm(1e3)
  11. b <- rnorm(1e4)
  12. d <- 0.5
  13. e <- rnorm(1e5) # unused variables
  14. # f is not a good method to get that result, it is just for benchmark
  15. f <- function(x) {
  16.    sum <- 0
  17.    for (i in seq(1, x)) sum <- sum + (mean(a) - mean(b))*d*i
  18.    return(sum)
  19. }
  20. sfExport("a", "b", "d")
  21. clusterExport(cl, c("a", "b", "d"))
  22.  
  23. g1 <- function(x) {
  24.   out1 <- vector("numeric", length = 100)
  25.   for (i in 1:1000) out1[[i]] <- f(i)
  26.   return(out1)
  27. }
  28.  
  29. g2 <- function(x) {
  30.   out2 <- sapply(1:1000, f)
  31.   return(out2)
  32. }
  33.  
  34. g3 <- function(x) {
  35.   out3 <- sfSapply(1:1000, f)
  36.   return(out3)
  37. }
  38.  
  39. g4 <- function(x) {
  40.   out4 <- parSapply(cl, 1:1000, f)
  41.   return(out4)
  42. }
  43.  
  44. g5 <- function(x) {
  45.   out5 <- foreach(i = 1:1000, .combine = c, .export = "f") %dopar% f(i)
  46.   return(out5)
  47. }
  48.  
  49. microbenchmark(g1(), g2(), g3(), g4(), g5(), times = 20L)
  50. # Unit: seconds
  51. # expr       min        lq      mean    median        uq       max neval
  52. # g1() 12.197837 12.289874 12.364563 12.351551 12.460248 12.549038    20
  53. # g2() 12.149057 12.213601 12.274867 12.262235 12.318187 12.548602    20
  54. # g3()  3.846017  3.983526  4.065733  4.047937  4.075122  4.530810    20
  55. # g4()  3.933617  3.973013  4.030316  4.016203  4.074077  4.224589    20
  56. # g5()  2.764633  2.800048  2.861919  2.845468  2.914351  3.008172    20
  57.  
  58. # find the main difference
  59. library(profvis)
  60. profvis(g3()) # using clusterApply
  61. profvis(g5()) # using clusterApplyLB
  62. # clusterApplyLB is a load balancing version of clusterApply. If the length p of seq is not greater
  63. # than the number of nodes n, then a job is sent to p nodes. Otherwise the first n jobs are placed
  64. # in order on the n nodes. When the first job completes, the next job is placed on the node that
  65. # has become free; this continues until all jobs are complete. Using clusterApplyLB can result in
  66. # better cluster utilization than using clusterApply, but increased communication can reduce performance.
  67. # Furthermore, the node that executes a particular job is non-deterministic.
  68.  
  69. g6 <- function(x) {
  70.   out6 <- clusterApplyLB(cl, 1:1000, f)
  71.   return(out6)
  72. }
  73. g7 <- function(x) {
  74.   out7 <- parSapplyLB(cl, 1:1000, f)
  75.   return(out7)
  76. }
  77. library(plyr)
  78. g8 <- function(x) {
  79.   out8 <- laply(1:1000, f, .parallel = TRUE)
  80.   return(out8)
  81. }
  82. microbenchmark(g5(), g6(), g7(), g8(), times = 20L)
  83. # Unit: seconds
  84. #  expr      min       lq     mean   median       uq      max neval
  85. #  g5() 2.811597 2.840651 2.899999 2.860215 2.925780 3.193207    20
  86. #  g6() 2.627146 2.640597 2.692733 2.680902 2.734413 2.816154    20
  87. #  g7() 3.949628 3.978429 4.045335 4.029595 4.068357 4.384041    20
  88. #  g8() 2.866590 2.883808 2.927684 2.909651 2.957875 3.070483    20
  89.  
  90. sfStop()
  91. rm(cl)
  92.  
  93. # without parallel
  94. f2 <- function(x) rnorm(5)
  95.  
  96. o1 <- foreach(i = 1:2, .combine = cbind) %do% f2(i)
  97. o2 <- sapply(cl, 1:2, f2)
  98.  
  99. o3 <- foreach(i = 1, .combine = cbind) %do% f2(i)
  100. o4 <- sapply(cl, 1, f2)
  101.  
  102. class(o1)  # "matrix"
  103. class(o2)  # "matrix"
  104. class(o3)  # "numeric"
  105. class(o4)  # "matrix"
Advertisement
Add Comment
Please, Sign In to add comment