The_Anomaly
The_Anomaly

Reputation: 2505

Rcpp with Intel MKL Multithreading

I wrote a C++ shared library that uses Intel MKL for BLAS operations, and it threads beautifully, using all 12 cores of the machine. I am now trying to use RCpp to call a function from my library, and I am finding that it is single threaded. As in, for the same data, when the same function is called from C++, it uses all 12 cores very quickly, whereas when Rcpp calls it, it is single threaded and takes much longer (but the results are consistent).

Intel MKL is dynamically linked to my library thusly:

Makefile:

LIBRARIES=-lpthread -Wl,--no-as-needed -L<directory>bin -liomp5 -L<bin_directory> -lmkl_mc3 -lmkl_intel_lp64 -lmkl_gnu_thread -ldl  -lmkl_core  -lm  -DMKL_ILP64  -fopenmp
LFLAGS=-O3 -I/opt/intel/composer_xe_2015/mkl/include -std=c++0x -m64

#Compiles the shared library
g++ -fPIC -shared <cpp files> -oliblibrary.so  $(LIBRARIES) -O3 -I/opt/intel/composer_xe_2015/mkl/include -std=c++0x -m64

#Compile a controller for R, so that it can be loaded as dyn.load()
PKG_LIBS='`Rscript -e "Rcpp:::LdFlags() $(LIBRARIES) $(LFLAGS)"`' \
PKG_CXXFLAGS='`Rscript -e "Rcpp:::CxxFlags()"` $(LIBRARIES) $(LFLAGS) ' \
R CMD SHLIB fastRPCA.cpp -o../bin/RProgram.so -L../bin -llibrary

Then I call it in R:

dyn.load("fastRPCA.so", local=FALSE);

Please note I would prefer not setting MKL as the BLAS/LAPACK alternative for R, so that when other people use this code they don't have to change it for all of R. As such, I am trying to just use it in the C code.

How can I make the program multithread in Rcpp just as it does when run outside of R?

Based on this discussion, I am concerned that this is not possible. However, I wanted to ask, because I believe that since Intel MKL uses the OpenMP, perhaps there was some way to make it work.

Upvotes: 3

Views: 1112

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368439

There are basically two rules for working with R code:

  1. Create a package.

  2. Follow rule 1.

You are making your life hard by ignoring these.

Moreover, there are a number of packages on CRAN happily using OpenMP -- study those. You need to know and learn about thread setting -- see eg the RhpcBLASctl package which does this.

Lastly, you can of course connect R directly with the MKL; see the gcbd package and its vignette.

Edit three years later: See this post for details on installing the MKL easily on a .deb system

Upvotes: 2

Related Questions