[R] Rcpp with OpenMP - Need example Makevars

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri May 24 12:31:36 CEST 2013


This really is the wrong list: have you looked at the posting guide? 
The non-Rcpp aspects belong on R-devel.

But in short, you cannot control whether the R installation supports 
OpenMP.  'Writing R Extensions' tells you how to set up Makevars, so the 
example has already been documented for you.  CRAN packages using OpenMP 
include CORElearn FastPCS LDExplorer OpenMPController RcppProgress 
SpatialExtremes TPmsm TimeMachine abn aws blockcluster brnn cghseg dti 
fanc mRMRe mmcm mritc phangorn robustHD sabreR smc sparseLTSEigen 
textir, so there are plenty of examples about.

Remember that one of the three compilers commonly used with R has no 
OpenMP support, and that it is present but does not work in the compiler 
currently used on OS X.

On 24/05/2013 10: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()"`
> KG_CFLAGS = $(SHLIB_OPENMP_CXXFLAGS)
> PKG_LIBS = $(SHLIB_OPENMP_CXXFLAGS)
> ## -- compiling for OpenMP
> PKG_CXXFLAGS=-fopenmp
> ##
> ## -- linking for OpenMP
> PKG_LIBS= -fopenmp -lgomp
>
> 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?
> Could anyone provide me with a working Makevars example, please?
>
> 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.
>


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list