Gỡ lỗi khi chạy song song

Lập trình song song trong R

Nabeel Imam

Data Scientist

Gỡ lỗi là gì?

Lập trình viên phát hiện lỗi trong mã trên màn hình và cố gắng loại bỏ.

Lập trình song song trong R

Đọc tệp song song

print(file_list)
 [1] "./stocks/2011.csv"
 [2] "./stocks/2012.csv"
 [3] "./stocks/2013.csv"
 [4] "./stocks/2014.csv"
 [5] "./stocks/2015.csv"
 ...
Lập trình song song trong R

Hàm lọc

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)
}
Lập trình song song trong R

Hàm apply song song

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
Lập trình song song trong R

Chạy tuần tự

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
...
Lập trình song song trong R

Xác định vị trí lỗi

Thông báo lỗi

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

Lập trình viên kiểm tra một dấu chấm than màu đỏ lớn trên màn hình.

Lập trình song song trong R

Xác định vị trí lỗi

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) }
Lập trình song song trong R

Xác định vị trí lỗi

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
Lập trình song song trong R

Xác định vị trí lỗi

cl <- makeCluster(4, outfile = "log.txt") # Ghi thông điệp print vào "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
Lập trình song song trong R

Xem xét log

Một văn bản hiển thị đường dẫn các tệp CSV và tên cột tương ứng. Đường dẫn tệp cho dữ liệu 2017 được tô sáng và thiếu cột "Company".

Lập trình song song trong R

Gỡ lỗi với foreach

cl <- makeCluster(4,
                  # Cung cấp tên tệp văn bản để ghi log thông điệp print
                  outfile = "log.txt")

registerDoParallel(cl)

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

stopCluster(cl)
Lập trình song song trong R

Điểm mạnh của furrr

plan(multisession, workers = 4)
future_map(file_list, filterCSV_debug)
plan(sequential)
Lập trình song song trong R

Điểm mạnh của 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
Lập trình song song trong R

Các bước

Khi lỗi xảy ra trong chạy song song
  • Chạy tuần tự trên một phần nhỏ đầu vào
  • Xem thông báo lỗi và in thông tin phù hợp
  • Xác định lỗi bằng cách in hoặc ghi log
  • Sửa lỗi
Lập trình song song trong R

Ayo berlatih!

Lập trình song song trong R

Preparing Video For Download...