ubuntuまたはmintを使用している場合に備えて、apt-get asを介してnumpyとopenblasの両方をインストールすることにより、openblasを簡単にnumpyにリンクできます
sudo apt-get install numpy libopenblas-dev
新しいdocker ubuntuで、ブログ投稿「Installing Numpy and OpenBLAS」からコピーした次のスクリプトをテストしました
import numpy as np
import numpy.random as npr
import time
# --- Test 1
N = 1
n = 1000
A = npr.randn(n,n)
B = npr.randn(n,n)
t = time.time()
for i in range(N):
C = np.dot(A, B)
td = time.time() - t
print("dotted two (%d,%d) matrices in %0.1f ms" % (n, n, 1e3*td/N))
# --- Test 2
N = 100
n = 4000
A = npr.randn(n)
B = npr.randn(n)
t = time.time()
for i in range(N):
C = np.dot(A, B)
td = time.time() - t
print("dotted two (%d) vectors in %0.2f us" % (n, 1e6*td/N))
# --- Test 3
m,n = (2000,1000)
A = npr.randn(m,n)
t = time.time()
[U,s,V] = np.linalg.svd(A, full_matrices=False)
td = time.time() - t
print("SVD of (%d,%d) matrix in %0.3f s" % (m, n, td))
# --- Test 4
n = 1500
A = npr.randn(n,n)
t = time.time()
w, v = np.linalg.eig(A)
td = time.time() - t
print("Eigendecomp of (%d,%d) matrix in %0.3f s" % (n, n, td))
openblas がない場合、結果は次のようになります。
dotted two (1000,1000) matrices in 563.8 ms
dotted two (4000) vectors in 5.16 us
SVD of (2000,1000) matrix in 6.084 s
Eigendecomp of (1500,1500) matrix in 14.605 s
でopenblasをインストールした後apt install openblas-dev
、numpyリンケージを確認しました
import numpy as np
np.__config__.show()
そして情報は
atlas_threads_info:
NOT AVAILABLE
openblas_info:
NOT AVAILABLE
atlas_blas_info:
NOT AVAILABLE
atlas_3_10_threads_info:
NOT AVAILABLE
blas_info:
library_dirs = ['/usr/lib']
libraries = ['blas', 'blas']
language = c
define_macros = [('HAVE_CBLAS', None)]
mkl_info:
NOT AVAILABLE
atlas_3_10_blas_threads_info:
NOT AVAILABLE
atlas_3_10_blas_info:
NOT AVAILABLE
openblas_lapack_info:
NOT AVAILABLE
lapack_opt_info:
library_dirs = ['/usr/lib']
libraries = ['lapack', 'lapack', 'blas', 'blas']
language = c
define_macros = [('NO_ATLAS_INFO', 1), ('HAVE_CBLAS', None)]
blas_opt_info:
library_dirs = ['/usr/lib']
libraries = ['blas', 'blas']
language = c
define_macros = [('NO_ATLAS_INFO', 1), ('HAVE_CBLAS', None)]
atlas_info:
NOT AVAILABLE
blas_mkl_info:
NOT AVAILABLE
lapack_mkl_info:
NOT AVAILABLE
atlas_3_10_info:
NOT AVAILABLE
lapack_info:
library_dirs = ['/usr/lib']
libraries = ['lapack', 'lapack']
language = f77
atlas_blas_threads_info:
NOT AVAILABLE
openblas へのリンクは表示されません。ただし、スクリプトの新しい結果は、numpy が openblas を使用したに違いないことを示しています。
dotted two (1000,1000) matrices in 15.2 ms
dotted two (4000) vectors in 2.64 us
SVD of (2000,1000) matrix in 0.469 s
Eigendecomp of (1500,1500) matrix in 2.794 s