Overriding system defaults for C++ compilation flags from R
Asked Answered
H

1

11

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?

Holder answered 24/1, 2014 at 19:41 Comment(3)
It is fine; gcc will use the highest level of optimization passed through its flags (IIUC).Houseman
@KevinUshey: are you sure? I always though it uses the leftmost flags? Do you have a source for this?Villein
Ah, you're right. From the manual -- If you use multiple -O options, with or without level numbers, the last such option is the one that is effective.. So it looks like it's actually the right-most that is used.Houseman
R
2

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

Roturier answered 2/10, 2015 at 14:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.