Skip to content
Snippets Groups Projects
Commit f0288e9f authored by Denis Arrivault's avatar Denis Arrivault
Browse files

Upadte cuda tests

parent ecba9ede
Branches
Tags
No related merge requests found
Pipeline #
...@@ -10,19 +10,33 @@ from timeit import default_timer as timer ...@@ -10,19 +10,33 @@ from timeit import default_timer as timer
import numpy as np import numpy as np
def test_svd(): def test_svd():
d = 100 d = 1000
A = np.asarray(np.random.randint(1, 10,(d, d)), dtype=np.float64) A = np.asarray(np.random.randint(1, 10,(d, d)), dtype=np.float32)
# scipy # scipy
print("\n****************** Scipy") print("\n****************** Scipy")
from scipy import linalg as sc_linalg from scipy.linalg import svd as sc_svd
start = timer() start = timer()
u_sc, s_sc, v_sc = sc_linalg.svd(A) u_sc, s_sc, v_sc = sc_svd(A)
duration = timer() - start duration = timer() - start
print("SVD scipy : " + str(duration)) print("SVD scipy : " + str(duration))
print(s_sc) print(s_sc)
print(type(s_sc)) print(type(s_sc))
# cupy
print("\n****************** Cupy")
import cupy as cp
from cupy.linalg import svd as cp_svd
start = timer()
A_cp = cp.asarray(A)
u_cp, s_cp, v_cp = cp_svd(A_cp)
u_cp, s_cp, v_cp = u_cp.get(), s_cp.get(), v_cp.get()
duration = timer() - start
print("SVD cupy : " + str(duration))
print(s_cp)
print(A_cp.device)
print(type(s_cp))
# scikit-cuda # scikit-cuda
print("\n****************** scikit-cuda") print("\n****************** scikit-cuda")
import pycuda.autoinit import pycuda.autoinit
...@@ -40,30 +54,44 @@ def test_svd(): ...@@ -40,30 +54,44 @@ def test_svd():
print(s_sk) print(s_sk)
print(type(s_sk)) print(type(s_sk))
# tensorflow # # tensorflow
print("\n****************** tensorflow") # print("\n****************** tensorflow")
import tensorflow as tf # import tensorflow as tf
from tensorflow import linalg as tf_linalg # start = timer()
start = timer() # with tf.device("/cpu:0"):
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess: # s_tf, u_tf, v_tf = tf.svd(A, compute_uv=True)
s_tf, u_tf, v_tf = tf_linalg.svd(A) # s_tf, u_tf, v_tf = tf.Session().run(s_tf), tf.Session().run(u_tf), tf.Session().run(v_tf)
s_tf, u_tf, v_tf = s_tf.eval(), u_tf.eval(), v_tf.eval() # duration = timer() - start
duration = timer() - start # print("SVD tensorflow CPU : " + str(duration))
print("SVD tensorflow : " + str(duration)) # print(s_tf)
print(s_tf) # print(type(s_tf))
print(type(s_tf)) # print()
# start = timer()
# with tf.device("/gpu:0"):
# s_tf, u_tf, v_tf = tf.svd(A, compute_uv=True)
# s_tf, u_tf, v_tf = tf.Session().run(s_tf), tf.Session().run(u_tf), tf.Session().run(v_tf)
# duration = timer() - start
# print("SVD tensorflow GPU : " + str(duration))
# print(s_tf)
# print(type(s_tf))
# Theano # Theano
print("\n****************** theano") print("\n****************** theano")
import theano.tensor as T from theano import function, shared, tensor, config
from theano import function
import theano.tensor.nlinalg as th_linalg import theano.tensor.nlinalg as th_linalg
x = T.dmatrix('x') x = tensor.dmatrix('x')
u, s, v = th_linalg.svd(x) u, s, v = th_linalg.svd(x)
f = function([x], (u, s, v)) f = function([x], (u, s, v))
start = timer() start = timer()
u_th, s_th, v_th = f(A) u_th, s_th, v_th = f(A)
duration = timer() - start duration = timer() - start
# x = shared(np.asarray(A, config.floatX))
# f = function([], th_linalg.svd(x))
# start = timer()
# u_th, s_th, v_th = f()
# duration = timer() - start
print("SVD theano : " + str(duration)) print("SVD theano : " + str(duration))
print(s_th) print(s_th)
print(type(s_th)) print(type(s_th))
......
...@@ -363,8 +363,17 @@ class Hankel(object): ...@@ -363,8 +363,17 @@ class Hankel(object):
"the smaller dimension of the Hankel Matrix (" + "the smaller dimension of the Hankel Matrix (" +
str(matrix_shape) + ")") str(matrix_shape) + ")")
if not self.sparse: if not self.sparse:
try:
import theano.tensor as T
from theano import function
import theano.tensor.nlinalg as th_linalg
x = T.dmatrix('x')
u, s, v = th_linalg.svd(x)
svd = function([x], (u, s, v))
except :
from scipy.linalg import svd
hankel = self.lhankel[0] hankel = self.lhankel[0]
[u, s, v] = np.linalg.svd(hankel) [u, s, v] = svd(hankel)
u = u[:, :rank] u = u[:, :rank]
v = v[:rank, :] v = v[:rank, :]
# ds = np.zeros((rank, rank), dtype=complex) # ds = np.zeros((rank, rank), dtype=complex)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment