forked from deepmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiago_bpcg_test.cpp
More file actions
306 lines (283 loc) · 11 KB
/
Copy pathdiago_bpcg_test.cpp
File metadata and controls
306 lines (283 loc) · 11 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
#include "source_base/inverse_matrix.h"
#include "source_base/module_external/lapack_connector.h"
#include "source_psi/psi.h"
#include "source_hamilt/hamilt.h"
#include "source_pw/module_pwdft/hamilt_pw.h"
#include "../diago_iter_assist.h"
#include "../diago_bpcg.h"
#include "diago_mock.h"
#include "mpi.h"
#include "source_basis/module_pw/test/test_tool.h"
#include <gtest/gtest.h>
#include <complex>
#include <random>
/************************************************
* unit test of functions in Diago_BPCG
***********************************************/
/**
* Class Diago_BPCG is an approach for eigenvalue problems
* This unittest test the function Diago_BPCG::diag() for FPTYPE=double, Device=cpu
* with different examples.
* - the Hermite matrices (npw=500,1000) produced using random numbers and with sparsity of 0%, 60%, 80%
* - the Hamiltonian matrix read from "data-H", produced by using out_hs in INPUT of a LCAO calculation
* - a 2x2 Hermite matrix for learning and checking
*
* Note:
* The test is passed when the eignvalues are closed to these calculated by LAPACK.
* It is used together with a header file diago_mock.h.
* The default Hermite matrix generated here is real symmetric, one can add an imaginary part
* by changing two commented out lines in diago_mock.h.
*
*/
// call lapack in order to compare to bpcg
void lapackEigen(int &npw, std::vector<std::complex<double>> &hm, double *e, bool outtime = false)
{
clock_t start, end;
start = clock();
int lwork = 2 * npw;
std::complex<double> *work2 = new std::complex<double>[lwork];
double *rwork = new double[3 * npw - 2];
int info = 0;
char tmp_c1 = 'V', tmp_c2 = 'U';
zheev_(&tmp_c1, &tmp_c2, &npw, hm.data(), &npw, e, work2, &lwork, rwork, &info);
end = clock();
if (outtime) {
std::cout << "Lapack Run time: " << (double)(end - start) / CLOCKS_PER_SEC << " S" << std::endl;
}
delete[] rwork;
delete[] work2;
}
class DiagoBPCGPrepare
{
public:
DiagoBPCGPrepare(int nband, int npw, int sparsity, bool reorder, double eps, int maxiter, double threshold)
: nband(nband), npw(npw), sparsity(sparsity), reorder(reorder), eps(eps), maxiter(maxiter),
threshold(threshold)
{
#ifdef __MPI
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &mypnum);
#endif
}
int nband, npw, sparsity, maxiter, notconv;
// eps is the convergence threshold within cg_diago
double eps, avg_iter;
bool reorder;
double threshold;
int nprocs=1, mypnum=0;
// threshold is the comparison standard between bpcg and lapack
void CompareEigen(double *precondition)
{
// calculate eigenvalues by LAPACK;
double *e_lapack = new double[npw];
auto ev = DIAGOTEST::hmatrix;
if(mypnum == 0) { lapackEigen(npw, ev, e_lapack, false);
}
// initial guess of psi by perturbing lapack psi
ModuleBase::ComplexMatrix psiguess(nband, npw);
std::default_random_engine p(1);
std::uniform_int_distribution<unsigned> u(1, 10);
for (int i = 0; i < nband; i++)
{
for (int j = 0; j < npw; j++)
{
double rand = static_cast<double>(u(p))/10.;
// psiguess(i,j) = ev(j,i)*(1+rand);
psiguess(i, j) = ev[j * DIAGOTEST::h_nc + i] * rand;
}
}
// run bpcg
//======================================================================
double *en = new double[npw];
int ik = 1;
hamilt::Hamilt<std::complex<double>>* ha;
ha =new hamilt::HamiltPW<std::complex<double>>(nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
int* ngk = new int [1];
//psi::Psi<std::complex<double>> psi(ngk,ik,nband,npw);
psi::Psi<std::complex<double>> psi;
psi.resize(ik,nband,npw);
//psi.fix_k(0);
for (int i = 0; i < nband; i++)
{
for (int j = 0; j < npw; j++)
{
psi(i,j)=psiguess(i,j);
}
}
psi::Psi<std::complex<double>> psi_local;
double* precondition_local;
DIAGOTEST::npw_local = new int[nprocs];
#ifdef __MPI
DIAGOTEST::cal_division(DIAGOTEST::npw);
DIAGOTEST::divide_hpsi(psi, psi_local, DIAGOTEST::hmatrix, DIAGOTEST::hmatrix_local); //will distribute psi and Hmatrix to each process
precondition_local = new double[DIAGOTEST::npw_local[mypnum]];
DIAGOTEST::divide_psi<double>(precondition,precondition_local);
#else
DIAGOTEST::hmatrix_local = DIAGOTEST::hmatrix;
DIAGOTEST::npw_local[0] = DIAGOTEST::npw;
psi_local = psi;
precondition_local = new double[DIAGOTEST::npw];
for(int i=0;i<DIAGOTEST::npw;i++) precondition_local[i] = precondition[i];
#endif
hsolver::DiagoBPCG<std::complex<double>> bpcg(precondition_local);
psi_local.fix_k(0);
double start, end;
start = MPI_Wtime();
using T = std::complex<double>;
const int dim = DIAGOTEST::npw;
const std::vector<T> &h_mat = DIAGOTEST::hmatrix_local;
auto hpsi_func = [h_mat, dim](T *psi_in, T *hpsi_out,
const int ld_psi, const int nvec) {
std::unique_ptr<T> one(new T(1.0));
std::unique_ptr<T> zero(new T(0.0));
const T *one_ = one.get();
const T *zero_ = zero.get();
base_device::DEVICE_CPU *ctx = {};
// hpsi_out(dim * nvec) = h_mat(dim * dim) * psi_in(dim * nvec)
ModuleBase::gemm_op<T, base_device::DEVICE_CPU>()(
'N', 'N',
dim, nvec, dim,
one_,
h_mat.data(), dim,
psi_in, ld_psi,
zero_,
hpsi_out, ld_psi);
};
const int ndim = psi_local.get_current_ngk();
bpcg.init_iter(nband, nband, npw, ndim);
std::vector<double> ethr_band(nband, 1e-5);
bpcg.diag(hpsi_func, psi_local.get_pointer(), en, ethr_band);
bpcg.diag(hpsi_func, psi_local.get_pointer(), en, ethr_band);
bpcg.diag(hpsi_func, psi_local.get_pointer(), en, ethr_band);
bpcg.diag(hpsi_func, psi_local.get_pointer(), en, ethr_band);
end = MPI_Wtime();
//if(mypnum == 0) printf("diago time:%7.3f\n",end-start);
delete [] DIAGOTEST::npw_local;
delete [] precondition_local;
//======================================================================
for (int i = 0; i < nband; i++)
{
EXPECT_NEAR(en[i], e_lapack[i], threshold);
}
delete[] en;
delete[] e_lapack;
delete ha;
}
};
class DiagoBPCGTest : public ::testing::TestWithParam<DiagoBPCGPrepare>
{
};
TEST_P(DiagoBPCGTest, RandomHamilt)
{
DiagoBPCGPrepare dcp = GetParam();
//std::cout << "npw=" << dcp.npw << ", nband=" << dcp.nband << ", sparsity="
// << dcp.sparsity << ", eps=" << dcp.eps << std::endl;
hsolver::DiagoIterAssist<std::complex<double>>::PW_DIAG_NMAX = dcp.maxiter;
hsolver::DiagoIterAssist<std::complex<double>>::PW_DIAG_THR = dcp.eps;
//std::cout<<"maxiter "<<hsolver::DiagoIterAssist<std::complex<double>>::PW_DIAG_NMAX<<std::endl;
//std::cout<<"eps "<<hsolver::DiagoIterAssist<std::complex<double>>::PW_DIAG_THR<<std::endl;
HPsi<std::complex<double>> hpsi(dcp.nband, dcp.npw, dcp.sparsity);
DIAGOTEST::hmatrix = hpsi.hamilt();
DIAGOTEST::npw = dcp.npw;
// ModuleBase::ComplexMatrix psi = hpsi.psi();
dcp.CompareEigen(hpsi.precond());
}
INSTANTIATE_TEST_SUITE_P(VerifyCG,
DiagoBPCGTest,
::testing::Values(
// nband, npw, sparsity, reorder, eps, maxiter, threshold
DiagoBPCGPrepare(10, 500, 0, true, 1e-5, 300, 5e-2)
// DiagoBPCGPrepare(20, 500, 6, true, 1e-5, 300, 5e-2)
// DiagoBPCGPrepare(20, 1000, 8, true, 1e-5, 300, 5e-2),
// DiagoBPCGPrepare(40, 1000, 8, true, 1e-6, 300, 5e-2)
));
//DiagoBPCGPrepare(40, 2000, 8, true, 1e-5, 500, 1e-2)));
// the last one is passed but time-consumming.
// check that the mock class HPsi work well
// in generating a Hermite matrix
TEST(DiagoBPCGTest, Hamilt)
{
int dim = 2;
int nbnd = 2;
HPsi<std::complex<double>> hpsi(nbnd, dim);
std::vector<std::complex<double>> hm = hpsi.hamilt();
EXPECT_EQ(DIAGOTEST::h_nr, 2);
EXPECT_EQ(DIAGOTEST::h_nc, 2);
EXPECT_EQ(hm[0].imag(), 0.0);
EXPECT_EQ(hm[DIAGOTEST::h_nc + 1].imag(), 0.0);
EXPECT_EQ(conj(hm[DIAGOTEST::h_nc]).real(), hm[1].real());
EXPECT_EQ(conj(hm[DIAGOTEST::h_nc]).imag(), hm[1].imag());
}
// check that lapack work well
// for an eigenvalue problem
/*TEST(DiagoBPCGTest, ZHEEV)
{
int dim = 100;
int nbnd = 2;
HPsi hpsi(nbnd, dim);
std::vector<std::complex<double>> hm = hpsi.hamilt();
std::vector<std::complex<double>> hm_backup = hm;
ModuleBase::ComplexMatrix eig(dim, dim);
double e[dim];
// using zheev to do a direct test
lapackEigen(dim, hm, e);
eig = transpose(hm, true) * hm_backup * hm;
// for (int i=0;i<dim;i++) std::cout<< " e[i] "<<e[i]<<std::endl;
for (int i = 0; i < dim; i++)
{
EXPECT_NEAR(e[i], eig(i, i).real(), 1e-10);
}
}*/
TEST(DiagoBPCGTest, readH)
{
// read Hamilt matrix from file data-H
std::vector<std::complex<double>> hm;
std::ifstream ifs;
std::string filename = "H-KPoints-Si2.dat";
ifs.open(filename);
// open file and check status
if (!ifs.is_open())
{
std::cout << "Error opening file " << filename << std::endl;
exit(1);
}
DIAGOTEST::readh(ifs, hm);
ifs.close();
int dim = DIAGOTEST::npw;
int nband = 10; // not nband < dim, here dim = 26 in data-H
// nband, npw, sub, sparsity, reorder, eps, maxiter, threshold
DiagoBPCGPrepare dcp(nband, dim, 0, true, 1e-5, 500, 1e-1);
hsolver::DiagoIterAssist<std::complex<double>>::PW_DIAG_NMAX = dcp.maxiter;
hsolver::DiagoIterAssist<std::complex<double>>::PW_DIAG_THR = dcp.eps;
hsolver::DiagoIterAssist<std::complex<double>>::SCF_ITER = 1;
HPsi<std::complex<double>> hpsi;
hpsi.create(nband, dim);
DIAGOTEST::hmatrix = hpsi.hamilt();
DIAGOTEST::npw = dim;
dcp.CompareEigen(hpsi.precond());
}
int main(int argc, char **argv)
{
int nproc = 1, myrank = 0;
#ifdef __MPI
int nproc_in_pool, kpar=1, mypool, rank_in_pool;
setupmpi(argc,argv,nproc, myrank);
divide_pools(nproc, myrank, nproc_in_pool, kpar, mypool, rank_in_pool);
MPI_Comm_split(MPI_COMM_WORLD,myrank,0,&BP_WORLD);
GlobalV::NPROC_IN_POOL = nproc;
#else
MPI_Init(&argc, &argv);
#endif
testing::InitGoogleTest(&argc, argv);
::testing::TestEventListeners &listeners = ::testing::UnitTest::GetInstance()->listeners();
if (myrank != 0) { delete listeners.Release(listeners.default_result_printer());
}
int result = RUN_ALL_TESTS();
if (myrank == 0 && result != 0)
{
std::cout << "ERROR:some tests are not passed" << std::endl;
return result;
}
MPI_Finalize();
return 0;
}