1 2 #define BLAS_FUNC(NAME) CAT(CAT(SCALAR_PREFIX,NAME),_) 3 4 template<> class blas_interface<SCALAR> : public c_interface_base<SCALAR> 5 { 6 7 public : 8 9 static SCALAR fone; 10 static SCALAR fzero; 11 name()12 static inline std::string name() 13 { 14 return MAKE_STRING(CBLASNAME); 15 } 16 matrix_vector_product(gene_matrix & A,gene_vector & B,gene_vector & X,int N)17 static inline void matrix_vector_product(gene_matrix & A, gene_vector & B, gene_vector & X, int N){ 18 BLAS_FUNC(gemv)(¬rans,&N,&N,&fone,A,&N,B,&intone,&fzero,X,&intone); 19 } 20 symv(gene_matrix & A,gene_vector & B,gene_vector & X,int N)21 static inline void symv(gene_matrix & A, gene_vector & B, gene_vector & X, int N){ 22 BLAS_FUNC(symv)(&lower, &N,&fone,A,&N,B,&intone,&fzero,X,&intone); 23 } 24 syr2(gene_matrix & A,gene_vector & B,gene_vector & X,int N)25 static inline void syr2(gene_matrix & A, gene_vector & B, gene_vector & X, int N){ 26 BLAS_FUNC(syr2)(&lower,&N,&fone,B,&intone,X,&intone,A,&N); 27 } 28 ger(gene_matrix & A,gene_vector & X,gene_vector & Y,int N)29 static inline void ger(gene_matrix & A, gene_vector & X, gene_vector & Y, int N){ 30 BLAS_FUNC(ger)(&N,&N,&fone,X,&intone,Y,&intone,A,&N); 31 } 32 rot(gene_vector & A,gene_vector & B,SCALAR c,SCALAR s,int N)33 static inline void rot(gene_vector & A, gene_vector & B, SCALAR c, SCALAR s, int N){ 34 BLAS_FUNC(rot)(&N,A,&intone,B,&intone,&c,&s); 35 } 36 atv_product(gene_matrix & A,gene_vector & B,gene_vector & X,int N)37 static inline void atv_product(gene_matrix & A, gene_vector & B, gene_vector & X, int N){ 38 BLAS_FUNC(gemv)(&trans,&N,&N,&fone,A,&N,B,&intone,&fzero,X,&intone); 39 } 40 matrix_matrix_product(gene_matrix & A,gene_matrix & B,gene_matrix & X,int N)41 static inline void matrix_matrix_product(gene_matrix & A, gene_matrix & B, gene_matrix & X, int N){ 42 BLAS_FUNC(gemm)(¬rans,¬rans,&N,&N,&N,&fone,A,&N,B,&N,&fzero,X,&N); 43 } 44 transposed_matrix_matrix_product(gene_matrix & A,gene_matrix & B,gene_matrix & X,int N)45 static inline void transposed_matrix_matrix_product(gene_matrix & A, gene_matrix & B, gene_matrix & X, int N){ 46 BLAS_FUNC(gemm)(¬rans,¬rans,&N,&N,&N,&fone,A,&N,B,&N,&fzero,X,&N); 47 } 48 49 // static inline void ata_product(gene_matrix & A, gene_matrix & X, int N){ 50 // ssyrk_(&lower,&trans,&N,&N,&fone,A,&N,&fzero,X,&N); 51 // } 52 aat_product(gene_matrix & A,gene_matrix & X,int N)53 static inline void aat_product(gene_matrix & A, gene_matrix & X, int N){ 54 BLAS_FUNC(syrk)(&lower,¬rans,&N,&N,&fone,A,&N,&fzero,X,&N); 55 } 56 axpy(SCALAR coef,const gene_vector & X,gene_vector & Y,int N)57 static inline void axpy(SCALAR coef, const gene_vector & X, gene_vector & Y, int N){ 58 BLAS_FUNC(axpy)(&N,&coef,X,&intone,Y,&intone); 59 } 60 axpby(SCALAR a,const gene_vector & X,SCALAR b,gene_vector & Y,int N)61 static inline void axpby(SCALAR a, const gene_vector & X, SCALAR b, gene_vector & Y, int N){ 62 BLAS_FUNC(scal)(&N,&b,Y,&intone); 63 BLAS_FUNC(axpy)(&N,&a,X,&intone,Y,&intone); 64 } 65 cholesky(const gene_matrix & X,gene_matrix & C,int N)66 static inline void cholesky(const gene_matrix & X, gene_matrix & C, int N){ 67 int N2 = N*N; 68 BLAS_FUNC(copy)(&N2, X, &intone, C, &intone); 69 char uplo = 'L'; 70 int info = 0; 71 BLAS_FUNC(potrf)(&uplo, &N, C, &N, &info); 72 if(info!=0) std::cerr << "potrf_ error " << info << "\n"; 73 } 74 partial_lu_decomp(const gene_matrix & X,gene_matrix & C,int N)75 static inline void partial_lu_decomp(const gene_matrix & X, gene_matrix & C, int N){ 76 int N2 = N*N; 77 BLAS_FUNC(copy)(&N2, X, &intone, C, &intone); 78 char uplo = 'L'; 79 int info = 0; 80 int * ipiv = (int*)alloca(sizeof(int)*N); 81 BLAS_FUNC(getrf)(&N, &N, C, &N, ipiv, &info); 82 if(info!=0) std::cerr << "getrf_ error " << info << "\n"; 83 } 84 trisolve_lower(const gene_matrix & L,const gene_vector & B,gene_vector & X,int N)85 static inline void trisolve_lower(const gene_matrix & L, const gene_vector& B, gene_vector & X, int N){ 86 BLAS_FUNC(copy)(&N, B, &intone, X, &intone); 87 BLAS_FUNC(trsv)(&lower, ¬rans, &nonunit, &N, L, &N, X, &intone); 88 } 89 trisolve_lower_matrix(const gene_matrix & L,const gene_matrix & B,gene_matrix & X,int N)90 static inline void trisolve_lower_matrix(const gene_matrix & L, const gene_matrix& B, gene_matrix & X, int N){ 91 BLAS_FUNC(copy)(&N, B, &intone, X, &intone); 92 BLAS_FUNC(trsm)(&right, &lower, ¬rans, &nonunit, &N, &N, &fone, L, &N, X, &N); 93 } 94 trmm(gene_matrix & A,gene_matrix & B,gene_matrix & X,int N)95 static inline void trmm(gene_matrix & A, gene_matrix & B, gene_matrix & X, int N){ 96 BLAS_FUNC(trmm)(&left, &lower, ¬rans,&nonunit, &N,&N,&fone,A,&N,B,&N); 97 } 98 99 #ifdef HAS_LAPACK 100 lu_decomp(const gene_matrix & X,gene_matrix & C,int N)101 static inline void lu_decomp(const gene_matrix & X, gene_matrix & C, int N){ 102 int N2 = N*N; 103 BLAS_FUNC(copy)(&N2, X, &intone, C, &intone); 104 char uplo = 'L'; 105 int info = 0; 106 int * ipiv = (int*)alloca(sizeof(int)*N); 107 int * jpiv = (int*)alloca(sizeof(int)*N); 108 BLAS_FUNC(getc2)(&N, C, &N, ipiv, jpiv, &info); 109 } 110 111 112 hessenberg(const gene_matrix & X,gene_matrix & C,int N)113 static inline void hessenberg(const gene_matrix & X, gene_matrix & C, int N){ 114 { 115 int N2 = N*N; 116 int inc = 1; 117 BLAS_FUNC(copy)(&N2, X, &inc, C, &inc); 118 } 119 int info = 0; 120 int ilo = 1; 121 int ihi = N; 122 int bsize = 64; 123 int worksize = N*bsize; 124 SCALAR* d = new SCALAR[N+worksize]; 125 BLAS_FUNC(gehrd)(&N, &ilo, &ihi, C, &N, d, d+N, &worksize, &info); 126 delete[] d; 127 } 128 tridiagonalization(const gene_matrix & X,gene_matrix & C,int N)129 static inline void tridiagonalization(const gene_matrix & X, gene_matrix & C, int N){ 130 { 131 int N2 = N*N; 132 int inc = 1; 133 BLAS_FUNC(copy)(&N2, X, &inc, C, &inc); 134 } 135 char uplo = 'U'; 136 int info = 0; 137 int ilo = 1; 138 int ihi = N; 139 int bsize = 64; 140 int worksize = N*bsize; 141 SCALAR* d = new SCALAR[3*N+worksize]; 142 BLAS_FUNC(sytrd)(&uplo, &N, C, &N, d, d+N, d+2*N, d+3*N, &worksize, &info); 143 delete[] d; 144 } 145 146 #endif // HAS_LAPACK 147 148 }; 149 150 SCALAR blas_interface<SCALAR>::fone = SCALAR(1); 151 SCALAR blas_interface<SCALAR>::fzero = SCALAR(0); 152