Reading List Faster With parallel, doParallel, and pbapply

Reading List Faster With parallel, doParallel, and pbapply

I have several tables that I would like to load as a sole data frame. Derived functions from read.table() have a lot of convenient features, but it seems like there is a lot of steps in the implementation that would slow things down. The gain in performance of reading 29 CSV files (about 2.2 GB) shows quite different picture. While the parallelization process does bring some improvement considering the ‘user time’, i.e. the CPU time charged for the process execution at the machine level, the ‘elapsed time’, i.e. the ‘real’ elapsed time since the process was started doesn’t show much difference. Let’s go through it.

Data

list_of_datasets <- list.files(pattern = "*.csv")

list_of_datasets

Function

library(pbapply)
library(parallel)
library(doParallel)
library(dplyr)


#' Reads a list of datasets
#' @param x A list of datasets (names of datasets are strings)
#' @param func A function, the read function to use to read the data
#' @param parallelize Parallelize the code
#' @param ... Further arguments passed to func

readListFaster <- function(x,  func, ..., parallelize = FALSE, rbind = FALSE){

  stopifnot(length(x) > 0)

  read_and_assign <- function(dataset, func){
    dataset_name <- as.name(dataset)
    dataset_name <- func(dataset, ...)
  }

  if (parallelize) {
    message("Reading in data in parallel")
    clusters <- parallel::detectCores() %>%
      parallel::makeCluster()

    doParallel::registerDoParallel(clusters)

    output <- invisible( # invisible is used to suppress the unneeded output
      pbapply::pblapply(x,
                        read_and_assign,
                        func = func,
                        ...,
                        cl = clusters)
    )

    parallel::stopCluster(clusters)

  } else if (!parallelize) {
    output <- invisible(
      pbapply::pblapply(x,
                        read_and_assign,
                        func = func)
    )
  }

  # Remove what's after the "." at the end of the data set names and what's before any / for url files.
  x <- stringr::str_replace_all(x,".*/|\\..*", "")

  names(output) <- x

  if (rbind) {
  rbindlist(mclapply(list_of_datasets, fread, mc.cores = detectCores()), fill=TRUE)
    # dplyr::bind_rows(output)
  } else {
    output
  }
}

Results

Without Paralelize

system.time(loaded_datasets1 <- readListFaster(list_of_datasets, 
                                 func = read.csv2, 
                                 parallelize=FALSE, 
                                 rbind = TRUE))

With Paralelize

system.time(loaded_datasets2 <- readListFaster(list_of_datasets, 
                                 func = read.csv2, 
                                 parallelize=TRUE, 
                                 rbind = TRUE))

It’s important to realise that every bit of optimisation matters. But it would not help us if the outcome data frames were different, don’t you agree? Luckily all 174 million records do match.


table(loaded_datasets1 == loaded_datasets2)