0

In Eigen FAQ it states that you need to enable vectorization in the compiler.

I am trying to develop an R package using RcppEigen. I would like it if the user would have the best performance without having to manually compile the package with specified flags.

What is best practice for an R package looking to enable vectorization in the Eigen library?

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
jds
  • 604
  • 4
  • 16

2 Answers2

2

Do exactly what the FAQ says and set the compiler flags. You may have to turn those on from a script configure after you test what the current compiler supports -- and CRAN may still tell you that the flags are not portable.

Also, just to fix terms here, there is no "library" here in our: RcppEigen only uses headers from Eigen which is designed as a templated header-only package.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 2
    Could you pretend like I’m dense and explicitly state what that configure script would look like and where it should go? :) – jds Dec 06 '18 at 04:43
  • You need to learn how to a) detect the compiler b) for each type of compiler interrogate it about its capabilities c) set those d) do it reliably for every cross product of compiler and version and OS. I don't -- I let R figure this out based on 25 years if trial and error. – Dirk Eddelbuettel Dec 06 '18 at 13:13
  • 1
    Sorry, I am having a little bit of trouble interpreting your response. Are you saying that vectorization of Eigen is enabled by default? (You comment that you let R figure this out was confusing to me). – jds Dec 06 '18 at 14:48
  • 1
    If you have questions about the Eigen FAQ I suggest you take it to the Eigen (FAQ) authors. I am trying to explain to you that how code is generated depends on compiler switches, and for R packages we rely on R itself for placing sensible settings which you are free to override, but that is difficult to do portably. Good luck. – Dirk Eddelbuettel Dec 06 '18 at 14:52
  • Just for some related colour: I ran into _just that_ at work where we optimise aggressively (_i.e._ via `-march=native`) and it then turns out that your binaries are no longer portable _even among other Linux machines of the same OS version_ as the cpu and chipset combos may differ. In short: this is genuinely tricky material. The _No Free Lunch_ theorem still holds. Highest performance may always require source builds, the `-march=...` and `-mtune=...` options may help you. – Dirk Eddelbuettel Dec 06 '18 at 15:53
1

I'm a beginner too and many hours trying to understand Rcpp may be relevant to you @jds. I wanted to enable vectorisation on my Dell Precision M2800 with AVX architecture so I added the -mavx2 flag to my configure file using the following chunk thrice:

CXXFLAGS= -O3 -std=c++11 -Wall -mavx2

This code change sped up my code (a series of double-nested for loops) from 4.1s to 1.4s!

Find out how to amend the compiler flags that get used by sourceCpp by building a skeleton package using configure and clean files to create your Makevars file as beautifully demonstrated by @nrussell in How to change and set Rcpp compile arguments