如何使用多核让gganimate更快 [英] How to use multiple cores to make gganimate faster

查看:29
本文介绍了如何使用多核让gganimate更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是如何利用 iMac 的多个内核来使 gganimate 运行得更快.还有另一个问题(更多链接到下面)提出了同样的问题——我的问题是关于这个问题的答案:加速 gganimate 渲染.

My question is how can I utilize multiple cores of my iMac in order to make gganimate go faster. There is another question (and more linked to below) that asks this same thing—my question is about an answer to this question: Speed Up gganimate Rendering.

在那个答案中,Roman 和 mhovd 从这个 GitHub 评论中指出了一个例子(另请参阅此 GitHub 帖子):

In that answer, Roman and mhovd point out an example from this 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)

我试过这个,但得到的时间彼此非常接近:

I have tried this, but get times that are very close to each other:

     user    system   elapsed 
1.0041475 0.9775679 0.9995509 

除此代码外,我还需要做些什么吗?根据前面提到的 StackOverflow 回答或 GitHub 页面,我无法判断此代码​​是否应该按原样工作,或者是否在幕后进行了其他修改.

Is there something else I need to do beyond this code? Based on the aforementioned StackOverflow answer or from the GitHub pages, I can't tell if this code is supposed to work as is or if there was other modifications behind the scene that were done.

如果有帮助,我使用的是配备 8 核英特尔处理器的 iMac.我也在 R 中运行它,因为 RStudio 说它不支持多核.

If it helps, I am using an iMac with an 8-Core Intel processor. I am also running this in R because RStudio said something about how it doesn't support multi-core.

另请注意,我的问题也与这三个过去的问题广泛相关:

Note also that my question also broadly relates to these three past questions:

  1. 在 R+ggplot2+gganimate 中使用多个 CPU 内核
  2. 如何让 R 利用双GPU?
  3. 如何管理使用动画 ggplot2-plot 进行并行处理?

推荐答案

这是一个 pull request,意思是 代码在 GitHub 上作为一个分支提供,但尚未合并到 gganimate master.

This is a pull request, meaning that the code is available on GitHub as a branch, but hasn't yet been merged in gganimate master.

您可以克隆它或复制修改后的包目录.

然后:

  • 确保安装了 devtools
  • 打开gganimate.Rproj
  • 运行 devtools::load_all(".")

并行版本已准备好运行:

The parallel version is ready to run :

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 文件并重命名包:

To avoid load_all you could open the DESCRIPTION file and rename the package :

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

由于小插图似乎难以构建,您只需删除 vignettes 目录即可.

As vignettes seem to have difficulties to build, you can just remove the vignettes directory.

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

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

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

gganimateparallel is now available on your system as a library.

感谢@HenrikBengtsson 在未来上完成的令人难以置信的工作!

Credits @HenrikBengtsson for the incredible job done on future!

这篇关于如何使用多核让gganimate更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆