Minsky
Minsky

Reputation: 513

OpenMP support in R and RcppArmadillo

I struggle to enable OpenMP support for R in Ubuntu 16.04. When installing RcppArmadillo, OpenMP is 'found', but I get the following message when installing packages (e.g. 'forecast') that depend on RcppArmadillo:

g++  -I/usr/share/R/include -DNDEBUG  -I"/usr/local/lib/R/site-
library/Rcpp/include" -I"/usr/local/lib/R/site-
library/RcppArmadillo/include"    -fpic  -g -O2 -fstack-protector-strong -
Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c 
calcBATS.cpp -o calcBATS.o
In file included from /usr/local/lib/R/site-
library/RcppArmadillo/include/armadillo:53:0,
             from /usr/local/lib/R/site-
library/RcppArmadillo/include/RcppArmadilloForward.h:46,
             from /usr/local/lib/R/site-library/RcppArmadillo/include/RcppArmadillo.h:31,
             from calcBATS.h:36,
             from calcBATS.cpp:1:
/usr/local/lib/R/site-
library/RcppArmadillo/include/armadillo_bits/compiler_setup.hpp:474:96: 
note: #pragma message: WARNING: use of OpenMP disabled; this compiler 
doesn't support OpenMP 3.0+
#pragma message ("WARNING: use of OpenMP disabled; this compiler doesn't 
support OpenMP 3.0+")

My ~/.R/Makevars are the following:

VER=-5.4
CC=ccache gcc-$(VER)
CXX=ccache g++-$(VER)
FC=ccache gfortran
F77=ccache gfortran
CXX11 =ccache g++-$(VER)
CXX14=ccache g++-$(VER)
CXX_STD = CXX11

CFLAGS= -O3 -Wall -pipe -pedantic -std=gnu11 -fopenmp
FCFLAGS=-O3 -Wall -pipe -pedantic -std=gnu11 -fopenmp
CXXFLAGS   = -O3 -Wall -pipe -pedantic -std=gnu11 -fopenmp
CXX11FLAGS = -O3 -Wall -pipe -pedantic -std=gnu11 -fopenmp
CXX14FLAGS = -O3 -Wall -pipe -pedantic -std=gnu14  -fopenmp

PKG_CFLAGS= -O3 -Wall -pipe -pedantic -std=gnu99 -fopenmp
PKG_CXXFLAGS= -fopenmp
PKG_LIBS = -fopenmp -lgomp

CFLAGS= -O3 -Wall -pipe -pedantic -std=gnu99 -fopenmp
FCFLAGS=-O3 -g0 -Wall -pipe -fopenmp

I have played around with these settings, but nothing seems to help. Any wise thoughts would be appreciated. Many thanks.

Update:

I think some packages are using openMP now (e.g. xgboost). However, I still get the error message with some packages (e.g. Amelia):

* installing *source* package ‘Amelia’ ...
** package ‘Amelia’ successfully unpacked and MD5 sums checked
** libs
g++  -I/usr/share/R/include -DNDEBUG  -I"/usr/local/lib/R/site-
library/Rcpp/include" -I"/usr/local/lib/R/site-
library/RcppArmadillo/include"    -fpic  -g -O2 -fstack-protector-strong -
Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c 
em.cpp -o em.o
In file included from /usr/local/lib/R/site-
library/RcppArmadillo/include/armadillo:53:0,
             from /usr/local/lib/R/site-
library/RcppArmadillo/include/RcppArmadilloForward.h:46,
             from /usr/local/lib/R/site-
library/RcppArmadillo/include/RcppArmadillo.h:31,
             from em.h:3,
             from em.cpp:2:
/usr/local/lib/R/site-
library/RcppArmadillo/include/armadillo_bits/compiler_setup.hpp:474:96: 
note: #pragma message: WARNING: use of OpenMP disabled; this compiler 
doesn't support OpenMP 3.0+
#pragma message ("WARNING: use of OpenMP disabled; this compiler doesn't 
support OpenMP 3.0+")

^
g++ -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o 
Amelia.so em.o -llapack -lblas -lgfortran -lm -lquadmath -L/usr/lib/R/lib -
lR
installing to /usr/local/lib/R/site-library/Amelia/libs

I can see that Amelia does not finish with '-fopenmp' unlike xgboost, but I don't know what else to add to my ~/.R/Makevars. Installation of xgboost ends in the following (note the -fopenmp):

g++ -std=gnu++11 -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-
z,relro -o xgboost.so ./xgboost_R.o ./xgboost_custom.o ./xgboost_assert.o 
./amalgamation/xgboost-all0.o ./amalgamation/dmlc-minimum0.o 
./rabit/src/engine_empty.o ./rabit/src/c_api.o -fopenmp -L/usr/lib/R/lib -lR
installing to /usr/local/lib/R/site-library/xgboost/libs

Upvotes: 3

Views: 1045

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368261

The comment by @hejseb is correct. (Rcpp)Armadillo now wants to use OpenMP when it can---on new enough compilers.

But client packages need to turn this on, meaning they have to change their file src/Makevars from having just

PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)

to

PKG_CXXFLAGS = $(SHLIB_OPENMP_CXXFLAGS)
PKG_LIBS = $(SHLIB_OPENMP_CFLAGS) $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)

to enable OpenMP in the client package on both compilation and linking.
And e.g. the forecast package does not yet do that.

You can also set "global" #define in your ~/.R/Makevars to skip OpenMP if you find this too noisy. But you'd leave some performance on the table, which is not a good default. Which is why (Rcpp)Armadillo makes the noisy comment.

Edit: There is one minor nuisance in that you need to set the flags for every C++ dialect so make sure you set CFLAGS, CXXFLAGS, CXX11FLAGS and CXX14FLAGS. You should see the -fopenmp on the command-line when R CMD INSTALL ... does its work.

Upvotes: 4

Related Questions