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 int info = 0; 79 int * ipiv = (int*)alloca(sizeof(int)*N); 80 BLAS_FUNC(getrf)(&N, &N, C, &N, ipiv, &info); 81 if(info!=0) std::cerr << "getrf_ error " << info << "\n"; 82 } 83 trisolve_lower(const gene_matrix & L,const gene_vector & B,gene_vector & X,int N)84 static inline void trisolve_lower(const gene_matrix & L, const gene_vector& B, gene_vector & X, int N){ 85 BLAS_FUNC(copy)(&N, B, &intone, X, &intone); 86 BLAS_FUNC(trsv)(&lower, ¬rans, &nonunit, &N, L, &N, X, &intone); 87 } 88 trisolve_lower_matrix(const gene_matrix & L,const gene_matrix & B,gene_matrix & X,int N)89 static inline void trisolve_lower_matrix(const gene_matrix & L, const gene_matrix& B, gene_matrix & X, int N){ 90 BLAS_FUNC(copy)(&N, B, &intone, X, &intone); 91 BLAS_FUNC(trsm)(&right, &lower, ¬rans, &nonunit, &N, &N, &fone, L, &N, X, &N); 92 } 93 trmm(gene_matrix & A,gene_matrix & B,gene_matrix &,int N)94 static inline void trmm(gene_matrix & A, gene_matrix & B, gene_matrix & /*X*/, int N){ 95 BLAS_FUNC(trmm)(&left, &lower, ¬rans,&nonunit, &N,&N,&fone,A,&N,B,&N); 96 } 97 98 #ifdef HAS_LAPACK 99 lu_decomp(const gene_matrix & X,gene_matrix & C,int N)100 static inline void lu_decomp(const gene_matrix & X, gene_matrix & C, int N){ 101 int N2 = N*N; 102 BLAS_FUNC(copy)(&N2, X, &intone, C, &intone); 103 int info = 0; 104 int * ipiv = (int*)alloca(sizeof(int)*N); 105 int * jpiv = (int*)alloca(sizeof(int)*N); 106 BLAS_FUNC(getc2)(&N, C, &N, ipiv, jpiv, &info); 107 } 108 109 110 hessenberg(const gene_matrix & X,gene_matrix & C,int N)111 static inline void hessenberg(const gene_matrix & X, gene_matrix & C, int N){ 112 { 113 int N2 = N*N; 114 int inc = 1; 115 BLAS_FUNC(copy)(&N2, X, &inc, C, &inc); 116 } 117 int info = 0; 118 int ilo = 1; 119 int ihi = N; 120 int bsize = 64; 121 int worksize = N*bsize; 122 SCALAR* d = new SCALAR[N+worksize]; 123 BLAS_FUNC(gehrd)(&N, &ilo, &ihi, C, &N, d, d+N, &worksize, &info); 124 delete[] d; 125 } 126 tridiagonalization(const gene_matrix & X,gene_matrix & C,int N)127 static inline void tridiagonalization(const gene_matrix & X, gene_matrix & C, int N){ 128 { 129 int N2 = N*N; 130 int inc = 1; 131 BLAS_FUNC(copy)(&N2, X, &inc, C, &inc); 132 } 133 char uplo = 'U'; 134 int info = 0; 135 int bsize = 64; 136 int worksize = N*bsize; 137 SCALAR* d = new SCALAR[3*N+worksize]; 138 BLAS_FUNC(sytrd)(&uplo, &N, C, &N, d, d+N, d+2*N, d+3*N, &worksize, &info); 139 delete[] d; 140 } 141 142 #endif // HAS_LAPACK 143 144 }; 145 146 SCALAR blas_interface<SCALAR>::fone = SCALAR(1); 147 SCALAR blas_interface<SCALAR>::fzero = SCALAR(0); 148