ComeçarComece de graça

Timings parSapply()

Running the dice game is embarrassingly parallel. These types of simulations usually (but not always) produce a good speed-up. As before, we can use microbenchmark() or system.time(). For simplicity, we'll use system.time() in this exercise.

Este exercício faz parte do curso

Writing Efficient R Code

Ver curso

Instruções do exercício

The play() function has been defined in your workspace. We'll assume that we want to play one hundred thousand games.

  • Set no_of_games to 1e5.
  • Use system.time() to time play() being repeatedly called in serial.
    • Call sapply() with 1:no_of_games and the play() wrapper function.
    • Assign the result to serial.
    • Wrap this call in system.time() to time it.
  • Create a 4 core cluster object and export the play() function to it.
  • Use system.time() to time play() being repeatedly called in parallel.
    • Rework the code you used to create serial, but make it work in parallel.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

# Set the number of games to play
no_of_games <- ___

# Time serial version
system.time(serial <- sapply(1:___, function(i) play()))

# Set up cluster
cl <- makeCluster(___)
clusterExport(cl, "play")

# Time parallel version
system.time(par <- ___)

# Stop cluster
stopCluster(cl)
Editar e executar o código