LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Parallel Programming in R

Kurs anzeigen

Anleitung zur Übung

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

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

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)
Code bearbeiten und ausführen