在R中并行运行for循环 [英] run a for loop in parallel in R

查看:836
本文介绍了在R中并行运行for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个for循环,就像这样:

I have a for loop that is something like this:

for (i=1:150000) {
   tempMatrix = {}
   tempMatrix = functionThatDoesSomething() #calling a function
   finalMatrix =  cbind(finalMatrix, tempMatrix)

}

你能告诉我如何使它平行吗?

Could you tell me how to make this parallel ?

我基于一个在线示例尝试了此操作,但是不确定语法是否正确.速度也没有太大提高.

I tried this based on an example online, but am not sure if the syntax is correct. It also didn't increase the speed much.

finalMatrix = foreach(i=1:150000, .combine=cbind) %dopar%  {
   tempMatrix = {}
   tempMatrix = functionThatDoesSomething() #calling a function

   cbind(finalMatrix, tempMatrix)

}

推荐答案

感谢您的反馈.发布此问题后,我确实查找了parallel.

Thanks for your feedback. I did look up parallel after I posted this question.

最后,经过几次尝试,我开始运行它.我添加了以下代码,以防对他人有用

Finally after a few tries, I got it running. I have added the code below in case it is useful to others

library(foreach)
library(doParallel)

#setup parallel backend to use many processors
cores=detectCores()
cl <- makeCluster(cores[1]-1) #not to overload your computer
registerDoParallel(cl)

finalMatrix <- foreach(i=1:150000, .combine=cbind) %dopar% {
   tempMatrix = functionThatDoesSomething() #calling a function
   #do other things if you want

   tempMatrix #Equivalent to finalMatrix = cbind(finalMatrix, tempMatrix)
}
#stop cluster
stopCluster(cl)

注释-我必须添加一条注释,如果用户分配了太多的进程,则用户可能会收到此错误:Error in serialize(data, node$con) : error writing to connection

Note - I must add a note that if the user allocates too many processes, then user may get this error: Error in serialize(data, node$con) : error writing to connection

注意-如果foreach语句中的.combinerbind,则将通过逐行附加每个循环的输出来创建返回的最终对象.

Note - If .combine in the foreach statement is rbind , then the final object returned would have been created by appending output of each loop row-wise.

希望这对像我这样第一次尝试在R中进行并行处理的人很有用.

Hope this is useful for folks trying out parallel processing in R for the first time like me.

参考: http://www.r-bloggers.com/Windows和Linux的并行R循环/ https://beckmw. wordpress.com/2014/01/21/a-brief-foray-into-parallel-processing-with-r/

这篇关于在R中并行运行for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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