R中的%dopar%无法正常工作

R中的%dopar%无法正常工作

本文介绍了R中的%dopar%无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是开始在R中使用foreach和%dopar%methodes来进行并行处理,但是我得到的结果令人困惑,与for循环不一样;这里是我用来测试这些方法的代码,结果是:

pre $ library $(plyr);库(doParallel);库(的foreach)

CS< - makeCluster(2)
registerDoParallel(CS)

sfor_start< - Sys.time()
s_for = as.numeric()
for(i in 1:1000){
s_for [i] = sqrt(i)
}
print(Sys.time() - sfor_start )

sdopar_start< - Sys.time()
sdopar = as.numeric()
的foreach(K = 1:1000)%dopar%{
sdopar (Sys.time() - sdopar_start)

结果如下:

 > s_for [1:10]。 sdopar [1:10] 
[1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427 3.000000 3.162278
[1] NA NA NA NA NA NA NA NA NA NA

阅读功能的文档,然后说,他们不工作。
$ b

foreach 更像一个 lapply for -loop。

例如, foreach(k = 1:1000)%dopar%sqrt(k)给出与 lapply(1:1000,sqrt)

然而,当使用 foreach 顺序即可。然而,在使用并行性时,向量 sdopar 会被复制到每个集群,以便修改副本,而不是初始对象。



因此,您必须按照@ChiPak提供的 .combine = c 或使用 do.call PS:始终初始化迭代填充的向量(为了不增长向量的效率),例如像这: s_for< - double(1000)


I just start to use the foreach and %dopar% methodes for parallel processing in R , but the results I'm getting are confusing and not the same as a for loop; here is the code I used to test those methodes and resultes I'm getting:

library(plyr); library(doParallel); library(foreach)

cs <- makeCluster(2)
registerDoParallel(cs)

sfor_start <- Sys.time()
s_for=as.numeric()
for (i in 1:1000) {
  s_for[i] = sqrt(i)
}
print(Sys.time() - sfor_start)

sdopar_start <- Sys.time()
sdopar=as.numeric()
foreach(k=1:1000) %dopar% {
  sdopar[k] = sqrt(k)
}
print(Sys.time() - sdopar_start)

And here the results:

> s_for[1:10]; sdopar[1:10]
 [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427 3.000000 3.162278
 [1] NA NA NA NA NA NA NA NA NA NA

Thanks in advance :)

解决方案

Please read the documentation of functions before saying that they don't work.

foreach works more like a lapply than a for-loop.

So, for example, foreach(k=1:1000) %dopar% sqrt(k) gives the same result as lapply(1:1000, sqrt).

Yet, it is true that you can modify global variable when using foreach SEQUENTIALLY. Yet, when using parallelism, the vector sdopar is copied to each "cluster" so that you modify a copy, not the initial object.

So, you'll have to do as mentioned by @ChiPak with option .combine = c or using do.call(sdopar, c) afterwards.

PS: Always initialize the vector you fill iteratively (for efficiency of not growing a vector), for example like this: s_for <- double(1000).

这篇关于R中的%dopar%无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 05:57