[Rcpp-devel] (no subject)

strokes k. casual.frog at gmail.com
Thu Oct 17 21:23:06 CEST 2013

Dear rcpp-devel,

I have been working for some time on an R package to perform simulation and
inference for stochastic differential equations (SDEs).  Due to a large
number of serial calculations, I have achieved a speed-up of several orders
of magnitude by writing most of the code in C++.  However, for the useR to
work with his or her own SDE, it is necessary to modify a very small
portion of the C++ code and then recompile it.  The C++ code looks
something like this:

double useRsde(double x) {
  // stuff

extern "C" {
  void sdeSim(double *y, double *x, int *N) {
    // do stuff requiring useRsde

  void sdePost(double *y, double *x, int *N) {
    // do stuff requiring useRsde

This all goes into a DLL which gets called from R:

sde.sim <- function(arg1, arg2, ...) {
  # do a bunch of preprocessing of the args
  ans <- .C("sdeSim", as.double(y), as.double(x), as.integer(N))

sde.post <- function(arg1, arg2, ...) {
  # do a bunch of preprocessing of the args
  ans <- .C("sdePost", as.double(y), as.double(x), as.integer(N))

I would like to create an R function called "make.sde.model" which works
like this:

make.sde.model <- function(model.name, useRcode) {
  # compile DLL, create functions sde.sim and sde.post which are
individually renamed

model.name <- "cir"
useRcode <- "character string of c++ code"

make.sde.model(model.name, useRcode)

cir.sim(x0, theta, N, dt)

cir.post(x0, theta0, nsamples)

In other words, make.sde.model created cir.sim and cir.post, which are
copies of the generic functions sde.sim and sde.post, but which call the
right DLL.

As it stands, I am:

- not able to automatically create the cir.sim and cir.post functions.  I
copy-paste large blocks of code every time I use a new SDE model.
- more importantly, not able to compile the C++ code from within R.  I'm
not comfortable enough to navigate my way around the "system" calls, and I
would not expect anything that works on my computer to work for anyone
else, especially if they are on a different OS...

I've spent a good amount of time looking at the "Inline" package.  I'd
really like to use it to do most the of the C++ compiling magic.  There's
tons of documentation and help for it online.  I figure if the useR can get
a simple "return R_NilValue" function to compile then they should be able
to compile my code as well (it's really nothing fancy: no external
libraries or even header files).  Also, the function "setCmethod" seems to
create the R functions silently exactly as I would like.  However, I can't
quite figure out how to put all the pieces together using Inline.
Specifically, I'm not sure how to best combine the R preprocessing steps
with Inline's capabilities.  I guess I could potentially do all the
processing in C++, but there's quite a bit of it to do and it really
doesn't seem like the right place to do it...

I would very much appreciate if you would care to answer the following

1.  Can I use Inline (or any of the tools in the Rcpp suite) to achieve the
effect I desire?
2.  Can you think of any problems with the implementation I've described,
i.e. horrible consequences to an unsuspecting useR?
3.  If the answers to 1 and 2 are No and Yes, could you please suggest as
to how I should proceed?

Thank you very much for your time.

Best wishes,

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20131017/fc5583c4/attachment-0001.html>

More information about the Rcpp-devel mailing list