R中更快的BLAS
R使用BLAS(基本线性代数子程序)和LAPACK(线性代数程序包)库进行线性代数操作。由于默认库尚未完全优化,对于依赖线性代数计算的代码,通过切换到不同的BLAS库,您可以看到速度大幅提高。传统的开源解决方案阿特拉斯,数学库(不再开发),以及最近OpenBLAS(开放BLAS)(转到BLAS2叉)。
这个数学库(数学内核库)提供了上述选项的封闭源代码替代方案。“英特尔MKL”非常棒,因为它是为英特尔处理器设计的通过英特尔,但它有自己的许可条款,在AMD处理器上可能没有那么好的性能。
通常,您会从源代码编译R和BLAS库,以实现最佳优化。然而,这样做的过程是动态的,可能会导致意外的行为。即使在R的默认值之外使用参考BLAS库也可能不是最好的(请参阅这). 有鉴于此,以下应提供一种简单而保守的方法来替换标准BLAS。
Ubuntu R中更快的BLAS
在Ubuntu中,有两个免费的开源解决方案,ATLAS和OpenBLAS。要安装ATLAS和OpenBLAS,请使用
#安装OpenBLASsudo apt-get安装libopenblas库#安装ATLASsudo apt-get安装libatlas3-base liblapack3
幸运的是,如果你同时安装了这两个,你可以使用
sudo update-alternatives--配置libblas.so.3-x86_64-linux-gnu
应该显示
@ubuntu:~$sudo更新替代--配置libblas.so.3-x86_64-linux-gnu替代libblas.so.3-x86_64-linux-gnu有三种选择(提供/usr/lib/x86_64-linux-gnu/libblas.so.3)。选择路径优先级状态------------------------------------------------------------*0/usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 100自动模式1/usr/lib/x86_64-linux-gnu/atlas/libblas.so.3 35手动模式2/usr/lib/x86_64-linux-gnu/blas/libblas.so.3 10手动模式3/usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 100手动模式按<enter>保留当前选择[*],或键入选择编号:
然后确保选择相应的LAPACK。运行
sudo update-alternatives--配置liblapack.so.3-x86_64-linux-gnu
应该显示
@ubuntu:~$sudo更新替代--config liblapack.so.3-x86_64-linux-gnu可选的liblapack.so.3-x86_64-linux-gnu有三种选择(提供/usr/lib/x86_64-linux-gnu/liblapack.so.3)。选择路径优先级状态------------------------------------------------------------*0/usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3 100自动模式1/usr/lib/x86_64-linux-gnu/atlas/liblapack.so.3 35手动模式2/usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3 10手动模式3/usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3 100手动模式按<enter>保留当前选择[*],或键入选择编号:
要检查R会话中当前使用的LAPACK和BLAS,请运行
#这些功能在windows操作系统上不起作用La_库()extSoftVersion()[“BLAS”]
更复杂的是,OpenBLAS实际上有三个不同版本:串行、OpenMP和pthread(POSIX线程)。OpenMP和pthreads允许并行处理。看起来libopenblas-base库
默认情况下安装pthreads。共识似乎支持OpenMP?
有关Stan(CmdStan和CmdStanR)中的其他OpenBLAS支持,请参阅此处https://discourse.mc-stan.org/t/speedup-by-using-external-blas-pack-with-cmdstan-and-cmdstanr-py/25441(https://discourse.mc-stan.org/t/speedup-by-using-external-blas-pack-with-cmdstan-and-cmdstanr-py/25441)
要使用英特尔MKL,您可以
- 使用英特尔MKL构建R(构建指南)
- 使用安装
适当的
正如我们对上面的OpenBLAS所做的那样
确保添加了以下环境变量:MKL_THREADING_LAYER=GNU
。否则,使用英特尔的MKL会出现数字错误。
R for Windows中更快的BLAS
这对Windows来说有点困难。我不知道当前有什么预编译的ATLAS或OpenBLAS文件可以放入。有两种较旧的优化BLAS文件来源,在这里和在这里,但它可能不是你想依赖的东西。更糟糕的是,Microsoft R Open截至2021年6月不再存在。当前最佳方法可能是手动安装英特尔的MKL,或从旧的MRO安装中复制“英特尔MKL”库。MRO将MKL文件安装在
C: \Program Files\Microsoft\R打开\R-4.0.2\bin\x64\libiomp5md.dllC: \Program Files\Microsoft\R打开\R-4.0.2\bin\x64\Rblas.dllC: \Program Files\Microsoft\R打开\R-4.0.2\bin\x64\Rlapack.dll
这三个文件可以放入并替换Rblas.dll软件
和拉帕克.dll
位于的开源R的文件
C: \程序文件\R\R-4.0.2\bin\x64\
还有一些特定于MRO的R包(请参阅雷沃*
)虽然我还没有测试过,但也可能会被转移。
但是,这在企业环境中可能会很麻烦:
Microsoft R服务MKL最终用户许可协议R Services MKL是一个与Microsoft R Server一起使用的软件包,Microsoft R Open以及由发布的任何后续版本或其他软件应用程序可与英特尔®;数学内核库一起使用的us(“许可产品”)(“MKL”)(有关详细信息,请参阅https://software.intel.com/en-us/intel-mkl). 此软件包在此称为“R服务MKL”Intel Corporation简称为“Intel”1.许可。R服务MKL被授权与许可方一起专用产品。根据本协议的所有条款和条件,Microsoft授予用户使用R的非决定性、不可转让、不可发布的权利服务MKL。R Services MKL必须作为单个集成软件使用应用程序,不得将其分为组成部分。
基准
使用以下脚本的一些人工基准测试:
这个一个也很有趣(来自在这里).
以下时间是少数跑步的大致平均值。没有精确或严格的内容。我会忽略10%左右的差异。
R基准25
time_sec(秒) | 中央处理器 | n核 | 第页 | 年 | 操作系统 | 布拉斯拉巴克 |
2.41 | AMD 5600x公司 | 6 | 4.1.0 | 2021 | Ubuntu 20.04版 | 英特尔MKL 2020.0.166-1 |
2.44 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | 英特尔MKL 2020.4.304-2 |
2.48 | AMD 5600x公司 | 6 | 4.0.3 | 2021 | Ubuntu 20.04版 | pthread/libopenblasp-r0.3.10.so |
3.03 | AMD 5600x公司 | 6 | 4.0.2 | 2021 | Windows 10 | 英特尔MKL MRO |
3.33 | AMD 5600x公司 | 6 | 4.0.2 | 2021 | Ubuntu 20.04版 | 英特尔MKL MRO |
3.41 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | pthread/libopenblasp-r0.3.13.so |
3.45 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | openmp/libopenblasp-r0.3.13.so |
3.50 | 英特尔i5-1135G7 | 4 | 4.0.2 | 2021 | Windows 10 | 英特尔MKL MRO |
4.64 | AMD 5600x公司 | 6 | 4.0.3 | 2021 | Ubuntu 20.04版 | 地图集/libblas.so.3.10.3 |
5.69 | 英特尔i5-3320M | 2 | 4.0.3 | 2021 | Ubuntu 20.04版 | OpenBLAS(开放BLAS) |
6.95 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Windows 8.1 | 数学库 |
7.06 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版 | 数学库 |
7.41 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版 | OpenBLAS(开放BLAS) |
10.37 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版 | 阿特拉斯 |
19.81 | AMD 5600x公司 | 6 | 4.0.3 | 2021 | Ubuntu 20.04版 | 违约 |
20.48 | AMD 5600x公司 | 6 | 4.0.3 | 2021 | Windows 10 | 违约 |
27.92 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | 违约 |
28.82 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Windows 10 | 违约 |
32.47 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Windows 8.1 | 违约 |
32.89 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版 | 违约 |
Revo脚本矩阵乘法
time_sec(秒) | 中央处理器 | n核 | 第页 | 年 | 操作系统 | 布拉斯拉巴克 |
1.22 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | 英特尔MKL 2020.4.304-2 |
1.26 | 英特尔i5-1135G7 | 4 | 4.0.2 | 2021 | Windows 10 | 英特尔MKL MRO |
1.32 | AMD 5600x公司 | 6 | 4.1.0 | 2021 | Ubuntu 20.04版 | 英特尔MKL 2020.0.166-1 |
2.01 | AMD 5600倍 | 6 | 4.0.3 | 2021 | Ubuntu 20.04版 | pthread/libopenblasp-r0.3.10.so |
3.26 | AMD 5600x公司 | 6 | 4.0.2 | 2021 | Windows 10 | 英特尔MKL MRO |
3.34 | AMD 5600x公司 | 6 | 4.0.2 | 2021 | Ubuntu 20.04版 | 英特尔MKL MRO |
4.97 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | pthread/libopenblasp-r0.3.13.so |
4.97 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | openmp/libopenblasp-r0.3.13.so |
5.81 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Windows 8.1 | 数学库 |
5.99 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版 | OpenBLAS(开放BLAS) |
6.08 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版 | 数学库 |
6.72 | 英特尔i5-3320M | 2 | 4.0.3 | 2021 | Ubuntu 20.04版 | OpenBLAS(开放BLAS) |
10.94 | AMD 5600x公司 | 6 | 4.0.3 | 2021 | Ubuntu 20.04版 | 地图集/libblas.so.3.10.3 |
21.37 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版 | 阿特拉斯 |
83.58 | AMD 5600倍 | 6 | 4.0.3 | 2021 | Ubuntu 20.04版 | 违约 |
85.01 | AMD 5600x公司 | 6 | 4.0.3 | 2021 | Windows 10 | 违约 |
122.44 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版 | 违约 |
126.43 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Windows 8.1 | 违约 |
126.47 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Windows 10 | 违约 |
127.50 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | 违约 |
Revo脚本Cholesky
time_sec(秒) | 中央处理器 | n核 | 第页 | 年 | 操作系统 | 布拉斯拉巴克 |
0.25 | AMD 5600x公司 | 6 | 4.1.0 | 2021 | Ubuntu 20.04版 | 英特尔MKL 2020.0.166-1 |
0.26 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | 英特尔MKL 2020.4.304-2 |
0.27 | 英特尔i5-1135G7 | 4 | 4.0.2 | 2021 | Windows 10 | 英特尔MKL MRO |
0.37 | AMD 5600x公司 | 6 | 4.0.3 | 2021 | Ubuntu 20.04版 | pthread/libopenblasp-r0.3.10.so |
0.55 | AMD 5600x公司 | 6 | 4.0.2 | 2021 | Windows 10 | 英特尔MKL MRO |
0.58 | AMD 5600x公司 | 6 | 4.0.2 | 2021 | Ubuntu 20.04版 | 英特尔MKL MRO |
0.89 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | pthread/libopenblasp-r0.3.13.so |
0.89 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | openmp/libopenblasp-r0.3.13.so |
1.09 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版 | 数学库 |
1.11 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Windows 8.1 | 数学库 |
1.29 | 英特尔i5-3320M | 2 | 4.0.3 | 2021 | Ubuntu 20.04版 | OpenBLAS(开放BLAS) |
1.49 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版 | OpenBLAS(开放BLAS) |
1.91 | AMD 5600x公司 | 6 | 4.0.3 | 2021 | Ubuntu 20.04版 | 地图集/libblas.so.3.10.3 |
3.75 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版 | 地图集 |
13.38 | AMD 5600x公司 | 6 | 4.0.3 | 2021 | Windows 10 | 违约 |
13.39 | AMD 5600x公司 | 6 | 4.0.3 | 2021 | Ubuntu 20.04版 | 违约 |
19.59 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Windows 10 | 违约 |
19.74 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版 | 违约 |
19.81 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | 违约 |
20.45 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Windows 8.1 | 违约 |
Revo脚本SVD
time_sec(秒) | 中央处理器 | n核 | 第页 | 年 | 操作系统 | 布拉斯拉巴克 |
1.25 | AMD 5600x公司 | 6 | 4.1.0 | 2021 | Ubuntu 20.04版 | 英特尔MKL 2020.0.166-1 |
1.39 | 英特尔i5-1135G7 | 4 | 4.0.2 | 2021 | Windows 10 | 英特尔MKL MRO |
1.51 | AMD 5600x公司 | 6 | 4.0.3 | 2021 | Ubuntu 20.04版 | pthread/libopenblasp-r0.3.10.so |
1.60 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | 英特尔MKL 2020.4.304-2 |
1.75 | AMD 5600x公司 | 6 | 4.0.2 | 2021 | Windows 10 | 英特尔MKL MRO |
1.86 | AMD 5600x公司 | 6 | 4.0.2 | 2021 | Ubuntu 20.04版 | 英特尔MKL MRO |
2.91 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | pthread/libopenblasp-r0.3.13.so |
2.98 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | openmp/libopenblasp-r0.3.13.so |
5.26 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版 | 数学库 |
5.41 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Windows 8.1 | 数学库 |
5.91 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版 | OpenBLAS(开放BLAS) |
6.20 | AMD 5600x公司 | 6 | 4.0.3 | 2021 | Ubuntu 20.04版 | 地图集/libblas.so.3.10.3 |
6.88 | 英特尔i5-3320M | 2 | 4.0.3 | 2021 | Ubuntu 20.04版 | OpenBLAS(开放BLAS) |
13.88 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版 | 阿特拉斯 |
23.50 | AMD 5600x公司 | 6 | 4.0.3 | 2021 | Ubuntu 20.04版 | 违约 |
26.27 | AMD 5600x公司 | 6 | 4.0.3 | 2021 | Windows 10 | 违约 |
34.67 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | 违约 |
34.70 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Windows 10 | 违约 |
39.21 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Windows 8.1 | 违约 |
41.78 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版 | 违约 |
吊销脚本PCA
时间(_sec) | 中央处理器 | n核 | 第页 | 年 | 操作系统 | 布拉斯拉巴克 |
3.12 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | 英特尔MKL 2020.4.304-2 |
3.12 | AMD 5600x公司 | 6 | 4.1.0 | 2021 | Ubuntu 20.04版 | 英特尔MKL 2020.0.166-1 |
3.75 | 英特尔i5-1135G7 | 4 | 4.0.2 | 2021 | Windows 10 | 英特尔MKL MRO |
4.23 | AMD 5600x公司 | 6 | 4.0.3 | 2021 | Ubuntu 20.04版 | pthread/libopenblasp-r0.3.10.so |
5.90 | AMD 5600x公司 | 6 | 4.0.2 | 2021 | Windows 10 | 英特尔MKL MRO |
5.97 | AMD 5600x公司 | 6 | 4.0.2 | 2021 | Ubuntu 20.04版 | 英特尔MKL MRO |
9.60 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | openmp/libopenblasp-r0.3.13.so |
10.56 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | pthread/libopenblasp-r0.3.13.so |
13.81 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版 | 数学库 |
14.23 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Windows 8.1 | 数学库 |
15.03 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版 | OpenBLAS(开放BLAS) |
17.16 | 英特尔i5-3320M | 2 | 4.0.3 | 2021 | Ubuntu 20.04版 | OpenBLAS(开放BLAS) |
19.49 | AMD 5600x公司 | 6 | 4.0.3 | 2021 | Ubuntu 20.04版 | 地图集/libblas.so.3.10.3 |
41.21 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版本 | 阿特拉斯 |
91.46 | AMD 5600x公司 | 6 | 4.0.3 | 2021 | Ubuntu 20.04版 | 违约 |
98.17 | AMD 5600x公司 | 6 | 4.0.3 | 2021 | Windows 10 | 违约 |
130.24 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | 违约 |
130.39 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Windows 10 | 违约 |
142.35 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Windows 8.1 | 违约 |
153.66 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版 | 违约 |
Revo脚本LDA
time_sec(秒) | 中央处理器 | n核 | 第页 | 年 | 操作系统 | 布拉斯拉巴克 |
12.27 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | 英特尔MKL 2020.4.304-2 |
12.86 | 英特尔i5-1135G7 | 4 | 4.0.2 | 2021 | Windows 10 | 英特尔MKL MRO |
18.01 | AMD 5600x公司 | 6 | 4.0.3 | 2021 | Ubuntu 20.04版 | pthread/libopenblasp-r0.3.10.so |
18.44 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | openmp/ibopenblasp-r0.3.13.so公司 |
18.50 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | pthread/ibopenblasp-r0.3.13.so |
18.80 | AMD 5600x公司 | 6 | 4.0.2 | 2021 | Windows 10 | 英特尔MKL MRO |
19.54 | AMD 5600x公司 | 6 | 4.0.2 | 2021 | Ubuntu 20.04版 | 英特尔MKL MRO |
19.90 | AMD 5600x公司 | 6 | 4.1.0 | 2021 | Ubuntu 20.04版 | 英特尔MKL 2020.0.166-1 |
22.43 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版 | 数学库 |
23.25 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版 | OpenBLAS(开放BLAS) |
23.66 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Windows 8.1 | 数学库 |
23.91 | 英特尔i5-3320M | 2 | 4.0.3 | 2021 | Ubuntu 20.04版 | OpenBLAS(开放BLAS) |
28.58 | AMD 5600x公司 | 6 | 4.0.3 | 2021 | Ubuntu 20.04版 | 地图集/libblas.so.3.10.3 |
41.09 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版 | 阿特拉斯 |
74.37 | AMD 5600x公司 | 6 | 4.0.3 | 2021 | Ubuntu 20.04版 | 违约 |
78.72 | AMD 5600x公司 | 6 | 4.0.3 | 2021 | Windows 10 | 违约 |
92.82 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Ubuntu 14.04版 | 违约 |
94.40 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Ubuntu 21.04版 | 违约 |
94.62 | 英特尔i5-1135G7 | 4 | 4.1.0 | 2021 | Windows 10 | 违约 |
104.58 | 英特尔i5-3320M | 2 | 3.1.1 | 2014 | Windows 8.1 | 违约 |