Repository navigation
Expand file tree
/
Copy pathdiago_lapack.cpp
More file actions
301 lines (262 loc) · 8.76 KB
/
Copy pathdiago_lapack.cpp
File metadata and controls
301 lines (262 loc) · 8.76 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
// Refactored according to diago_scalapack
// This code will be futher refactored to remove the dependency of psi and hamilt
#include "source_io/module_parameter/parameter.h"
#include "diago_lapack.h"
#include "source_base/global_variable.h"
#include "source_base/module_external/lapack_connector.h"
#include "source_base/timer.h"
#include "source_base/tool_quit.h"
#include "source_pw/module_pwdft/global.h"
typedef hamilt::MatrixBlock<double> matd;
typedef hamilt::MatrixBlock<std::complex<double>> matcd;
namespace hsolver
{
template <>
void DiagoLapack<double>::diag(hamilt::Hamilt<double>* phm_in, psi::Psi<double>& psi, Real* eigenvalue_in)
{
ModuleBase::TITLE("DiagoLapack", "diag");
// Prepare H and S matrix
matd h_mat, s_mat;
phm_in->matrix(h_mat, s_mat);
assert(h_mat.col == s_mat.col && h_mat.row == s_mat.row && h_mat.desc == s_mat.desc);
std::vector<double> eigen(PARAM.globalv.nlocal, 0.0);
// Diag
this->dsygvx_diag(h_mat.col, h_mat.row, h_mat.p, s_mat.p, eigen.data(), psi);
// Copy result
int size = eigen.size();
for (int i = 0; i < size; i++)
{
eigenvalue_in[i] = eigen[i];
}
}
template <>
void DiagoLapack<std::complex<double>>::diag(hamilt::Hamilt<std::complex<double>>* phm_in,
psi::Psi<std::complex<double>>& psi,
Real* eigenvalue_in)
{
ModuleBase::TITLE("DiagoLapack", "diag");
matcd h_mat, s_mat;
phm_in->matrix(h_mat, s_mat);
assert(h_mat.col == s_mat.col && h_mat.row == s_mat.row && h_mat.desc == s_mat.desc);
std::vector<double> eigen(PARAM.globalv.nlocal, 0.0);
this->zhegvx_diag(h_mat.col, h_mat.row, h_mat.p, s_mat.p, eigen.data(), psi);
int size = eigen.size();
for (int i = 0; i < size; i++)
{
eigenvalue_in[i] = eigen[i];
}
}
template <typename T>
int DiagoLapack<T>::dsygvx_once(const int ncol,
const int nrow,
const double* const h_mat,
const double* const s_mat,
double* const ekb,
psi::Psi<double>& wfc_2d) const
{
// Copy matrix to temp variables
ModuleBase::matrix h_tmp(ncol, nrow, false);
memcpy(h_tmp.c, h_mat, sizeof(double) * ncol * nrow);
ModuleBase::matrix s_tmp(ncol, nrow, false);
memcpy(s_tmp.c, s_mat, sizeof(double) * ncol * nrow);
// Prepare caculate parameters
const char jobz = 'V', range = 'I', uplo = 'U';
const int itype = 1, il = 1, iu = PARAM.inp.nbands, one = 1;
int M = 0, info = 0;
double vl = 0, vu = 0;
const double abstol = 0;
int lwork = (ncol + 2) * ncol;
std::vector<double> work(3, 0);
std::vector<int> iwork(1, 0);
std::vector<int> ifail(PARAM.globalv.nlocal, 0);
// Original Lapack caculate, obelsete
/*dsygvx_(&itype,
&jobz,
&range,
&uplo,
&PARAM.globalv.nlocal,
h_tmp.c,
&ncol,
s_tmp.c,
&ncol,
&vl,
&vu,
&il,
&iu,
&abstol,
&M,
ekb,
wfc_2d.get_pointer(),
&ncol,
work.data(),
&lwork,
iwork.data(),
ifail.data(),
&info);
// Throw error if it returns info
if (info)
throw std::runtime_error("info = " + ModuleBase::GlobalFunc::TO_STRING(info) + ".\n"
+ std::string(__FILE__) + " line "
+ std::to_string(__LINE__));
//lwork = work[0];
//work.resize(std::max(lwork, 3), 0);
//iwork.resize(iwork[0], 0);
dsygvx_(&itype,
&jobz,
&range,
&uplo,
&PARAM.globalv.nlocal,
h_tmp.c,
&PARAM.globalv.nlocal,
s_tmp.c,
&PARAM.globalv.nlocal,
&vl,
&vu,
&il,
&iu,
&abstol,
&M,
ekb,
wfc_2d.get_pointer(),
&ncol,
work.data(),
&lwork,
iwork.data(),
ifail.data(),
&info);*/
double *ev = new double[ncol * ncol];
dsygv_(&itype, &jobz, &uplo, &PARAM.globalv.nlocal, h_tmp.c, &ncol, s_tmp.c, &ncol, ekb, ev, &lwork, &info);
return info;
}
template <typename T>
int DiagoLapack<T>::zhegvx_once(const int ncol,
const int nrow,
const std::complex<double>* const h_mat,
const std::complex<double>* const s_mat,
double* const ekb,
psi::Psi<std::complex<double>>& wfc_2d) const
{
ModuleBase::ComplexMatrix h_tmp(ncol, nrow, false);
memcpy(h_tmp.c, h_mat, sizeof(std::complex<double>) * ncol * nrow);
ModuleBase::ComplexMatrix s_tmp(ncol, nrow, false);
memcpy(s_tmp.c, s_mat, sizeof(std::complex<double>) * ncol * nrow);
const char jobz = 'V', range = 'I', uplo = 'U';
const int itype = 1, il = 1, iu = PARAM.inp.nbands, one = 1;
int M = 0, lrwork = -1, info = 0;
const double abstol = 0;
int lwork = (ncol + 2) * ncol;
const double vl = 0, vu = 0;
std::vector<std::complex<double>> work(1, 0);
double *rwork = new double[3 * ncol - 2];
std::vector<int> iwork(1, 0);
std::vector<int> ifail(PARAM.globalv.nlocal, 0);
// Original Lapack caculate, obelsete
/*
zhegvx_(&itype,
&jobz,
&range,
&uplo,
&PARAM.globalv.nlocal,
h_tmp.c,
&PARAM.globalv.nlocal,
s_tmp.c,
&PARAM.globalv.nlocal,
&vl,
&vu,
&il,
&iu,
&abstol,
&M,
ekb,
wfc_2d.get_pointer(),
&ncol,
work.data(),
&lwork,
rwork.data(),
iwork.data(),
ifail.data(),
&info);
if (info)
throw std::runtime_error("info=" + ModuleBase::GlobalFunc::TO_STRING(info) + ". "
+ std::string(__FILE__) + " line "
+ std::to_string(__LINE__));
// GlobalV::ofs_running<<"lwork="<<work[0]<<"\t"<<"lrwork="<<rwork[0]<<"\t"<<"liwork="<<iwork[0]<<std::endl;
//lwork = work[0].real();
//work.resize(lwork, 0);
//int maxlrwork = std::max(lrwork, 3);
//rwork.resize(maxlrwork, 0);
//iwork.resize(iwork[0], 0);
zhegvx_(&itype,
&jobz,
&range,
&uplo,
&PARAM.globalv.nlocal,
h_tmp.c,
&PARAM.globalv.nlocal,
s_tmp.c,
&PARAM.globalv.nlocal,
&vl,
&vu,
&il,
&iu,
&abstol,
&M,
ekb,
wfc_2d.get_pointer(),
&ncol,
work.data(),
&lwork,
rwork.data(),
iwork.data(),
ifail.data(),
&info);
*/
std::complex<double> *ev = new std::complex<double>[ncol * ncol];
zhegv_(&itype, &jobz, &uplo, &PARAM.globalv.nlocal, h_tmp.c, &ncol, s_tmp.c, &ncol, ekb, ev, &lwork, rwork, &info);
return info;
}
template <typename T>
void DiagoLapack<T>::dsygvx_diag(const int ncol,
const int nrow,
const double* const h_mat,
const double* const s_mat,
double* const ekb,
psi::Psi<double>& wfc_2d)
{
while (true)
{
int info_result = dsygvx_once(ncol, nrow, h_mat, s_mat, ekb, wfc_2d);
if (info_result == 0) {
break;
}
}
}
template <typename T>
void DiagoLapack<T>::zhegvx_diag(const int ncol,
const int nrow,
const std::complex<double>* const h_mat,
const std::complex<double>* const s_mat,
double* const ekb,
psi::Psi<std::complex<double>>& wfc_2d)
{
while (true)
{
int info_result = zhegvx_once(ncol, nrow, h_mat, s_mat, ekb, wfc_2d);
if (info_result == 0) {
break;
}
}
}
template <typename T>
void DiagoLapack<T>::post_processing(const int info, const std::vector<int>& vec)
{
const std::string str_info = "info = " + ModuleBase::GlobalFunc::TO_STRING(info) + ".\n";
const std::string str_FILE
= std::string(__FILE__) + " line " + std::to_string(__LINE__) + ".\n";
const std::string str_info_FILE = str_info + str_FILE;
if (info == 0)
{
return;
}
}
} // namespace hsolver