Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rcpp with openmp

Tags:

r

rcpp

openmp

I got this Rcpp implementation to the rmvnorm function of the mvtnorm package, and I was wondering what I'd need to add in order for it to use openmp so it can take advantage of multiple cores.

I though this ought to do it:

library(Rcpp)
library(RcppArmadillo)
library(inline)
settings <- getPlugin("RcppArmadillo")
settings$env$PKG_CXXFLAGS <- paste('-fopenmp', settings$env$PKG_CXXFLAGS)
settings$env$PKG_LIBS <- paste('-fopenmp -lgomp', settings$env$PKG_LIBS)

code <- '
#include <omp.h>
using namespace Rcpp;
int cores = 1;
cores = as<int>(cores_);
omp_set_num_threads(cores);
int n = as<int>(n_);
arma::vec mu = as<arma::vec>(mu_);
arma::mat sigma = as<arma::mat>(sigma_);
int ncols = sigma.n_cols;
#pragma omp parallel for schedule(static)
arma::mat Y = arma::randn(n, ncols);
return wrap(arma::repmat(mu, 1, n).t() + Y * arma::chol(sigma));
'

rmvnorm.rcpp <- cxxfunction(signature(n_="integer", mu_="numeric", sigma_="matrix", cores_="integer"), body=code, plugin="RcppArmadillo", settings=settings, verbose=TRUE)

But apparently I'm wrong as I'm getting this compilation error message:

Compilation argument:
 /software/free/Linux/redhat_5_x86_64/pkgs/r_3.0.2/lib64/R/bin/R CMD SHLIB file50825babe43a.cpp 2> file50825babe43a.cpp.err.txt
/software/free/Linux/redhat_5_x86_64/pkgs/gcc_4.5.3/bin/g++ -I/software/free/Linux/redhat_5_x86_64/pkgs/r_3.0.2/lib64/R/include -DNDEBUG  -I/usr/local/include -I"/software/free/Linux/redhat_5_x86_64/pkgs/r_3.0.2/lib64/R/library/RcppArmadillo/include" -I"/software/free/Linux/redhat_5_x86_64/pkgs/r_3.0.2/lib64/R/library/Rcpp/include"  -fopenmp  -fpic  -g -O2  -c file50825babe43a.cpp -o file50825babe43a.o
In file included from file50825babe43a.cpp:31:0:
/software/free/Linux/redhat_5_x86_64/pkgs/gcc_4.5.3/lib/gcc/x86_64-unknown-linux-gnu/4.5.3/include/omp.h: In function 'SEXPREC* file50825babe43a(SEXPREC*, SEXPREC*, SEXPREC*, SEXPREC*)':
/software/free/Linux/redhat_5_x86_64/pkgs/gcc_4.5.3/lib/gcc/x86_64-unknown-linux-gnu/4.5.3/include/omp.h:56:8: error: expected unqualified-id before string constant
file50825babe43a.cpp:35:26: error: 'omp_set_num_threads' was not declared in this scope
file50825babe43a.cpp:41:1: error: for statement expected before 'arma'
make: *** [file50825babe43a.o] Error 1

ERROR(s) during compilation: source code errors or compiler configuration errors!

. . .

Error in compileCode(f, code, language = language, verbose = verbose) :
  Compilation ERROR, function(s)/method(s) not created! In file included from file50825babe43a.cpp:31:0:
/software/free/Linux/redhat_5_x86_64/pkgs/gcc_4.5.3/lib/gcc/x86_64-unknown-linux-gnu/4.5.3/include/omp.h: In function 'SEXPREC* file50825babe43a(SEXPREC*, SEXPREC*, SEXPREC*, SEXPREC*)':
/software/free/Linux/redhat_5_x86_64/pkgs/gcc_4.5.3/lib/gcc/x86_64-unknown-linux-gnu/4.5.3/include/omp.h:56:8: error: expected unqualified-id before string constant
file50825babe43a.cpp:35:26: error: 'omp_set_num_threads' was not declared in this scope
file50825babe43a.cpp:41:1: error: for statement expected before 'arma'
make: *** [file50825babe43a.o] Error 1

I may be missing something trivial but I don't know what it is.

like image 758
user1701545 Avatar asked Mar 30 '14 18:03

user1701545


People also ask

Is OpenMP still used?

The OpenMP standard was formulated in 1997 as an API for writing portable, multithreaded applications. It started as a Fortran-based standard, but later grew to include C and C++. The current version is OpenMP 2.0, and Visual C++® 2005 supports the full standard. OpenMP is also supported by the Xbox 360™ platform.

Can you use OpenMP in C++?

OpenMP is a library for parallel programming in the SMP (symmetric multi-processors, or shared-memory processors) model. When programming with OpenMP, all threads share memory and data. OpenMP supports C, C++ and Fortran. The OpenMP functions are included in a header file called omp.

Is OpenMP multithreaded?

OpenMP is an implementation of multithreading, a method of parallelizing whereby a primary thread (a series of instructions executed consecutively) forks a specified number of sub-threads and the system divides a task among them.


1 Answers

This has been covered before:

  • There are two posts at the Rcpp Gallery about OpenMP and Rcpp

  • There is also an (older, may need an update) example directory OpenMP in the sources which also gets copied into your installation

  • And Rcpp Attributes defines a plugin for OpenMP

like image 130
Dirk Eddelbuettel Avatar answered Oct 26 '22 05:10

Dirk Eddelbuettel