Parallel loops 비교

Published by onesixx on

parallel R – 병렬처리

속도와 메모리 제약

같은 혹은 다른 기계의 여러 R 프로세스가 하나의 일을 수행

loops

https://medium.com/@ambodi/performance-benchmarks-of-serial-and-parallel-loops-in-r-5a59e29051f9

3.1 GHz Quad-Core Intel Core i7, processor 1, Cores 4, detectCore 8, 16GB Macbook pro

library("tictoc")

library("doParallel")  
no_cores <- detectCores() -2

library("furrr")
options(future.fork.enable=TRUE)
no_workers <- availableCores() -2

getPrimeNumbers <- function(n) {  
  n <- as.integer(n)
  if(n > 1e6) stop("n too large")
  primes <- rep(TRUE, n)
  primes[1] <- FALSE
  last.prime <- 2L
  for(i in last.prime:floor(sqrt(n))){
    primes[seq.int(2L*last.prime, n, last.prime)] <- FALSE
    last.prime <- last.prime + min(which(primes[(last.prime+1):n]))
  }
  which(primes)
}

resetResult <- function() {
  object.size(result) %>% print() # 26801480 bytes
  rm(list=c("result", "cl","i"), envir = .GlobalEnv)
}

index <- 10:10000

######### lapply #########
tic()
result <- lapply(index, function(x) getPrimeNumbers(x))
toc()
resetResult()
# 32.401 sec elapsed

######### map #########                  
tic()
result <- index %>% map( ~ getPrimeNumbers(.x))
toc()
resetResult()
# 32.609 sec elapsed                 
                 
######### for loop #########
tic()
result <- c()
for (i in index) {result[[i]] <- getPrimeNumbers(i) }
toc()
resetResult()
# 31.875 sec elapsed

######### furrr #########
plan("multicore", workers=no_workers)
tic()
result <- index %>% future_map( ~ getPrimeNumbers(.x))
toc()
plan("sequential")
resetResult()
# 16.515 sec elapsed

######### parLapply #########
registerDoParallel(cores=no_cores)
cl <- makeCluster(no_cores, type="FORK")
tic()
result <- parLapply(cl, index, getPrimeNumbers)
toc()
stopCluster(cl)
resetResult()
# 16.092 sec elapsed

######### foreach #########
cl <- makeCluster(no_cores, type="FORK")  
registerDoParallel(cl)
tic()
result <- foreach(i=index) %dopar% getPrimeNumbers(i)
toc()
stopCluster(cl)
resetResult()
# 11.437 sec elapsed

######### mcapply #########
tic()
result <- index %>% mclapply(getPrimeNumbers, mc.cores=no_cores)
toc()
resetResult()
# 9.519 sec elapsed
Categories: R Basic

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x