Get startedGet started for free

Population growth rates with mclapply()

As a Statistical Programmer, you do a lot of bootstrapping in R. One of the routine analyses you conduct is to bootstrap a distribution for the mean population growth rate for different countries across a given time period.

The current code in production uses a PSOCK cluster and parLapply() to parallelize this process for efficient execution. However, this is still running slow for production needs. Your boss has asked you to compare the performance of PSOCK vs. FORK clusters.

gr_list has been loaded in your workspace. Each element of gr_list contains population growth rates for a given country from 2001 to 2021. The bootstrap function, boot_dist(), is also available. parallel and microbenchmark packages have been loaded.

This exercise is part of the course

Parallel Programming in R

View Course

Exercise instructions

  • Use the FORK version of parLapply().
  • Supply the list of inputs.
  • Supply the function to apply to each element of input.
  • Specify four cores for the FORK cluster.

Hands-on interactive exercise

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

microbenchmark(
  "PSOCK" = {
    cl <- makeCluster(4)
    parLapply(cl, gr_list, boot_dist)
    stopCluster(cl)
  },
  # Use the FORK version of parLapply()
  "FORK" = ___(
    # Supply input
    ___,
    # Supply function to apply
    ___,
    # Specify number of cores
    ___ = ___),
  times = 1)
Edit and Run Code