Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing R functions to C as C functions

Background

I am writing a package implementing a specific optimization algorithm in R for various pre-specified objective functions (functions to optimize). The optimization code itself is written in C. In order to make the code as efficient as possible I also re-implemented the objective function in C. At the current stage the role of R is to provide an interface to the C code using .Call.

Goal

I would like the package to be able to handle any user supplied objective function. However, I do not want to call R functions from C, that would incur a large overhead and will most likely be excruciatingly slow.

What I really want is a way to pass a limited class of R functions (Those you would typically see as cost functions in statistical and machine learning) as a parameter, have this R function converted to a C function and then subsequently used as an objective function in the optimization algorithm.

Solution Idea

R has a strong parenting with LISP. It is pretty straight forward to get a function's abstract syntax tree(AST). I am thinking it may be possible to parse the AST as a C function and then either compile it or just pass it dynamically (I don't know if that is even is doable in C)

Questions

  • Is the solution idea above even feasible?
  • Is there any other way to do this?
  • What must I learn to be able to do this?

Note: Doing this as an R package is not necessary. Trying to do this inside the rigid structure of an R package might make things even more difficult.

like image 938
Drmanifold Avatar asked Mar 18 '15 15:03

Drmanifold


1 Answers

Some pointers at prior experiments:

  • R to C compiler at Rice: abandonded

  • R to C compiler by Simon: also stale.

  • R and LLVM attempts by Duncan Temple Lang: There are two packages, and a published paper, yet (seemingly) nobody uses it. Been a while since I read the paper(s) but one limitation was that it didn't even attempted to cover all of R.

One of the reasons you can't easily do this is that the languages excels at computing on the language as well as non-standard evaluation versus standard evaluation, promises, forced evaluation and so.

Lastly, for your narrow case of passing a compiled objective function to an optmizer: see my RcppDE package which does that.

H/t to Josh for the reminder about Simon's attempt.

like image 183
Dirk Eddelbuettel Avatar answered Oct 17 '22 14:10

Dirk Eddelbuettel