Get startedGet started for free

Using parSapply()

We previously played the following game:

  • Initialize: total = 0.
  • Roll a single die and add it to total.
  • If total is even, reset total to zero.
  • If total is greater than 10. The game finishes.

The game could be simulated using the play() function:

play <- function() {
  total <- no_of_rolls <- 0
  while(total < 10) {
    total <- total + sample(1:6, 1)

    # If even. Reset to 0
    if(total %% 2 == 0) total <- 0 
    no_of_rolls <- no_of_rolls + 1
  }
  no_of_rolls
}

To simulate the game 100 times, we could use a for loop or sapply():

res <- sapply(1:100, function(i) play())

This is perfect for running in parallel!

To make functions available on a cluster, you use the clusterExport() function. This takes a cluster and a string naming the function.

clusterExport(cl, "some_function")

This exercise is part of the course

Writing Efficient R Code

View Course

Exercise instructions

The play() function has been defined in your workspace.

  • Create a cluster using makeCluster(); set the number of cores used equal to 2. Store the result as cl.
  • Export the play() function to the cluster.
  • Rewrite the above sapply() function as parSapply().
  • Stop the cluster using stopCluster().

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

library("parallel")
# Create a cluster via makeCluster (2 cores)
cl <- ___

# Export the play() function to the cluster
___

# Re-write sapply as parSapply
res <- sapply(1:100, function(i) play())

# Stop the cluster
___
Edit and Run Code