title: Rcpp with Intel MKL Multithreading
body: <p>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).</p>
  
  <p>Intel MKL is dynamically linked to my library thusly:</p>
  
  <p>Makefile:</p>
  
  <pre><code>LIBRARIES=-lpthread -Wl,--no-as-needed -L&lt;directory&gt;bin -liomp5 -L&lt;bin_directory&gt; -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 &lt;cpp files&gt; -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
  </code></pre>
  
  <p>Then I call it in R:</p>
  
  <pre><code>dyn.load("fastRPCA.so", local=FALSE);
  </code></pre>
  
  <p>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.</p>
  
  <p>How can I make the program multithread in Rcpp just as it does when run outside of R?</p>
  
  <p>Based on <a href="http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2010-May/000646.html" rel="nofollow">this</a> 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.</p>