R parallel study notes package 2

This part of my study notes, visualization of poor performance in datacamp above, rarely used functions.
You can refer to the chiefs of the garden blog personal feeling they speak really detailed
https://cosx.org/2016/09/r-and-parallel-computing
https://blog.csdn.net/quety/article/ details / 79825615

Personal Summary:

R underlying design is still a single-threaded, the upper application package dependencies strong.
Parallel computing technology is to solve the stand-alone single-core memory capacity and computing power in the practical application of computing needs can not be met and the issue raised. Thus, parallel computing techniques very effectively expand the range R and the scene. The latest version of R has the parallel packet to the default installation package. R core development team is also visible on parallel computing very seriously.

An example of complete care

# 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 usage

It is a loop function

> # 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)

Guess you like

Origin www.cnblogs.com/gaowenxingxing/p/12023710.html