高性能Numpy/Scipy加速:使用Intel MKL和Intel Compilers或OpenBLAS(待续)

来源:互联网 发布:linux下c ide 编辑:程序博客网 时间:2024/05/17 09:42

Numpy/Scipy加速:使用Intel MKL和Intel Compilers



1.获取Intel Parallel Studio XE


Intel免费软件工具提供免费的软件包,其中包括完整的Intel编译器和计算库及其激活码,软件和激活码一一对应。注意需要使用教育邮箱注册,否则不予通过。


2.安装过程


解压后在终端进入./install_GUI.sh,按照提示进行就好,其中激活的步骤需要联网,且最多只能激活5次。最后整个库会安装在\opt\intel路径下,重要的是配置环境变量。


首先加入Intel编译器icc的路径

export INTELCOMP=/opt/intel/binexport PATH=$INTELCOMP:$PATH

接着加入共享库的路径,其中库的名称一定要有对应的版本号,这里是.2.174

export MKLHOME=/opt/intel/mkl/lib/intel64export LIB1=/opt/intel/compilers_and_libraries_2017.2.174/linux/mkl/lib/intel64_linexport LIB2=/opt/intel/compilers_and_libraries_2017.2.174/linux/compiler/lib/intel64_linexport LD_LIBRARY_PATH=$MKLHOME:$LIB1:$LIB2:$LD_LIBRARY_PATH

注意:如果使用中还有一些.so库找不到,则可以定位这些库然后加入到LD_LIBRARY_PATH中。如果需要有全局的作用,则可以编辑/etc/ld.so.conf文件,增加这些路径,并用ldconfig刷新。


构建numpy和scipy


下载 NumPy & SciPy libraries

git clone https://github.com/numpy/numpy.git numpygit clone https://github.com/scipy/scipy.git scipy

Build instructions
Building From Source on Linux


主要依据Numpy/Scipy with Intel® MKL and Intel® Compilers官网提示。总结起来就是在numpy的安装路径下增加site.cfg文件,内容如下:


cd numpysudo gedit site.cfg

添加如下代码

[mkl]library_dirs = /opt/intel/compilers_and_libraries_2017.2.174/linux/mkl/lib/intel64include_dirs = /opt/intel/compilers_and_libraries_2017.2.174/linux/mkl/includemkl_libs = mkl_rtlapack_libs =

接着更改numpy/distutils/intelccompiler.py文件中的self.cc_exe语句,如果是64位,则在IntelEM64TCCompiler类中将之改为

self.cc_exe = 'icc -O3 -g -fPIC -fp-model strict -fomit-frame-pointer -openmp -xhost'

然后就可以编译安装numpy了

$ python setup.py config --compiler=intelem build_clib --compiler=intelem build_ext --compiler=intelem install

构建安装scipy可以直接进行

python setup.py config --compiler=intelem --fcompiler=intelem build_clib --compiler=intelem --fcompiler=intelem build_ext --compiler=intelem --fcompiler=intelem install

Numpy/Scipy加速:使用OpenBLAS



1. Install OpenBLAS


~$ git clone git://github.com/xianyi/OpenBLAS~$ cd OpenBLAS && make FC=gfortran~$ sudo make PREFIX=/opt/OpenBLAS install

2. 配置环境变量


sudo gedit ~/.bashrc

添加

export LD_LIBRARY_PATH=/opt/OpenBLAS/lib:$LD_LIBRARY_PATH

The LD_LIBRARY_PATH environment variable will be updated when you start a new terminal session

source ~/.bashrc

to force an update within the same session


sudo gedit  /etc/profile

添加

export LD_LIBRARY_PATH=/opt/OpenBLAS/lib:$LD_LIBRARY_PATH

生效

source /etc/profile

another option that will work for multiple users is to create a .conf file

~$ sudo -s "echo '/opt/OpenBLAS/lib' > /etc/ld.so.conf.d/openblas.conf"

生成链接

sudo ldconfig -v

3. 安装numpy

~$ git clone https://github.com/numpy/numpy~$ cd numpy

Copy site.cfg.example to site.cfg and edit the copy

~$ cp site.cfg.example site.cfg~$ nano site.cfg

Check configuration, build, install (optionally in a virutalenv)

~$ python setup.py config

Then just build and install:

~$ python setup.py build && python setup.py install


参考文献


Numpy/Scipy with Intel® MKL and Intel® Compilers

高性能Numpy/Scipy:使用Intel MKL和Intel Compilers

如何在 Linux* 中安装英特尔® 数据分析加速库(英特尔® DAAL)的 Python* 版本

caffe 上OpenBLAS的安装使用

安装OpenBlas(ubuntu 14.04)

OpenBLAS,Numpy,Scipy For Linux

安装线性计算库、numPy和sciPy的编译方法(推荐)

Python accelerated (using Intel® MKL)

Intel Distribution for Python 正式版windows

官网Intel® Distribution for Python

Numpy使用MKL库提升计算性能

0 0
原创粉丝点击