[Rcpp-devel] OpenMP and Rcpp - compiler error

Dirk Eddelbuettel edd at debian.org
Thu Mar 24 18:03:10 CET 2011


Michael,

As a quick off-the-cuff remark, I suspect it is your compiler. I have done
quite a few builds with OpenMP when experimenting with a parallel version of
RcppDE (the differential evolution optimisation I "ported" from C to
C++/Rcpp).  I had no issues building this (but issues getting replicable
streams going which is still unsolved though I have some tests working).

The incomplete RcppParDE fragment is actually in Rcpp's SVN and there I
simply do 

## Hey Emacs make this a -*- mode: makefile; -*- file 
##
## -- compiling for OpenMP 
PKG_CXXFLAGS=-fopenmp
##
## -- linking for OpenMP
#PKG_LIBS= -fopenmp -lgomp $(shell $(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()") $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
##
## -- linking for OpenMP and Google Perftools profiling
PKG_LIBS= -fopenmp -lgomp $(shell $(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()") $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) -lprofiler

and that is all it takes to deploy OpenMP with R and Rcpp on my Linux box:

edd at max:~/svn/rcpp/pkg/RcppParDE$ R CMD INSTALL .
* installing to library ‘/usr/local/lib/R/site-library’
* installing *source* package ‘RcppParDE’ ...
** libs
ccache g++ -I/usr/share/R/include   -I"/usr/local/lib/R/site-library/Rcpp/include" -I"/usr/local/lib/R/site-library/RcppArmadillo/include"  -fopenmp -fpic  -g -O3 -Wall -pipe -pedantic -Wno-variadic-macros  -c deoptim.cpp -o deoptim.o
ccache g++ -I/usr/share/R/include   -I"/usr/local/lib/R/site-library/Rcpp/include" -I"/usr/local/lib/R/site-library/RcppArmadillo/include"  -fopenmp -fpic  -g -O3 -Wall -pipe -pedantic -Wno-variadic-macros  -c devol.cpp -o devol.o
ccache g++ -I/usr/share/R/include   -I"/usr/local/lib/R/site-library/Rcpp/include" -I"/usr/local/lib/R/site-library/RcppArmadillo/include"  -fopenmp -fpic  -g -O3 -Wall -pipe -pedantic -Wno-variadic-macros  -c permute.cpp -o permute.o
g++ -shared -o RcppParDE.so deoptim.o devol.o permute.o -fopenmp -lgomp -L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib -llapack -lblas -lgfortran -lm -lprofiler -L/usr/lib64/R/lib -lR
installing to /usr/local/lib/R/site-library/RcppParDE/libs
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices ...
** testing if installed package can be loaded

* DONE (RcppParDE)
edd at max:~/svn/rcpp/pkg/RcppParDE$ 

Now this packages has run-time issue, and my recent test code is somewhere so
take the package for a working package -- but it does prove that OpenMP can
easily be thrown in the mix on suitable systems.

And what "suitable systems" are is an open question. We know Rtools for
Windows does not include the libgomp library needed.  Maybe OS X is not
suitable either in that version?

Your file certainly builds here:

edd at max:/tmp$ cat michael.cpp 

#include <Rcpp.h>

RcppExport SEXP omptest (SEXP X) {

  BEGIN_RCPP

 Rcpp::NumericVector Y = X;
 int n = Y.size();
 Rcpp::NumericVector Z(n);
 int i;
 double a;
 
#pragma omp parallel
 {
#pragma omp for
 for (i=0; i<n; i++) {
   a = sqrt(Y(i));
   Z(i) = a;
 }
 }
 return Z;

 END_RCPP

}
edd at max:/tmp$ PKG_LIBS="-fopenmp -lgomp -L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib -llapack -lblas -lgfortran -lm -L/usr/lib64/R/lib -lR" PKG_CXXFLAGS="-I/usr/share/R/include   -I"/usr/local/lib/R/site-library/Rcpp/include" -I"/usr/local/lib/R/site-library/RcppArmadillo/include"  -fopenmp -fpic  -g -O3 -Wall -pipe -pedantic" R CMD SHLIB michael.cpp
ccache g++ -I/usr/share/R/include     -I/usr/share/R/include   -I/usr/local/lib/R/site-library/Rcpp/include -I/usr/local/lib/R/site-library/RcppArmadillo/include  -fopenmp -fpic  -g -O3 -Wall -pipe -pedantic -fpic  -g -O3 -Wall -pipe -pedantic -Wno-variadic-macros  -c michael.cpp -o michael.o
g++ -shared -o michael.so michael.o -fopenmp -lgomp -L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib -llapack -lblas -lgfortran -lm -L/usr/lib64/R/lib -lR -L/usr/lib64/R/lib -lR
edd at max:/tmp$ 

I am using g++ 4.4 (and sometimes 4.5), current R 2.12.2, current Rcpp 0.9.2.

Regards, Dirk




-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list