def test_svd_ss_float64(self): a = np.asarray(np.random.randn(9, 6), np.float64) a_gpu = gpuarray.to_gpu(a) u_gpu, s_gpu, vh_gpu = linalg.svd(a_gpu, 's', 's') assert np.allclose(a, np.dot(u_gpu.get(), np.dot(np.diag(s_gpu.get()), vh_gpu.get())), atol=atol_float64)
def test_svd_so_complex128(self): a = np.asarray(np.random.randn(6, 6) + 1j*np.random.randn(6, 6), np.complex128) a_gpu = gpuarray.to_gpu(a) u_gpu, s_gpu, vh_gpu = linalg.svd(a_gpu, 's', 'o') assert np.allclose(a, np.dot(u_gpu.get(), np.dot(np.diag(s_gpu.get()), vh_gpu.get())), atol=atol_float64)
def test_svd_aa_cusolver_float32(self): a = np.asarray(np.random.randn(6, 6), np.float32) a_gpu = gpuarray.to_gpu(a) u_gpu, s_gpu, vh_gpu = linalg.svd(a_gpu, lib='cusolver') assert np.allclose(a, np.dot(u_gpu.get(), np.dot(np.diag(s_gpu.get()), vh_gpu.get())), atol=atol_float32)
def test_svd_so_complex128(self): a = np.asarray( np.random.randn(6, 6) + 1j * np.random.randn(6, 6), np.complex128) a_gpu = gpuarray.to_gpu(a) u_gpu, s_gpu, vh_gpu = linalg.svd(a_gpu, 's', 'o') assert np.allclose(a, np.dot(u_gpu.get(), np.dot(np.diag(s_gpu.get()), vh_gpu.get())), atol=atol_float64)
def svd_skcuda(X, typ): """ Singular Value Decomposition with scikit-cuda and CULA library Usage: U_gpu, S, Vt_gpu = svd_skcuda(X, typ) Input: X matrix to decompose (array) typ data type (string) CULA free only support typ = float32 and complex64 Output: U_gpu left unitary matrix on GPU (array) S singular values on CPU (vector) Vt_gpu transposed right unitary matrix on GPU (array) """ X = X.astype(typ) X_gpu = gpuarray.to_gpu(X) # send data to gpu U_gpu, S_gpu, Vt_gpu = cu_linalg.svd( X_gpu, jobu='S', jobvt='S', lib='cula') # small U and Vt matrices S = S_gpu.get() X_gpu.gpudata.free() # release gpu memory S_gpu.gpudata.free() return U_gpu, S, Vt_gpu
""" import pycuda.autoinit import pycuda.driver as drv import pycuda.gpuarray as gpuarray import numpy as np import skcuda.linalg as culinalg import skcuda.misc as cumisc culinalg.init() # Double precision is only supported by devices with compute # capability >= 1.3: import string import scikits.cuda.cula as cula demo_types = [np.float32, np.complex64] if cula._libcula_toolkit == 'premium' and \ cumisc.get_compute_capability(pycuda.autoinit.device) >= 1.3: demo_types.extend([np.float64, np.complex128]) for t in demo_types: print 'Testing svd for type ' + str(np.dtype(t)) a = np.asarray((np.random.rand(50, 50) - 0.5) / 10, t) a_gpu = gpuarray.to_gpu(a) u_gpu, s_gpu, vh_gpu = culinalg.svd(a_gpu) a_rec = np.dot(u_gpu.get(), np.dot(np.diag(s_gpu.get()), vh_gpu.get())) print 'Success status: ', np.allclose(a, a_rec, atol=1e-3) print 'Maximum error: ', np.max(np.abs(a - a_rec)) print ''
def gpu_skcuda_linalg_svd(input): input_gpu = gpuarray.to_gpu(input) linalg.init() u_gpu, s_gpu, vh_gpu = linalg.svd(input_gpu, 'A', 'A', 'cusolver')
exit() """ Using svd to factorize the copa_matrix into cause/effect vector representations """ epsilon = 1e-3 data = json.loads(open('copa_matrix.json').read()) arr = np.asarray(np.array(data), np.float32) arr[np.diag(np.diag(arr) < epsilon)] = 0.0 #print arr.shape[0] #print np.identity(arr.shape[0]) #print gpuarray.to_gpu(arr) a_gpu = gpuarray.to_gpu(arr + epsilon*np.identity(arr.shape[0])) #u_gpu, s_gpu, vh_gpu = culinalg.svd(a_gpu) u_gpu, s_gpu, vh_gpu = culinalg.svd(a_gpu, jobu='S', jobvt='S') print u_gpu.shape print vh_gpu.shape print type(u_gpu.get()) with open('u.json','wb') as outf: outf.write(json.dumps(u_gpu.get().tolist())) with open('v.json','wb') as outf: outf.write(json.dumps(vh_gpu.get().tolist()))
# This example demonstrates simple SVD-computation using CUDA-Solver library. import pycuda.autoinit from pycuda import gpuarray import numpy as np from skcuda import linalg a = np.random.rand(1000, 5000).astype(np.float32) a_gpu = gpuarray.to_gpu(a) U_d, s_d, V_d = linalg.svd(a_gpu, lib='cusolver') U = U_d.get() s = s_d.get() V = V_d.get() S = np.zeros((1000, 5000)) S[:1000, :1000] = np.diag(s) print('Can we reconstruct a from its SVD decomposition? : {}'.format( np.allclose(a, np.dot(U, np.dot(S, V), atol=1e-5))))
from __future__ import print_function import pycuda.autoinit import pycuda.driver as drv import pycuda.gpuarray as gpuarray import numpy as np import skcuda.linalg as culinalg import skcuda.misc as cumisc culinalg.init() # Double precision is only supported by devices with compute # capability >= 1.3: import string import scikits.cuda.cula as cula demo_types = [np.float32, np.complex64] if cula._libcula_toolkit == 'premium' and \ cumisc.get_compute_capability(pycuda.autoinit.device) >= 1.3: demo_types.extend([np.float64, np.complex128]) for t in demo_types: print('Testing svd for type ' + str(np.dtype(t))) a = np.asarray((np.random.rand(50, 50) - 0.5) / 10, t) a_gpu = gpuarray.to_gpu(a) u_gpu, s_gpu, vh_gpu = culinalg.svd(a_gpu) a_rec = np.dot(u_gpu.get(), np.dot(np.diag(s_gpu.get()), vh_gpu.get())) print('Success status: ', np.allclose(a, a_rec, atol=1e-3)) print('Maximum error: ', np.max(np.abs(a - a_rec))) print('')