Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding system defaults for C++ compilation flags from R

Tags:

c++

r

g++

rcpp

eigen

I'm using RcppEigen to write some C++ functions for my R code, and I'd like to optimize their compilation as much as possible. When I've used Eigen in the past, I've gotten a significant boost from -O3 and -fopenmp. Following Dirk's advice, I edited ~/.R/Makevars so that my Eigen code would be compiled with these flags:

CPPFLAGS=-O3 -fopenmp

This works--when I check what's happening during compilation (ps ax | grep cpp) I see:

27097 pts/6    R+     0:00 /usr/libexec/gcc/x86_64-redhat-linux/4.4.7/cc1plus -quiet -I/usr/include/R -I/home/sf/R/x86_64-redhat-linux-gnu-library/3.0/Rcpp/include -I/home/sf/R/x86_64-redhat-linux-gnu-library/3.0/RcppEigen/include -D_GNU_SOURCE -D_REENTRANT -DNDEBUG -D_FORTIFY_SOURCE=2 file69b757e053ad.cpp -quiet -dumpbase file69b757e053ad.cpp -m64 -mtune=generic -auxbase-strip file69b757e053ad.o -g -O3 -O2 -Wall -fopenmp -fpic -fexceptions -fstack-protector --param ssp-buffer-size=4 -o -

The flags I wanted are there, -O3 and -fopenmp. But I also see -O2 there, which is presumably the system-wide default (I verified this by removing ~/.R/Makevars and indeed, -O2 is there but -O3 and -fopenmp are not.)

So the question: how do I get rid of the -O2? Or, does it actually matter? The g++ man page says:

   -O3 Optimize yet more.  -O3 turns on all optimizations specified by -O2 and also 
   turns on the -finline-functions, -funswitch-loops, -fpredictive-commoning, -fgcse- 
   after-reload, -ftree-vectorize and -fipa-cp-clone options.

So maybe it's fine to have both -O2 and -O3?

like image 606
stackoverflax Avatar asked Jan 24 '14 19:01

stackoverflax


1 Answers

I think you need CXXFLAGS not CPPFLAGS in your ~/.R/Makevars

I set Makevars in the following repo to benchmark various C++ compiler flags in R/Rcpp https://github.com/jackwasey/optimization-comparison

I use a function from https://github.com/jimhester/covr to do that programmatically, if that's of use to you.

Also, did you see the following? R: C++ Optimization flag when using the inline package

like image 125
Jack Wasey Avatar answered Nov 19 '22 09:11

Jack Wasey