[英]Parallelizing a double for loop in R
I've been using the parallel
package in R to do loops like: 我一直在R中使用parallel
包来执行如下循环:
cl <- makeCluster(getOption("cl.cores", 6))
result <- parSapply(cl,1:k,function(i){ ... })
Is there a natural way to parallelize a nested for loop in R using this package? 有没有自然的方法可以使用此包并行化R中嵌套的for循环? Or perhaps another package? 还是另一个包装? I know there are several ways to implement parallelism in R. 我知道有几种方法可以在R中实现并行性。
My loop looks something like this. 我的循环看起来像这样。 I simplified a bit but it gets the message across: 我简化了一点,但它传达了以下信息:
sup_mse <- matrix(0,nrow=k,ncol=length(sigma))
k <- 100000 #Number of iterations
sigma <- seq(from=0.1,to=10,by=0.2)
for(i in 1:k){
for(j in 1:length(sigma)){
sup<-supsmu(x,y)
sup_mse[i,j] <- mean((m(x)-sup$y)^2)
}
}
Thanks for making the reproducible example! 感谢您提出可复制的示例! I prefer snowfall for my parallel processing, so here's how it looks in there. 我更喜欢降雪进行并行处理,因此这里是它的外观。
install.packages('snowfall')
require(snowfall)
### wasn't sure what you were using for x or y
set.seed(1001)
x <- sample(seq(1,100),20)
y <- sample(seq(1,100),20)
k <- 100
sigma <- seq(0.1, 10, 0.2)
### makes a local cluster on 4 cores and puts the data each core will need onto each
sfInit(parallel=TRUE,cpus=4, type="SOCK",socketHosts=rep("localhost",4))
sfExport('x','y','k','sigma')
answers <- sfSapply(seq(1,k), function(M)
sapply(seq(1,length(sigma)), function(N)
mean((mean(x)-supsmu(x,y)$y)^2) ## wasn't sure what you mean by m(x) so guessed mean
)
)
sup_mse <- t(answers) ## will give you a matrix with length(sigma) columns and k rows
sfStop()
I remember reading somewhere that you only want to use sfSapply
in the outer loops and then use your regular apply functions inside of that loop. 我记得在某处读过,您只想在外部循环中使用sfSapply
,然后在该循环中使用常规的apply函数。 Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.