R nested foreach loop

亡梦爱人 提交于 2021-02-11 15:21:46

问题


I have an input dataset:

# environment
require(pacman)

p_load(
  data.table
  , doParallel
  , foreach
)


doParallel::registerDoParallel(makeCluster(4))

# create input
runDT <- data.table(run = c(F,T,F,T)
                    , input1 = 1:4
                    , run_id = 1:4)
print(runDT)
     run input1 run_id
1: FALSE      1      1
2:  TRUE      2      2
3: FALSE      3      3
4:  TRUE      4      4

and this is another raw dataset:

dataDT <- data.table(
  ID = 1:4
  , c1 = c(1:4))
print(dataDT)
   ID c1
1:  1  1
2:  2  2
3:  3  3
4:  4  4

I would like to run nested foreach loops, but it's giving me an error:

# run
row_run <- runDT[run == T, run_id]

resultsDT <- foreach::foreach(
  k = 1:length(row_run), .inorder = FALSE, .packages = c("data.table")) %dopar% {

    # get the input for this run
    inputDT <- runDT[run_id == row_run[k],]

    # apply the input for all dataDT rows
    result_run <- foreach::foreach(
      j = 1:nrow(dataDT), .inorder = FALSE, .packages = c("data.table")) %dopar% {

        dataDT_run <- dataDT[ID == j,]
        dataDT_run[, c("o1", "run_id") := list(
          c1 + inputDT[, input1]
          , inputDT[, run_id]
        )]
        return(dataDT_run[, c("o1", "run_id"), with = FALSE])
      }
    result_run <- rbindlist(result_run)
    return(result_run)
  }
Error in { : task 1 failed - "could not find function "%dopar%""
resultsDT <- rbindlist(resultsDT)
print(resultsDT)

The result I expect to see is:

resultsDT <- data.table(
  o1 = c((1:4) + 2,c(1:4) + 4)
  , run_id = c(rep(2,4),rep(4,4))
)
print(resultsDT)
   o1 run_id
1:  3      2
2:  4      2
3:  5      2
4:  6      2
5:  5      4
6:  6      4
7:  7      4
8:  8      4

Then I changed the first %dopar% to %:%, but it's giving another error:

Error in foreach::foreach(k = 1:length(row_run), .inorder = FALSE, .packages = c("data.table")) %:%  : 
  no function to return from, jumping to top level

How to fix it?


回答1:


Fixed.. It seems I'll have to put inputDT <- runDT[run_id == row_run[k],] inside the loop:

resultsDT <- foreach::foreach(
  k = 1:length(row_run), .inorder = FALSE, .packages = c("data.table"), .combine = 'rbind') %:%
    # apply the input for all dataDT rows
    foreach::foreach(
      j = 1:nrow(dataDT), .combine = 'rbind') %dopar% {

        # get the input for this run
        inputDT <- runDT[run_id == row_run[k],]

        dataDT_run <- dataDT[ID == j,]
        dataDT_run[, c("o1", "run_id") := list(
          c1 + inputDT[, input1]
          , inputDT[, run_id]
        )]
        return(dataDT_run[, c("o1", "run_id"), with = FALSE])
}
print(resultsDT)
   o1 run_id
1:  3      2
2:  4      2
3:  5      2
4:  6      2
5:  5      4
6:  6      4
7:  7      4
8:  8      4

But if we do it this way, is runDT being copied in RAM k * j times? Because my actual runDT is quite big.




回答2:


But if we do it this way, is runDT being copied in RAM k * j times? Because my actual runDT is quite big.

I will answer about your additional question

doParallel::registerDoParallel(makeCluster(4))

As you make 4 cluster, runDT will copied into your 4 cluster.

 inputDT <- runDT[run_id == row_run[k],]

Additionally, assume k*j as 8 and all the inputDT sizes are 100MB.

size(Cluster1) : runDT + inputDT(100MB) + inputDT(100MB) + etc
size(Cluster2) : runDT + inputDT(100MB) + inputDT(100MB) + etc
size(Cluster3) : runDT + inputDT(100MB) + inputDT(100MB) + etc
size(Cluster4) : runDT + inputDT(100MB) + inputDT(100MB) + etc


来源:https://stackoverflow.com/questions/54778292/r-nested-foreach-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!