5

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?
bill999
  • 2,147
  • 8
  • 51
  • 103

2 Answers2

6

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!

Waldi
  • 39,242
  • 6
  • 30
  • 78
  • Thank you! This works well. One quick follow-up if you don't mind: what does the "L" of `workers = 4L` mean? And how did you come up with 4L workers? I tried 8L (as my machine has 8 cores) and it went slower than with 4L. – bill999 May 03 '21 at 15:41
  • `4L` is the notation for [integer numbers in R](https://stackoverflow.com/a/24350749/13513328). Actually I just used the number of cores of the example. You could try to use one or two cores less than what you have on your system, so that other processes aren't stuck. – Waldi May 03 '21 at 15:47
  • 1
    When will this be officially released? It should be prioritized. Really needs a speed-up. – Brad Jan 02 '23 at 08:29
0

"open gganimate.Rproj run devtools::load_all(".")"

I did this and it still clearly uses 1 core. No change in time spent and task manager says CPU at 9% as it usually is when running R.

user3456588
  • 609
  • 4
  • 9