0

I've written an RcppEigen package with extensive OpenMP usage, all of which is in a single .cpp file.

When I compile the code using Rcpp::sourceCpp() everything is extremely fast -- amazing.

When I compile using devtools::document() and run the same function it's terribly slow -- not amazing.

I suspect this is something to do with my Makevars file and/or compiler optimization. I've seen a few questions on this topic (Overriding system defaults for C++ compilation flags from R, R: C++ Optimization flag when using the inline package).

This is my makevars file:

CXXFLAGS=-O3 -fopenmp
CXX_STD = CXX11
coatless
  • 20,011
  • 13
  • 69
  • 84
zdebruine
  • 3,687
  • 6
  • 31
  • 50
  • 3
    Run it by hand (`R CMD INSTALL ...`) and pay attention to the compilation flag. You may want `-fopenmp` also in `CXX11FLAGS` and `CXX14FLAGS`. You could also check what other packages using OpenMP do. – Dirk Eddelbuettel Mar 28 '21 at 04:36
  • @DirkEddelbuettel Thanks. Adding `PKG_CXXFLAGS = $(SHLIB_OPENMP_CXXFLAGS)` to `Makevars` fixed the OpenMP issue, but I'm still fighting the `-O2` vs. `-O3` issue. Yes, I can change this in my `~.R/Makevars` file locally, but that doesn't guarantee the user on the other end will compile with `-O3`. My understanding is it is not possible to guarantee `-O3` compiler optimization for an R package? – zdebruine Mar 28 '21 at 14:00
  • 1
    For a CRAN package, you can set whatever you want _portably_ via a `configure` file that detects the compiler and then, conditional on the compiler, "does the right thing". CRAN is cool with that. For a non-CRAN package you can do whatever you want. All this has, of course, been discussed numerous times before as you are not the first person write a package with C++ who is interested about performance. – Dirk Eddelbuettel Mar 28 '21 at 14:35

0 Answers0