R parallel包学习笔记2

时间:2022-10-07 19:17:36

这个部分我在datacamp上面学习笔记,可视化的性能很差,使用的函数也很少。

可以参考一下大佬的博客园个人感觉他们讲的真的很详细

https://cosx.org/2016/09/r-and-parallel-computing

https://blog.csdn.net/quety/article/details/79825615

个人小结:

R的底层设计仍是单线程,上层应用包依赖性很强。

并行计算技术正是为了在实际应用中解决单机内存容量和单核计算能力无法满足计算需求的问题而提出的。因此,并行计算技术将非常有力地扩充R的使用范围和场景。最新版本的R已经将parallel包设为了默认安装包。可见R核心开发组也对并行计算非常重视了。

一个计较全的例子

# foreach
library(foreach)
library(doParallel)
# Real physical cores in the computer
cores <- detectCores(logical=F)
cl <- makeCluster(cores)
registerDoParallel(cl, cores=cores)
# split data by ourselves
chunk.size <- len/cores
system.time(
res2.p <- foreach(i=1:cores, .combine='rbind') %dopar%
{ # local data for results
res <- matrix(0, nrow=chunk.size, ncol=2)
for(x in ((i-1)*chunk.size+1):(i*chunk.size)) {
res[x - (i-1)*chunk.size,] <- solve.quad.eq(a[x], b[x], c[x])
}
# return local results
res
}
)
stopImplicitCluster()
stopCluster(cl)
# Export data and functions
> clusterExport(cl, c("ar1est", "ar1_one_trajectory", "ar1_block_of_trajectories"))
>
> # Process ar1_multiple_blocks_of_trajectories in parallel
> res <- clusterApply(cl,
1:nrow(ar1est),
fun = ar1_multiple_blocks_of_trajectories)
>
> # Combine results into a matrix and show results
> trajs <- do.call(rbind, res)
> show_migration(trajs)

foreach 用法

也是一个循环函数

> # foreach()%do% construct with 2 iterators
> result <- foreach(let = letters, n = c(rep(2, 13), rep(6,13)), .combine = c) %do%
max_frequency(let, words = words, min_length = n)
>
> # Plot results
> barplot(result, las = 2)

R parallel包学习笔记2