Geleceği eşlemek

R'da Paralel Programlama

Nabeel Imam

Data Scientist

Geleceklerimizi biliyoruz

report <- "<important report text>"

task_future <- future({
  print("Hi, please find attached the report.")
  report
})
R'da Paralel Programlama

purrr ile eşleme

library(purrr)


numbers <- 1:1000000 map(numbers, sqrt)
[[1]]
[1] 1

[[2]]
[1] 1.414214

[[3]]
[1] 1.732051

[[4]]
[1] 2
...
R'da Paralel Programlama

map'in türleri

map_dbl(numbers, sqrt)
 [1]  1.000000  1.414214  1.732051 
 [4]  2.000000  2.236068  2.449490
 [7]  2.645751  2.828427  3.000000  
[10]  3.162278  3.316625  3.464102
[13]  3.605551  3.741657  3.872983
  ...
  • map + "_dbl" soneki
    • Çıktı double/ondalıklı sayı için

 

  • Sonuçlar bir vektör olarak döner
R'da Paralel Programlama

map'in türleri

map_chr(numbers, sqrt)
 [1] "1.000000"  "1.414214"  "1.732051"
 [4] "2.000000"  "2.236068"  "2.449490"
 [7] "2.645751"  "2.828427"  "3.000000"
[10] "3.162278"  "3.316625"  "3.464102"
[13] "3.605551"  "3.741657"  "3.872983"
  ...
  • map + "_chr" soneki
    • Karakter/dize çıktısı için

 

  • Karakter dizelerinden oluşan vektör
R'da Paralel Programlama

map'in türleri

Fonksiyon

  • map()
  • map_chr()
  • map_dbl()
  • map_int()
  • map_lgl()

Çıktı biçimi

  • Vektörler, veri çerçeveleri vb. listesi
  • Karakter ya da dize (tek değer)
  • Double ya da ondalıklı sayılar (tek değer)
  • Tamsayılar (tek değer)
  • Mantıksal/boolean (tek değer)
R'da Paralel Programlama

Tür belirtimi

microbenchmark(
  "map" = map(numbers, sqrt),
  "map_dbl" = map_dbl(numbers, sqrt),
  "map_chr" = map_chr(numbers, sqrt),
  times = 10
)
Unit: milliseconds
     expr     min    mean     max neval
1     map  794.20 1254.46 1904.02    10
2 map_dbl  889.33 1067.41 1496.56    10
3 map_chr 1735.96 1934.97 2269.59    10
R'da Paralel Programlama

future + purrr = furrr

purrr

Sıralı

library(purrr)
map_dbl(1:1000000, sqrt)

furrr

Sıralı

library(furrr)
future_map_dbl(1:1000000, sqrt)
R'da Paralel Programlama

furrr ile paralel

n_cores <- detectCore() - 2


plan(multisession, workers = n_cores)
future_map_dbl(1:1000000, sqrt) # Future enabled map_dbl()
plan(sequential)
   [1]  1.000000  1.414214  1.732051  2.000000  2.236068  2.449490  2.645751
   [8]  2.828427  3.000000  3.162278  3.316625  3.464102  3.605551  3.741657
  [15]  3.872983  4.000000  4.123106  4.242641  4.358899  4.472136  4.582576
R'da Paralel Programlama

future_map ve ailesi

purrr fonksiyonu

  • map()
  • map_chr()
  • map_dbl()
  • map_int()
  • map_lgl()

future özellikli furrr fonksiyonu

  • future_map()
  • future_map_chr()
  • future_map_dbl()
  • future_map_int()
  • future_map_lgl()
R'da Paralel Programlama

furrr'ın avantajı

  • Bir girdi dizisi input_list ve eşlenecek bir fonksiyon calculate() verildiğinde:
future_list <- lapply(input_list, function (x) future(calculate(x)))

result_list <- value(future_list) # result_list üzerinde işlem yapıp sayısal bir vektör elde etme
library(furrr)

# Sayısal bir vektör almak için future_map_dbl() çeşidini kullanma
result_list <- future_map_dbl(input_list, calculate)
R'da Paralel Programlama

Hadi pratik yapalım!

R'da Paralel Programlama

Preparing Video For Download...