假设我有一个函数f()
和一个向量d
f <- function(x) dexp(x, 2)
d <- runif(10, 1, 5)
现在我想执行一个for循环
dnew <- numeric(length(d))
for (i in seq_along(dnew)){
dnew[i] <- f(d[i])
}
。如何并行执行此操作?
答案 0 :(得分:2)
没有for循环的示例代码更快:
dnew2 <- f(d) # 'f()' and 'd' from question
all.equal(dnew, dnew2) # 'dnew' from question
[1] TRUE
library(microbenchmark)
microbenchmark('for loop' = for (i in seq_along(dnew)){ dnew[i] <- f(d[i]) },
'vectorized' = { dnew2 = f(d) })
Unit: microseconds
expr min lq mean median uq max neval
for loop 15.639 16.4455 17.66640 17.0045 18.089 43.938 100
vectorized 1.249 1.3140 1.44039 1.3845 1.516 2.424 100
它可以与 foreach 并行化:
library(foreach)
library(doParallel); registerDoParallel(2)
dnew3 <- foreach(i=seq_along(dnew), .combine=c) %dopar% {
f(d[i])
}
all.equal(dnew, dnew3)
[1] TRUE
并行化版本较慢,因为并行开销大于收益。
microbenchmark('for loop' = for (i in seq_along(dnew)){ dnew[i] <- f(d[i]) },
'foreach' = { dnew3 <- foreach(i=seq_along(dnew), .combine=c) %dopar% {
f(d[i]) }
})
Unit: microseconds
expr min lq mean median uq max neval
for loop 17.799 22.048 31.01027 32.7615 37.0945 67.265 100
foreach 11875.845 13003.558 13576.64759 13427.1015 14041.3455 17782.638 100
如果f()
需要花费更多时间进行评估,则 foreach 版本会更快:
f <- function(x){
Sys.sleep(.3)
dexp(x, 2)
}
microbenchmark('for loop' = for (i in seq_along(dnew)){ dnew[i] <- f(d[i]) },
'foreach' = {dnew3 <- foreach(i=seq_along(dnew), .combine=c) %dopar% {
f(d[i]) }
}, times=2)
Unit: seconds
expr min lq mean median uq max neval
for loop 3.004271 3.004271 3.004554 3.004554 3.004837 3.004837 2
foreach 1.515458 1.515458 1.515602 1.515602 1.515746 1.515746 2
答案 1 :(得分:1)
简单的循环
a <- function(x) {dexp(x,2)}
d<- runif(10,1,5)
d
dnew < - numeric(length(d))
for (i in 1: length(dnew)){
dnew[i]<- a(d[i])
}
dnew
并行版本
library(doParallel)
dnew < - numeric(length(d))
no_cores <- detectCores() - 1
registerDoParallel(cores=no_cores)
cl <- makeCluster(no_cores, type="FORK")
dnew <- clusterApply(cl=cl, x=d, fun = a)
stopCluster(cl)
dnew
看看这个博客的帖子:https://www.r-bloggers.com/lets-be-faster-and-more-parallel-in-r-with-doparallel-package/
希望有帮助!