我正在網格上運行一系列大型仿真。我正在逐行實施模擬,並且發現我的採樣功能是一個瓶頸。我試圖用foreach和doMC庫來加速這個過程,但是我發現並行方法比較慢,或者我一直無法編寫一個可以被foreach正確解釋的函數。在R中的網格中進行大型仿真的並行化
綜觀其他一些帖子,看來使用的foreach我的做法可能會誤導該職位我試圖人數大大超過可用處理器的數量。我想知道在我的情況下,人們是否會對如何最好地實現並行化提出一些建議。我的模擬通常有兩種類型。首先,我計算一個矩陣,該矩陣包含我正在處理的網格行內的每個元素的採樣間隔(行)。然後使用runif進行採樣(在真實模擬中,我的行包含〜9000個單元,並且我正在執行10000次模擬)。
#number of simulations per element
n = 5
#Generate an example sampling interval.
m.int1 <- matrix (seq (1, 20, 1), ncol=10, nrow=2)
#Define a function to sample over the interval defined in m.int1
f.rand1 <- function(a) {
return (runif (n, a[1], a[2]))
}
#run the simulation with each columns corresponding to the row element and rows
#the simultions.
sim1 <- round(apply (m.int1, 2, f.rand1))
在第二種情況下,我試圖從一組矩陣中按列索引的經驗分佈中抽樣。網格行元素的值對應於要採樣的列。
#number of simulations per element
n = 5
#generate a vector represeting a row of grid values
v.int2 <- round(runif(10,1,3))
#define matrix of data that contains the distributions to be sampled.
m.samples<-cbind(rep(5,10),rep(4,10),rep(3,10))
f.sample <- function(a) {
return (sample (m.samples [ ,a], n,))
}
#Sample m.samples indexed by column number.
sim2<- sapply(v.int2,f.sample)
在第二個例子,我能夠利用的foreach()和%dopar%並行運行,但仿真花基本上長於串行代碼。在上面的第一個例子中,我無法寫出一個正確的函數來利用foreach並行化。我將把我在第二種情況下使用的代碼放在一起來展示我的想法 - 但現在我意識到我的方法在開銷方面太昂貴了。
library(foreach)
library(doMC)
registerDoMC(2)
n = 5
#Sample m.samples indexed by column number using parallel method.
sim2.par <- foreach (i = 1 : length (v.int2),
.combine="cbind") %dopar% sample (
m.samples [ , v.int2 [i] ] , n)
我會很感激的做法提出了一些建議(和一些代碼!),這將幫助我有效地利用並行。再次,我正在處理的行通常包含約9000個元素,我們正在對每個元素進行10000次模擬。所以我的輸出仿真矩陣一般在10000 X 9000的數量級。感謝您的幫助。
在情況下,個人迭代短,開銷可能會非常昂貴,相對來說。這就是爲什麼在許多內核上運行時沒有看到任何提升。換句話說,工作速度如此之快,以至於溝通比實際工作花費更多時間。 – 2013-02-12 19:52:21