[Rcpp-devel] [R] Rcpp with OpenMP - Need example Makevars

Dirk Eddelbuettel edd at debian.org
Fri May 24 13:26:15 CEST 2013


Hi Asis,

On 24 May 2013 at 11:10, Asis Hallab wrote:
| Dear R experts,
| 
| recently I started developing a Rcpp package "OpenMPTest".
| Within that package I want to use OpenMP, as in the following code example:
| 
| // header file
| #include <omp.h>
| using namespace Rcpp ;
| RcppExport SEXP testOpenMP( SEXP nThreads ) ;
| 
| // cpp file
| SEXP testOpenMP( SEXP nThreads ) {
|   BEGIN_RCPP
| 
|   NumericVector numberThreads = NumericVector( nThreads );
|   omp_set_num_threads( numberThreads(0) );
|   #pragma omp parallel
|   {
|     // Code inside this region runs in parallel.
|     printf("Hello!\n");
|   }
| 
|   END_RCPP
| }
| 
| As I am an absolute newbie with writing C++ extensions and have not
| much understanding of the Makevars file, I am unsure what to write
| into it. Currently I have:
| 
| ## Use the R_HOME indirection to support installations of multiple R version
| PKG_LIBS = `$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"`

This sets a value to PKG_LIBS

| KG_CFLAGS = $(SHLIB_OPENMP_CXXFLAGS)

[ Why KG_CLAGS? ]

| PKG_LIBS = $(SHLIB_OPENMP_CXXFLAGS)

This _overwrites_ the previous value of PKG_LIBS. You need to have both.

| ## -- compiling for OpenMP
| PKG_CXXFLAGS=-fopenmp
| ##
| ## -- linking for OpenMP
| PKG_LIBS= -fopenmp -lgomp

This again overwrites.

The fix is pretty simple:

  PKG_LIBS = $(SHLIB_OPENMP_CXXFLAGS) `$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"`
  PKG_CXXFLAGS=-fopenmp

should do. 

| Obviously compilation fails with error:
| 
| R CMD INSTALL OpenMPTest
| Symbol not found: __ZTIN4Rcpp19index_out_of_boundsE
| 
| With standard setup of the above Makevars I get the
| Symbol not found: omp_set_num_threads
| 
| Inline also does not work:
| 
| fb <- 'omp_set_num_threads(10);
| + #pragma omp parallel
| + { Rf_PrintValue(wrap("HALLO JUPP")); }
| + '
| > funk <-  cxxfunction( signature(), body=fb, plugin='Rcpp' )
| 
| error: ‘omp_set_num_threads’ was not declared in this scope
| 
| How do set the PKG_LIBS -and eventually other variables- to all
| required values in a single statement?

For example in the way it is done here:

   http://gallery.rcpp.org/articles/using-rcppprogress/

because sourceCpp() later expands on this initial values ("appends, not
overwrites").

| Could anyone provide me with a working Makevars example, please?

We long had those examples, several are on R-Forge as well. Did you even try
to google 'Rcpp OpenMP'? 
 
Try

PKG_CXXFLAGS="-fopenmp"
PKG_LIBS=$(shell $(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()") $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) -fopenmp

Dirk

| Help will be much appreciated!
| Kind regards and have a nice weekend!
| Cheers!
| 
| ______________________________________________
| R-help at r-project.org mailing list
| https://stat.ethz.ch/mailman/listinfo/r-help
| PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
| and provide commented, minimal, self-contained, reproducible code.

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


More information about the Rcpp-devel mailing list