Search code examples
rmacosggplot2parallel-processinggganimate

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?

Solution

  • 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!