I want to compute the singular value decomposition of each slice of a 3D matrix.
I used numpy and scipy to compute the SVD, but both of them are significantly slower than the MATLAB implementation. While the numpy and scipy versions take around 7 s, the MATLAB version takes 0.7 s only.
Is there a way to accelerate the SVD computation in Python?
Python
import time
import scipy.linalg
import numpy.linalg
A = np.random.rand(100, 100, 1000) + 1j * np.random.rand(100, 100, 1000)
S = np.empty((A.shape[2], min(A.shape[0:1])))
t1 = time.time()
for i in range(A.shape[2]):
S[i, :] = numpy.linalg.svd(A[:, :, i], compute_uv=False)
print("[numpy] Elapsed time: {:.3f} s".format(time.time() - t1))
t1 = time.time()
for i in range(A.shape[2]):
S[i, :] = scipy.linalg.svdvals(A[:, :, i])
print("[scipy] Elapsed time: {:.3f} s".format(time.time() - t1))
# [numpy] Elapsed time: 7.137 s
# [scipy] Elapsed time: 7.435 s
MATLAB
A = randn(100, 100, 1000) + 1j * randn(100, 100, 1000);
S = nan(size(A,3), min(size(A, [1 2])));
tic;
for i = 1:size(A, 3)
S(i, :) = svd(A(:,:,i));
end
toc;
% Elapsed time is 0.702556 seconds.
This is the output of np.show_config()
:
blas_mkl_info:
NOT AVAILABLE
blis_info:
NOT AVAILABLE
openblas_info:
library_dirs = ['D:\\a\\1\\s\\numpy\\build\\openblas_info']
libraries = ['openblas_info']
language = f77
define_macros = [('HAVE_CBLAS', None)]
blas_opt_info:
library_dirs = ['D:\\a\\1\\s\\numpy\\build\\openblas_info']
libraries = ['openblas_info']
language = f77
define_macros = [('HAVE_CBLAS', None)]
lapack_mkl_info:
NOT AVAILABLE
openblas_lapack_info:
library_dirs = ['D:\\a\\1\\s\\numpy\\build\\openblas_lapack_info']
libraries = ['openblas_lapack_info']
language = f77
define_macros = [('HAVE_CBLAS', None)]
lapack_opt_info:
library_dirs = ['D:\\a\\1\\s\\numpy\\build\\openblas_lapack_info']
libraries = ['openblas_lapack_info']
language = f77
define_macros = [('HAVE_CBLAS', None)]
Supported SIMD extensions in this NumPy install:
baseline = SSE,SSE2,SSE3
found = SSSE3,SSE41,POPCNT,SSE42,AVX,F16C,FMA3,AVX2,AVX512F,AVX512CD,AVX512_SKX
not found = AVX512_CLX,AVX512_CNL
None