퓨처스

R의 병렬 프로그래밍

Nabeel Imam

Data Scientist

까다로운 상사

확성기에 대고 말하는 정장 남성.

R의 병렬 프로그래밍

즉시 실행

report <- "<important report text>"

task <- { print("Hi, here is the report.") # Email text report }
"Hi, please find attached the report."
task
"<important report text>"
R의 병렬 프로그래밍

지연 실행

library(future)


task_future <- future({ print("Hi, here is the report.") report })


R의 병렬 프로그래밍

지연 실행

print(task_future)
Expression: {
print("Hi, here is the report.")
report
}

Environment: R_GlobalEnv
Resolved: TRUE
Value: 136 bytes of class "character" ...

 

 

  • 작업의 코드(방법)

 

  • 작업의 환경(컨텍스트)
  • 완료 여부: 완료로 보임
  • 결과 설명
R의 병렬 프로그래밍

지연 실행

value(task_future)
[1] "Hi, here is the report."
[1] "<important report text>"
R의 병렬 프로그래밍

작업 팀

관리자로부터 업무를 받는 팀.

1 Designed by Freepik
R의 병렬 프로그래밍

작업 팀

task1 <- future({
  print("Hi, here is the report.")
  report
})

task2 <- future({
  run_analysis()
  print("Analysis done")
})

task3 <- future({
  book_meeting_rooms()
  print("Meeting rooms booked!")
})
value(task1) # 10:00 AM 사무 관리자
[1] "Hi, here is the report."
[1] "<important report text>"
value(task3) # 11:00 AM 사무 관리자
"Meeting rooms booked!"
value(task2) # 03:00 PM 애널리스트
"Analysis done"
R의 병렬 프로그래밍

퓨처스로 이동 평균

print(amazon_prices)
 [1] 312.58 307.01 302.24
 [4] 297.50 300.32 301.48
 [7] 297.56 297.48 291.93
[10] 294.00 286.28 292.59
...
ma <- future({moving_average(amazon_prices)})


moving_average <- function (prices) {

  N <- length(prices) - 2
  moving_avgs <- rep(NA, N)

  for (i in 1:N) {
    moving_avgs[i] <- mean(prices[i: (i + 2)])
  }

  return(moving_avgs)
}
R의 병렬 프로그래밍

퓨처스로 이동 평균

ggplot() +
  geom_line(aes(x = 1:(length(amazon_prices) - 2),
                y = value(ma))) + # 여기서는 future의 값이 필요
  labs(y = "Moving price average (USD)", x = "Days")

한 달간 아마존 주식 이동 평균의 선 그래프.

R의 병렬 프로그래밍

병렬 퓨처스

print(amazon_list)
$`2015-01`
 [1] 312.58 307.01 302.24
 [4] 297.50 300.32 301.48
 ...
$`2015-02`
 [1] 350.05 360.29 358.38
 [4] 366.00 374.87 371.00
 ...
$`2015-03`
 [1] 380.85 383.95 385.71
 [4] 385.61 385.52 378.40
 ...
...
moving_average <- function (prices) {

  N <- length(prices) - 2
  moving_avgs <- rep(NA, N)

  for (i in 1:N) {
    moving_avgs[i] <- mean(prices[i: (i + 2)])
  }

  return(moving_avgs)
}
R의 병렬 프로그래밍

병렬 퓨처스

plan(multisession, workers = 4)


ma <- lapply(amazon_list, function (x) { future(moving_average(x)) })
  • 멀티세션으로 워커 코어 4개 사용 계획

  • amazon_list의 각 원소마다 퓨처 작업 생성

    • 멀티세션에서는 퓨처가 병렬로 실행됨
R의 병렬 프로그래밍

병렬 퓨처스

value(ma)


plan(sequential)
$`2015-01`
 [1] 307.2767 302.2500 300.0200 299.7667 299.7867 298.8400 295.6567 ...
$`2015-02`
 [1] 356.2400 361.5567 366.4167 370.6233 372.3533 371.1400 372.5067 ...
$`2015-03`
 [1] 383.5033 385.0900 385.6133 383.1767 380.4567 375.4867 372.2933 ...
...
R의 병렬 프로그래밍

왜 future인가?

순차

plan(sequential)

ma <- lapply(amazon_list,
             function (x) {
               future(moving_average(x))
               })

value(ls)

병렬

plan(multisession, workers = 4)

ls <- lapply(amazon_list,
             function (x) {
               future(moving_average(x))
               })

value(ls)

plan(sequential)
R의 병렬 프로그래밍

Vamos praticar!

R의 병렬 프로그래밍

Preparing Video For Download...