-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdftd_cblas.h
More file actions
291 lines (271 loc) · 6.57 KB
/
Copy pathdftd_cblas.h
File metadata and controls
291 lines (271 loc) · 6.57 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* This file contains code adapted from the ORCA quantum chemistry program.
* ORCA is developed by the group of Prof. Frank Neese at the
* Max-Planck-Institut für Kohlenforschung, Mülheim an der Ruhr and FAccTs GmbH.
* ORCA is licensed by the Max-Planck-Institut für Kohlenforschung and FAccTs
* GmbH.
*
* The inclusion of ORCA code in this file has been done with the explicit
* permission of the ORCA developers.
*
* For reuse or licensing of this code, please contact the ORCA team at the
* Max-Planck-Institut für Kohlenforschung (https://orcaforum.kofo.mpg.de/) or
* FAccTs GmbH (https://www.faccts.de/).
*/
#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 (Transpose) {
if (A.cols == C.N && A.rows == V.N) {
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 {
if (A.rows == C.N && A.cols == V.N) {
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) {
delete[] ipiv;
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
);
delete[] ipiv;
if (info != 0) { return EXIT_FAILURE; }
return EXIT_SUCCESS;
}
/**
* @brief Solve a symmetric linear system A * X = B for X.
*
* This routine factorizes a symmetric matrix A using Bunch-Kaufman
* factorization and solves for the right-hand side vector B. The matrix A is
* overwritten by its factorization. The solution overwrites B.
*
* @param A Symmetric matrix of size (m x m). Overwritten by the factorization.
* @param B Right-hand side vector of size m. Overwritten by the solution.
* @return int Returns EXIT_SUCCESS (0) on success, EXIT_FAILURE (1) on error.
*/
inline int BLAS_SolveSymmetric(
TMatrix<double> &A, // symmetric matrix
TVector<double> &B // RHS vector (becomes solution)
) {
const lapack_int m = A.rows;
const lapack_int nrhs = 1;
if (A.cols != m || B.N != m) {
fprintf(stderr, "BLAS_SolveSymmetric error: dimension mismatch\n");
return EXIT_FAILURE;
}
lapack_int info;
lapack_int *ipiv = new lapack_int[A.rows];
// Factorization
info = LAPACKE_dsytrf(LAPACK_ROW_MAJOR, 'L', m, A.p, m, ipiv);
if (info != 0) {
delete[] ipiv;
fprintf(stderr, "dsytrf failed: info=%d\n", (int)info);
return EXIT_FAILURE;
}
// Solve for all RHS columns
info =
LAPACKE_dsytrs(LAPACK_ROW_MAJOR, 'L', m, nrhs, A.p, m, ipiv, B.p, nrhs);
delete[] ipiv;
if (info != 0) {
fprintf(stderr, "dsytrs failed: info=%d\n", (int)info);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
} // namespace dftd4