如何使用多核使 gganimate 更快

How to use multiple cores to make gganimate faster

我的问题是如何利用我的 iMac 的多个内核来使 gganimate 运行得更快。还有另一个问题(更多链接在下面)问同样的事情——我的问题是关于这个问题的答案Speed Up gganimate Rendering.

在那个回答中,Roman 和 mhovd 指出了这个 GitHub comment (see also this GitHub post) 的例子:

library(gganimate)
library(future)

anim <- ggplot(mtcars, aes(mpg, disp)) +
  transition_states(gear, transition_length = 2, state_length = 1) +
  enter_fade() +
  exit_fade()

future::plan("sequential")  ## default
t0 <- system.time(animate(anim))
print(t0)

future::plan("multiprocess", workers = 4L)
t1 <- system.time(animate(anim))
print(t1)

我已经试过了,但是得到的时间彼此非常接近:

     user    system   elapsed 
1.0041475 0.9775679 0.9995509 

除了这段代码,我还需要做些什么吗?根据前面提到的 Whosebug 答案或来自 GitHub 页面,我无法判断这段代码是否应该按原样工作,或者是否在幕后进行了其他修改。

如果有帮助,我正在使用配备 8 核 Intel 处理器的 iMac。我也是 运行 R 中的这个,因为 RStudio 说了一些关于它如何不支持多核的事情。

另请注意,我的问题也广泛涉及这三个过去的问题:

  1. Using multiple CPU cores in R+ggplot2+gganimate
  2. How can I make R take advantage of dual GPU?

这是一个拉取请求,意味着 the code is available on GitHub 作为一个分支,但尚未合并到 gganimate master。

您可以克隆它或 copy the modified package directory 在您的系统上。

然后:

  • 确保 devtools 软件包已安装
  • 打开gganimate.Rproj
  • 运行 devtools::load_all(".")

平行版准备运行:

anim <- ggplot(mtcars, aes(mpg, disp)) +
  transition_states(gear, transition_length = 2, state_length = 1) +
  enter_fade() +
  exit_fade()

future::plan("sequential")  ## default
t0 <- system.time(animate(anim))
print(t0)

#   user        system      total 
#   4.384615    1.360656    1.893855 

future::plan("multiprocess", workers = 4L)
t1 <- system.time(animate(anim))
#   user        system      total 
#   1.30        0.61        3.58 

print(t0 / t1)
#   user        system      total 
#   4.384615    1.360656    1.893855 

要避免 load_all,您可以打开 DESCRIPTION 文件并重命名包:

Package: gganimateparallel
Type: Package
Title: A Grammar of Animated Graphics
...

由于 vignettes 似乎很难构建,您可以删除 vignettes 目录。

然后 RStudio / Build / Install and restart 或来自包的目录

Rcmd.exe INSTALL --no-multiarch --with-keep.source .

gganimateparallel 现在可以作为库在您的系统上使用。

感谢@HenrikBengtsson 在 future 上所做的令人难以置信的工作!

"打开 gganimate.Rproj 运行 devtools::load_all(".")"

我这样做了,它显然仍然使用 1 个核心。花费的时间没有变化,任务管理器说 CPU 为 9%,这通常是 运行ning R.

时的情况