简体   繁体   English

Rcpp与英特尔MKL多线程

[英]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. 我编写了一个C ++共享库,它使用英特尔MKL进行BLAS操作,并使用机器的所有12个核心进行精美的线程。 I am now trying to use RCpp to call a function from my library, and I am finding that it is single threaded. 我现在正在尝试使用RCpp从我的库中调用一个函数,我发现它是单线程的。 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). 同样,对于相同的数据,当从C ++调用相同的函数时,它会非常快速地使用所有12个内核,而当Rcpp调用它时,它是单线程的并且需要更长的时间(但结果是一致的)。

Intel MKL is dynamically linked to my library thusly: 英特尔MKL动态链接到我的库:

Makefile: 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: 然后我在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. 请注意我不希望将MKL设置为R的BLAS / LAPACK替代方案,因此当其他人使用此代码时,他们不必为所有R更改它。因此,我试图在其中使用它C代码。

How can I make the program multithread in Rcpp just as it does when run outside of R? 如何在Rcpp中使程序多线程就像在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. 但是,我想问一下,因为我相信由于英特尔MKL使用OpenMP,也许有一些方法可以使它工作。

There are basically two rules for working with R code: 使用R代码基本上有两个规则:

  1. Create a package. 创建一个包。

  2. Follow rule 1. 遵守规则1。

You are making your life hard by ignoring these. 你忽略了这些让你的生活变得艰难。

Moreover, there are a number of packages on CRAN happily using OpenMP -- study those. 此外,CRAN上有很多使用OpenMP的软件包 - 研究它们。 You need to know and learn about thread setting -- see eg the RhpcBLASctl package which does this. 您需要了解并了解线程设置 - 请参阅例如执行此操作的RhpcBLASctl包。

Lastly, you can of course connect R directly with the MKL; 最后,您当然可以直接将R与MKL连接; see the gcbd package and its vignette . 看看gcbd包及其插图

Edit three years later: See this post for details on installing the MKL easily on a .deb system 三年后编辑:有关在.deb系统上轻松安装MKL的详细信息,请参阅此文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM