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!
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
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
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@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@debian.org | http://dirk.eddelbuettel.com ______________________________________________ R-help@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.
Many thanks to Dirk and Brian! The tips helped me a lot. Kind regards!