एडवांस्ड डिबगिंग

R में पैरेलल प्रोग्रामिंग

Nabeel Imam

Data Scientist

डिफॉल्ट एरर व्यवहार

एक प्रोग्रामर स्क्रीन देखकर परेशान है और डेस्क पर सभी कागज़ बिखरे हैं.

R में पैरेलल प्रोग्रामिंग

टॉय उदाहरण

var_list <- list(1:10,
                 1:10,
                 c("a", "b", "c"))

lapply(var_list, sqrt)
Error in FUN(X[[i]], ...) : non-numeric
argument to mathematical function
  • Numeric
  • Numeric
  • Character
R में पैरेलल प्रोग्रामिंग

अस्पष्टता

एक मानवीय कंप्यूटर उलझन में है क्योंकि उसकी स्क्रीन पर एरर दिख रहा है.

1 Designed by Freepik
R में पैरेलल प्रोग्रामिंग

एरर पकड़ना

sqrt_custom <- function(var) {

tryCatch( # Expression to evaluate sqrt(var),
# What to do with an error error = function (e) return(e) )
}

एक वर्कर रिंच से एरर पकड़कर ठीक कर रहा है.

1 Designed by Freepik
R में पैरेलल प्रोग्रामिंग

एरर पकड़ना

lapply(var_list, sqrt_custom)
[[1]]
 [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427 3.000000
[10] 3.162278

[[2]]
 [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427 3.000000
[10] 3.162278

[[3]]
<simpleError in sqrt(var): non-numeric argument to mathematical function>
R में पैरेलल प्रोग्रामिंग

पैरेलल में एरर पकड़ना

cl <- makeCluster(3)
parLapply(cl, var_list, sqrt_custom)
stopCluster(cl)
[[1]]
 [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427 3.000000
[10] 3.162278

[[2]]
 [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427 3.000000
[10] 3.162278

[[3]]
<simpleError in sqrt(var): non-numeric argument to mathematical function>
R में पैरेलल प्रोग्रामिंग

जन्म डेटा

print(ls_births)
$AK
 month  plurality
     1          1
     2          1
 ...
$AL
   month  plurality
      10          1
       9          1
 ...
...
R में पैरेलल प्रोग्रामिंग

सार-संक्षेप फंक्शन

summarise_births <- function(df) {


tryCatch({
df %>% group_by(month) %>% summarise(total = sum(plurality))
},
error = function (e) "Error! Check data")
}
R में पैरेलल प्रोग्रामिंग

पैरेलल apply

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

parLapply(cl, ls_births, summarise_births)
stopCluster(cl)
$AK
    month total
  1     1     7
  2     2    13
  ...

$AL
[1] "Error! Check data"

$AR
    month total
  1     1    28
  2     2    28
  ...
...
R में पैरेलल प्रोग्रामिंग

स्रोत जाँचें

head(ls_births[["AL"]], n = 10)
     month plurality
263     10         1
335      9         1
473     12         1
474      6         1
475      9         1
839      9         1
1291    11     Twins
1369     4         1
1609     5         1
1610     5  Triplets
R में पैरेलल प्रोग्रामिंग

फ्यूचर map

plan(multisession, workers = 4)
config <- furrr_options(packages = "dplyr")
future_map(ls_births, summarise_births,
           .options = config)
plan(sequential)
$AK
    month total
  1     1     7
  2     2    13
  ...

$AL
[1] "Error! Check data"

$AR
    month total
  1     1    28
  2     2    28
  ...
...
R में पैरेलल प्रोग्रामिंग

foreach केस

cl <- makeCluster(4)
registerDoParallel(cl)

foreach(df = ls_births,
        .packages = "dplyr"
       ) %dopar% {
  summarise_births(df)
}

stopCluster(cl)
$AK
    month total
  1     1     7
  2     2    13
  ...

$AL
[1] "Error! Check data"

$AR
    month total
  1     1    28
  2     2    28
  ...
...
R में पैरेलल प्रोग्रामिंग

अभ्यास करते हैं!

R में पैरेलल प्रोग्रामिंग

Preparing Video For Download...