进行并行复制的最简单方法 [英] Simplest way to do parallel replicate

查看:112
本文介绍了进行并行复制的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢R中的parallel软件包,并且喜欢并行编写applysapply等版本是多么容易和直观.

I am fond of the parallel package in R and how easy and intuitive it is to do parallel versions of apply, sapply, etc.

replicate是否有类似的并行函数?

Is there a similar parallel function for replicate?

推荐答案

您可以仅使用lapplysapply的并行版本,而不是在您对1:n而不是提供表达式,而是将该表达式包装在忽略发送给它的参数的函数中.

You can just use the parallel versions of lapply or sapply, instead of saying to replicate this expression n times you do the apply on 1:n and instead of giving an expression, you wrap that expression in a function that ignores the argument sent to it.

可能类似于:

#create cluster
library(parallel)
cl <- makeCluster(detectCores()-1)  
# get library support needed to run the code
clusterEvalQ(cl,library(MASS))
# put objects in place that might be needed for the code
myData <- data.frame(x=1:10, y=rnorm(10))
clusterExport(cl,c("myData"))
# Set a different seed on each member of the cluster (just in case)
clusterSetRNGStream(cl)
#... then parallel replicate...
parSapply(cl, 1:10000, function(i,...) { x <- rnorm(10); mean(x)/sd(x) } )
#stop the cluster
stopCluster(cl)

等同于:

replicate(10000, {x <- rnorm(10); mean(x)/sd(x) } )

这篇关于进行并行复制的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆