i solving ode's in r
using desolve
package. in order speed calculations want use compiled code, using instructions here
i showing below example of ode system - coding using rcpp
below. details of ode system taken example matlab
code (can found here). wanted simulate non-trivial set of ode in order see difference in r
, compile code. following drive file, calculated mass-balances in 2 different ways (only r
, compiled code)
library(desolve) library(ggplot2) library(microbenchmark) source('parameters_gprotein.r') p <- parameters() source('ic_gprotein.r') ic <- initial_conditions() time = seq(from = 0, = 600) source('odes_gprotein.r') sim.data.df <- as.data.frame(vode(ic,time,ode_gprotein,p, mf = 22, rtol=1e-3,atol=1e-6,maxord = 5, verbose = f)) rcpp::sourcecpp("odes_gprotein.cpp") sim.data.df <- as.data.frame(vode(ic,time,odes_gprotein,p, mf = 22, rtol = 1e-3, atol = 1e-6, maxord = 5, verbose = f))
my question since vode
call made in r
. mean equations solved in compiled code if mass balances formed in cpp
, speed gains realized, or have make vode
call in cpp
file.
certainly, microbenchmarking results show there speed gain when using odes_gprotein.cpp
unit: milliseconds expr sim.data.df1 <- as.data.frame(vode(ic, time, ode_gprotein, p, mf = 22, rtol = 0.001, atol = 1e-06, maxord = 5, verbose = f)) sim.data.df2 <- as.data.frame(vode(ic, time, odes_gprotein, p, mf = 22, rtol = 0.001, atol = 1e-06, maxord = 5, verbose = f)) min lq mean median uq max neval 27.801954 29.543624 31.213758 30.565434 31.399140 86.28537 100 8.188846 8.577824 9.177491 8.817025 9.437214 18.94304 100
thanks
reproducible example
in future, please link / provide written code question based on.
comparisons
when writing comparison of r , rcpp function code should append end of c++ function name _cpp
call different implementations clear. e.g. ode_gprotein()
or odes_gprotein()
c++ implementation?
in case of benchmark, appears if odes_gprotein()
cpp()
call since microbenchmark lower. means vode()
function calling rcpp wrapper introduced namespace via rcpp::sourcecpp
.
gaining speed
if possible, code should embedded within cpp
greatest gain. means may have implementation scratch if speed huge factor (e.g. working large data or performing computationally intensive simulations). recommendation comes result of decreasing relay between r code front , c++ code. in essence, when c++ code called r objects must copied , reformatted different objects behind scenes before can worked upon. particularly case if using objects outside of default rcpp types (e.g. rcpparmadillo, rcppgsl, et cetera) copy must happen r's object structure importing structure.
what's happening in rcpp::sourcecpp
(behind scenes)
let's dive little bit deeper happening behind rcpp::sourcecpp()
. specifically, when method called different attributes extracted (e.g. // [[rcpp::]]
tags) , options enabled.
the common tag // [[rcpp::export]
creates wrapper or abstraction layer around c++ code can work r objects. behavior important 3 reasons: 1. not need worry sexp types , memory protection, 2. automatic creation of r level function default .call("fname")
, , 3. code caches enable compiled code recompiled on change.
thus, when call odes_gprotein()
calling c++ function.
Comments
Post a Comment