blob: fc4ba2a1f39954351cf520bafd649f118280926b [file] [log] [blame]
Narayan Kamathc981c482012-11-02 10:59:05 +00001
2#define BLAS_FUNC(NAME) CAT(CAT(SCALAR_PREFIX,NAME),_)
3
4template<> class blas_interface<SCALAR> : public c_interface_base<SCALAR>
5{
6
7public :
8
9 static SCALAR fone;
10 static SCALAR fzero;
11
12 static inline std::string name()
13 {
14 return MAKE_STRING(CBLASNAME);
15 }
16
17 static inline void matrix_vector_product(gene_matrix & A, gene_vector & B, gene_vector & X, int N){
18 BLAS_FUNC(gemv)(&notrans,&N,&N,&fone,A,&N,B,&intone,&fzero,X,&intone);
19 }
20
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
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
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
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
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
41 static inline void matrix_matrix_product(gene_matrix & A, gene_matrix & B, gene_matrix & X, int N){
42 BLAS_FUNC(gemm)(&notrans,&notrans,&N,&N,&N,&fone,A,&N,B,&N,&fzero,X,&N);
43 }
44
45 static inline void transposed_matrix_matrix_product(gene_matrix & A, gene_matrix & B, gene_matrix & X, int N){
46 BLAS_FUNC(gemm)(&notrans,&notrans,&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
53 static inline void aat_product(gene_matrix & A, gene_matrix & X, int N){
54 BLAS_FUNC(syrk)(&lower,&notrans,&N,&N,&fone,A,&N,&fzero,X,&N);
55 }
56
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
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
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
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);
Narayan Kamathc981c482012-11-02 10:59:05 +000078 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
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, &notrans, &nonunit, &N, L, &N, X, &intone);
87 }
88
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, &notrans, &nonunit, &N, &N, &fone, L, &N, X, &N);
92 }
93
Miao Wang2b8756b2017-03-06 13:45:08 -080094 static inline void trmm(gene_matrix & A, gene_matrix & B, gene_matrix & /*X*/, int N){
Narayan Kamathc981c482012-11-02 10:59:05 +000095 BLAS_FUNC(trmm)(&left, &lower, &notrans,&nonunit, &N,&N,&fone,A,&N,B,&N);
96 }
97
98 #ifdef HAS_LAPACK
99
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);
Narayan Kamathc981c482012-11-02 10:59:05 +0000103 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
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
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;
Narayan Kamathc981c482012-11-02 10:59:05 +0000135 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
146SCALAR blas_interface<SCALAR>::fone = SCALAR(1);
147SCALAR blas_interface<SCALAR>::fzero = SCALAR(0);