[Rcpp-devel] R package linking to C: coding advice

Guillaume Chapron carnivorescience at gmail.com
Sun May 31 20:46:27 CEST 2015


Hello list,

I am writing a R package for an ecological audience to model some populations. I think at this stage some advise may avoid me make naive mistakes. I have already done quite a lot of work (but before thinking about an R package) and now I need to glue all but this does not look simple!

The population model is already written in C. It uses the GSL for RNG, the Glib for managing pointer arrays and command line passing, and libdispatch (Grand Central Dispatch on a Mac) for multithreading. I control this model from R using the system() function, and read the csv files the model exports. This is somewhat a caveman approach that works well for my research, however, I think a proper package needs to be done in a much more elegant way. Here is what I intend to do:

1) I forget about multi-threading with GCD as this is Mac and Ubuntu derivatives only. I am not aware of a cross platform way of multi-threading C so things will just run more slow even if this is unfortunate.

2) I obviously forget about using the system() function and will call the C model from R using .Call() or (better?) use Rccp instead. Question: what is the best approach to pass a 3 dimensional numerical array? What is the best approach to return several arrays, including a 3 dimensional one of size 10,000 * 100 * 10 (at least)?

3) I also forget about using the Glib functions and rewrite myself the pointer array functions (the model has a lot of arrays of pointers to C structures)

4) For the RNG, I am not sure what to do. Right now, things work fine with the GSL, but the whole GLS is not easily portable to Windows. I could call the RNG defined in R, however it seems that R's RNG are much slower than the GSL ones. Should I instead rewrite the RNG in the C model so that they are standalone from the GSL or use something else like http://www.pcg-random.org ?

Thanks very much. I think your expert knowledge will allow me to avoid making wrong foundational choices. It will be my second package (after MDPtoolbox) but the technical level will be much higher!

Thanks!

Guillaume

--
Guillaume Chapron, PhD

Associate Professor

Grimsö Wildlife Research Station
Swedish University of Agricultural Sciences
SE - 73091 Riddarhyttan, Sweden

http://www.carnivore.science/
Twitter @CarnivoreSci


More information about the Rcpp-devel mailing list