现实中的 parLapply

R 并行编程

Nabeel Imam

Data Scientist

认识一下工作进程

cluster <- makeCluster(4)
clusterEvalQ(cluster, {
  id <- Sys.getpid()
  print(
    paste("Hello, my worker ID is", id)
  )
})
[[1]]
[1] "Hello, my worker ID is 425108"

[[2]]
[1] "Hello, my worker ID is 425129"

[[3]]
[1] "Hello, my worker ID is 425150"

[[4]]
[1] "Hello, my worker ID is 425171"
R 并行编程

并行筛选数据

print(file_list)
 [1] "./health/Afghanistan.csv"            
 [2] "./health/Albania.csv"            
 [3] "./health/Algeria.csv"            
 [4] "./health/American Samoa.csv"     
 [5] "./health/Andorra.csv"            
...

一个听诊器放在一叠一百美元上。

R 并行编程

并行筛选数据

filterCSV <- function (csv) {
  read.csv(csv) %>% 
    dplyr::filter(!is.na(health_exp_pc))
}


cl <- makeCluster(4) ls_df <- parLapply(cl, file_list, filterCSV) stopCluster(cl)
Error in checkForRemoteErrors(val) :
  first error: could not find function "%>%"
R 并行编程

clusterEvalQ 解救现场

在集群上加载包

cl <- makeCluster(4)
clusterEvalQ(cl, library(dplyr))


ls_df <- parLapply(cl, file_list, filterCSV) stopCluster(cl)

在集群上加载多个包

clusterEvalQ(cl, {
  library(dplyr)
  library(stringr)
})
[[1]]
       Country health_exp_pc Year
1  Afghanistan      81.27103 2002
2  Afghanistan      82.45785 2003
3  Afghanistan      89.47005 2004
...

[[2]]
   Country health_exp_pc Year
1  Albania      300.2757 2001
2  Albania      314.3254 2002
3  Albania      343.9442 2003
...
R 并行编程

条件筛选

# Function with an argument for starting year
filterCSV <- function (csv, min_year) { 

  read.csv(csv) %>% 
    dplyr::filter(!is.na(health_exp_pc),

                  # Filter data for min_year and onwards
                  Year >= min_year) 
}


selected_year <- 2010 # Value to be supplied to min_year
R 并行编程

条件筛选

cl <- makeCluster(4)
clusterEvalQ(cl, library(dplyr))

clusterExport(cl, "selected_year",
envir = environment())
ls_df <- parLapply(cl, file_list, filterCSV,
min_year = selected_year)
stopCluster(cl)

   

  • selected_year 导出到集群
  • 从当前环境导出

 

  • selected_year 传给 min_year
R 并行编程

条件筛选

[[1]]
       Country health_exp_pc Year
1  Afghanistan      143.6695 2010
2  Afghanistan      143.0915 2011
3  Afghanistan      151.9180 2012
...
  Country health_exp_pc Year
1 Albania      451.8820 2010
2 Albania      485.5835 2011
3 Albania      529.6322 2012
...
R 并行编程

集群规范清单

  • 确定核心数
  • 创建合适的集群
    • PSOCK:跨平台兼容
    • FORK:Linux 或 Mac(更快)
  • 加载所需包
  • 导出所需变量
  • 将导出变量传给命名参数
  • 完成后停止集群
n_cores <- detectCores() - 2


cluster <- makeCluster(n_cores)
clusterEvalQ(cluster, library(crucial_package))
clusterExport(cluster, "variable_we_need")
parLapply(cluster, ls_inputs, our_function, named_argument = variable_we_need)
stopCluster(cluster)
R 并行编程

Vamos praticar!

R 并行编程

Preparing Video For Download...