Repository navigation
Expand file tree
/
Copy pathdngvd_op.cpp
More file actions
341 lines (306 loc) · 12.2 KB
/
Copy pathdngvd_op.cpp
File metadata and controls
341 lines (306 loc) · 12.2 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "source_hsolver/kernels/dngvd_op.h"
#include <algorithm>
#include <fstream>
#include <iostream>
namespace hsolver
{
template <typename T>
struct dngvd_op<T, base_device::DEVICE_CPU>
{
using Real = typename GetTypeReal<T>::type;
void operator()(const base_device::DEVICE_CPU* d,
const int nstart,
const int ldh,
const T* hcc,
const T* scc,
Real* eigenvalue,
T* vcc)
{
for (int i = 0; i < nstart * ldh; i++)
{
vcc[i] = hcc[i];
}
int info = 0;
int lwork = 2 * nstart + nstart * nstart;
T* work = new T[lwork];
Parallel_Reduce::ZEROS(work, lwork);
int lrwork = 1 + 5 * nstart + 2 * nstart * nstart;
Real* rwork = new Real[lrwork];
Parallel_Reduce::ZEROS(rwork, lrwork);
int liwork = 3 + 5 * nstart;
int* iwork = new int[liwork];
Parallel_Reduce::ZEROS(iwork, liwork);
//===========================
// calculate all eigenvalues
//===========================
LapackWrapper::xhegvd(1,
'V',
'U',
nstart,
vcc,
ldh,
scc,
ldh,
eigenvalue,
work,
lwork,
rwork,
lrwork,
iwork,
liwork,
info);
if (info != 0)
{
std::cout << "Error: xhegvd failed, linear dependent basis functions\n"
<< ", wrong initialization of wavefunction, or wavefunction information loss\n"
<< ", output overlap matrix scc.txt to check\n"
<< std::endl;
// print scc to file scc.txt
std::ofstream ofs("scc.txt");
for (int i = 0; i < nstart; i++)
{
for (int j = 0; j < nstart; j++)
{
ofs << scc[i * ldh + j] << " ";
}
ofs << std::endl;
}
ofs.close();
}
assert(0 == info);
delete[] work;
delete[] rwork;
delete[] iwork;
}
};
template <typename T>
struct dngv_op<T, base_device::DEVICE_CPU>
{
using Real = typename GetTypeReal<T>::type;
void operator()(const base_device::DEVICE_CPU* d,
const int nbase,
const int ldh,
const T* hcc,
T* scc,
Real* eigenvalue,
T* vcc)
{
for (int i = 0; i < nbase * ldh; i++)
{
vcc[i] = hcc[i];
}
int info = 0;
int lwork = 2 * nbase - 1;
T* work = new T[lwork];
Parallel_Reduce::ZEROS(work, lwork);
int lrwork = 3 * nbase - 2;
Real* rwork = new Real[lrwork];
Parallel_Reduce::ZEROS(rwork, lrwork);
//===========================
// calculate all eigenvalues
//===========================
LapackWrapper::xhegv(1, 'V', 'U', nbase, vcc, ldh, scc, ldh, eigenvalue, work, lwork, rwork, info);
if (info != 0)
{
std::cout << "Error: xhegv failed, linear dependent basis functions\n"
<< ", wrong initialization of wavefunction, or wavefunction information loss\n"
<< ", output overlap matrix scc.txt to check\n"
<< std::endl;
// print scc to file scc.txt
std::ofstream ofs("scc.txt");
for (int i = 0; i < nbase; i++)
{
for (int j = 0; j < nbase; j++)
{
ofs << scc[i * ldh + j] << " ";
}
ofs << std::endl;
}
ofs.close();
}
assert(0 == info);
delete[] work;
delete[] rwork;
}
};
template <typename T>
struct dnevx_op<T, base_device::DEVICE_CPU>
{
using Real = typename GetTypeReal<T>::type;
void operator()(const base_device::DEVICE_CPU* /*ctx*/,
const int nstart,
const int ldh,
const T* hcc, // hcc
const int nbands, // nbands
Real* eigenvalue, // eigenvalue
T* vcc) // vcc
{
T* aux = new T[nstart * ldh];
for (int ii = 0; ii < nstart * ldh; ii++)
{
aux[ii] = hcc[ii];
}
int info = 0;
int lwork = -1;
T* work = new T[1];
Real* rwork = new Real[7 * nstart];
int* iwork = new int[5 * nstart];
int* ifail = new int[nstart];
// When lwork = -1, the demension of work will be assumed
// Assume the denmension of work by output work[0]
LapackWrapper::xheevx(
1, // ITYPE = 1: A*x = (lambda)*B*x
'V', // JOBZ = 'V': Compute eigenvalues and eigenvectors.
'I', // RANGE = 'I': the IL-th through IU-th eigenvalues will be found.
'L', // UPLO = 'L': Lower triangles of A and B are stored.
nstart, // N = base
aux, // A is COMPLEX*16 array dimension (LDA, N)
ldh, // LDA = base
0.0, // Not referenced if RANGE = 'A' or 'I'.
0.0, // Not referenced if RANGE = 'A' or 'I'.
1, // IL: If RANGE='I', the index of the smallest eigenvalue to be returned. 1 <= IL <= IU <= N,
nbands, // IU: If RANGE='I', the index of the largest eigenvalue to be returned. 1 <= IL <= IU <= N,
0.0, // ABSTOL
nbands, // M: The total number of eigenvalues found. 0 <= M <= N. if RANGE = 'I', M = IU-IL+1.
eigenvalue, // W store eigenvalues
vcc, // store eigenvector
ldh, // LDZ: The leading dimension of the array Z.
work,
lwork,
rwork,
iwork,
ifail,
info);
lwork = int(get_real(work[0]));
delete[] work;
work = new T[lwork];
// The A and B storage space is (nstart * ldh), and the data that really participates in the zhegvx
// operation is (nstart * nstart). In this function, the data that A and B participate in the operation will
// be extracted into the new local variables aux and bux (the internal of the function).
// V is the output of the function, the storage space is also (nstart * ldh), and the data size of valid V
// obtained by the zhegvx operation is (nstart * nstart) and stored in zux (internal to the function). When
// the function is output, the data of zux will be mapped to the corresponding position of V.
LapackWrapper::xheevx(
1, // ITYPE = 1: A*x = (lambda)*B*x
'V', // JOBZ = 'V': Compute eigenvalues and eigenvectors.
'I', // RANGE = 'I': the IL-th through IU-th eigenvalues will be found.
'L', // UPLO = 'L': Lower triangles of A and B are stored.
nstart, // N = base
aux, // A is COMPLEX*16 array dimension (LDA, N)
ldh, // LDA = base
0.0, // Not referenced if RANGE = 'A' or 'I'.
0.0, // Not referenced if RANGE = 'A' or 'I'.
1, // IL: If RANGE='I', the index of the smallest eigenvalue to be returned. 1 <= IL <= IU <= N,
nbands, // IU: If RANGE='I', the index of the largest eigenvalue to be returned. 1 <= IL <= IU <= N,
0.0, // ABSTOL
nbands, // M: The total number of eigenvalues found. 0 <= M <= N. if RANGE = 'I', M = IU-IL+1.
eigenvalue, // W store eigenvalues
vcc, // store eigenvector
ldh, // LDZ: The leading dimension of the array Z.
work,
lwork,
rwork,
iwork,
ifail,
info);
delete[] aux;
delete[] work;
delete[] rwork;
delete[] iwork;
delete[] ifail;
assert(0 == info);
}
};
template <typename T>
struct dngvx_op<T, base_device::DEVICE_CPU>
{
using Real = typename GetTypeReal<T>::type;
void operator()(const base_device::DEVICE_CPU* d,
const int nbase,
const int ldh,
T* hcc,
T* scc,
const int m,
Real* eigenvalue,
T* vcc)
{
int info = 0;
int mm = m;
int lwork = -1;
T* work = new T[1];
Real* rwork = new Real[7 * nbase];
int* iwork = new int[5 * nbase];
int* ifail = new int[nbase];
LapackWrapper::xhegvx(
1, // ITYPE = 1: A*x = (lambda)*B*x
'V', // JOBZ = 'V': Compute eigenvalues and eigenvectors.
'I', // RANGE = 'I': the IL-th through IU-th eigenvalues will be found.
'U', // UPLO = 'L': Lower triangles of A and B are stored.
nbase, // N = base
hcc, // A is COMPLEX*16 array dimension (LDA, N)
ldh, // LDA = base
scc,
ldh,
0.0, // Not referenced if RANGE = 'A' or 'I'.
0.0, // Not referenced if RANGE = 'A' or 'I'.
1, // IL: If RANGE='I', the index of the smallest eigenvalue to be returned. 1 <= IL <= IU <= N,
m, // IU: If RANGE='I', the index of the largest eigenvalue to be returned. 1 <= IL <= IU <= N,
0.0, // ABSTOL
mm, // M: The total number of eigenvalues found. 0 <= M <= N. if RANGE = 'I', M = IU-IL+1.
eigenvalue, // W store eigenvalues
vcc, // store eigenvector
ldh, // LDZ: The leading dimension of the array Z.
work,
lwork,
rwork,
iwork,
ifail,
info);
lwork = int(get_real(work[0]));
delete[] work;
work = new T[lwork];
LapackWrapper::xhegvx(1,
'V',
'I',
'U',
nbase,
hcc,
ldh,
scc,
ldh,
0.0,
0.0,
1,
m,
0.0,
mm,
eigenvalue,
vcc,
ldh,
work,
lwork,
rwork,
iwork,
ifail,
info);
delete[] work;
delete[] rwork;
delete[] iwork;
delete[] ifail;
}
};
template struct dngvd_op<std::complex<float>, base_device::DEVICE_CPU>;
template struct dngvd_op<std::complex<double>, base_device::DEVICE_CPU>;
template struct dnevx_op<std::complex<float>, base_device::DEVICE_CPU>;
template struct dnevx_op<std::complex<double>, base_device::DEVICE_CPU>;
template struct dngvx_op<std::complex<float>, base_device::DEVICE_CPU>;
template struct dngvx_op<std::complex<double>, base_device::DEVICE_CPU>;
template struct dngv_op<std::complex<float>, base_device::DEVICE_CPU>;
template struct dngv_op<std::complex<double>, base_device::DEVICE_CPU>;
#ifdef __LCAO
template struct dngvd_op<double, base_device::DEVICE_CPU>;
template struct dnevx_op<double, base_device::DEVICE_CPU>;
template struct dngvx_op<double, base_device::DEVICE_CPU>;
template struct dngv_op<double, base_device::DEVICE_CPU>;
#endif
} // namespace hsolver