2016-04-14 22 views
0

我想使用snow::clusterApply並行應用功能。我的函數在函數的一部分中使用臨時(預定義)種子,但通常應保持獨立的隨機數。每個「工作」都有不同的臨時種子。R snow plecuyer:使用臨時種子功能

我可以做到以下幾點:

# setting up cluster of type="SOCK" 
library(snow) 
cl <- makeSOCKcluster(2) 

# this is my function 
myfu <- function(seed){ 
    # temporary seed: 
    x <- R.utils::withSeed(rnorm(10),seed) 
    # more calculation with independent RNG: 
    y <- x*rnorm(10) 
    return(list(stays=x,changes=y)) 
} 

# run example: 
seed <- 1:4 
data.frame(clusterApply(cl,seed,myfu)) 
# reproduce 
data.frame(clusterApply(cl,seed,myfu)) 

stopCluster(cl) 

然而,部分「雪集羣均勻隨機數生成」雪包文檔(http://homepage.stat.uiowa.edu/~luke/R/cluster/cluster.html)在給定的鏈接之後,我讀了「默認隨機數字生成器可能是非常相關的「,並且應該使用額外的軟件包,例如包rlecuyer。現在

,如果我嘗試包括在我的代碼,set.seedwithSeed沒有使用任何更多:

# setting up cluster of type="SOCK" 
library(snow) 
library(rlecuyer) 
cl <- makeSOCKcluster(2) 

# setup RNG Stream 
clusterSetupRNGstream(cl,seed=1:6) 

# run example: 
seed <- 1:4 
data.frame(clusterApply(cl,seed,myfu)) 
# can't reproduce 
data.frame(clusterApply(cl,seed,myfu)) 

stopCluster(cl) 

我怎樣才能解決並行set.seedwithSeed當我需要給他們打電話上工作的基礎上,而不是之前的clusterApply電話?

回答

0

愚蠢的我。當致電set.seed/withSeed時,使用kind="L'Ecuyer-CMRG"解決了(當然)我的問題。