简体   繁体   English

将英特尔的数学 Kernel 库 (MKL) 链接到 Windows 上的 R

[英]Linking Intel's Math Kernel Library (MKL) to R on Windows

Using an alternative BLAS for R has several advantages, see eghttps://cran.r-project.org/web/packages/gcbd/vignettes/gcbd.pdf .为 R 使用替代 BLAS 有几个优点,请参见例如https://cran.r-project.org/web/packages/gcbd/vignettes/gcbd.Z437175BA41937410EE004E1

Microsoft R Open https://mran.revolutionanalytics.com/documents/rro/installation/#sysreq is using Intel's MKL instead of the default Reference BLAS to speed up calculations. Microsoft R Open https://mran.revolutionanalytics.com/documents/rro/installation/#sysreq正在使用 Intel 的 MKL 而不是默认的 Reference BLAS 来加速计算。

My question is:我的问题是:

What would be the exact steps to link Intel's MKL library **manually to R**'s most recent version on Windows ( https://cran.r-project.org/bin/windows/base/ )?英特尔的 MKL 库**手动链接到 R** 在 Windows 上的最新版本( https://cran.r-project.org/bin/windows/base/ )的确切步骤是什么?


UPDATE 20-07-2016: Here is very detailed description on how to build a OpenBLAS-based Rblas.dll for 64-bit R for Windows for R ≥ 3.3.0: http://www.avrahamadler.com/r-tips/build-openblas-for-windows-r64/ UPDATE 20-07-2016: Here is very detailed description on how to build a OpenBLAS-based Rblas.dll for 64-bit R for Windows for R ≥ 3.3.0: http://www.avrahamadler.com/r-tips /build-openblas-for-windows-r64/

Easier solution than having to recompile R against the Intel MKL libraries on Windows is just to比在 Windows 上针对英特尔 MKL 库重新编译 R 更简单的解决方案是

  1. Install Microsoft R Open from https://mran.microsoft.com/download , which comes with the outdated R version 3.5.3 but also with the Intel MKL multithreaded BLAS librarieshttps://mran.microsoft.com/download安装 Microsoft R Open,它随过时的 R 版本 3.5.3 以及英特尔 MKL 多线程 BLAS 库一起提供
  2. Install the latest version of R from https://cran.r-project.org/bin/windows/base/ , ie currently R 3.6.2https://cran.r-project.org/bin/windows/base/安装最新版本的 R,即当前 R 3.6.2
  3. copy files libiomp5md.dll, Rblas.dll and Rlapack.dll from C:\\Program Files\\Microsoft\\R Open\\R-3.5.3\\bin\\x64 to C:\\Program Files\\R\\R-3.6.2\\bin\\x64 (you can back up your existing default non-hyperthreaded Rblas.dll and Rlapack.dll files first if you like)将文件 libiomp5md.dll、Rblas.dll 和 Rlapack.dll 从 C:\\Program Files\\Microsoft\\R Open\\R-3.5.3\\bin\\x64 复制到 C:\\Program Files\\R\\R-3.6.2\\bin \\x64(如果愿意,可以先备份现有的默认非超线程 Rblas.dll 和 Rlapack.dll 文件)
  4. copy Microsoft R Open libraries/packages MicrosoftR, RevoIOQ, RevoMods, RevoUtils, RevoUtilsMath and doParallel from C:\\Program Files\\Microsoft\\R Open\\R-3.5.5\\library to your default package directory, eg C:\\Documents\\R\\win-library\\3.6将 Microsoft R Open 库/包 MicrosoftR、RevoIOQ、RevoMods、RevoUtils、RevoUtilsMath 和 doParallel 从 C:\\Program Files\\Microsoft\\R Open\\R-3.5.5\\library 复制到您的默认包目录,例如 C:\\Documents\\R \\win-库\\3.6
  5. copy files Rprofile.site and Renviron.site from directory C:\\Program Files\\Microsoft\\R Open\\R-3.5.5\\etc to C:\\Progral Files\\R\\R-3.6.2\\etc将文件 Rprofile.site 和 Renviron.site 从目录 C:\\Program Files\\Microsoft\\R Open\\R-3.5.5\\etc 复制到 C:\\Progral Files\\R\\R-3.6.2\\etc
  6. replace line 24 in file Rprofile.site options(repos=r) with options(repos="https://cran.rstudio.com") (or your favourite CRAN repository - you can also use " https://cran.revolutionanalytics.com ", the MRO repository that has the latest daily builds of all packages) to make sure that it will install the latest CRAN packages as opposed to the outdated mran.microsoft.com mirror that has outdated package versions, frozen at the 15th of April 2019. Also comment out lines 153, 154 and 155 with a #将文件 Rprofile.site options(repos=r)第 24 行替换为options(repos="https://cran.rstudio.com") (或您最喜欢的 CRAN 存储库 - 您也可以使用“ https://cran.revolutionanalytics .com ”,MRO 存储库,具有所有包的最新每日版本),以确保它将安装最新的 CRAN 包,而不是过时的 mran.microsoft.com 镜像,该镜像具有过时的包版本,在 15 日冻结2019 年 4 月。同时用 # 注释掉第 153、154 和 155 行

Then restart RStudio to check that it works, with small SVD benchmark on my Intel Core i7-4700HQ 2.4GHz 4 core/8 thread laptop:然后重新启动 RStudio 以检查它是否有效,在我的英特尔酷睿 i7-4700HQ 2.4GHz 4 核/8 线程笔记本电脑上使用小型 SVD 基准测试:

getMKLthreads()
4

# Singular Value Decomposition
m <- 10000
n <- 2000
A <- matrix (runif (m*n),m,n)
system.time (S <- svd (A,nu=0,nv=0))
   user  system elapsed 
  15.20    0.64    4.17

That same benchmark without Intel MKL installed ran at未安装英特尔 MKL 的相同基准测试运行于

   user  system elapsed 
  35.11    0.10   35.21 

so we get a >8 fold speed increase here!所以我们在这里获得了 8 倍以上的速度提升!

Screenshot of Microsoft R Open 6.2 with Intel MKL up and running:启动并运行英特尔 MKL 的 Microsoft R Open 6.2 的屏幕截图:

在此处输入图片说明

Alternatively, if you don't like copying files from MRO to your latest R installation, you can also copy the files from the free Intel MKL installation to your R installation to get multithreaded operation (as outlined in the other answer below):或者,如果您不喜欢将文件从 MRO 复制到最新的 R 安装,您还可以将文件从免费的英特尔 MKL 安装复制到您的 R 安装以获得多线程操作(如以下其他答案所述):

  1. Install Intel MKL from https://software.intel.com/en-us/mkl/choose-download (free)https://software.intel.com/en-us/mkl/choose-download (免费)安装英特尔 MKL

Copy all the contents from inside these folders从这些文件夹中复制所有内容

C:\Program Files (x86)\IntelSWTools\compilers_and_libraries\windows\redist\intel64\mkl
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries\windows\redist\intel64\compiler

to

C:\Program Files\R\R-3.6.1\bin\x64
  1. Inside the destination folder, create 2 copies of mkl_rt.dll and rename one of them Rblas.dll and the other Rlapack.dll replacing the originals and also keeping mkl_rt.dll.在目标文件夹中,创建 mkl_rt.dll 的 2 个副本,并将其中一个重命名为 Rblas.dll,另一个重命名为 Rlapack.dll,替换原始文件并保留 mkl_rt.dll。
  2. This will not provide you with the function setMKLthreads() and getMKLthreads() functions though to set the nr of intel MKL threads, as these come with the MRO package RevoUtilsMath.这不会为您提供 setMKLthreads() 和 getMKLthreads() 函数来设置英特尔 MKL 线程的 nr,因为它们随 MRO 包 RevoUtilsMath 一起提供。 But for most people the default nr of threads set equal to the nr of physical cores will be OK though...但是对于大多数人来说,线程的默认 nr 设置等于物理内核的 nr 将是可以的...

Not sure what's up with Microsoft, and why they are no longer updatig MRO... And why they also dropped Mac OS X support...不知道微软怎么了,为什么他们不再更新 MRO ......为什么他们也放弃了对 Mac OS X 的支持......

I hope that, given that Intel MKL is free now, the R core people will sooner or later provide a precompiled R version that is compiled to use the Intel MKL libs, or possibly detect at run time if Intel MKL is installed, and if it is, use it.我希望,鉴于现在英特尔 MKL 是免费的,R 核心人员迟早会提供一个预编译的 R 版本,该版本被编译为使用英特尔 MKL 库,或者可能在运行时检测是否安装了英特尔 MKL,如果它是,使用它。 I think this is important, especially since the easy availability of a good multithreaded BLAS also determines how one would develop packages - eg if a good multithreaded BLAS would be available for all OSes one would veer towards using RcppArmadillo, which falls back on whatever BLAS one has installed (but on Windows would give drastically worse timings if Intel MKL is not installed), and if not RcppEigen would be the best option, as that has its own multithreaded matrix algebra, irrespective of the BLAS against which R is compiled......我认为这很重要,特别是因为一个好的多线程 BLAS 的易用性也决定了一个人将如何开发包 - 例如,如果一个好的多线程 BLAS 可用于所有操作系统,那么人们将转向使用 RcppArmadillo,它依赖于任何 BLAS已安装(但在 Windows 上,如果未安装英特尔 MKL,时间会大大缩短),如果没有,RcppEigen 将是最佳选择,因为它具有自己的多线程矩阵代数,无论编译 R 所针对的 BLAS 是什么... ...

On Ubuntu btw it's very easy to make R use Intel MKL, without having to recompile R, as outlined here: https://github.com/eddelbuettel/mkl4deb在 Ubuntu 上,很容易让 R 使用 Intel MKL,而无需重新编译 R,如下所述: https : //github.com/eddelbuettel/mkl4deb

PS Slight problem is that running setMKLthreads(4) will crash RStudio (that was already a problem in the official MRO 3.5.3 though) but it does work OK in the R console... PS 一个小问题是运行 setMKLthreads(4) 会使 RStudio 崩溃(虽然这在官方 MRO 3.5.3 中已经是一个问题)但它在 R 控制台中运行正常......

I was able to link R 3.6.0 with custom dlls you create using the builder .我能够将 R 3.6.0 与您使用builder创建的自定义 dll 链接起来。 Basically you have to export the same symbols Rblas.dll and Rlapack.dll do.基本上,您必须导出与Rblas.dllRlapack.dll相同的符号。 Start the Compiler 19.0 Update 4 for Intel 64 Visual Studio 2017 environment command prompt:启动Compiler 19.0 Update 4 for Intel 64 Visual Studio 2017 environment命令提示符:

Get the symbols:获取符号:

dumpbin /exports Rblas.dll > Rblas_list
dumpbin /exports Rlapack.dll > Rlapack_list_R

Edit both files deleting the "header" and "footer" and have all the lines with the symbol names (ex.: 248 F7 00138CE0 dgeevx_ ) be like dgeevx_ (only with the names).编辑删除“页眉”和“页脚”的两个文件,并使所有带有符号名称的行(例如: 248 F7 00138CE0 dgeevx_ )像dgeevx_ (仅带有名称)。 Copy the builder directory to somewhere in your pc and inside it run:builder目录复制到你电脑的某个地方,然后在里面运行:

# blas links fine
nmake libintel64 export=..path..\Rblas_list name=Rblas 
# save lapack errors in another list
nmake libintel64 export=..path..\Rlapack_list_R name=Rlapack 1> undefined_symbols_list

Edit undefined_symbols_list keep only the names in each line and create a new list with the difference编辑undefined_symbols_list仅保留每行中的名称并创建一个具有差异的新列表

findstr /v /g:undefined_symbols_list Rlapack_list_R > Rlapack_list
nmake libintel64 export=..path..\Rlapack_list name=Rlapack

With dumpbin /dependents Rlapack.dll , you can see that they depend on libiomp5md.dll , which you can find inside the redist folder in mkl installation.使用dumpbin /dependents Rlapack.dll ,您可以看到它们依赖于libiomp5md.dll ,您可以在 mkl 安装的redist文件夹中找到它。


Method 2方法二

This method uses more disk space, but it's simpler.这种方法使用更多的磁盘空间,但更简单。 Copy all the contents from inside these folders从这些文件夹中复制所有内容

C:\Program Files (x86)\IntelSWTools\compilers_and_libraries\windows\redist\intel64\mkl
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries\windows\redist\intel64\compiler

to

C:\Program Files\R\R-3.6.1\bin\x64

Inside the destination folder, create 2 copies of mkl_rt.dll and rename one of them Rblas.dll and the other Rlapack.dll replacing the originals and also keeping mkl_rt.dll .在目标文件夹中,创建mkl_rt.dll 2 个副本,并将其中一个重命名为Rblas.dll ,另一个重命名为Rlapack.dll替换原件并保留mkl_rt.dll

just tried for R 3.5.1 installation.刚刚尝试安装 R 3.5.1。 I installed Microsoft R Open alongside with the CRAN R and copy libiomp5md.dll and overwrite Rblas.dll, Rlapack.dll from MRO MKL counterparts to link to CRAN R on Windows (similar to another answer above but need to copy the file libiomp5md.dll as well).我将 Microsoft R Open 与 CRAN R 一起安装并复制 libiomp5md.dll 并覆盖 MRO MKL 对应的 Rblas.dll、Rlapack.dll 以链接到 Windows 上的 CRAN R(类似于上面的另一个答案,但需要复制文件 libiomp5md.dll同样)。 This worked out fine and the CRAN R runs as fast as MRO according to the version.compare package on Github ( https://github.com/andrie/version.compare )根据 Github ( https://github.com/andrie/version.compare ) 上的 version.compare 包,结果很好,CRAN R 的运行速度与 MRO 一样快

The solution presented by Tom Weenseleers worked for me with the latest R version. Tom Weenseleers 提出的解决方案适用于我最新的 R 版本。 Thank you.谢谢你。

I wanted to add something to this discussion, as its related, and I was unsure how to add this to the greater community.我想在这个讨论中添加一些东西,因为它是相关的,我不确定如何将它添加到更大的社区。 Please forgive my descriptions, I am an amateur.请原谅我的描述,我是业余爱好者。

This workaround seems to break at least two other R packages, igraph, and clusterProfiler.此解决方法似乎破坏了至少两个其他 R 包 igraph 和 clusterProfiler。 Clusterprofiler depends on igraph, so the root cause was igraph. Clusterprofiler 依赖于 igraph,所以根本原因是 igraph。 Likely other igraph dependent packages as well.可能还有其他 igraph 依赖包。

I am posting this because I found a simple workaround, and after extensive searching I never found this addressed explicitly on any forum, and this may help someone else.我发布这个是因为我找到了一个简单的解决方法,经过广泛搜索后,我从未在任何论坛上发现这个问题,这可能对其他人有所帮助。

for reference, clusterProfiler_4.4.4 and igraph_1.3.4 R version 4.2.1, Platform: x86_64-w64-mingw32/x64 (64-bit) Windows 10 x64 (build 22000)供参考,clusterProfiler_4.4.4 和 igraph_1.3.4 R 版本 4.2.1,平台:x86_64-w64-mingw32/x64(64 位)ZAEA23489CE3AA9B6406EBB28E0CDA4200Z 10 x64(构建)

calling library(igraph) after implementing the above workaround yielded the following error in a popup:在实现上述解决方法后调用 library(igraph) 在弹出窗口中产生以下错误:

rsession-utf8.exe Entry Point Not Found The procedure entry point quadmath_snprintf could not be located in the dynamic link library C:Program Files\R\R->4.2.1\library\graph\libs\x64\igraph.dll. rsession-utf8.exe Entry Point Not Found 程序入口点 quadmath_snprintf 无法在动态链接库 C:Program Files\R\R->4.2.1\library\graph\libs\x64\igraph.Z06416233FE5EC4C5933122E4AB248AF1 中找到。

Pressing OK yields an error message in R按 OK 在 R 中产生错误消息

Error: package or namespace load failed for 'igraph' in inDL(x, >as.logical(local), as.logical(now), ...): unable to load shared object 'C:/Program Files/R/R->4.2.1/library/igraph/libs/x64/igraph.dll': LoadLibrary failure: The specified procedure could not be found.错误:package 或 inDL(x, >as.logical(local), as.logical(now), ...) 中的“igraph”的命名空间加载失败:无法加载共享 object 'C:/Program Files/R/ R->4.2.1/library/igraph/libs/x64/igraph.dll': LoadLibrary failure: 找不到指定的程序。

The workaround - When implementing the workaround to use MKL in R, keep the original Rlapack.dll and Rlablas.dll (i simply renamed these ie Rblas_orig.dll) To use igraph or dependent packages, swap the.dll filenames, such that the two original R blas file has the original name, and the MKL file is renamed (ie Rblas_mkl.dll). The workaround - When implementing the workaround to use MKL in R, keep the original Rlapack.dll and Rlablas.dll (i simply renamed these ie Rblas_orig.dll) To use igraph or dependent packages, swap the.dll filenames, such that the two原R blas文件有原名,MKL文件改名(即Rblas_mkl.dll)。 Restart R, and igraph/clusterprofiler loads fine.重新启动 R,igraph/clusterprofiler 加载正常。

Unfortunately, this will disable the MKL until you revert the.dll filenames again and restart R, but as long as you don't need to use both igraph and MKL at the same time it works, although frustrating.不幸的是,这将禁用 MKL,直到您再次恢复 .dll 文件名并重新启动 R,但只要您不需要同时使用 igraph 和 MKL,它就可以工作,尽管令人沮丧。

If anyone finds a better solution please let me know.如果有人找到更好的解决方案,请告诉我。

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

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