| @@ -0,0 +1 @@ | |||
| Benchmark graphs are at https://ev-br.github.io/ob-bench-asv/ | |||
| @@ -0,0 +1,258 @@ | |||
| # Write the benchmarking functions here. | |||
| # See "Writing benchmarks" in the asv docs for more information. | |||
| ''' | |||
| class TimeSuite: | |||
| """ | |||
| An example benchmark that times the performance of various kinds | |||
| of iterating over dictionaries in Python. | |||
| """ | |||
| def setup(self): | |||
| self.d = {} | |||
| for x in range(500): | |||
| self.d[x] = None | |||
| def time_keys(self): | |||
| for key in self.d.keys(): | |||
| pass | |||
| def time_values(self): | |||
| for value in self.d.values(): | |||
| pass | |||
| def time_range(self): | |||
| d = self.d | |||
| for key in range(500): | |||
| d[key] | |||
| class MemSuite: | |||
| def mem_list(self): | |||
| return [0] * 256 | |||
| ''' | |||
| import numpy as np | |||
| from openblas_wrap import ( | |||
| # level 1 | |||
| dnrm2, ddot, daxpy, | |||
| # level 3 | |||
| dgemm, dsyrk, | |||
| # lapack | |||
| dgesv, # linalg.solve | |||
| dgesdd, dgesdd_lwork, # linalg.svd | |||
| dsyev, dsyev_lwork, # linalg.eigh | |||
| ) | |||
| # ### BLAS level 1 ### | |||
| # dnrm2 | |||
| dnrm2_sizes = [100, 1000] | |||
| def run_dnrm2(n, x, incx): | |||
| res = dnrm2(x, n, incx=incx) | |||
| return res | |||
| class Nrm2: | |||
| params = [100, 1000] | |||
| param_names = ["size"] | |||
| def setup(self, n): | |||
| rndm = np.random.RandomState(1234) | |||
| self.x = rndm.uniform(size=(n,)).astype(float) | |||
| def time_dnrm2(self, n): | |||
| run_dnrm2(n, self.x, 1) | |||
| # ddot | |||
| ddot_sizes = [100, 1000] | |||
| def run_ddot(x, y,): | |||
| res = ddot(x, y) | |||
| return res | |||
| class DDot: | |||
| params = ddot_sizes | |||
| param_names = ["size"] | |||
| def setup(self, n): | |||
| rndm = np.random.RandomState(1234) | |||
| self.x = np.array(rndm.uniform(size=(n,)), dtype=float) | |||
| self.y = np.array(rndm.uniform(size=(n,)), dtype=float) | |||
| def time_ddot(self, n): | |||
| run_ddot(self.x, self.y) | |||
| # daxpy | |||
| daxpy_sizes = [100, 1000] | |||
| def run_daxpy(x, y,): | |||
| res = daxpy(x, y, a=2.0) | |||
| return res | |||
| class Daxpy: | |||
| params = daxpy_sizes | |||
| param_names = ["size"] | |||
| def setup(self, n): | |||
| rndm = np.random.RandomState(1234) | |||
| self.x = np.array(rndm.uniform(size=(n,)), dtype=float) | |||
| self.y = np.array(rndm.uniform(size=(n,)), dtype=float) | |||
| def time_daxpy(self, n): | |||
| run_daxpy(self.x, self.y) | |||
| # ### BLAS level 3 ### | |||
| # dgemm | |||
| gemm_sizes = [100, 1000] | |||
| def run_dgemm(a, b, c): | |||
| alpha = 1.0 | |||
| res = dgemm(alpha, a, b, c=c, overwrite_c=True) | |||
| return res | |||
| class Dgemm: | |||
| params = gemm_sizes | |||
| param_names = ["size"] | |||
| def setup(self, n): | |||
| rndm = np.random.RandomState(1234) | |||
| self.a = np.array(rndm.uniform(size=(n, n)), dtype=float, order='F') | |||
| self.b = np.array(rndm.uniform(size=(n, n)), dtype=float, order='F') | |||
| self.c = np.empty((n, n), dtype=float, order='F') | |||
| def time_dgemm(self, n): | |||
| run_dgemm(self.a, self.b, self.c) | |||
| # dsyrk | |||
| syrk_sizes = [100, 1000] | |||
| def run_dsyrk(a, c): | |||
| res = dsyrk(1.0, a, c=c, overwrite_c=True) | |||
| return res | |||
| class DSyrk: | |||
| params = syrk_sizes | |||
| param_names = ["size"] | |||
| def setup(self, n): | |||
| rndm = np.random.RandomState(1234) | |||
| self.a = np.array(rndm.uniform(size=(n, n)), dtype=float, order='F') | |||
| self.c = np.empty((n, n), dtype=float, order='F') | |||
| def time_dsyrk(self, n): | |||
| run_dsyrk(self.a, self.c) | |||
| # ### LAPACK ### | |||
| # linalg.solve | |||
| dgesv_sizes = [100, 1000] | |||
| def run_dgesv(a, b): | |||
| res = dgesv(a, b, overwrite_a=True, overwrite_b=True) | |||
| return res | |||
| class Dgesv: | |||
| params = dgesv_sizes | |||
| param_names = ["size"] | |||
| def setup(self, n): | |||
| rndm = np.random.RandomState(1234) | |||
| self.a = (np.array(rndm.uniform(size=(n, n)), dtype=float, order='F') + | |||
| np.eye(n, order='F')) | |||
| self.b = np.array(rndm.uniform(size=(n, 1)), order='F') | |||
| def time_dgesv(self, n): | |||
| run_dgesv(self.a, self.b) | |||
| # XXX: how to run asserts? | |||
| # lu, piv, x, info = benchmark(run_gesv, a, b) | |||
| # assert lu is a | |||
| # assert x is b | |||
| # assert info == 0 | |||
| # linalg.svd | |||
| dgesdd_sizes = ["100, 5", "1000, 222"] | |||
| def run_dgesdd(a, lwork): | |||
| res = dgesdd(a, lwork=lwork, full_matrices=False, overwrite_a=False) | |||
| return res | |||
| class Dgesdd: | |||
| params = dgesdd_sizes | |||
| param_names = ["(m, n)"] | |||
| def setup(self, mn): | |||
| m, n = (int(x) for x in mn.split(",")) | |||
| rndm = np.random.RandomState(1234) | |||
| a = np.array(rndm.uniform(size=(m, n)), dtype=float, order='F') | |||
| lwork, info = dgesdd_lwork(m, n) | |||
| lwork = int(lwork) | |||
| assert info == 0 | |||
| self.a, self.lwork = a, lwork | |||
| def time_dgesdd(self, mn): | |||
| run_dgesdd(self.a, self.lwork) | |||
| # linalg.eigh | |||
| dsyev_sizes = [50, 200] | |||
| def run_dsyev(a, lwork): | |||
| res = dsyev(a, lwork=lwork, overwrite_a=True) | |||
| return res | |||
| class Dsyev: | |||
| params = dsyev_sizes | |||
| param_names = ["size"] | |||
| def setup(self, n): | |||
| rndm = np.random.RandomState(1234) | |||
| a = rndm.uniform(size=(n, n)) | |||
| a = np.asarray(a + a.T, dtype=float, order='F') | |||
| a_ = a.copy() | |||
| lwork, info = dsyev_lwork(n) | |||
| lwork = int(lwork) | |||
| assert info == 0 | |||
| self.a = a_ | |||
| self.lwork = lwork | |||
| def time_dsyev(self, n): | |||
| run_dsyev(self.a, self.lwork) | |||
| @@ -0,0 +1,48 @@ | |||
| # | |||
| # Taken from SciPy (of course) | |||
| # | |||
| project( | |||
| 'openblas-wrap', | |||
| 'c', 'fortran', | |||
| version: '0.1', | |||
| license: 'BSD-3', | |||
| meson_version: '>= 1.1.0', | |||
| default_options: [ | |||
| 'buildtype=debugoptimized', | |||
| 'b_ndebug=if-release', | |||
| 'c_std=c17', | |||
| 'fortran_std=legacy', | |||
| ], | |||
| ) | |||
| py3 = import('python').find_installation(pure: false) | |||
| py3_dep = py3.dependency() | |||
| cc = meson.get_compiler('c') | |||
| _global_c_args = cc.get_supported_arguments( | |||
| '-Wno-unused-but-set-variable', | |||
| '-Wno-unused-function', | |||
| '-Wno-conversion', | |||
| '-Wno-misleading-indentation', | |||
| ) | |||
| add_project_arguments(_global_c_args, language : 'c') | |||
| # We need -lm for all C code (assuming it uses math functions, which is safe to | |||
| # assume for SciPy). For C++ it isn't needed, because libstdc++/libc++ is | |||
| # guaranteed to depend on it. For Fortran code, Meson already adds `-lm`. | |||
| m_dep = cc.find_library('m', required : false) | |||
| if m_dep.found() | |||
| add_project_link_arguments('-lm', language : 'c') | |||
| endif | |||
| generate_f2pymod = find_program('openblas_wrap/generate_f2pymod.py') | |||
| openblas = dependency('scipy_openblas', method: 'pkg-config', required: true) | |||
| openblas_dep = declare_dependency( | |||
| dependencies: openblas, | |||
| compile_args: [] | |||
| ) | |||
| subdir('openblas_wrap') | |||
| @@ -0,0 +1,29 @@ | |||
| """ | |||
| Trampoline to hide the LAPACK details (scipy.lapack.linalg or scipy_openblas32 or...) | |||
| from benchmarking. | |||
| """ | |||
| __version__ = "0.1" | |||
| import scipy_openblas32 # preload symbols. typically done in _distributor_init.py | |||
| #from scipy.linalg.blas import ( | |||
| from ._flapack import ( | |||
| # level 1 | |||
| scipy_dnrm2 as dnrm2, | |||
| scipy_ddot as ddot, | |||
| scipy_daxpy as daxpy, | |||
| # level 3 | |||
| scipy_dgemm as dgemm, | |||
| scipy_dsyrk as dsyrk, | |||
| ) | |||
| #from scipy.linalg.lapack import ( | |||
| from openblas_wrap._flapack import ( | |||
| # linalg.solve | |||
| scipy_dgesv as dgesv, | |||
| # linalg.svd | |||
| scipy_dgesdd as dgesdd, scipy_dgesdd_lwork as dgesdd_lwork, | |||
| # linalg.eigh | |||
| scipy_dsyev as dsyev, scipy_dsyev_lwork as dsyev_lwork | |||
| ) | |||
| @@ -0,0 +1,4 @@ | |||
| ''' | |||
| Helper to preload OpenBLAS from scipy_openblas32 | |||
| ''' | |||
| import scipy_openblas32 | |||
| @@ -0,0 +1,327 @@ | |||
| ! | |||
| ! Taken from scipy/linalg | |||
| ! | |||
| ! Shorthand notations | |||
| ! | |||
| ! <tchar=s,d,cs,zd> | |||
| ! <tchar2c=cs,zd> | |||
| ! | |||
| ! <prefix=scipy_s,scipy_d,scipy_c,scipy_z> | |||
| ! <prefix2=scipy_s,scipy_d> | |||
| ! <prefix2c=scipy_c,scipy_z> | |||
| ! <prefix3=scipy_s,scipy_sc> | |||
| ! <prefix4=scipy_d,scipy_dz> | |||
| ! <prefix6=scipy_s,scipy_d,scipy_c,scipy_z,scipy_c,scipy_z> | |||
| ! | |||
| ! <ftype2=real,double precision> | |||
| ! <ftype2c=complex,double complex> | |||
| ! <ftype3=real,complex> | |||
| ! <ftype4=double precision,double complex> | |||
| ! <ftypereal3=real,real> | |||
| ! <ftypereal4=double precision,double precision> | |||
| ! <ftype6=real,double precision,complex,double complex,\2,\3> | |||
| ! <ftype6creal=real,double precision,complex,double complex,\0,\1> | |||
| ! | |||
| ! <ctype2=float,double> | |||
| ! <ctype2c=complex_float,complex_double> | |||
| ! <ctype3=float,complex_float> | |||
| ! <ctype4=double,complex_double> | |||
| ! <ctypereal3=float,float> | |||
| ! <ctypereal4=double,double> | |||
| ! <ctype6=float,double,complex_float,complex_double,\2,\3> | |||
| ! <ctype6creal=float,double,complex_float,complex_double,\0,\1> | |||
| ! | |||
| ! | |||
| ! Level 1 BLAS | |||
| ! | |||
| python module _flapack | |||
| usercode ''' | |||
| #define F_INT int | |||
| ''' | |||
| interface | |||
| subroutine <prefix>axpy(n,a,x,offx,incx,y,offy,incy) | |||
| ! Calculate z = a*x+y, where a is scalar. | |||
| callstatement (*f2py_func)(&n,&a,x+offx,&incx,y+offy,&incy) | |||
| callprotoargument F_INT*,<ctype>*,<ctype>*,F_INT*,<ctype>*,F_INT* | |||
| <ftype> dimension(*), intent(in) :: x | |||
| <ftype> dimension(*), intent(in,out,out=z) :: y | |||
| <ftype> optional, intent(in):: a=<1.0,\0,(1.0\,0.0),\2> | |||
| integer optional, intent(in),check(incx>0||incx<0) :: incx = 1 | |||
| integer optional, intent(in),check(incy>0||incy<0) :: incy = 1 | |||
| integer optional, intent(in),depend(x) :: offx=0 | |||
| integer optional, intent(in),depend(y) :: offy=0 | |||
| check(offx>=0 && offx<len(x)) :: offx | |||
| check(offy>=0 && offy<len(y)) :: offy | |||
| integer optional, intent(in),depend(x,incx,offx,y,incy,offy) :: & | |||
| n = (len(x)-offx)/abs(incx) | |||
| check(len(x)-offx>(n-1)*abs(incx)) :: n | |||
| check(len(y)-offy>(n-1)*abs(incy)) :: n | |||
| end subroutine <prefix>axpy | |||
| function scipy_ddot(n,x,offx,incx,y,offy,incy) result (xy) | |||
| ! Computes a vector-vector dot product. | |||
| callstatement scipy_ddot_return_value = (*f2py_func)(&n,x+offx,&incx,y+offy,&incy) | |||
| callprotoargument F_INT*,double*,F_INT*,double*,F_INT* | |||
| intent(c) scipy_ddot | |||
| fortranname F_FUNC(scipy_ddot,DDOT) | |||
| double precision dimension(*), intent(in) :: x | |||
| double precision dimension(*), intent(in) :: y | |||
| double precision ddot,xy | |||
| integer optional, intent(in),check(incx>0||incx<0) :: incx = 1 | |||
| integer optional, intent(in),check(incy>0||incy<0) :: incy = 1 | |||
| integer optional, intent(in),depend(x) :: offx=0 | |||
| integer optional, intent(in),depend(y) :: offy=0 | |||
| check(offx>=0 && offx<len(x)) :: offx | |||
| check(offy>=0 && offy<len(y)) :: offy | |||
| integer optional, intent(in),depend(x,incx,offx,y,incy,offy) :: & | |||
| n = (len(x)-offx)/abs(incx) | |||
| check(len(x)-offx>(n-1)*abs(incx)) :: n | |||
| check(len(y)-offy>(n-1)*abs(incy)) :: n | |||
| end function scipy_ddot | |||
| function <prefix4>nrm2(n,x,offx,incx) result(n2) | |||
| <ftypereal4> <prefix4>nrm2, n2 | |||
| callstatement <prefix4>nrm2_return_value = (*f2py_func)(&n,x+offx,&incx) | |||
| callprotoargument F_INT*,<ctype4>*,F_INT* | |||
| intent(c) <prefix4>nrm2 | |||
| fortranname F_FUNC(<prefix4>nrm2,<D,DZ>NRM2) | |||
| <ftype4> dimension(*),intent(in) :: x | |||
| integer optional, intent(in),check(incx>0) :: incx = 1 | |||
| integer optional,intent(in),depend(x) :: offx=0 | |||
| check(offx>=0 && offx<len(x)) :: offx | |||
| integer optional,intent(in),depend(x,incx,offx) :: n = (len(x)-offx)/abs(incx) | |||
| check(len(x)-offx>(n-1)*abs(incx)) :: n | |||
| end function <prefix4>nrm2 | |||
| ! | |||
| ! Level 3 BLAS | |||
| ! | |||
| subroutine <prefix>gemm(m,n,k,alpha,a,b,beta,c,trans_a,trans_b,lda,ka,ldb,kb) | |||
| ! Computes a scalar-matrix-matrix product and adds the result to a | |||
| ! scalar-matrix product. | |||
| ! | |||
| ! c = gemm(alpha,a,b,beta=0,c=0,trans_a=0,trans_b=0,overwrite_c=0) | |||
| ! Calculate C <- alpha * op(A) * op(B) + beta * C | |||
| callstatement (*f2py_func)((trans_a?(trans_a==2?"C":"T"):"N"), & | |||
| (trans_b?(trans_b==2?"C":"T"):"N"),&m,&n,&k,&alpha,a,&lda,b,&ldb,&beta,c,&m) | |||
| callprotoargument char*,char*,F_INT*,F_INT*,F_INT*,<ctype>*,<ctype>*,F_INT*,<ctype>*, & | |||
| F_INT*,<ctype>*,<ctype>*,F_INT* | |||
| integer optional,intent(in),check(trans_a>=0 && trans_a <=2) :: trans_a = 0 | |||
| integer optional,intent(in),check(trans_b>=0 && trans_b <=2) :: trans_b = 0 | |||
| <ftype> intent(in) :: alpha | |||
| <ftype> intent(in),optional :: beta = <0.0,\0,(0.0\,0.0),\2> | |||
| <ftype> dimension(lda,ka),intent(in) :: a | |||
| <ftype> dimension(ldb,kb),intent(in) :: b | |||
| <ftype> dimension(m,n),intent(in,out,copy),depend(m,n),optional :: c | |||
| check(shape(c,0)==m && shape(c,1)==n) :: c | |||
| integer depend(a),intent(hide) :: lda = shape(a,0) | |||
| integer depend(a),intent(hide) :: ka = shape(a,1) | |||
| integer depend(b),intent(hide) :: ldb = shape(b,0) | |||
| integer depend(b),intent(hide) :: kb = shape(b,1) | |||
| integer depend(a,trans_a,ka,lda),intent(hide):: m = (trans_a?ka:lda) | |||
| integer depend(a,trans_a,ka,lda),intent(hide):: k = (trans_a?lda:ka) | |||
| integer depend(b,trans_b,kb,ldb,k),intent(hide),check(trans_b?kb==k:ldb==k) :: & | |||
| n = (trans_b?ldb:kb) | |||
| end subroutine <prefix>gemm | |||
| subroutine <prefix6><sy,\0,\0,\0,he,he>rk(n,k,alpha,a,beta,c,trans,lower,lda,ka) | |||
| ! performs one of the symmetric rank k operations | |||
| ! C := alpha*A*A**T + beta*C, or C := alpha*A**T*A + beta*C, | |||
| ! | |||
| ! c = syrk(alpha,a,beta=0,c=0,trans=0,lower=0,overwrite_c=0) | |||
| ! | |||
| callstatement (*f2py_func)((lower?"L":"U"), & | |||
| (trans?(trans==2?"C":"T"):"N"), &n,&k,&alpha,a,&lda,&beta,c,&n) | |||
| callprotoargument char*,char*,F_INT*,F_INT*,<ctype6>*,<ctype6>*,F_INT*,<ctype6>*, & | |||
| <ctype6>*,F_INT* | |||
| integer optional, intent(in),check(lower==0||lower==1) :: lower = 0 | |||
| integer optional,intent(in),check(trans>=0 && trans <=2) :: trans = 0 | |||
| <ftype6> intent(in) :: alpha | |||
| <ftype6> intent(in),optional :: beta = <0.0,\0,(0.0\,0.0),\2,\2,\2> | |||
| <ftype6> dimension(lda,ka),intent(in) :: a | |||
| <ftype6> dimension(n,n),intent(in,out,copy),depend(n),optional :: c | |||
| check(shape(c,0)==n && shape(c,1)==n) :: c | |||
| integer depend(a),intent(hide) :: lda = shape(a,0) | |||
| integer depend(a),intent(hide) :: ka = shape(a,1) | |||
| integer depend(a, trans, ka, lda), intent(hide) :: n = (trans ? ka : lda) | |||
| integer depend(a, trans, ka, lda), intent(hide) :: k = (trans ? lda : ka) | |||
| end subroutine <prefix6><sy,\0,\0,\0,he,he>rk | |||
| ! | |||
| ! LAPACK | |||
| ! | |||
| subroutine <prefix>gesv(n,nrhs,a,piv,b,info) | |||
| ! lu,piv,x,info = gesv(a,b,overwrite_a=0,overwrite_b=0) | |||
| ! Solve A * X = B. | |||
| ! A = P * L * U | |||
| ! U is upper diagonal triangular, L is unit lower triangular, | |||
| ! piv pivots columns. | |||
| callstatement {F_INT i;(*f2py_func)(&n,&nrhs,a,&n,piv,b,&n,&info);for(i=0;i\<n;--piv[i++]);} | |||
| callprotoargument F_INT*,F_INT*,<ctype>*,F_INT*,F_INT*,<ctype>*,F_INT*,F_INT* | |||
| integer depend(a),intent(hide):: n = shape(a,0) | |||
| integer depend(b),intent(hide):: nrhs = shape(b,1) | |||
| <ftype> dimension(n,n),check(shape(a,0)==shape(a,1)) :: a | |||
| integer dimension(n),depend(n),intent(out) :: piv | |||
| <ftype> dimension(n,nrhs),check(shape(a,0)==shape(b,0)),depend(n) :: b | |||
| integer intent(out)::info | |||
| intent(in,out,copy,out=x) b | |||
| intent(in,out,copy,out=lu) a | |||
| end subroutine <prefix>gesv | |||
| subroutine <prefix2>gesdd(m,n,minmn,u0,u1,vt0,vt1,a,compute_uv,full_matrices,u,s,vt,work,lwork,iwork,info) | |||
| ! u,s,vt,info = gesdd(a,compute_uv=1,lwork=..,overwrite_a=0) | |||
| ! Compute the singular value decomposition (SVD) using divide and conquer: | |||
| ! A = U * SIGMA * transpose(V) | |||
| ! A - M x N matrix | |||
| ! U - M x M matrix or min(M,N) x N if full_matrices=False | |||
| ! SIGMA - M x N zero matrix with a main diagonal filled with min(M,N) | |||
| ! singular values | |||
| ! transpose(V) - N x N matrix or N x min(M,N) if full_matrices=False | |||
| callstatement (*f2py_func)((compute_uv?(full_matrices?"A":"S"):"N"),&m,&n,a,&m,s,u,&u0,vt,&vt0,work,&lwork,iwork,&info) | |||
| callprotoargument char*,F_INT*,F_INT*,<ctype2>*,F_INT*,<ctype2>*,<ctype2>*,F_INT*,<ctype2>*,F_INT*,<ctype2>*,F_INT*,F_INT*,F_INT* | |||
| integer intent(in),optional,check(compute_uv==0||compute_uv==1):: compute_uv = 1 | |||
| integer intent(in),optional,check(full_matrices==0||full_matrices==1):: full_matrices = 1 | |||
| integer intent(hide),depend(a):: m = shape(a,0) | |||
| integer intent(hide),depend(a):: n = shape(a,1) | |||
| integer intent(hide),depend(m,n):: minmn = MIN(m,n) | |||
| integer intent(hide),depend(compute_uv,minmn) :: u0 = (compute_uv?m:1) | |||
| integer intent(hide),depend(compute_uv,minmn, full_matrices) :: u1 = (compute_uv?(full_matrices?m:minmn):1) | |||
| integer intent(hide),depend(compute_uv,minmn, full_matrices) :: vt0 = (compute_uv?(full_matrices?n:minmn):1) | |||
| integer intent(hide),depend(compute_uv,minmn) :: vt1 = (compute_uv?n:1) | |||
| <ftype2> dimension(m,n),intent(in,copy,aligned8) :: a | |||
| <ftype2> dimension(minmn),intent(out),depend(minmn) :: s | |||
| <ftype2> dimension(u0,u1),intent(out),depend(u0, u1) :: u | |||
| <ftype2> dimension(vt0,vt1),intent(out),depend(vt0, vt1) :: vt | |||
| <ftype2> dimension(lwork),intent(hide,cache),depend(lwork) :: work | |||
| integer optional,intent(in),depend(minmn,compute_uv) & | |||
| :: lwork = max((compute_uv?4*minmn*minmn+MAX(m,n)+9*minmn:MAX(14*minmn+4,10*minmn+2+25*(25+8))+MAX(m,n)),1) | |||
| integer intent(hide,cache),dimension(8*minmn),depend(minmn) :: iwork | |||
| integer intent(out)::info | |||
| end subroutine <prefix2>gesdd | |||
| subroutine <prefix2>gesdd_lwork(m,n,minmn,u0,vt0,a,compute_uv,full_matrices,u,s,vt,work,lwork,iwork,info) | |||
| ! LWORK computation for (S/D)GESDD | |||
| fortranname <prefix2>gesdd | |||
| callstatement (*f2py_func)((compute_uv?(full_matrices?"A":"S"):"N"),&m,&n,&a,&m,&s,&u,&u0,&vt,&vt0,&work,&lwork,&iwork,&info) | |||
| callprotoargument char*,F_INT*,F_INT*,<ctype2>*,F_INT*,<ctype2>*,<ctype2>*,F_INT*,<ctype2>*,F_INT*,<ctype2>*,F_INT*,F_INT*,F_INT* | |||
| integer intent(in),optional,check(compute_uv==0||compute_uv==1):: compute_uv = 1 | |||
| integer intent(in),optional,check(full_matrices==0||full_matrices==1):: full_matrices = 1 | |||
| integer intent(in) :: m | |||
| integer intent(in) :: n | |||
| integer intent(hide),depend(m,n):: minmn = MIN(m,n) | |||
| integer intent(hide),depend(compute_uv,minmn) :: u0 = (compute_uv?m:1) | |||
| integer intent(hide),depend(compute_uv,minmn, full_matrices) :: vt0 = (compute_uv?(full_matrices?n:minmn):1) | |||
| <ftype2> intent(hide) :: a | |||
| <ftype2> intent(hide) :: s | |||
| <ftype2> intent(hide) :: u | |||
| <ftype2> intent(hide) :: vt | |||
| <ftype2> intent(out) :: work | |||
| integer intent(hide) :: lwork = -1 | |||
| integer intent(hide) :: iwork | |||
| integer intent(out) :: info | |||
| end subroutine <prefix2>gesdd_lwork | |||
| subroutine <prefix2>syev(compute_v,lower,n,w,a,lda,work,lwork,info) | |||
| ! w,v,info = syev(a,compute_v=1,lower=0,lwork=3*n-1,overwrite_a=0) | |||
| ! Compute all eigenvalues and, optionally, eigenvectors of a | |||
| ! real symmetric matrix A. | |||
| ! | |||
| ! Performance tip: | |||
| ! If compute_v=0 then set also overwrite_a=1. | |||
| callstatement (*f2py_func)((compute_v?"V":"N"),(lower?"L":"U"),&n,a,&lda,w,work,&lwork,&info) | |||
| callprotoargument char*,char*,F_INT*,<ctype2>*,F_INT*,<ctype2>*,<ctype2>*,F_INT*,F_INT* | |||
| integer optional,intent(in):: compute_v = 1 | |||
| check(compute_v==1||compute_v==0) compute_v | |||
| integer optional,intent(in),check(lower==0||lower==1) :: lower = 0 | |||
| integer intent(hide),depend(a):: n = shape(a,0) | |||
| integer intent(hide),depend(a):: lda = MAX(1,shape(a,0)) | |||
| <ftype2> dimension(n,n),check(shape(a,0)==shape(a,1)) :: a | |||
| intent(in,copy,out,out=v) :: a | |||
| <ftype2> dimension(n),intent(out),depend(n) :: w | |||
| integer optional,intent(in),depend(n) :: lwork=max(3*n-1,1) | |||
| check(lwork>=3*n-1) :: lwork | |||
| <ftype2> dimension(lwork),intent(hide),depend(lwork) :: work | |||
| integer intent(out) :: info | |||
| end subroutine <prefix2>syev | |||
| subroutine <prefix2>syev_lwork(lower,n,w,a,lda,work,lwork,info) | |||
| ! LWORK routines for syev | |||
| fortranname <prefix2>syev | |||
| callstatement (*f2py_func)("N",(lower?"L":"U"),&n,&a,&lda,&w,&work,&lwork,&info) | |||
| callprotoargument char*,char*,F_INT*,<ctype2>*,F_INT*,<ctype2>*,<ctype2>*,F_INT*,F_INT* | |||
| integer intent(in):: n | |||
| integer optional,intent(in),check(lower==0||lower==1) :: lower = 0 | |||
| integer intent(hide),depend(n):: lda = MAX(1, n) | |||
| <ftype2> intent(hide):: a | |||
| <ftype2> intent(hide):: w | |||
| integer intent(hide):: lwork = -1 | |||
| <ftype2> intent(out):: work | |||
| integer intent(out):: info | |||
| end subroutine <prefix2>syev_lwork | |||
| end interface | |||
| end python module _flapack | |||
| @@ -0,0 +1,299 @@ | |||
| #!/usr/bin/env python3 | |||
| """ | |||
| Process f2py template files (`filename.pyf.src` -> `filename.pyf`) | |||
| Usage: python generate_pyf.py filename.pyf.src -o filename.pyf | |||
| """ | |||
| import os | |||
| import sys | |||
| import re | |||
| import subprocess | |||
| import argparse | |||
| # START OF CODE VENDORED FROM `numpy.distutils.from_template` | |||
| ############################################################# | |||
| """ | |||
| process_file(filename) | |||
| takes templated file .xxx.src and produces .xxx file where .xxx | |||
| is .pyf .f90 or .f using the following template rules: | |||
| '<..>' denotes a template. | |||
| All function and subroutine blocks in a source file with names that | |||
| contain '<..>' will be replicated according to the rules in '<..>'. | |||
| The number of comma-separated words in '<..>' will determine the number of | |||
| replicates. | |||
| '<..>' may have two different forms, named and short. For example, | |||
| named: | |||
| <p=d,s,z,c> where anywhere inside a block '<p>' will be replaced with | |||
| 'd', 's', 'z', and 'c' for each replicate of the block. | |||
| <_c> is already defined: <_c=s,d,c,z> | |||
| <_t> is already defined: <_t=real,double precision,complex,double complex> | |||
| short: | |||
| <s,d,c,z>, a short form of the named, useful when no <p> appears inside | |||
| a block. | |||
| In general, '<..>' contains a comma separated list of arbitrary | |||
| expressions. If these expression must contain a comma|leftarrow|rightarrow, | |||
| then prepend the comma|leftarrow|rightarrow with a backslash. | |||
| If an expression matches '\\<index>' then it will be replaced | |||
| by <index>-th expression. | |||
| Note that all '<..>' forms in a block must have the same number of | |||
| comma-separated entries. | |||
| Predefined named template rules: | |||
| <prefix=s,d,c,z> | |||
| <ftype=real,double precision,complex,double complex> | |||
| <ftypereal=real,double precision,\\0,\\1> | |||
| <ctype=float,double,complex_float,complex_double> | |||
| <ctypereal=float,double,\\0,\\1> | |||
| """ | |||
| routine_start_re = re.compile( | |||
| r'(\n|\A)(( (\$|\*))|)\s*(subroutine|function)\b', | |||
| re.I | |||
| ) | |||
| routine_end_re = re.compile(r'\n\s*end\s*(subroutine|function)\b.*(\n|\Z)', re.I) | |||
| function_start_re = re.compile(r'\n (\$|\*)\s*function\b', re.I) | |||
| def parse_structure(astr): | |||
| """ Return a list of tuples for each function or subroutine each | |||
| tuple is the start and end of a subroutine or function to be | |||
| expanded. | |||
| """ | |||
| spanlist = [] | |||
| ind = 0 | |||
| while True: | |||
| m = routine_start_re.search(astr, ind) | |||
| if m is None: | |||
| break | |||
| start = m.start() | |||
| if function_start_re.match(astr, start, m.end()): | |||
| while True: | |||
| i = astr.rfind('\n', ind, start) | |||
| if i==-1: | |||
| break | |||
| start = i | |||
| if astr[i:i+7]!='\n $': | |||
| break | |||
| start += 1 | |||
| m = routine_end_re.search(astr, m.end()) | |||
| ind = end = m and m.end()-1 or len(astr) | |||
| spanlist.append((start, end)) | |||
| return spanlist | |||
| template_re = re.compile(r"<\s*(\w[\w\d]*)\s*>") | |||
| named_re = re.compile(r"<\s*(\w[\w\d]*)\s*=\s*(.*?)\s*>") | |||
| list_re = re.compile(r"<\s*((.*?))\s*>") | |||
| def find_repl_patterns(astr): | |||
| reps = named_re.findall(astr) | |||
| names = {} | |||
| for rep in reps: | |||
| name = rep[0].strip() or unique_key(names) | |||
| repl = rep[1].replace(r'\,', '@comma@') | |||
| thelist = conv(repl) | |||
| names[name] = thelist | |||
| return names | |||
| def find_and_remove_repl_patterns(astr): | |||
| names = find_repl_patterns(astr) | |||
| astr = re.subn(named_re, '', astr)[0] | |||
| return astr, names | |||
| item_re = re.compile(r"\A\\(?P<index>\d+)\Z") | |||
| def conv(astr): | |||
| b = astr.split(',') | |||
| l = [x.strip() for x in b] | |||
| for i in range(len(l)): | |||
| m = item_re.match(l[i]) | |||
| if m: | |||
| j = int(m.group('index')) | |||
| l[i] = l[j] | |||
| return ','.join(l) | |||
| def unique_key(adict): | |||
| """ Obtain a unique key given a dictionary.""" | |||
| allkeys = list(adict.keys()) | |||
| done = False | |||
| n = 1 | |||
| while not done: | |||
| newkey = '__l%s' % (n) | |||
| if newkey in allkeys: | |||
| n += 1 | |||
| else: | |||
| done = True | |||
| return newkey | |||
| template_name_re = re.compile(r'\A\s*(\w[\w\d]*)\s*\Z') | |||
| def expand_sub(substr, names): | |||
| substr = substr.replace(r'\>', '@rightarrow@') | |||
| substr = substr.replace(r'\<', '@leftarrow@') | |||
| lnames = find_repl_patterns(substr) | |||
| substr = named_re.sub(r"<\1>", substr) # get rid of definition templates | |||
| def listrepl(mobj): | |||
| thelist = conv(mobj.group(1).replace(r'\,', '@comma@')) | |||
| if template_name_re.match(thelist): | |||
| return "<%s>" % (thelist) | |||
| name = None | |||
| for key in lnames.keys(): # see if list is already in dictionary | |||
| if lnames[key] == thelist: | |||
| name = key | |||
| if name is None: # this list is not in the dictionary yet | |||
| name = unique_key(lnames) | |||
| lnames[name] = thelist | |||
| return "<%s>" % name | |||
| substr = list_re.sub(listrepl, substr) # convert all lists to named templates | |||
| # newnames are constructed as needed | |||
| numsubs = None | |||
| base_rule = None | |||
| rules = {} | |||
| for r in template_re.findall(substr): | |||
| if r not in rules: | |||
| thelist = lnames.get(r, names.get(r, None)) | |||
| if thelist is None: | |||
| raise ValueError('No replicates found for <%s>' % (r)) | |||
| if r not in names and not thelist.startswith('_'): | |||
| names[r] = thelist | |||
| rule = [i.replace('@comma@', ',') for i in thelist.split(',')] | |||
| num = len(rule) | |||
| if numsubs is None: | |||
| numsubs = num | |||
| rules[r] = rule | |||
| base_rule = r | |||
| elif num == numsubs: | |||
| rules[r] = rule | |||
| else: | |||
| print("Mismatch in number of replacements (base <{}={}>) " | |||
| "for <{}={}>. Ignoring." | |||
| .format(base_rule, ','.join(rules[base_rule]), r, thelist)) | |||
| if not rules: | |||
| return substr | |||
| def namerepl(mobj): | |||
| name = mobj.group(1) | |||
| return rules.get(name, (k+1)*[name])[k] | |||
| newstr = '' | |||
| for k in range(numsubs): | |||
| newstr += template_re.sub(namerepl, substr) + '\n\n' | |||
| newstr = newstr.replace('@rightarrow@', '>') | |||
| newstr = newstr.replace('@leftarrow@', '<') | |||
| return newstr | |||
| def process_str(allstr): | |||
| newstr = allstr | |||
| writestr = '' | |||
| struct = parse_structure(newstr) | |||
| oldend = 0 | |||
| names = {} | |||
| names.update(_special_names) | |||
| for sub in struct: | |||
| cleanedstr, defs = find_and_remove_repl_patterns(newstr[oldend:sub[0]]) | |||
| writestr += cleanedstr | |||
| names.update(defs) | |||
| writestr += expand_sub(newstr[sub[0]:sub[1]], names) | |||
| oldend = sub[1] | |||
| writestr += newstr[oldend:] | |||
| return writestr | |||
| include_src_re = re.compile( | |||
| r"(\n|\A)\s*include\s*['\"](?P<name>[\w\d./\\]+\.src)['\"]", | |||
| re.I | |||
| ) | |||
| def resolve_includes(source): | |||
| d = os.path.dirname(source) | |||
| with open(source) as fid: | |||
| lines = [] | |||
| for line in fid: | |||
| m = include_src_re.match(line) | |||
| if m: | |||
| fn = m.group('name') | |||
| if not os.path.isabs(fn): | |||
| fn = os.path.join(d, fn) | |||
| if os.path.isfile(fn): | |||
| lines.extend(resolve_includes(fn)) | |||
| else: | |||
| lines.append(line) | |||
| else: | |||
| lines.append(line) | |||
| return lines | |||
| def process_file(source): | |||
| lines = resolve_includes(source) | |||
| return process_str(''.join(lines)) | |||
| _special_names = find_repl_patterns(''' | |||
| <_c=s,d,c,z> | |||
| <_t=real,double precision,complex,double complex> | |||
| <prefix=s,d,c,z> | |||
| <ftype=real,double precision,complex,double complex> | |||
| <ctype=float,double,complex_float,complex_double> | |||
| <ftypereal=real,double precision,\\0,\\1> | |||
| <ctypereal=float,double,\\0,\\1> | |||
| ''') | |||
| # END OF CODE VENDORED FROM `numpy.distutils.from_template` | |||
| ########################################################### | |||
| def main(): | |||
| parser = argparse.ArgumentParser() | |||
| parser.add_argument("infile", type=str, | |||
| help="Path to the input file") | |||
| parser.add_argument("-o", "--outdir", type=str, | |||
| help="Path to the output directory") | |||
| args = parser.parse_args() | |||
| if not args.infile.endswith(('.pyf', '.pyf.src', '.f.src')): | |||
| raise ValueError(f"Input file has unknown extension: {args.infile}") | |||
| outdir_abs = os.path.join(os.getcwd(), args.outdir) | |||
| # Write out the .pyf/.f file | |||
| if args.infile.endswith(('.pyf.src', '.f.src')): | |||
| code = process_file(args.infile) | |||
| fname_pyf = os.path.join(args.outdir, | |||
| os.path.splitext(os.path.split(args.infile)[1])[0]) | |||
| with open(fname_pyf, 'w') as f: | |||
| f.write(code) | |||
| else: | |||
| fname_pyf = args.infile | |||
| # Now invoke f2py to generate the C API module file | |||
| if args.infile.endswith(('.pyf.src', '.pyf')): | |||
| p = subprocess.Popen([sys.executable, '-m', 'numpy.f2py', fname_pyf, | |||
| '--build-dir', outdir_abs], #'--quiet'], | |||
| stdout=subprocess.PIPE, stderr=subprocess.PIPE, | |||
| cwd=os.getcwd()) | |||
| out, err = p.communicate() | |||
| if not (p.returncode == 0): | |||
| raise RuntimeError(f"Writing {args.outfile} with f2py failed!\n" | |||
| f"{out}\n" | |||
| r"{err}") | |||
| if __name__ == "__main__": | |||
| main() | |||
| @@ -0,0 +1,50 @@ | |||
| # find numpy & f2py includes | |||
| inc_numpy = run_command(py3, | |||
| ['-c', 'import os; os.chdir(".."); import numpy; print(numpy.get_include())'], | |||
| check : true | |||
| ).stdout().strip() | |||
| inc_f2py = run_command(py3, | |||
| ['-c', 'import os; os.chdir(".."); import numpy.f2py; print(numpy.f2py.get_include())'], | |||
| check : true | |||
| ).stdout().strip() | |||
| inc_np = include_directories(inc_numpy, inc_f2py) | |||
| fortranobject_c = inc_f2py / 'fortranobject.c' | |||
| fortranobject_lib = static_library('_fortranobject', | |||
| fortranobject_c, | |||
| # c_args: numpy_nodepr_api, | |||
| dependencies: py3_dep, | |||
| include_directories: [inc_np, inc_f2py], | |||
| gnu_symbol_visibility: 'hidden', | |||
| ) | |||
| fortranobject_dep = declare_dependency( | |||
| link_with: fortranobject_lib, | |||
| include_directories: [inc_np, inc_f2py], | |||
| ) | |||
| # f2py generated wrappers | |||
| flapack_module = custom_target('flapack_module', | |||
| output: ['_flapackmodule.c'], | |||
| input: 'blas_lapack.pyf.src', | |||
| command: [generate_f2pymod, '@INPUT@', '-o', '@OUTDIR@'], | |||
| ) | |||
| py3.extension_module('_flapack', | |||
| flapack_module, | |||
| link_args: [], # version_link_args, | |||
| dependencies: [openblas_dep, fortranobject_dep], | |||
| install: true, | |||
| subdir: 'openblas_wrap' | |||
| ) | |||
| py3.install_sources( | |||
| ['__init__.py'], | |||
| subdir: 'openblas_wrap' | |||
| ) | |||
| @@ -0,0 +1,22 @@ | |||
| [build-system] | |||
| build-backend = "mesonpy" | |||
| requires = [ | |||
| "meson-python>=0.16.0", | |||
| "numpy", | |||
| "scipy_openblas32" | |||
| ] | |||
| [project] | |||
| name = "openblas_wrap" | |||
| version = "0.1" | |||
| maintainers = [ | |||
| {name = ".", email = ".@gmail.com"} | |||
| ] | |||
| description = "a wrapper" | |||
| requires-python = ">=3.10" | |||
| dependencies = ["numpy>=1.23,<3", | |||
| "scipy_openblas32" | |||
| ] | |||