[R-sig-dyn-mod] compiled code and parallel computing with deSolve
Fantastic, thanks Thomas. Works like a charm and so much faster! -----Urspr?ngliche Nachricht----- Von: Thomas Petzoldt [mailto:thomas.petzoldt at tu-dresden.de] Gesendet: Thursday, 9 November 2017 17:22 An: Special Interest Group for Dynamic Simulation Models in R Cc: Simeon Lisovski Betreff: Re: [R-sig-dyn-mod] compiled code and parallel computing with deSolve Hi, if I understand you correctly, then you want to use "top level parallelization". It means that R starts several R instances as child processes, each process can call its own ode(). What I do is creating an ordinary function (lets say "foo") that gets all the necessary data (!) and packages (!) and that calls ode() with the appropriate dll. Then a parallelizer, e.g. parLapply calls this function "foo". Package "growthrates" (https://github.com/tpetzoldt/growthrates/) uses such a mechanism for fitting parametric models in parallel. The function to be fitted can be given in closed form, as an R ODE or a compiled ODE -- it does not matter at the top level. Please check if your R child processes get all relevant data, including the event function. Thomas Am 09.11.2017 um 14:18 schrieb Simeon Lisovski:
Hallo,
I am having problems with some of my code that uses compiled code (C)
within the "ode" function.
For an optimization routine, I need to run a particular ODE in each
iteration and with multiple parameters to find a solution that is then
incorporated in the main function. To speed things up I was trying to
run this step in parallel (using the parallel package). However, I
cannot find a way to export the compiled code to the nodes so that
it`ll become available for the ode function. On a mac the mcmcApply
function is doing the trick, but I cannot get it to work on a windows computer.
The basic idea for code (not reproducible since that would require
very long
code):
dyn.load(model.dll) ## c-code with functions "dem", "initforc" and "event"
event_times <- c(1,5,10,15)
t_fnc <- function(t) {.}
parallel::makeCluster(nr.cores)
parallel::clusterSetRNGStream(mycl)
parallel::clusterEvalQ(mycl, library("deSolve"))
parallel::clusterExport(mycl,c("event_times", "t_fnc"),
envir=environment())
mu <- seq(0.01, 0.1, length = 100)
fnc <- function(x, ..) {
parms <- c(alpha = x, . )
out <- ode(y = xstart, t, func = "popM", parms = parms,
dllname = "dem", initforc = "initforc",
forcings = list(t, t_fnc(t)),
initfunc = "initmod", nout = 0,
events = list(func = "event",
times = event_times),
method = rkMethod("rk34f")
out
}
parallel::parSapply(mycl, mu, FUN = fnc, .)
This won`t work and R complains that event$func should be loaded.
Any thoughts?
Thanks,
Simeon