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
return;
}
void sdePost(double *y, double *x, int *N) {
// do stuff requiring useRsde
return;
}
}
This all goes into a DLL which gets called from R:
sde.sim <- function(arg1, arg2, ...) {
# do a bunch of preprocessing of the args
dyn.load("sdeMain.dll")
ans <- .C("sdeSim", as.double(y), as.double(x), as.integer(N))
ans$y
}
sde.post <- function(arg1, arg2, ...) {
# do a bunch of preprocessing of the args
dyn.load("sdeMain.dll")
ans <- .C("sdePost", as.double(y), as.double(x), as.integer(N))
ans$y
}
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
questions:
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,
Antoine
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20131017/fc5583c4/attachment-0001.html>
[Rcpp-devel] (no subject)
2 messages · strokes k., Dirk Eddelbuettel
Thanks for posting. Just for the record, and just like the main R lists, we prefer posts with full (correct) names and some sort of affiliation where available. You do know who you are dealing with when you get reply from me; it seems polite to offer the same.
On 17 October 2013 at 15:23, strokes k. wrote:
| I would very much appreciate if you would care to answer the following | questions: | | 1.? Can I use Inline (or any of the?tools in the Rcpp suite)?to achieve the | effect I desire? Look at the Rcpp attributes vignette. "Attributes", as we call, is inline in steroids. You never touch "system()", you never touch ".C()" and of course you never touch "dyn.load()". | 2.? Can you think of any problems with the implementation I've described, i.e. | horrible consequences to an unsuspecting useR? You can do better. A while back, in the RcppDE package I managed offer passage of user-defined C++ functions which the DE engine then optimized. The XPtr class in Rcpp makes that easy. A couple of days ago I gave a talk at U of C / Booth and worked a similar example into my slides (see the 2nd set -- available at my 'Presentations' page). I think that scheme is what you'd want here too. Hope this helps. The topic is good: how to pass user-controlled _compiled_ code through so we should work this out / document it. Cheers, Dirk
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com