Seurat4.0系列教程19:多线程并行策略

在Seurat,我们选择使用future框架进行并行。如果您有兴趣了解更多有关future框架的内容,请点击此处了解全面而详细的描述。

如何在Seurat4.0使用并行

要访问 Seurat 中的并行函数版本,您需要加载future包并设置planplan将指定如何运行该函数。默认行为是以非并行方式(按顺序)进行。为了实现并行,我们通常建议"多线程"策略。默认情况下,这将调用所有可用的核,但可以设置workers参数以限制同时活动future的数量。

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)

seurat的"futurized"功能

以下函数已被编写可以利用future 框架,如果设置适当的plan,将进行并行。

  • NormalizeData()
  • ScaleData()
  • JackStraw()
  • FindMarkers()
  • FindIntegrationAnchors()
  • FindClusters() - if clustering over multiple resolutions

例如,要运行并行版本,您只需要设置future 并照常调用FindMarkers()功能。

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

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

顺序与并行的比较

这里,我们将执行一个简单的比较,比较有和没有并行运行的时间差异。请注意,虽然我们预计使用并行策略将减少上述函数的运行时间,但这种减少的幅度将取决于许多因素(例如数据集的大小、线程数、系统的规格、future框架等)。以下基准是在运行 Ubuntu 16.04.5 LTS 的计算机上执行的,配置是 Intel(R) Core(TM) i7-6800K CPU @ 3.40GHz and 96 GB of RAM

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()
image

常见问题

  1. 我的进度栏去哪里了?
    遗憾的是,在任何平行模式下运行这些函数时,您将失去进度栏。这是由于future框架和 R 中的一些技术限制造成的。如果要监控函数进度,则需要放弃并行化,选择使用plan("sequential")。

  2. 如果我不断看到以下错误,该怎么办?

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 ... 

对于某些函数,每个线程需要访问某些全局变量。如果这些大于默认限制,将看到此错误。要绕过这一点,可以设置 options(future.globals.maxSize = X),X 是字节中允许的最大值。因此,要将其设置为1GB,可运行options(future.globals.maxSize = 1000 * 1024^2)。请注意,这将增加RAM使用量,因此请注意设置合适的数字。

你可能感兴趣的:(Seurat4.0系列教程19:多线程并行策略)