精華區beta R_Language 關於我們 聯絡資訊
文章分類提示: - 問題: 當你想要問問題時,請使用這個類別。 [問題類型]: 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來) [軟體熟悉度]: 新手(沒寫過程式,R 是我的第一次) [問題敘述]: 原本程式會有兩個global變數,去承接for loop內的數值,但由於效能的關係 目前想把for loop改為 Parallel的方式,但僅查詢到回傳一個的方式, 是有解法可以處理這種狀況呢? 原始情境: df <- NULL df1 <- NULL for(i in 1:10000){ # 經運算後得到temp與temp1 temp <- c(1,2,3,4) temp1 <- c(1,2,3,4) df <- rbind(df,temp) df1 <- rbind(df1, temp1) } [程式範例]: 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) Ref: http://tinyurl.com/hdmlpsh [環境敘述]: Win7 64bit, R 3.3.1 [關鍵字]: Parallel -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.195.56.139 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1488986361.A.F22.html
cywhale: dtcomb <- function(...) {rblindlist(list(...))} 03/09 12:31
cywhale: out <- foreach(..,.combine="dtcomb",..) %dopar% {.. 03/09 12:33
cywhale: ... return(your_temp_dataframe)}) 03/09 12:34
> -------------------------------------------------------------------------- < 作者: celestialgod (天) 看板: R_Language 標題: Re: [問題] foreach 與 Parallel 回傳多個matrix? 時間: Wed Mar 8 23:30:51 2017 ※ 引述《girl5566 (5566520)》之銘言: : 文章分類提示: : - 問題: 當你想要問問題時,請使用這個類別。 : [問題類型]: : 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來) : [軟體熟悉度]: : 新手(沒寫過程式,R 是我的第一次) : [問題敘述]: : 原本程式會有兩個global變數,去承接for loop內的數值,但由於效能的關係 : 目前想把for loop改為 Parallel的方式,但僅查詢到回傳一個的方式, : 是有解法可以處理這種狀況呢? : 原始情境: : df <- NULL : df1 <- NULL : for(i in 1:10000){ : # 經運算後得到temp與temp1 : temp <- c(1,2,3,4) : temp1 <- c(1,2,3,4) : df <- rbind(df,temp) : df1 <- rbind(df1, temp1) : } 我對foreach不熟,不過foreach如果接doParallel底層是用parLapplyLB 我這裡直接用parallel的parLapplyLB直接示範怎麼做~~~ library(parallel) cl <- makeCluster(detectCores()-1L) valueList <- parLapplyLB(cl, 1:150000, function(x){ temp <- rnorm(4) temp1 <- rnorm(3) return(list(temp, temp1)) }) df <- do.call(rbind, lapply(valueList , `[[`, 1)) df1 <- do.call(rbind, lapply(valueList , `[[`, 2)) stopCluster(cl) -- R資料整理套件系列文: magrittr #1LhSWhpH (R_Language) https://goo.gl/72l1m9 data.table #1LhW7Tvj (R_Language) https://goo.gl/PZa6Ue dplyr(上.下) #1LhpJCfB,#1Lhw8b-s (R_Language) https://goo.gl/I5xX9b tidyr #1Liqls1R (R_Language) https://goo.gl/i7yzAz pipeR #1NXESRm5 (R_Language) https://goo.gl/zRUISx -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.232.184.141 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1488987055.A.28F.html ※ 編輯: celestialgod (36.232.184.141), 03/08/2017 23:33:37
cywhale: foreach應該可以這樣解,就把.combine="list" 這樣寫 03/09 12:37
cywhale: 後面的do.call(rbind,.. 看起來就是rbinlist?~~ 03/09 12:39
> -------------------------------------------------------------------------- < 作者: allen1985 (我要低調 拯救形象) 看板: R_Language 標題: Re: [問題] foreach 與 Parallel 回傳多個matrix? 時間: Fri Mar 10 11:19:18 2017 我之前的做法是 回傳list comb <- function(...){ mapply('rbind',..., SIMPLIFY = FALSE) } tmp <- foreach(i = 1:n, .combine = comb, multicombine = TRUE, .init = list(list(),list())) %dopar% { return(list(list.1, list.2)) } ※ 引述《celestialgod (天)》之銘言: : ※ 引述《girl5566 (5566520)》之銘言: : : 文章分類提示: : : - 問題: 當你想要問問題時,請使用這個類別。 : : [問題類型]: : : 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來) : : [軟體熟悉度]: : : 新手(沒寫過程式,R 是我的第一次) : : [問題敘述]: : : 原本程式會有兩個global變數,去承接for loop內的數值,但由於效能的關係 : : 目前想把for loop改為 Parallel的方式,但僅查詢到回傳一個的方式, : : 是有解法可以處理這種狀況呢? : : 原始情境: : : df <- NULL : : df1 <- NULL : : for(i in 1:10000){ : : # 經運算後得到temp與temp1 : : temp <- c(1,2,3,4) : : temp1 <- c(1,2,3,4) : : df <- rbind(df,temp) : : df1 <- rbind(df1, temp1) : : } : 我對foreach不熟,不過foreach如果接doParallel底層是用parLapplyLB : 我這裡直接用parallel的parLapplyLB直接示範怎麼做~~~ : library(parallel) : cl <- makeCluster(detectCores()-1L) : valueList <- parLapplyLB(cl, 1:150000, function(x){ : temp <- rnorm(4) : temp1 <- rnorm(3) : return(list(temp, temp1)) : }) : df <- do.call(rbind, lapply(valueList , `[[`, 1)) : df1 <- do.call(rbind, lapply(valueList , `[[`, 2)) : stopCluster(cl) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 162.237.102.128 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1489115961.A.DA5.html