Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
951 views
in Technique[技术] by (71.8m points)

r - How to use multiple cores to make gganimate faster

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.

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 

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.

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. Using multiple CPU cores in R+ggplot2+gganimate
  2. How can I make R take advantage of dual GPU?
  3. How to manage parallel processing with animated ggplot2-plot?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

You could clone it or copy the modified package directory on your system.

Then :

  • make sure that devtools package is installed
  • open gganimate.Rproj
  • run 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 

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

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

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

Then RStudio / Build / Install and restart or from the package's directory

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

gganimateparallel is now available on your system as a library.

Credits @HenrikBengtsson for the incredible job done on future!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...