R package that automatically uses several cores?

前端 未结 6 889
伪装坚强ぢ
伪装坚强ぢ 2020-11-29 16:28

I have noticed that R only uses one core while executing one of my programs which requires lots of calculations. I would like to take advantage of my multi-core processor to

6条回答
  •  情歌与酒
    2020-11-29 17:32

    The package future makes it extremely simple to work in R using parallel and distributed processing. More info here. If you want to apply a function to elements in parallel, the future.apply package provides a quick way to use the "apply" family functions (e.g. apply(), lapply(), and vapply()) in parallel.

    Example:

    library("future.apply")
    library("stats")
    x <- 1:10
    
    # Single core
      y <- lapply(x, FUN = quantile, probs = 1:3/4)
    
    # Multicore in parallel
      plan(multiprocess)
      y <- future_lapply(x, FUN = quantile, probs = 1:3/4)
    
    

提交回复
热议问题