<s> """<STR_LIT>""" <EOL> from __future__ import division , print_function , absolute_import <EOL> import warnings <EOL> import time <EOL> import timeit <EOL> import numpy <EOL> import numpy as np <EOL> from numpy import ones , array , asarray , empty , random , zeros <EOL> try : <EOL> from scipy import sparse <EOL> from scipy . sparse import ( csr_matrix , coo_matrix , dia_matrix , lil_matrix , <EOL> dok_matrix , rand , SparseEfficiencyWarning ) <EOL> except ImportError : <EOL> pass <EOL> from . common import Benchmark <EOL> def random_sparse ( m , n , nnz_per_row ) : <EOL> rows = numpy . arange ( m ) . repeat ( nnz_per_row ) <EOL> cols = numpy . random . randint ( <NUM_LIT:0> , n , size = nnz_per_row * m ) <EOL> vals = numpy . random . random_sample ( m * nnz_per_row ) <EOL> return coo_matrix ( ( vals , ( rows , cols ) ) , ( m , n ) ) . tocsr ( ) <EOL> def poisson2d ( N , dtype = '<STR_LIT:d>' , format = None ) : <EOL> """<STR_LIT>""" <EOL> if N == <NUM_LIT:1> : <EOL> diags = asarray ( [ [ <NUM_LIT:4> ] ] , dtype = dtype ) <EOL> return dia_matrix ( ( diags , [ <NUM_LIT:0> ] ) , shape = ( <NUM_LIT:1> , <NUM_LIT:1> ) ) . asformat ( format ) <EOL> offsets = array ( [ <NUM_LIT:0> , - N , N , - <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> diags = empty ( ( <NUM_LIT:5> , N ** <NUM_LIT:2> ) , dtype = dtype ) <EOL> diags [ <NUM_LIT:0> ] = <NUM_LIT:4> <EOL> diags [ <NUM_LIT:1> : ] = - <NUM_LIT:1> <EOL> diags [ <NUM_LIT:3> , N - <NUM_LIT:1> : : N ] = <NUM_LIT:0> <EOL> diags [ <NUM_LIT:4> , N : : N ] = <NUM_LIT:0> <EOL> return dia_matrix ( ( diags , offsets ) , shape = ( N ** <NUM_LIT:2> , N ** <NUM_LIT:2> ) ) . asformat ( format ) <EOL> class Arithmetic ( Benchmark ) : <EOL> param_names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> params = [ <EOL> [ '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> ] <EOL> def setup ( self , format , XY , op ) : <EOL> matrices = dict ( A = poisson2d ( <NUM_LIT> , format = format ) , <EOL> B = poisson2d ( <NUM_LIT> , format = format ) ** <NUM_LIT:2> ) <EOL> x = matrices [ XY [ <NUM_LIT:0> ] ] <EOL> self . y = matrices [ XY [ <NUM_LIT:1> ] ] <EOL> self . fn = getattr ( x , op ) <EOL> self . fn ( self . y ) <EOL> def time_arithmetic ( self , format , XY , op ) : <EOL> self . fn ( self . y ) <EOL> class Sort ( Benchmark ) : <EOL> params = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> param_names = [ '<STR_LIT>' ] <EOL> def setup ( self , matrix ) : <EOL> n = <NUM_LIT> <EOL> if matrix . startswith ( '<STR_LIT>' ) : <EOL> k = int ( matrix [ <NUM_LIT:4> : ] ) <EOL> self . A = random_sparse ( n , n , k ) <EOL> self . A . has_sorted_indices = False <EOL> self . A . indices [ : <NUM_LIT:2> ] = <NUM_LIT:2> , <NUM_LIT:1> <EOL> else : <EOL> raise NotImplementedError ( ) <EOL> def time_sort ( self , matrix ) : <EOL> """<STR_LIT>""" <EOL> self . A . sort_indices ( ) <EOL> class Matvec ( Benchmark ) : <EOL> params = [ <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> ] <EOL> param_names = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def setup ( self , matrix , format ) : <EOL> if matrix == '<STR_LIT>' : <EOL> if format in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise NotImplementedError ( ) <EOL> self . A = sparse . eye ( <NUM_LIT> , <NUM_LIT> , format = format ) <EOL> elif matrix == '<STR_LIT>' : <EOL> self . A = poisson2d ( <NUM_LIT> , format = format ) <EOL> elif matrix == '<STR_LIT>' : <EOL> if format not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise NotImplementedError ( ) <EOL> b = ( <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> self . A = sparse . kron ( poisson2d ( <NUM_LIT> ) , <EOL> ones ( b ) ) . tobsr ( blocksize = b ) . asformat ( format ) <EOL> elif matrix == '<STR_LIT>' : <EOL> if format not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise NotImplementedError ( ) <EOL> b = ( <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> self . A = sparse . kron ( poisson2d ( <NUM_LIT:100> ) , <EOL> ones ( b ) ) . tobsr ( blocksize = b ) . asformat ( format ) <EOL> else : <EOL> raise NotImplementedError ( ) <EOL> self . x = ones ( self . A . shape [ <NUM_LIT:1> ] , dtype = float ) <EOL> def time_matvec ( self , matrix , format ) : <EOL> self . A * self . x <EOL> class Matvecs ( Benchmark ) : <EOL> params = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> param_names = [ "<STR_LIT>" ] <EOL> def setup ( self , format ) : <EOL> self . A = poisson2d ( <NUM_LIT> , format = format ) <EOL> self . x = ones ( ( self . A . shape [ <NUM_LIT:1> ] , <NUM_LIT:10> ) , dtype = self . A . dtype ) <EOL> def time_matvecs ( self , format ) : <EOL> self . A * self . x <EOL> class Matmul ( Benchmark ) : <EOL> def setup ( self ) : <EOL> H1 , W1 = <NUM_LIT:1> , <NUM_LIT> <EOL> H2 , W2 = W1 , <NUM_LIT:1000> <EOL> C1 = <NUM_LIT:10> <EOL> C2 = <NUM_LIT> <EOL> random . seed ( <NUM_LIT:0> ) <EOL> matrix1 = lil_matrix ( zeros ( ( H1 , W1 ) ) ) <EOL> matrix2 = lil_matrix ( zeros ( ( H2 , W2 ) ) ) <EOL> for i in range ( C1 ) : <EOL> matrix1 [ random . randint ( H1 ) , random . randint ( W1 ) ] = random . rand ( ) <EOL> for i in range ( C2 ) : <EOL> matrix2 [ random . randint ( H2 ) , random . randint ( W2 ) ] = random . rand ( ) <EOL> self . matrix1 = matrix1 . tocsr ( ) <EOL> self . matrix2 = matrix2 . tocsr ( ) <EOL> def time_large ( self ) : <EOL> for i in range ( <NUM_LIT:100> ) : <EOL> self . matrix1 * self . matrix2 <EOL> class Construction ( Benchmark ) : <EOL> params = [ <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> ] <EOL> param_names = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def setup ( self , name , format ) : <EOL> if name == '<STR_LIT>' : <EOL> self . A = coo_matrix ( ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> elif name == '<STR_LIT>' : <EOL> self . A = sparse . eye ( <NUM_LIT> , format = '<STR_LIT>' ) <EOL> else : <EOL> self . A = poisson2d ( <NUM_LIT:100> , format = '<STR_LIT>' ) <EOL> formats = { '<STR_LIT>' : lil_matrix , '<STR_LIT>' : dok_matrix } <EOL> self . cls = formats [ format ] <EOL> def time_construction ( self , name , format ) : <EOL> T = self . cls ( self . A . shape ) <EOL> for i , j , v in zip ( self . A . row , self . A . col , self . A . data ) : <EOL> T [ i , j ] = v <EOL> class Conversion ( Benchmark ) : <EOL> params = [ <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> ] <EOL> param_names = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def setup ( self , fromfmt , tofmt ) : <EOL> base = poisson2d ( <NUM_LIT:100> , format = fromfmt ) <EOL> try : <EOL> self . fn = getattr ( base , '<STR_LIT:to>' + tofmt ) <EOL> except : <EOL> def fn ( ) : <EOL> raise RuntimeError ( ) <EOL> self . fn = fn <EOL> def time_conversion ( self , fromfmt , tofmt ) : <EOL> self . fn ( ) <EOL> class Getset ( Benchmark ) : <EOL> params = [ <EOL> [ <NUM_LIT:1> , <NUM_LIT:10> , <NUM_LIT:100> , <NUM_LIT:1000> , <NUM_LIT> ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> ] <EOL> param_names = [ '<STR_LIT:N>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> unit = "<STR_LIT>" <EOL> def setup ( self , N , sparsity_pattern , format ) : <EOL> if format == '<STR_LIT>' and N > <NUM_LIT> : <EOL> raise NotImplementedError ( ) <EOL> self . A = rand ( <NUM_LIT:1000> , <NUM_LIT:1000> , density = <NUM_LIT> ) <EOL> A = self . A <EOL> N = int ( N ) <EOL> i , j = [ ] , [ ] <EOL> while len ( i ) < N : <EOL> n = N - len ( i ) <EOL> ip = numpy . random . randint ( <NUM_LIT:0> , A . shape [ <NUM_LIT:0> ] , size = n ) <EOL> jp = numpy . random . randint ( <NUM_LIT:0> , A . shape [ <NUM_LIT:1> ] , size = n ) <EOL> i = numpy . r_ [ i , ip ] <EOL> j = numpy . r_ [ j , jp ] <EOL> v = numpy . random . rand ( n ) <EOL> if N == <NUM_LIT:1> : <EOL> i = int ( i ) <EOL> j = int ( j ) <EOL> v = float ( v ) <EOL> base = A . asformat ( format ) <EOL> self . m = base . copy ( ) <EOL> self . i = i <EOL> self . j = j <EOL> self . v = v <EOL> def _timeit ( self , kernel , recopy ) : <EOL> min_time = <NUM_LIT> <EOL> if not recopy : <EOL> kernel ( self . m , self . i , self . j , self . v ) <EOL> number = <NUM_LIT:1> <EOL> start = time . time ( ) <EOL> while time . time ( ) - start < <NUM_LIT:0.1> : <EOL> if recopy : <EOL> m = self . m . copy ( ) <EOL> else : <EOL> m = self . m <EOL> while True : <EOL> duration = timeit . timeit ( <EOL> lambda : kernel ( m , self . i , self . j , self . v ) , number = number ) <EOL> if duration > <NUM_LIT> : <EOL> break <EOL> else : <EOL> number *= <NUM_LIT:10> <EOL> min_time = min ( min_time , duration / number ) <EOL> return min_time <EOL> def track_fancy_setitem ( self , N , sparsity_pattern , format ) : <EOL> def kernel ( A , i , j , v ) : <EOL> A [ i , j ] = v <EOL> with warnings . catch_warnings ( ) : <EOL> warnings . simplefilter ( '<STR_LIT:ignore>' , SparseEfficiencyWarning ) <EOL> return self . _timeit ( kernel , sparsity_pattern == '<STR_LIT>' ) <EOL> def time_fancy_getitem ( self , N , sparsity_pattern , format ) : <EOL> self . m [ self . i , self . j ] <EOL> class NullSlice ( Benchmark ) : <EOL> params = [ [ <NUM_LIT> , <NUM_LIT> ] , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ] <EOL> param_names = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def setup ( self , density , format ) : <EOL> n = <NUM_LIT> <EOL> k = <NUM_LIT:1000> <EOL> self . X = sparse . rand ( n , k , format = format , density = density ) <EOL> def time_3_rows ( self , density , format ) : <EOL> self . X [ [ <NUM_LIT:0> , <NUM_LIT:100> , <NUM_LIT> ] , : ] <EOL> def time_10000_rows ( self , density , format ) : <EOL> self . X [ np . arange ( <NUM_LIT> ) , : ] <EOL> def time_3_cols ( self , density , format ) : <EOL> self . X [ : , [ <NUM_LIT:0> , <NUM_LIT:100> , <NUM_LIT> ] ] <EOL> def time_100_cols ( self , density , format ) : <EOL> self . X [ : , np . arange ( <NUM_LIT:100> ) ] <EOL> class Diagonal ( Benchmark ) : <EOL> params = [ [ <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT:0.5> ] , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ] <EOL> param_names = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def setup ( self , density , format ) : <EOL> n = <NUM_LIT:1000> <EOL> if format == '<STR_LIT>' and n * density >= <NUM_LIT> : <EOL> raise NotImplementedError ( ) <EOL> self . X = sparse . rand ( n , n , format = format , density = density ) <EOL> def time_diagonal ( self , density , format ) : <EOL> self . X . diagonal ( ) <EOL> class Sum ( Benchmark ) : <EOL> params = [ [ <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT:0.5> ] , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ] <EOL> param_names = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def setup ( self , density , format ) : <EOL> n = <NUM_LIT:1000> <EOL> if format == '<STR_LIT>' and n * density >= <NUM_LIT> : <EOL> raise NotImplementedError ( ) <EOL> self . X = sparse . rand ( n , n , format = format , density = density ) <EOL> def time_sum ( self , density , format ) : <EOL> self . X . sum ( ) <EOL> def time_sum_axis0 ( self , density , format ) : <EOL> self . X . sum ( axis = <NUM_LIT:0> ) <EOL> def time_sum_axis1 ( self , density , format ) : <EOL> self . X . sum ( axis = <NUM_LIT:1> ) </s>
