【问题标题】:multiple dataframes in parallel function in RR中的多个数据帧并行功能
【发布时间】:2020-03-23 04:51:04
【问题描述】:

在 R 中,我在列表上调用 parLapply(),并使用列表中的元素过滤函数内的 2 个数据帧,例如

myfunction <- function(id) {
  r1 <- r %>% filter(ID == id)
  b1<- b %>% filter(ID == id)
  doSomething(r1,b1)
}
result <- parLapply(cluster, listOfIDs, myfunction)

我使用的 SLURM 系统内存不足,因为我认为,每次从 parLapply() 调用 myfunction() 时,我都会加载两个大型数据帧(rb)。较小的数据集不会超出内存。

因此,每次调用该函数以降低内存要求时,我只想加载一大块数据帧,rb。像这样的东西(系列测试):

library(doParallel)
library(foreach)
foreach(r1= split(r,
                      rep(1:nrow(r),
                          each = 1)))  %do% {
                            b1 <- b %>% filter(rowname == as.numeric(r1$rowname))
print(b1) # doSomething(r1, b1) 
}

但我还想在函数之外过滤b,这样就不会在每个实例中加载整个数据框。 b1r1 必须具有相同的 rowname。这可能吗??

数据

> dput(r)
structure(list(ID_DRAIN = c(115504, 115865, 115892, 115955, 115983, 
115940, 116033, 116028, 115873, 115905, 115835, 115885, 115452, 
115472, 115749, 115900, 115944, 115817, 115860, 115234, 115753, 
115505, 115899, 115939, 116015, 115191, 115214, 115339, 115799, 
115809, 115898, 115864), rowname = c("1", "7", "8", "9", "10", 
"11", "12", "14", "18", "19", "22", "23", "25", "26", "27", "29", 
"30", "37", "38", "39", "42", "44", "45", "46", "49", "50", "51", 
"57", "59", "60", "61", "63")), row.names = c(1L, 7L, 8L, 9L, 
10L, 11L, 12L, 14L, 18L, 19L, 22L, 23L, 25L, 26L, 27L, 29L, 30L, 
37L, 38L, 39L, 42L, 44L, 45L, 46L, 49L, 50L, 51L, 57L, 59L, 60L, 
61L, 63L), class = "data.frame")

> dput(b)
structure(list(LabelAtlas = structure(c(2L, 2L, 2L, 2L, 4L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L), .Label = c("Culvert", "dam", "Ford", 
"Ramp/bed_sill", "sluice", "unknown", "weir"), class = "factor"), 
    rowname = c("57", "11", "7", "19", "11", "25", "38", "37", 
    "57", "57", "25", "25", "7")), row.names = c(325L, 413L, 
414L, 1607L, 2382L, 2837L, 2870L, 2945L, 3272L, 3402L, 3433L, 
3562L, 4753L), class = "data.frame")

【问题讨论】:

    标签: r memory parallel-processing slurm


    【解决方案1】:

    原来你可以给foreach多个参数...

    bgrouped <- b %>% group_by(groupID)
    
    foreach(b1 = group_split(bgrouped), 
       r1 = split(r, rep(1:nrow(r), each = 1)), .combine=data.frame) %dopar% {
       function(b1, r1)
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-25
      • 2014-03-27
      • 1970-01-01
      • 2017-01-15
      • 1970-01-01
      • 2021-12-11
      • 2020-07-11
      • 2013-01-03
      • 1970-01-01
      相关资源
      最近更新 更多