OpenBLAS,Numpy,Scipy For Linux

来源:互联网 发布:淘宝客定向计划取名 编辑:程序博客网 时间:2024/05/19 14:18

安装过程有点繁琐,但是如果想要成功安装,只有耐心把步骤走完,肯定能安装成功的,因人而异,出现其他问题,可以随时搜索,立即解决,都是些繁琐的小细节问题了。

Install OpenBLAS
  1. Compile OpenBLAS:

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

    If you don't have sudo rights you could set PREFIX= to a directory you have write privileges to (just modify the corresponding steps below accordingly).

  2. Make sure that the directory containing libopenblas.so is in your shared library search path.

    • To do this locally, you could edit your ~/.bashrc file to contain the line

      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 (use $ source ~/.bashrc to force an update within the same session).

    • you can also set the path in your /etc/profile to contain the bellow line:

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

    • remember to use the command: source /etc/profile
    • Another option that will work for multiple users is to create a .conf file in /etc/ld.so.conf.d/ containing the line /opt/OpenBLAS/lib, e.g.:

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

    Once you are done with either option, run

    ~$ sudo ldconfig
  3. Install Numpy
  4. Grab the numpy source code:

    ~$ git clone https://github.com/numpy/numpy~$ cd numpy
  5. Copy site.cfg.example to site.cfg and edit the copy:

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

    Uncomment these lines:

    ....[openblas]libraries = openblaslibrary_dirs = /opt/OpenBLAS/libinclude_dirs = /opt/OpenBLAS/include....
  6. Check configuration, build, install (optionally in a virutalenv)

    ~$ python setup.py config

    The output should look something like this:

    ...openblas_info:  FOUND:    libraries = ['openblas', 'openblas']    library_dirs = ['/opt/OpenBLAS/lib']    language = c    define_macros = [('HAVE_CBLAS', None)]  FOUND:    libraries = ['openblas', 'openblas']    library_dirs = ['/opt/OpenBLAS/lib']    language = c    define_macros = [('HAVE_CBLAS', None)]...

    Then just build and install:

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

Install Scipy

将numpy使用的更改过的site.cfg文件拷贝到scipy根目录下,执行相同的编译和安装过程即可。


如果使用低版本的numpy,可以参考

OpenBLAS

git clone git://github.com/xianyi/OpenBLAS

Change into the new directory, then

make FC=gfortransudo make PREFIX=/usr/local/ install

The latest lapack release is downloaded, compiled and integreated into OpenBLAS automatically. Ready for the python part!

Numpy

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

Usually one is interested in OpenBLAS because of its fast matrix-matrix multiplication. Whether numpy has a fast dot function is indicated by the presence of core/_dotblas.so. However, currently (June 2013) this file is only build, if site.cfg has an [atlas] section(also see here and here). There is an interesting thread here with a link to this fix in order to add support for using OpenBLAS for the _dotblas function. However, right now dovi site.cfg and:

[default]library_dirs = /usr/local/lib[atlas]atlas_libs = openblaslibrary_dirs = /usr/local/lib[lapack]lapack_libs = openblaslibrary_dirs = /usr/local/lib

Without the [lapack] section there were problems with installing scipy later on. Additionally:

export BLAS=/usr/local/lib/libopenblas.aexport LAPACK=/usr/local/lib/libopenblas.aexport LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/

Check the detected configuration with python setup.py config. You should see severalATLAS_INFO paired with None. If there is something like 3.8.2 instead of None then you have an ATLAS install somewhere on your system and numpy won't use OpenBLAS. Get rid of this install (e.g. via apt-get purge) and rerun the config command. If ATLAS_INFO is completely missing then numpy couldn't find your libopenblas* libraries — you need to check all previous steps again. Now:

python setup.py build

If you look into build/numpy/core, the file _dotblas.so should be available.

sudo python setup.py install

installs a system wide numpy that uses OpenBLAS for dot products. This test produces

FAST BLASversion: 1.8.0.dev-3f10c36maxint: 9223372036854775807dot: 0.162246799469

You can change the number of cores OpenBLAS utilizes via OPENBLAS_NUM_THREADS, e.g.export OPENBLAS_NUM_THREADS=2 results in

 :::bashFAST BLASversion: 1.8.0.dev-3f10c36maxint: 9223372036854775807dot: 0.0949754238129 sec

Scipy

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

Scipy can be installed without any workaround:

python setup.py buildsudo python setup.py install

Make sure that in your sudo command the variables BLASLAPACK and LD_LIBRARY_PATH are correctly set, as shown above! This test script (it has some room for improvement, though ...) works on my machine after the system wide install:

cholesky: 0.080588388443 secsvd: 1.13443040848 sec

Finally, if you feel good enough (and do a pip install nose), you can run nose tests via

python -c "import numpy; numpy.test(verbose=2)"python -c "import scipy; scipy.test(verbose=2)"


祝你成功。。。

0 0
原创粉丝点击