knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
Suppose that we want to simulate a function of two variables, $x$ and $y$, for every combination of $x = 1, 2$ and $y = 1, 3, 9, 27$, in a factorial design.
The function ultimately returns $x\times y$ and a random number
between 0 and 1, but to get there, it relies on an external constant
a equalling to 8 that is defined in its environment and on the
package rlang.
Unfortunately, our initial implementation also has some bugs that cause it to crash if the sum of the product and the hundredths digit of the random number is divisible by 4, with an error message that depends on the remainder of dividing it by 8.
Here it is:
a <- 8 f <- function(x, y) { p <- x*y u <- runif(1) errcond <- p + floor(u * 100) %% 10 + a if(errcond %% 4 == 0) stop("condition ", errcond %% 8, call. = FALSE) # rlang::dbl dbl(p = p, u = u) }
We begin by setting the directory for run results. We will use R's temporary directory:
outdir <- file.path(tempdir(), "piecemeal_demo")
Initialise the Piecemeal object.
sim <- piecemeal::init(outdir) # short for `piecemeal::Piecemeal$new(outdir)`
The following will clear all simulation results.
sim$reset()
The cluster has 2 nodes. (We could also pass a preexisting cluster object.)
sim$cluster(2)
Set a factorial design with $x = 1, 2$ and $y = 1, 3, 9, 27$:
sim$factorial(x = 2^(0:1), y = 3^(0:3))
Use 3 replications per combination of $x$ and $y$:
sim$nrep(3)
Set the function to be called for each treatment combination:
sim$worker(f)
Here is our set up so far:
sim
Note that we could have chained all these together:
sim$ cluster(2)$ factorial(x = 2^(0:1), y = 3^(0:3))$ nrep(3)$ worker(f)
We can obtain a list of configurations (combinations of treatment and seed) to be run. (Here, first two are shown.)
head(sim$todo(), 2)
We can now execute this setup. It will print a summary of the results of each run.
sim$run()
We can also view a summary of completed, pending, and ongoing runs any time. If any runs have been completed, an estimated time to finish the rest at the current rate will also be estimated.
sim$status() # or summary(sim)
Looks like all configurations failed, because the worker nodes can't
find the variable a. Let's export it to the worker nodes, and try
again. This will also automatically clear the failed runs next time we
run the simulation.
sim$export_vars("a") sim$run()
We have new errors. Most of the nodes complain that we haven't
loaded the rlang package. Let's fix that:
sim$setup({library(rlang)}) sim$run()
Some runs have succeeded. Here's what the individual run output files look like:
list.files(outdir, recursive = TRUE)
Notice that they are split up into subdirectories. This improves
performance on some file systems. It can be controlled by the
$options(split=) setting.
If we run again, completed runs will be skipped:
sim$run()
At any time, we can obtain the data frame of successful runs. If your
configuration or output data structure is more complex, you may need
to use custom functions for $result_df() or use $result_list() instead.
sim$result_df()
The remaining errors are more subtle. We can see which treatments and seeds led to unsuccessful runs:
head(sim$erred(), 2)
In fact, we can easily test-run the function to reproduce the error:
errcfg <- sim$erred()[[1]] # debugonce(f) # to step through it set.seed(errcfg$seed) do.call(f, errcfg$treatment)
Let's say that based on the above, we were able to fix the bug. Our new functions is as follows:
f <- function(x, y) { p <- x*y u <- runif(1) dbl(p = p, u = u) }
Replace the worker function. What does our simulation look like now?
sim$worker(f) sim
Run again:
sim$run()
Success!
We have all 24 combinations!
sim$result_df()
Lastly, suppose that we want to run additional 2 replications of each treatment combination. We can add more replications, and the simulation will pick up where it left off.
sim$nrep(5)
Incidentally, we can estimate how much longer the simulation will take based on the past runs. (This can also be run while the simulation is running.)
sim$eta()
sim$status() will also calculate and print the estimate, but it is typically much slower.
In any case, we resume our run:
sim$run() sim$result_df()
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.