From ecba9ede19d1ae75004f1bca07dd8c2314696210 Mon Sep 17 00:00:00 2001 From: Denis Arrivault <denis.arrivault@lif.univ-mrs.fr> Date: Thu, 1 Mar 2018 14:17:25 +0100 Subject: [PATCH] Svd tests for gpu --- examples/performances_calculation.py | 9 ++-- examples/svd_tests.py | 73 ++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 examples/svd_tests.py diff --git a/examples/performances_calculation.py b/examples/performances_calculation.py index 0c2987e..771fe0b 100644 --- a/examples/performances_calculation.py +++ b/examples/performances_calculation.py @@ -5,14 +5,13 @@ Created on 20 févr. 2018 @author: arrivault ''' -import sys from timeit import default_timer as timer from splearn import Spectral from splearn.tests.datasets.get_dataset_path import get_dataset_path from splearn.datasets.base import load_data_sample -def launch(X, version='classic', partial=True, sparse=True, smooth_method='none'): +def launch_fit(X, version='classic', partial=True, sparse=True, smooth_method='none'): param = "****** {:s} - partial = {:b} - sparse = {:b}, {:s} *****".format(version, partial, sparse, smooth_method) print(param) @@ -33,7 +32,7 @@ def launch(X, version='classic', partial=True, sparse=True, smooth_method='none' # else: # print("The result is different", file=sys.stderr) -def test(): +def test_fit(): adr = get_dataset_path("3.pautomac_light.train") data = load_data_sample(adr=adr) X = data.data @@ -43,7 +42,7 @@ def test(): # for partial in param['partial']: for sparse in param['sparse']: for smooth_method in param['smooth_method']: - launch(X, version=version, partial=True, sparse=sparse, smooth_method=smooth_method) + launch_fit(X, version=version, partial=True, sparse=sparse, smooth_method=smooth_method) if __name__ == '__main__': - test() + test_fit() diff --git a/examples/svd_tests.py b/examples/svd_tests.py new file mode 100644 index 0000000..6fca06d --- /dev/null +++ b/examples/svd_tests.py @@ -0,0 +1,73 @@ +# -*- coding: utf-8 -*- + +''' +Created on 20 févr. 2018 + +@author: arrivault +''' +import sys +from timeit import default_timer as timer +import numpy as np + +def test_svd(): + d = 100 + A = np.asarray(np.random.randint(1, 10,(d, d)), dtype=np.float64) + + # scipy + print("\n****************** Scipy") + from scipy import linalg as sc_linalg + start = timer() + u_sc, s_sc, v_sc = sc_linalg.svd(A) + duration = timer() - start + print("SVD scipy : " + str(duration)) + print(s_sc) + print(type(s_sc)) + + # scikit-cuda + print("\n****************** scikit-cuda") + import pycuda.autoinit + import pycuda.gpuarray as gpuarray + #import ctypes + #ctypes.CDLL('libgomp.so.1', mode=ctypes.RTLD_GLOBAL) + from skcuda import linalg as sk_linalg + start = timer() + sk_linalg.init() + A_sk = gpuarray.to_gpu(A) + u_sk, s_sk, vh_sk = sk_linalg.svd(A_sk, jobu='S', jobvt='S', lib='cusolver') + u_sk, s_sk, v_sk = u_sk.get_async(), s_sk.get_async(), vh_sk.get_async() + duration = timer() - start + print("SVD skcuda : " + str(duration)) + print(s_sk) + print(type(s_sk)) + + # tensorflow + print("\n****************** tensorflow") + import tensorflow as tf + from tensorflow import linalg as tf_linalg + start = timer() + with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess: + s_tf, u_tf, v_tf = tf_linalg.svd(A) + s_tf, u_tf, v_tf = s_tf.eval(), u_tf.eval(), v_tf.eval() + duration = timer() - start + print("SVD tensorflow : " + str(duration)) + print(s_tf) + print(type(s_tf)) + + # Theano + print("\n****************** theano") + 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) + f = function([x], (u, s, v)) + start = timer() + u_th, s_th, v_th = f(A) + duration = timer() - start + print("SVD theano : " + str(duration)) + print(s_th) + print(type(s_th)) + + +if __name__ == '__main__': + test_svd() -- GitLab