R: C++ Optimization flag when using the inline package
Asked Answered
D

2

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

Dentoid answered 26/4, 2011 at 11:32 Comment(0)
V
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.

Venus answered 26/4, 2011 at 12:31 Comment(10)
Thanks for confirming and accepting, you may also give this an 'up-vote' by clicking on the upwards facing triangle :)Venus
I would, but apparently my reputation is too low (below 15) :-)Dentoid
you could increase @tibo's reputation by up-voting their question. ;-)Deciare
Very fair point :) Especially as I do acknowledge that ~/.R/Makevars seems somewhat under-documented for R.Venus
Thank you for the answer. It would really help if you gave the syntax for setting CFLAGS or CXXFLAGS with an example line.Zackzackariah
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.Venus
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!Zackzackariah
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 github.com/search?q=org%3Acran+Makevars&type=codeVenus
I use withr::with_makevars if I only need to temporarily set CPPFLAGSBeethoven
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 ...Venus
O
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.

Outfox answered 26/4, 2011 at 11:50 Comment(1)
But heck it works; we've all done similar things to get the job done.Venus

© 2022 - 2024 — McMap. All rights reserved.