In Seurat, we have chosen to use the future framework for parallelization. In this vignette, we will demonstrate how you can take advantage of the future implementation of certain Seurat functions from a user’s perspective. If you are interested in learning more about the future framework beyond what is described here, please see the package vignettes here for a comprehensive and detailed description.

How to use parallelization in Seurat

To access the parallel version of functions in Seurat, you need to load the future package and set the plan. The plan will specify how the function is executed. The default behavior is to evaluate in a non-parallelized fashion (sequentially). To achieve parallel (asynchronous) behavior, we typically recommend the “multiprocess” strategy. By default, this uses all available cores but you can set the workers parameter to limit the number of concurrently active futures.

library(future)
# check the current active plan
plan()
## sequential:
## - args: function (..., envir = parent.frame())
## - tweaked: FALSE
## - call: NULL
# change the current plan to access parallelization
plan("multiprocess", workers = 4)
plan()
## multiprocess:
## - args: function (..., envir = parent.frame(), workers = 4)
## - tweaked: TRUE
## - call: plan("multiprocess", workers = 4)

‘Futurized’ functions in Seurat

The following functions have been written to take advantage of the future framework and will be parallelized if the current plan is set appropriately. Importantly, the way you call the function shouldn’t change.

For example, to run the parallel version of FindMarkers(), you simply need to set the plan and call the function as usual.

library(Seurat)
pbmc <- readRDS("../data/pbmc3k_final.rds")

# Enable parallelization
plan("multiprocess", workers = 4)
markers <- FindMarkers(pbmc, ident.1 = "NK", verbose = FALSE)

Comparison of sequential vs. parallel

Here we’ll perform a brief comparison the runtimes for the same function calls with and without parallelization. Note that while we expect that using a parallelized strategy will decrease the runtimes of the functions listed above, the magnitude of that decrease will depend on many factors (e.g. the size of the dataset, the number of workers, specs of the system, the future strategy, etc). The following benchmarks were performed on a desktop computer running Ubuntu 16.04.5 LTS with an Intel(R) Core(TM) i7-6800K CPU @ 3.40GHz and 96 GB of RAM.

Click to see bencharking code

timing.comparisons <- data.frame(fxn = character(), time = numeric(), strategy = character())
plan("sequential")
start <- Sys.time()
pbmc <- ScaleData(pbmc, vars.to.regress = "percent.mt", verbose = FALSE)
end <- Sys.time()
timing.comparisons <- rbind(timing.comparisons, data.frame(fxn = "ScaleData", time = as.numeric(end - 
    start, units = "secs"), strategy = "sequential"))

start <- Sys.time()
markers <- FindMarkers(pbmc, ident.1 = "NK", verbose = FALSE)
end <- Sys.time()
timing.comparisons <- rbind(timing.comparisons, data.frame(fxn = "FindMarkers", time = as.numeric(end - 
    start, units = "secs"), strategy = "sequential"))

plan("multiprocess", workers = 4)
start <- Sys.time()
pbmc <- ScaleData(pbmc, vars.to.regress = "percent.mt", verbose = FALSE)
end <- Sys.time()
timing.comparisons <- rbind(timing.comparisons, data.frame(fxn = "ScaleData", time = as.numeric(end - 
    start, units = "secs"), strategy = "multiprocess"))

start <- Sys.time()
markers <- FindMarkers(pbmc, ident.1 = "NK", verbose = FALSE)
end <- Sys.time()
timing.comparisons <- rbind(timing.comparisons, data.frame(fxn = "FindMarkers", time = as.numeric(end - 
    start, units = "secs"), strategy = "multiprocess"))
library(ggplot2)
library(cowplot)
ggplot(timing.comparisons, aes(fxn, time)) + geom_bar(aes(fill = strategy), stat = "identity", position = "dodge") + 
    ylab("Time(s)") + xlab("Function") + theme_cowplot()

Frequently asked questions

  1. Where did my progress bar go?
    Unfortunately, the when running these functions in any of the parallel plan modes you will lose the progress bar. This is due to some technical limitations in the future framework and R generally. If you want to monitor function progress, you’ll need to forgo parallelization and use plan("sequential").

  2. What should I do if I keep seeing the following error?

Error in getGlobalsAndPackages(expr, envir = envir, globals = TRUE) : 
  The total size of the X globals that need to be exported for the future expression ('FUN()') is X GiB. 
  This exceeds the maximum allowed size of 500.00 MiB (option 'future.globals.maxSize'). The X largest globals are ... 

For certain functions, each worker needs access to certain global variables. If these are larger than the default limit, you will see this error. To get around this, you can set options(future.globals.maxSize = X), where X is the maximum allowed size in bytes. So to set it to 1GB, you would run options(future.globals.maxSize = 1000 * 1024^2). Note that this will increase your RAM usage so set this number mindfully.

Session Info

## R version 4.0.3 (2020-10-10)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04 LTS
## 
## Matrix products: default
## BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.8.so
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=C             
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] cowplot_1.1.1      ggplot2_3.3.3      SeuratObject_4.0.0 Seurat_4.0.0      
## [5] future_1.21.0     
## 
## loaded via a namespace (and not attached):
##   [1] Rtsne_0.15           colorspace_2.0-0     deldir_0.2-9        
##   [4] ellipsis_0.3.1       ggridges_0.5.3       rprojroot_2.0.2     
##   [7] fs_1.5.0             spatstat.data_1.7-0  farver_2.0.3        
##  [10] leiden_0.3.7         listenv_0.8.0        ggrepel_0.9.1       
##  [13] codetools_0.2-16     splines_4.0.3        cachem_1.0.3        
##  [16] knitr_1.31           polyclip_1.10-0      jsonlite_1.7.2      
##  [19] ica_1.0-2            cluster_2.1.0        png_0.1-7           
##  [22] uwot_0.1.10          shiny_1.6.0          sctransform_0.3.2   
##  [25] compiler_4.0.3       httr_1.4.2           assertthat_0.2.1    
##  [28] Matrix_1.2-18        fastmap_1.1.0        lazyeval_0.2.2      
##  [31] limma_3.46.0         later_1.1.0.1        formatR_1.7         
##  [34] htmltools_0.5.1.1    tools_4.0.3          igraph_1.2.6        
##  [37] gtable_0.3.0         glue_1.4.2           RANN_2.6.1          
##  [40] reshape2_1.4.4       dplyr_1.0.4          Rcpp_1.0.6          
##  [43] spatstat_1.64-1      scattermore_0.7      pkgdown_1.6.1       
##  [46] vctrs_0.3.6          nlme_3.1-149         lmtest_0.9-38       
##  [49] xfun_0.20            stringr_1.4.0        globals_0.14.0      
##  [52] mime_0.9             miniUI_0.1.1.1       lifecycle_0.2.0     
##  [55] irlba_2.3.3          goftest_1.2-2        MASS_7.3-53         
##  [58] zoo_1.8-8            scales_1.1.1         ragg_0.4.1          
##  [61] promises_1.1.1       spatstat.utils_2.0-0 parallel_4.0.3      
##  [64] RColorBrewer_1.1-2   yaml_2.2.1           memoise_2.0.0       
##  [67] reticulate_1.18      pbapply_1.4-3        gridExtra_2.3       
##  [70] rpart_4.1-15         stringi_1.5.3        highr_0.8           
##  [73] desc_1.2.0           rlang_0.4.10         pkgconfig_2.0.3     
##  [76] systemfonts_1.0.0    matrixStats_0.58.0   evaluate_0.14       
##  [79] lattice_0.20-41      tensor_1.5           ROCR_1.0-11         
##  [82] purrr_0.3.4          labeling_0.4.2       patchwork_1.1.1     
##  [85] htmlwidgets_1.5.3    tidyselect_1.1.0     parallelly_1.23.0   
##  [88] RcppAnnoy_0.0.18     plyr_1.8.6           magrittr_2.0.1      
##  [91] R6_2.5.0             generics_0.1.0       DBI_1.1.1           
##  [94] withr_2.4.1          mgcv_1.8-33          pillar_1.4.7        
##  [97] fitdistrplus_1.1-3   abind_1.4-5          survival_3.2-7      
## [100] tibble_3.0.6         future.apply_1.7.0   crayon_1.4.0        
## [103] KernSmooth_2.23-17   plotly_4.9.3         rmarkdown_2.6       
## [106] grid_4.0.3           data.table_1.13.6    digest_0.6.27       
## [109] xtable_1.8-4         tidyr_1.1.2          httpuv_1.5.5        
## [112] textshaping_0.2.1    munsell_0.5.0        viridisLite_0.3.0