-
Notifications
You must be signed in to change notification settings - Fork 36
future
library(mrgsolve)
library(future.apply)
. Loading required package: future
library(tidyverse)
theme_set(theme_bw())
library(future.apply)
library(tidyverse)
The document illustrates how simulations with mrgsolve can be run in parallel. This approach should replace previous advice regarding the doParallel pacakge.
Please read to the bottom for important details.
code <- '
[ param ] CL=1.2, V=50, KA=1.3
[ pkmodel ] cmt="DEPOT CENT", depot=TRUE
[ table ] capture CP = CENT/(V/1000);
'
mod <- mcode_cache("future_model",code)
Data set
data(exTheoph)
head(exTheoph)
. ID WT Dose time conc cmt amt evid
. 1 1 79.6 4.02 0.00 0.00 1 4.02 1
. 2 1 79.6 4.02 0.25 2.84 0 0.00 0
. 3 1 79.6 4.02 0.57 6.57 0 0.00 0
. 4 1 79.6 4.02 1.12 10.50 0 0.00 0
. 5 1 79.6 4.02 2.02 9.66 0 0.00 0
. 6 1 79.6 4.02 3.82 8.58 0 0.00 0
The plan says how the jobs are to be executed. In order to parallelize with forking, we must enable it first
options(future.fork.enable=TRUE)
Now select the plan and the number of workers
plan(multiprocess, workers = 8)
The multiprocess plan will use multicore evaluation (with forked R processes) if it is supported, otherwise it will use multisession (basically a cluster like doParallel). # Run the model in parallel
As one example, we send each ID
going to a worker in this example.
There are probably other strategies for splitting the work that will
make more sense in your problem.
sp <- split(exTheoph,exTheoph$ID)
out <- future_lapply(sp, function(chunk) {
mrgsim_df(mod,chunk)
}) %>% bind_rows()
head(out)
. ID time DEPOT CENT CP
. 1 1 0.00 4.02000000 0.000000 0.00000
. 2 1 0.25 2.90455996 1.111920 22.23840
. 3 1 0.57 1.91608090 2.087845 41.75690
. 4 1 1.12 0.93733166 3.032026 60.64052
. 5 1 2.02 0.29091676 3.605404 72.10807
. 6 1 3.82 0.02802332 3.708273 74.16547
ggplot(out, aes(time,CP,group=ID)) +
geom_line() + facet_wrap(~ID)
Say we want to run 5 replicate simulations of exTheoph
. The code would
be
out <- future_lapply(seq(5), function(i) {
mrgsim(mod,exTheoph) %>% mutate(rep = i)
}) %>% bind_rows()
head(out)
. # A tibble: 6 x 6
. ID time DEPOT CENT CP rep
. <dbl> <dbl> <dbl> <dbl> <dbl> <int>
. 1 1 0 4.02 0 0 1
. 2 1 0.25 2.90 1.11 22.2 1
. 3 1 0.570 1.92 2.09 41.8 1
. 4 1 1.12 0.937 3.03 60.6 1
. 5 1 2.02 0.291 3.61 72.1 1
. 6 1 3.82 0.0280 3.71 74.2 1
dim(exTheoph)
. [1] 132 8
count(out,rep)
. # A tibble: 5 x 2
. rep n
. <int> <int>
. 1 1 132
. 2 2 132
. 3 3 132
. 4 4 132
. 5 5 132
If your system doesn’t support forking, then multiprocess evaluation will switch to multisession. multisession evaluation is done in another R session. Basically future sets up a cluster for you.
This requires one extra step in your code. Inside the code you run
on the worker, you need to call loadso()
. This function finds the
model shared object (the compiled code underlying your model) and loads
it into the new session.
plan(multisession, workers = 2)
out <- future_lapply(seq(5), function(i) {
loadso(mod)
mrgsim(mod,exTheoph) %>% mutate(rep = i)
}) %>% bind_rows()
If you don’t do this step you will see
plan(multisession, workers = 2)
try(
out <- future_lapply(seq(5), function(i) {
mrgsim(mod,exTheoph) %>% mutate(rep = i)
}) %>% bind_rows()
)
. Error in pointers(x) :
. There was a problem accessing the model shared object.
. Either the model object is corrupted or the model was
. not properly compiled and/or loaded. If the model is
. not loaded, use loadso(mod) to do so. This is usually
. required for parallel simuilation on a worker node that
. was not forked (e.g. when using future::multisession).
. Also check mrgsolve:::funset(mod) for more information.
devtools::session_info()
. ─ Session info ───────────────────────────────────────────────────────────────
. setting value
. version R version 3.6.2 (2019-12-12)
. os macOS Sierra 10.12.6
. system x86_64, darwin15.6.0
. ui X11
. language (EN)
. collate en_US.UTF-8
. ctype en_US.UTF-8
. tz America/Chicago
. date 2020-02-24
.
. ─ Packages ───────────────────────────────────────────────────────────────────
. package * version date lib source
. assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.0)
. backports 1.1.5 2019-10-02 [1] CRAN (R 3.6.0)
. broom 0.5.3 2019-12-14 [1] CRAN (R 3.6.0)
. callr 3.4.0 2019-12-09 [1] CRAN (R 3.6.0)
. cellranger 1.1.0 2016-07-27 [1] CRAN (R 3.6.0)
. cli 2.0.0 2019-12-09 [1] CRAN (R 3.6.0)
. codetools 0.2-16 2018-12-24 [2] CRAN (R 3.6.2)
. colorspace 1.4-1 2019-03-18 [1] CRAN (R 3.6.0)
. crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.0)
. DBI 1.1.0 2019-12-15 [1] CRAN (R 3.6.0)
. dbplyr 1.4.2 2019-06-17 [1] CRAN (R 3.6.0)
. desc 1.2.0 2018-05-01 [1] CRAN (R 3.6.0)
. devtools 2.2.1 2019-09-24 [1] CRAN (R 3.6.0)
. digest 0.6.23 2019-11-23 [1] CRAN (R 3.6.0)
. dplyr * 0.8.3 2019-07-04 [1] CRAN (R 3.6.0)
. ellipsis 0.3.0 2019-09-20 [1] CRAN (R 3.6.0)
. evaluate 0.14 2019-05-28 [1] CRAN (R 3.6.0)
. fansi 0.4.0 2018-10-05 [1] CRAN (R 3.6.0)
. farver 2.0.1 2019-11-13 [1] CRAN (R 3.6.0)
. forcats * 0.4.0 2019-02-17 [1] CRAN (R 3.6.0)
. fs 1.3.1 2019-05-06 [1] CRAN (R 3.6.0)
. future * 1.15.1 2019-11-25 [1] CRAN (R 3.6.0)
. future.apply * 1.3.0 2019-06-18 [1] CRAN (R 3.6.0)
. generics 0.0.2 2018-11-29 [1] CRAN (R 3.6.0)
. ggplot2 * 3.2.1 2019-08-10 [1] CRAN (R 3.6.0)
. globals 0.12.5 2019-12-07 [1] CRAN (R 3.6.0)
. glue 1.3.1 2019-03-12 [1] CRAN (R 3.6.0)
. gtable 0.3.0 2019-03-25 [1] CRAN (R 3.6.0)
. haven 2.2.0 2019-11-08 [1] CRAN (R 3.6.0)
. highr 0.8 2019-03-20 [1] CRAN (R 3.6.0)
. hms 0.5.2 2019-10-30 [1] CRAN (R 3.6.0)
. htmltools 0.4.0 2019-10-04 [1] CRAN (R 3.6.0)
. httr 1.4.1 2019-08-05 [1] CRAN (R 3.6.0)
. jsonlite 1.6 2018-12-07 [1] CRAN (R 3.6.0)
. knitr 1.26 2019-11-12 [1] CRAN (R 3.6.0)
. labeling 0.3 2014-08-23 [1] CRAN (R 3.6.0)
. lattice 0.20-38 2018-11-04 [2] CRAN (R 3.6.2)
. lazyeval 0.2.2 2019-03-15 [1] CRAN (R 3.6.0)
. lifecycle 0.1.0 2019-08-01 [1] CRAN (R 3.6.0)
. listenv 0.8.0 2019-12-05 [1] CRAN (R 3.6.0)
. lubridate 1.7.4 2018-04-11 [1] CRAN (R 3.6.0)
. magrittr 1.5 2014-11-22 [1] CRAN (R 3.6.0)
. memoise 1.1.0 2017-04-21 [1] CRAN (R 3.6.0)
. modelr 0.1.5 2019-08-08 [1] CRAN (R 3.6.0)
. mrgsolve * 0.10.1.9000 2020-02-21 [1] local
. munsell 0.5.0 2018-06-12 [1] CRAN (R 3.6.0)
. nlme 3.1-142 2019-11-07 [2] CRAN (R 3.6.2)
. pillar 1.4.3 2019-12-20 [1] CRAN (R 3.6.0)
. pkgbuild 1.0.6 2019-10-09 [1] CRAN (R 3.6.0)
. pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 3.6.0)
. pkgload 1.0.2 2018-10-29 [1] CRAN (R 3.6.0)
. prettyunits 1.0.2 2015-07-13 [1] CRAN (R 3.6.0)
. processx 3.4.1 2019-07-18 [1] CRAN (R 3.6.0)
. ps 1.3.0 2018-12-21 [1] CRAN (R 3.6.0)
. purrr * 0.3.3 2019-10-18 [1] CRAN (R 3.6.1)
. R6 2.4.1 2019-11-12 [1] CRAN (R 3.6.0)
. Rcpp 1.0.3 2019-11-08 [1] CRAN (R 3.6.0)
. RcppArmadillo 0.9.800.3.0 2019-12-07 [1] CRAN (R 3.6.0)
. readr * 1.3.1 2018-12-21 [1] CRAN (R 3.6.0)
. readxl 1.3.1 2019-03-13 [1] CRAN (R 3.6.0)
. remotes 2.1.0 2019-06-24 [1] CRAN (R 3.6.0)
. reprex 0.3.0 2019-05-16 [1] CRAN (R 3.6.0)
. rlang 0.4.2 2019-11-23 [1] CRAN (R 3.6.0)
. rmarkdown 2.1 2020-01-20 [1] CRAN (R 3.6.0)
. rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.6.0)
. rstudioapi 0.11 2020-02-07 [1] CRAN (R 3.6.0)
. rvest 0.3.5 2019-11-08 [1] CRAN (R 3.6.0)
. scales 1.1.0 2019-11-18 [1] CRAN (R 3.6.0)
. sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.6.0)
. stringi 1.4.3 2019-03-12 [1] CRAN (R 3.6.0)
. stringr * 1.4.0 2019-02-10 [1] CRAN (R 3.6.0)
. testthat 2.3.1 2019-12-01 [1] CRAN (R 3.6.0)
. tibble * 2.1.3 2019-06-06 [1] CRAN (R 3.6.0)
. tidyr * 1.0.0 2019-09-11 [1] CRAN (R 3.6.0)
. tidyselect 0.2.5 2018-10-11 [1] CRAN (R 3.6.0)
. tidyverse * 1.3.0 2019-11-21 [1] CRAN (R 3.6.0)
. usethis 1.5.1 2019-07-04 [1] CRAN (R 3.6.0)
. utf8 1.1.4 2018-05-24 [1] CRAN (R 3.6.0)
. vctrs 0.2.1 2019-12-17 [1] CRAN (R 3.6.0)
. withr 2.1.2 2018-03-15 [1] CRAN (R 3.6.0)
. xfun 0.11 2019-11-12 [1] CRAN (R 3.6.0)
. xml2 1.2.2 2019-08-09 [1] CRAN (R 3.6.0)
. yaml 2.2.0 2018-07-25 [1] CRAN (R 3.6.0)
. zeallot 0.1.0 2018-01-28 [1] CRAN (R 3.6.0)
.
. [1] /Users/kyleb/Rlibs
. [2] /Library/Frameworks/R.framework/Versions/3.6/Resources/library