Cygwin 裡面已經有提供 OpenBLAS 與針對 OpenBLAS 編譯的 LAPACK 函式庫,可以用 CPU 處理多執行緒數值線性代數運算,以及針對這兩個東西編譯的 Python2/Python3 是值運算模組 Numpy。用這些就可以作數值運算了。
不過一般都會再裝 Scipy 和 Matplotlib 來用;要在 Cygwin 裡面用 Matplotlib 與 Scipy,就得用 setuptools 的 easy_install 或 pip 來裝;但是安裝過程有一些套件相依性要處理,也是有一點麻煩步驟的。
一些舊的資料提到可以使用 Tcl/Tk 作 Matplotlib 的互動顯示,不過在安裝的時候不斷遇到錯誤 ... 於是換成使用 Qt4/5,但是僅挑 PyQt5 的套件又不能用,要同時裝 PyQt4 才行
以下是目前整理出來,針對 Python3 的 Cygwin 套件。請直接用 Cygwin 安裝檔點選並進行安裝,這樣會一併連同相依套件一起挑出來;紅字部份是 Tcl/Tk 的部份,但是安裝後還是有問題,因此可以不用安裝。
編譯器、環境
|
gcc-fortran, gcc-g++, pkg-config
|
是值運算函式庫
|
必要:libopenblas, lapack-devel, libqhull-devel, libqrupdate-devel, libfftw3-devel, libcrypt-devel
選裝:libarpack-devel, libsuitesparseconfig-devel, libamd-devel, libcamd-devel, libccolamd-devel, libcholmod-devel, libcolamd-devel, libumfpack-devel
|
Python 環境
|
python3-devel, python3-pip, python3-numpy3, python3-pexpect, python3-six, python3-pyparsing, python3-tkinter, python3-pyqt4, python3-pyqt5
|
Matplotlib 相依的圖形相關函式庫
|
libfontconfig-devel, libpng-devel, libfreetype-devel, tcl-tk, tcl-itk
|
顯示圖案的 X-Window 系統
|
xorg-server, xinit, xlaunch
|
裝完之後,用下面指令使用 pip 安裝
user@localhost ~ $ pip3 -v install matplotlib user@localhost ~ $ pip3 -v install scipy
Matplotlib 編譯過程中跳出來的的相依性檢查
============================================================================ Edit setup.cfg to change the build options BUILDING MATPLOTLIB matplotlib: yes [2.0.2] python: yes [3.6.1 (default, Mar 21 2017, 21:49:16) [GCC 5.4.0]] platform: yes [cygwin] REQUIRED DEPENDENCIES AND EXTENSIONS numpy: yes [version 1.11.2] six: yes [using six version 1.10.0] dateutil: yes [using dateutil version 2.6.0] functools32: yes [Not required] subprocess32: yes [Not required] pytz: yes [using pytz version 2017.2] cycler: yes [using cycler version 0.10.0] tornado: yes [tornado was not found. It is required for the WebAgg backend. pip/easy_install may attempt to install it after matplotlib.] pyparsing: yes [using pyparsing version 2.1.10] libagg: yes [pkg-config information for 'libagg' could not be found. Using local copy.] freetype: yes [version 2.6.5] png: yes [version 1.6.28] qhull: yes [pkg-config information for 'qhull' could not be found. Using local copy.] OPTIONAL SUBPACKAGES sample_data: yes [installing] toolkits: yes [installing] tests: no [skipping due to configuration] toolkits_tests: no [skipping due to configuration] OPTIONAL BACKEND EXTENSIONS macosx: no [Mac OS-X only] qt5agg: yes [installing, Qt: 5.6.2, PyQt: 5.6.2] qt4agg: yes [installing, Qt: 4.8.7, PyQt: 4.8.7; PySide not found] gtk3agg: no [Requires gtk3 development files to be installed.] gtk3cairo: no [Requires gtk3 development files to be installed.] gtkagg: no [Requires pygtk] tkagg: yes [installing; run-time loading from Python Tcl / Tk] wxagg: no [requires wxPython] gtk: no [Requires pygtk] agg: yes [installing] cairo: yes [installing, pycairo version 1.12.0] windowing: no [Microsoft Windows only] OPTIONAL LATEX DEPENDENCIES dvipng: no ghostscript: no latex: no pdftops: no OPTIONAL PACKAGE DATA dlls: no [skipping due to configuration] ============================================================================
Scipy 的部份就沒有截到有關資訊就不列了。
接著是各部份的安裝簡單檢查~
先來看 Numpy 安裝資訊。以下所顯示的資料提到,這個安裝套件「沒有」用 OpenBLAS?
user@localhost ~ $ python3 Python 3.6.1 (default, Mar 21 2017, 21:49:16) [GCC 5.4.0] on cygwin Type "help", "copyright", "credits" or "license" for more information. >>> import numpy >>> numpy.__config__.show() blas_mkl_info: NOT AVAILABLE openblas_info: NOT AVAILABLE atlas_3_10_blas_threads_info: NOT AVAILABLE atlas_3_10_blas_info: NOT AVAILABLE atlas_blas_threads_info: NOT AVAILABLE atlas_blas_info: NOT AVAILABLE blas_info: libraries = ['blas', 'blas'] library_dirs = ['/usr/lib'] language = c define_macros = [('HAVE_CBLAS', None)] blas_opt_info: define_macros = [('NO_ATLAS_INFO', 1), ('HAVE_CBLAS', None)] libraries = ['blas', 'blas'] library_dirs = ['/usr/lib'] language = c openblas_lapack_info: NOT AVAILABLE lapack_mkl_info: NOT AVAILABLE atlas_3_10_threads_info: NOT AVAILABLE atlas_3_10_info: NOT AVAILABLE atlas_threads_info: NOT AVAILABLE atlas_info: NOT AVAILABLE lapack_info: libraries = ['lapack', 'lapack'] library_dirs = ['/usr/lib'] language = f77 lapack_opt_info: define_macros = [('NO_ATLAS_INFO', 1), ('HAVE_CBLAS', None)] libraries = ['lapack', 'lapack', 'blas', 'blas'] library_dirs = ['/usr/lib'] language = c >>> quit()
有點奇怪,跑一個矩陣內積運算看看,確認有沒有多執行緒進行
import numpy as np size=10000 a = np.random.random_sample((size,size)) b = np.random.random_sample((size,size)) np.dot(a,b)
看工作管理員,4 個 CPU Core 都有在跑,所以的確有使用 OpenBLAS(如果使用 Reference BLAS 的話,就只會看到一個 CPU Core 在忙而已~)
(本圖不純砍頭!!~)
接著檢查 Matplotlib 安裝。以下指令沒有跳出怎樣的 Error 就行了
user@localhost ~ $ python3 Python 3.6.1 (default, Mar 21 2017, 21:49:16) [GCC 5.4.0] on cygwin Type "help", "copyright", "credits" or "license" for more information. >>> import matplotlib >>> print(matplotlib.get_backend()) Qt5Agg >>> import matplotlib.pyplot as plt >>>
如果要即時繪圖,則需要「啟動」X Window 程序,用裡面的 Virtual Terminal 才行...
Cygwin 的 X Window 是一個背景程序,啟動後會在工作列有相關按鈕,從那裡啟用 XTerm 來跑下面從 Matplotlib 借來的範例,的確會跳出圖案
import matplotlib.pyplot as plt import numpy as np t = np.arange(0.0, 2.0, 0.01) s = 1 + np.sin(2*np.pi*t) plt.plot(t, s) plt.xlabel('time (s)') plt.ylabel('voltage (mV)') plt.title('Sine Wave') plt.grid(True) plt.show()
本圖絕對不是網頁摳來的~~
至於 Scipy 的檢查就和 numpy 差不多,請各位找方法自行檢查
user@localhost ~ $ python3 Python 3.6.1 (default, Mar 21 2017, 21:49:16) [GCC 5.4.0] on cygwin Type "help", "copyright", "credits" or "license" for more information. >>> import scipy >>> scipy.__config__.show() openblas_lapack_info: NOT AVAILABLE lapack_mkl_info: NOT AVAILABLE atlas_3_10_threads_info: NOT AVAILABLE atlas_3_10_info: NOT AVAILABLE atlas_threads_info: NOT AVAILABLE atlas_info: NOT AVAILABLE lapack_info: libraries = ['lapack', 'lapack'] library_dirs = ['/usr/lib'] language = f77 blas_info: libraries = ['blas', 'blas'] library_dirs = ['/usr/lib'] language = c define_macros = [('HAVE_CBLAS', None)] lapack_opt_info: define_macros = [('NO_ATLAS_INFO', 1), ('HAVE_CBLAS', None)] libraries = ['lapack', 'lapack', 'blas', 'blas'] library_dirs = ['/usr/lib'] language = c blas_mkl_info: NOT AVAILABLE openblas_info: NOT AVAILABLE atlas_3_10_blas_threads_info: NOT AVAILABLE atlas_3_10_blas_info: NOT AVAILABLE atlas_blas_threads_info: NOT AVAILABLE atlas_blas_info: NOT AVAILABLE blas_opt_info: define_macros = [('NO_ATLAS_INFO', 1), ('HAVE_CBLAS', None)] libraries = ['blas', 'blas'] library_dirs = ['/usr/lib'] language = c >>> quit()
上面這樣就安裝完成,可以使用了。如果只是用原生的 Python 發行版,就能從下面網站抓套件 Python Extension Packages for Windows - Christoph Gohlke,裡面的 BLAS 用的是 Intel MKL。
更流行的方式,就是用 Anaconda 或是 Python(X,Y) 等 Python 發行版(這裡有列清單:Installing the SciPy Stack — SciPy.org),安裝就更簡單了。
雖然數值線性代數運算模組除了 OpenBLAS 之外還有 Intel Math Kernel Library,甚至最近好像很流行的 GPU 運算套件(OpenCL 還是 CUDA 還是??等等),不過對於一般個人使用,OpenBLAS 的速度也是很不錯的。只要不是 Reference BLAS(Netlib BLAS)或是 ATLAS 就都很好處理~~
參考資料:
- 安裝部份
- 過程中可能會遇到的問題
gcc: error:spawn: No such file or directory :發生原因是 g++ 沒有裝到
其他可能問題
- 確認功能
沒有留言:
張貼留言