furrr 고급 작업

R의 병렬 프로그래밍

Nabeel Imam

Data Scientist

부트스트랩 예시

boot_dist <- function (variable, B) {

  est <- rep(0, B)

  for (i in 1:B) {
    b <- sample(variable, replace = T)
    est[i] <- mean(b)
  }

  return(est)
}


n_samples <- 10000
print(age_list)
$AK
  [1] 43 34 26 19 22 36 17 24 33 25 18 44 21
 [14] 32 31 27 27 21 27 30 20 37 31 21 29 32
  ...
$AL
   [1] 33 25 24 22 27 21 34 21 45 36 31 29 25
  [14] 23 20 27 28 30 33 16 29 27 24 26 20 15
  ...
$AR
  [1] 28 27 20 27 26 21 20 30 22 17 22 30 15
 [14] 27 31 30 30 19 25 25 22 26 23 18 26 20
  ...
...
R의 병렬 프로그래밍

전역 변수 문제

plan(multisession, workers = 4)


future_map(age_list, boot_dist)
Error in ...furrr_fn(...) : argument "B" is missing, with no default

 

  • boot_dist에는 인자 B 값이 필요합니다
  • multisession 계획은 워크스페이스를 공유하지 않습니다
R의 병렬 프로그래밍

furrr_options

# 구성 생성
config <- furrr_options(globals = "n_samples")


# 4개의 멀티세션 계획 plan(multisession, workers = 4)
# 입력과 함수를 future_map에 전달하고, future_map(age_list, boot_dist,
# boot_dist의 두 번째 인자 값을 지정하고, B = n_samples,
# 구성은 .options로 전달합니다 .options = config) plan(sequential)
$AK
   [1] 27.22053 27.29658 26.03422 26.85551
   [5] 27.13688 26.77947 27.23574 26.67300
   ...
$AL
   [1] 25.16612 25.25577 25.05735 25.18062
   [5] 24.89914 24.82729 25.29071 25.00725
   ...
$AR
   [1] 24.50978 24.74450 24.38631 24.48533
   [5] 24.91565 24.50000 24.48533 24.82152
   ...
...
R의 병렬 프로그래밍

births 데이터셋 필터링

print(ls_births)
$AK
  state month plurality weight_gain_pounds mother_age
     AK     6         1                 30         15
     AK     3         1                 38         15
    ...
$AL
  state month plurality weight_gain_pounds mother_age
     AL     1         1                 22         15
     AL     6         1                 53         16
    ...
...
R의 병렬 프로그래밍

패키지와 함께하는 future_map

filter_df <- function (df, min_value) {
  df %>% dplyr::filter(mother_age >= min_value)
}
cutoff <- 20


config <- furrr_options(globals = "cutoff", # 전역 변수
packages = "dplyr") # 패키지
plan(multisession, workers = 4) ls_filtered <- future_map(ls_births, filter_df, min_value = cutoff, .options = config) plan(sequential)
R의 병렬 프로그래밍

패키지와 함께하는 future_map

print(ls_filtered)
$AK
  state month plurality weight_gain_pounds mother_age
     AK     4         1                 99         20
     AK     6         1                 22         21
    ...
$AL
  state month plurality weight_gain_pounds mother_age
     AL     8         1                 19         20
     AL     4         1                 50         22
    ...
...
R의 병렬 프로그래밍

여러 인자를 반복 처리

print(ls_weights)
$AK
  [1] 99 22  2 60 20 18 43 10 37 29 30
 [12] 15 26 50 11 22 17 20 40 30 99 24
 ...
$AL
   [1] 19 50 14 32 40 40 41 20 59 23 99
  [12] 36 31 50 34 39 15 73 99 99 38 40
  ...
...
print(ls_plur)
$AK
  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 [18] 1 1 1 1 1 1 1 1 1 2 1 1 1 3 1 1 1
 ...
$AL
  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1
 [18] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  ...
...
R의 병렬 프로그래밍

여러 인자를 반복 처리

future_pmap()

plan(multisession, workers = 4)


calculate <- function (weights, babies) { weights/babies }
future_pmap(list(ls_weights, ls_plur), # 인자를 하나의 리스트로 결합
calculate) # 적용할 함수 plan(sequential)
R의 병렬 프로그래밍

여러 인자를 반복 처리

$AK
  [1] 99.0 22.0  2.0 60.0 20.0 18.0 43.0 10.0 37.0 29.0 30.0
 [12] 15.0 26.0 50.0 11.0 22.0 17.0 20.0 40.0 30.0 99.0 24.0
 ...
$AL
   [1] 19.00 50.00 14.00 32.00 40.00 40.00 41.00 20.00 59.00
  [10] 23.00 99.00 36.00 31.00 25.00 34.00 39.00 15.00 73.00
  ...                          
...
R의 병렬 프로그래밍

인자 순서에 대한 참고

future_map

  1. 하나의 입력만 반복
    • 리스트 또는 벡터 1개
  2. 적용할 함수
  3. 기타 인자

future_pmap

  1. 여러 입력을 반복
    • 리스트/벡터를 하나의 리스트로 결합
  2. 적용할 함수
  3. 기타 인자
R의 병렬 프로그래밍

연습해 봅시다!

R의 병렬 프로그래밍

Preparing Video For Download...