Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: C++ Optimization flag when using the inline package

Tags:

In R when using the cxxfunction from the inline package, how does one change the optimization flag for the cpp compiler?

By default, on my machine, it compiles with -g -O2. But I would like to use the -O3 optimization to gain speed. I use the Rcpp plugin if that makes any difference.

I have tried creating my own plugin, and I have tried to set the different arguments of the cxxfunction but nothing worked.

I guess one option would be to compile it using R CMD SHLIB instead of using cxxfunction. But Rcpp recommends the use of inline because most of their test cases are using it.

thanks for your help, let me know if you need any clarification

like image 332
tlamadon Avatar asked Apr 26 '11 11:04

tlamadon


2 Answers

There are a couple of options:

  1. The best solution is to modify this for all usage by R so create e.g. a file ~/.R/Makevars and set CFLAGS, CXXFLAGS, ... there. This will affect all use by R CMD INSTALL ..., R CMD SHLIB ... etc pp and as cxxfunction() from inline uses it, it works here too.

  2. Specific to inline and Rcpp: modify the plugin, that's why it is a plugin system. See Rcpp:::Rcpp.plugin.maker().

  3. Switch back from cxxfunction() to cfunction(), hence do not use a plugin and set all arguments manually.

Needless to say, I like option 1 and use it myself.

Edit: A fourth (and crude !!) method I used to use in the past is to edit $R_HOME/Makeconf and/or Makeconf.site.

like image 93
Dirk Eddelbuettel Avatar answered Sep 24 '22 15:09

Dirk Eddelbuettel


I can suggest a hack. Write a little wrapper program (also called cpp) which calls the real cpp and passes all arguments to it as is except that it passes -O3 for optimization. Then make sure your program occurs first in the executable path resolution for R.

like image 35
hawk Avatar answered Sep 24 '22 15:09

hawk