-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdftd_cblas.h
240 lines (226 loc) · 5.24 KB
/
dftd_cblas.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* This file is part of cpp-d4.
*
* Copyright (C) 2019 Sebastian Ehlert, Marvin Friede
*
* cpp-d4 is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* cpp-d4 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with cpp-d4. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "dftd_matrix.h"
#include "cblas.h"
#include "lapacke.h"
namespace dftd4 {
/**
* @brief General matrix vector multiplication (`C = alpha * A * V + C`).
*
* @param C Result vector C. Modified in-place.
* @param A Matrix A.
* @param V Vector V.
* @param Transpose Specifies whether to transpose matrix A.
* @param alpha Scaling factor for the product of matrix A and vector X.
* @return Exit code
*/
inline int BLAS_Add_Mat_x_Vec(
TVector<double> &C,
TMatrix<double> &A,
TVector<double> &V,
bool Transpose,
double alpha
) {
if (A.rows == C.N && A.cols == V.N) {
if (Transpose) {
cblas_dgemv(
CblasRowMajor,
CblasTrans,
A.rows,
A.cols,
alpha,
A.p,
A.cols,
V.p,
1,
1.0,
C.p,
1
);
return EXIT_SUCCESS;
} else {
cblas_dgemv(
CblasRowMajor,
CblasNoTrans,
A.rows,
A.cols,
alpha,
A.p,
A.cols,
V.p,
1,
1.0,
C.p,
1
);
return EXIT_SUCCESS;
};
};
return EXIT_FAILURE;
};
/**
* @brief General matrix-matrix multiplication (`C = alpha * A * B + C`).
*
* @param C Result matrix C. Modified in-place.
* @param A Matrix A.
* @param B Matrix B.
* @param TransposeA Specifies whether to transpose matrix A.
* @param TransposeB Specifies whether to transpose matrix B.
* @param alpha Scaling factor for the product of matrix A and matrix B.
* @return Exit code.
*/
inline int BLAS_Add_Mat_x_Mat(
TMatrix<double> &C,
const TMatrix<double> &A,
const TMatrix<double> &B,
const bool TransposeA,
const bool TransposeB,
const double alpha
) {
// check for size 0 matrices
if (A.cols == 0 || A.rows == 0 || B.cols == 0 || B.rows == 0 || C.cols == 0 ||
C.rows == 0)
exit(EXIT_FAILURE);
// check for transpositions
if (!TransposeA) {
if (!TransposeB) {
// check dimensions
if (A.cols != B.rows || A.rows != C.rows || B.cols != C.cols) {
exit(EXIT_FAILURE);
};
cblas_dgemm(
CblasRowMajor,
CblasNoTrans,
CblasNoTrans,
C.rows,
C.cols,
A.cols,
alpha,
A.p,
A.cols,
B.p,
B.cols,
1.0,
C.p,
C.cols
);
} // B not transposed
else {
// check dimensions for C=A*BT
if (A.cols != B.cols || A.rows != C.rows || B.rows != C.cols) {
exit(EXIT_FAILURE);
};
// B is transposed, A not
cblas_dgemm(
CblasRowMajor,
CblasNoTrans,
CblasTrans,
C.rows,
C.cols,
A.cols,
alpha,
A.p,
A.cols,
B.p,
B.cols,
1.0,
C.p,
C.cols
);
}; // B transposed
} // A not transposed
else {
if (!TransposeB) {
// check dimensions for C=AT*B
if (A.rows != B.rows || A.cols != C.rows || B.cols != C.cols) {
exit(EXIT_FAILURE);
};
// A is transposed and B not
cblas_dgemm(
CblasRowMajor,
CblasTrans,
CblasNoTrans,
C.rows,
C.cols,
A.rows,
alpha,
A.p,
A.cols,
B.p,
B.cols,
1.0,
C.p,
C.cols
);
} // B not transposed
else {
// check dimensions for C=AT*BT
if (A.rows != B.cols || A.cols != C.rows || B.rows != C.cols) {
exit(EXIT_FAILURE);
};
// both are transposed
cblas_dgemm(
CblasRowMajor,
CblasTrans,
CblasTrans,
C.rows,
C.cols,
A.rows,
alpha,
A.p,
A.cols,
B.p,
B.cols,
1.0,
C.p,
C.cols
);
}; // B transposed
};
return EXIT_SUCCESS;
};
/**
* @brief Compute inverse of a matrix using LU decomposition.
*
* @param a Matrix a.
* @return Exit code.
*/
inline int BLAS_InvertMatrix(TMatrix<double> &a) {
if (a.rows != a.cols) { return EXIT_FAILURE; }
lapack_int info;
lapack_int *ipiv = new lapack_int[a.rows];
// LU factorization of a general m-by-n matrix
info = LAPACKE_dgetrf(
LAPACK_ROW_MAJOR,
(lapack_int)a.rows,
(lapack_int)a.cols,
a.p,
(lapack_int)a.cols,
ipiv
);
if (info != 0) { return EXIT_FAILURE; }
// Inverse of an LU-factored general matrix
info = LAPACKE_dgetri(
LAPACK_ROW_MAJOR, (lapack_int)a.rows, a.p, (lapack_int)a.cols, ipiv
);
if (info != 0) { return EXIT_FAILURE; }
delete[] ipiv;
return EXIT_SUCCESS;
};
} // namespace dftd4