Déboguer en parallèle

Programmation parallèle en R

Nabeel Imam

Data Scientist

Qu'est-ce que le débogage ?

Des programmeurs repèrent un bogue dans du code à l'écran et tentent de le corriger.

Programmation parallèle en R

Lire des fichiers en parallèle

print(file_list)
 [1] "./stocks/2011.csv"
 [2] "./stocks/2012.csv"
 [3] "./stocks/2013.csv"
 [4] "./stocks/2014.csv"
 [5] "./stocks/2015.csv"
 ...
Programmation parallèle en R

La fonction de filtrage

filterCSV <- function (filepath) {

  # Read CSV
  df <- read.csv(filepath)

  # Filter data
  df <- df %>%
    dplyr::filter(Company == "Tesla")

  # Write to back to same path
  write.csv(df, filepath)
}
Programmation parallèle en R

Le apply parallèle

cl <- makeCluster(4)

clusterEvalQ(cl, library(dplyr))
dummy <- parLapply(cl, file_list, filterCSV)
stopCluster(cl)
Error in checkForRemoteErrors(val) : 
  one node produced an error: ℹ In argument: `Company == "Tesla"`.
Caused by error:
! object 'Company' not found
Programmation parallèle en R

L'exécution séquentielle

short_list <- file_list[1:5]


dummy <- lapply(short_list, filterCSV)
read.csv(short_list[1])
         Date  Open  High   Low Close Adj.Close   Volume Company Year
1  2011-01-03 5.368 5.400 5.180 5.324     5.324  6415000   Tesla 2011
2  2011-01-04 5.332 5.390 5.204 5.334     5.334  5937000   Tesla 2011
3  2011-01-05 5.296 5.380 5.238 5.366     5.366  7233500   Tesla 2011
...
Programmation parallèle en R

Repérer l'erreur

Message d'erreur

Error in checkForRemoteErrors(val) : 
  one node produced an error: 
  In argument: `Company == "Tesla"`.
Caused by error:
! object 'Company' not found

Des programmeurs examinent un grand point d'exclamation rouge à l'écran.

Programmation parallèle en R

Repérer l'erreur

filterCSV <- function (filepath) {

  # Read CSV
  df <- read.csv(filepath)

  # Filter data
  df <- df %>%
    dplyr::filter(Company == "Tesla")

  # Write to back to same path
  write.csv(df, filepath)
}
filterCSV_debug <- function (filepath) {

  df <- read.csv(filepath)

print(
# Paste file path and column names paste(filepath, ":",
# Collapse column names into one string paste0(colnames(df), collapse = ","))
)
df <- df %>% dplyr::filter(Company == "Tesla") write.csv(df, filepath) }
Programmation parallèle en R

Repérer l'erreur

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

dummy <- parLapply(cl, file_list, filterCSV_debug)
stopCluster(cl)
Error in checkForRemoteErrors(val) : 
  one node produced an error: ℹ In argument: `Company == "Microsoft"`.
Caused by error:
! object 'Company' not found
Programmation parallèle en R

Repérer l'erreur

cl <- makeCluster(4, outfile = "log.txt") # Consigner les messages print dans « log.txt »

clusterEvalQ(cl, library(dplyr)) parLapply(cl, file_list, filterCSV_debug) stopCluster(cl)
Error in checkForRemoteErrors(val) : 
  one node produced an error: ℹ In argument: `Company == "Tesla"`.
Caused by error:
! object 'Company' not found
Programmation parallèle en R

Examiner les journaux

Un texte affiche des chemins de fichiers CSV et les noms de colonnes correspondants. Le chemin du fichier 2017 est mis en évidence et la colonne « Company » y manque.

Programmation parallèle en R

Déboguer avec foreach

cl <- makeCluster(4,
                  # Supply a text file name to log print messages
                  outfile = "log.txt")

registerDoParallel(cl)

foreach(f = file_list,
        .packages = "dplyr") %dopar% {
  filterCSV_debug(f)
}

stopCluster(cl)
Programmation parallèle en R

L'avantage de furrr

plan(multisession, workers = 4)
future_map(file_list, filterCSV_debug)
plan(sequential)
Programmation parallèle en R

L'avantage de furrr

[1] "./stocks/2011.csv : Date,Open,High,Low,Close,Adj.Close,Volume,Company,Year"
[1] "./stocks/2012.csv : Date,Open,High,Low,Close,Adj.Close,Volume,Company,Year"
[1] "./stocks/2013.csv : Date,Open,High,Low,Close,Adj.Close,Volume,Company,Year"
[1] "./stocks/2014.csv : Date,Open,High,Low,Close,Adj.Close,Volume,Company,Year"
[1] "./stocks/2015.csv : Date,Open,High,Low,Close,Adj.Close,Volume,Company,Year"
[1] "./stocks/2016.csv : Date,Open,High,Low,Close,Adj.Close,Volume,Company,Year"
[1] "./stocks/2017.csv : Date,Open,High,Low,Close,Adj.Close,Volume,Year"
Error in (function (.x, .f, ..., .progress = FALSE)  : 
  ℹ In index: 1.
Caused by error in `dplyr::filter()`:
ℹ In argument: `Company == "Tesla"`.
Caused by error:
! object 'Company' not found
Programmation parallèle en R

Les étapes

Pour les erreurs en parallèle
  • Exécuter en séquentiel sur un sous-ensemble de l'entrée
  • Examiner le message d'erreur et afficher des messages pertinents
  • Repérer l'erreur en affichant ou en consignant des messages
  • Corriger l'erreur
Programmation parallèle en R

Passons à la pratique !

Programmation parallèle en R

Preparing Video For Download...