21

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

tlamadon
  • 970
  • 9
  • 18

2 Answers2

16

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.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Thanks for confirming and accepting, you may also give this an 'up-vote' by clicking on the upwards facing triangle :) – Dirk Eddelbuettel Apr 26 '11 at 12:47
  • I would, but apparently my reputation is too low (below 15) :-) – tlamadon Apr 26 '11 at 12:59
  • 1
    you could increase @tibo's reputation by up-voting their question. ;-) – Joshua Ulrich Apr 26 '11 at 13:14
  • Very fair point :) Especially as I do acknowledge that `~/.R/Makevars` seems somewhat under-documented for R. – Dirk Eddelbuettel Apr 26 '11 at 13:34
  • Thank you for the answer. It would really help if you gave the syntax for setting CFLAGS or CXXFLAGS with an example line. – markgalassi Jan 04 '21 at 19:52
  • hi @markgalassi: Exactly what are you after (with this nine your answer)? Did you try `cat $(Rscript -e 'cat(file.path( R.home("etc"), "Makeconf" ))')` (_i.e._ use R to tell you where `RHOME` is, then look at `etc/Makeconf` therein. It has your default values. In `~/.R/Makevars` you just override / mod as needed. – Dirk Eddelbuettel Jan 04 '21 at 20:00
  • I was just hoping for an example of a line that can go into the Makevars file. Your idea to find the system versions of those files was very useful - the shell snippet did not work on my system, but a "locate Makevars | grep R" did and I was able to find examples; thanks! – markgalassi Jan 06 '21 at 23:05
  • Ok. Note the difference between `Makeconf` (in R's own `etc/`) and `Makevars` (in packages). For the latter you have several thousand examples at CRAN, and you can _e.g._ search them via https://github.com/search?q=org%3Acran+Makevars&type=code – Dirk Eddelbuettel Jan 06 '21 at 23:12
  • I use `withr::with_makevars` if I only need to temporarily set CPPFLAGS – stephematician Feb 24 '22 at 10:16
  • Sure, one can almost always wrap everything in yet another layer. At the end of the day I suspect this just sets the environment variable for you. Up to you to use `Sys.setenv()` from base R, or something that calls it for you. Nice to have choices but it all ends in the same machine room: `R` calling `make` calling your compiler ... – Dirk Eddelbuettel Feb 24 '22 at 14:43
1

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.

hawk
  • 1,827
  • 2
  • 14
  • 28