标签:python lapack altas numpy install
上篇笔记介绍了不依赖lapack和atlas库的NumPy包源码编译/安装方法,但“纯净版”的NumPy会损失性能,故本篇笔记说明如何源码编译安装依赖lapack和atlas库的NumPy包。
1. GCC版本要求
使用较新版本的GCC工具集(尽量不低于v4.7)且集成有gfortran编译器。
备注1:这里大写的"GCC"是指GNU Compiler Collection,它除包含C语言编译器gcc外,还包含很多其它语言的编译器(如g++/gfortran等)
备注2:3.x版的的C语言编译器gcc会由于某些头文件缺失导致编译atlas库报错
备注3:若GCC工具集中没有gfortran编译器,则编译lapack库时会遇到一些莫名其妙的错误(因为lapack是用fortran编写的),好在GCC4.7及以上版本中已经集成了gfortran编译器
在GCC版本符合要求的前提下,临时将其加入环境变量PATH并设置动态库查找路径:
$ export PATH=/home/slvher/tools/gcc48/bin/:$PATH $ export LD_LIBRARY_PATH=/home/slvher/tools/gcc48/lib64:/home/slvher/tools/gcc48/lib备注4:在当前shell会话中临时设置LD_LIBRARY_PATH可以保证编译过程中正确搜索到GCC库,但最好不要设置到.bash_profile中,因为那样会影响其它程序的查找路径,可能会踩到坑。
$ ../configure --shared -b 64 --prefix=/home/slvher/tools/scikit-learn-virtualenv/dep-libs/sklearn-libs --with-netlib-lapack-tarfile=/home/slvher/tools/scikit-learn-virtualenv/dep-libs/lapack-3.5.0.tgz其中,--shared表明要编译atlas共享库(configure会自动在编译命令中插入"-fPIC"参数,无需在这里显式指定);--prefix指定编译结果的安装路径;--with-netlib-lapack-tarfile表明编译atlas库时会用相同的编译器及编译/链接参数自动编译lapack库,这里指定lapack源码包的路径后,configure运行后会自动解压lapack源码并将其拷贝至BLDdir/src/lapack/reference/这个目录下。
3. 编译优化版NumPy包
前提:官网下载NumPy源码包并解压,这里以目前最新版numpy-1.9.2.tar.gz为例进行说明。
1) cd至解压目录numpy-1.9.2
2) cp site.cfg.example site.cfg
3) 在site.cfg中配置atlas项,其中include_dirs和library_dirs是atlas库安装路径下的include和lib目录
[atlas] atlas_libs = lapack,f77blas,cblas,atlas library_dirs = /home/slvher/tools/scikit-learn-virtualenv/dep-libs/sklearn-libs/lib include_dirs = /home/slvher/tools/scikit-learn-virtualenv/dep-libs/sklearn-libs/include4) python setup.py config
>>> import numpy as np >>> np.__config__.show() atlas_3_10_blas_threads_info: libraries = ['lapack', 'f77blas', 'cblas', 'atlas'] library_dirs = ['/home/slvher/tools/scikit-learn-virtualenv/dep-libs/sklearn-libs/lib'] define_macros = [('HAVE_CBLAS', None), ('ATLAS_INFO', '"\\"3.10.2\\""')] language = c include_dirs = ['/home/slvher/tools/scikit-learn-virtualenv/dep-libs/sklearn-libs/include'] lapack_opt_info: libraries = ['tatlas', 'lapack', 'f77blas', 'cblas', 'atlas'] library_dirs = ['/home/slvher/tools/scikit-learn-virtualenv/dep-libs/sklearn-libs/lib'] define_macros = [('ATLAS_INFO', '"\\"3.10.2\\""')] language = f77 include_dirs = ['/home/slvher/tools/scikit-learn-virtualenv/dep-libs/sklearn-libs/include'] blas_opt_info: libraries = ['lapack', 'f77blas', 'cblas', 'atlas'] library_dirs = ['/home/slvher/tools/scikit-learn-virtualenv/dep-libs/sklearn-libs/lib'] define_macros = [('HAVE_CBLAS', None), ('ATLAS_INFO', '"\\"3.10.2\\""')] language = c include_dirs = ['/home/slvher/tools/scikit-learn-virtualenv/dep-libs/sklearn-libs/include'] openblas_info: NOT AVAILABLE openblas_lapack_info: NOT AVAILABLE atlas_3_10_threads_info: libraries = ['tatlas', 'lapack', 'f77blas', 'cblas', 'atlas'] library_dirs = ['/home/slvher/tools/scikit-learn-virtualenv/dep-libs/sklearn-libs/lib'] define_macros = [('ATLAS_INFO', '"\\"3.10.2\\""')] language = f77 include_dirs = ['/home/slvher/tools/scikit-learn-virtualenv/dep-libs/sklearn-libs/include'] lapack_mkl_info: NOT AVAILABLE blas_mkl_info: NOT AVAILABLE mkl_info: NOT AVAILABLE也可以用具体的例子来验证其功能是否正常:
>>> import numpy as np >>> np.arange(15).reshape(3, 5) array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]]) >>> >>> a = np.arange(15).reshape(3, 5) >>> a array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]]) >>> type(a) <type 'numpy.ndarray'> >>> >>> >>> from numpy.linalg import * >>> b = np.array([[1.0, 2.0], [3.0, 4.0]]) >>> b array([[ 1., 2.], [ 3., 4.]]) >>> b.transpose() array([[ 1., 3.], [ 2., 4.]]) >>> inv(b) array([[-2. , 1. ], [ 1.5, -0.5]]) >>>【参考资料】
======================= EOF ======================
【Python笔记】如何源码编译依赖LAPACK和ATLAS库的NumPy包
标签:python lapack altas numpy install
原文地址:http://blog.csdn.net/slvher/article/details/44853721