ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
diago_mock.h
Go to the documentation of this file.
1#include "mpi.h"
4#include "source_psi/psi.h"
5#include <random>
6
7#include <algorithm>
8#include <complex>
9#include <fstream>
10#include <iostream>
11#include <vector>
12
13namespace DIAGOTEST
14{
15std::vector<double> hmatrix_d;
16std::vector<double> hmatrix_local_d;
17std::vector<std::complex<double>> hmatrix;
18std::vector<std::complex<double>> hmatrix_local;
19std::vector<std::complex<float>> hmatrix_f;
20std::vector<std::complex<float>> hmatrix_local_f;
21int h_nr;
22int h_nc;
23int npw;
24int *npw_local; // number of plane wave distributed to each process
25
26void readh(std::ifstream &inf, std::vector<double> &hm)
27{
28 int npw_;
29 inf >> npw_;
30 DIAGOTEST::npw = npw_;
31 hm.resize(npw * npw);
32 h_nr = npw;
33 h_nc = npw;
34 for (int i = 0; i < npw; i++)
35 {
36 for (int j = i; j < npw; j++)
37 {
38 inf >> hm[i * npw + j];
39 if (i != j)
40 {
41 hm[j * npw + i] = hm[i * npw + j];
42 }
43 }
44 }
45}
46
47void readh(std::ifstream &inf, std::vector<std::complex<double>> &hm)
48{
49 int npw_;
50 inf >> npw_;
51 DIAGOTEST::npw = npw_;
52 hm.resize(npw * npw);
53 h_nr = npw;
54 h_nc = npw;
55 for (int i = 0; i < npw; i++)
56 {
57 for (int j = i; j < npw; j++)
58 {
59 inf >> hm[i * npw + j];
60 if (i != j)
61 {
62 hm[j * npw + i] = conj(hm[i * npw + j]);
63 }
64 }
65 double real = hm[i * npw + i].real();
66 hm[i * npw + i] = std::complex<double>{real, 0.0};
67 }
68}
69
70void readh(std::ifstream &inf, std::vector<std::complex<float>> &hm)
71{
72 int npw_;
73 inf >> npw_;
74 DIAGOTEST::npw = npw_;
75 hm.resize(npw * npw);
76 h_nr = npw;
77 h_nc = npw;
78 for (int i = 0; i < npw; i++)
79 {
80 for (int j = i; j < npw; j++)
81 {
82 inf >> hm[i * npw + j];
83 if (i != j)
84 {
85 hm[j * npw + i] = conj(hm[i * npw + j]);
86 }
87 }
88 float real = hm[i * npw + i].real();
89 hm[i * npw + i] = std::complex<float>{real, 0.0};
90 }
91}
92
93template <class T> void divide_psi(T *psi, T *psi_local)
94{
95 int nprocs = 1, mypnum = 0;
96#ifdef __MPI
97 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
98 MPI_Comm_rank(MPI_COMM_WORLD, &mypnum);
99#endif
100
101 if (mypnum == 0)
102 {
103 for (int j = 0; j < npw_local[0]; j++)
104 {
105 psi_local[j] = psi[j];
106 }
107#ifdef __MPI
108 int start_point = npw_local[0];
109 for (int j = 1; j < nprocs; j++)
110 {
111 if (std::is_same<T, double>::value)
112 {
113 MPI_Send(
114 &(psi[start_point]),
115 npw_local[j],
116 MPI_DOUBLE,
117 j,
118 0,
119 MPI_COMM_WORLD);
120 }
121 else if (std::is_same<T, std::complex<double>>::value)
122 {
123 MPI_Send(
124 &(psi[start_point]),
125 npw_local[j],
126 MPI_DOUBLE_COMPLEX,
127 j,
128 0,
129 MPI_COMM_WORLD);
130 }
131 else if (std::is_same<T, float>::value)
132 {
133 MPI_Send(
134 &(psi[start_point]),
135 npw_local[j],
136 MPI_FLOAT,
137 j,
138 0,
139 MPI_COMM_WORLD);
140 }
141 else if (std::is_same<T, std::complex<float>>::value)
142 {
143 MPI_Send(
144 &(psi[start_point]),
145 npw_local[j],
146 MPI_C_FLOAT_COMPLEX,
147 j,
148 0,
149 MPI_COMM_WORLD);
150 }
151 start_point += npw_local[j];
152 }
153 }
154 else
155 {
156 int recv_len = mypnum < (npw % nprocs) ? npw / nprocs + 1 : npw / nprocs;
157 if (std::is_same<T, double>::value)
158 {
159 MPI_Recv(
160 psi_local,
161 npw_local[mypnum],
162 MPI_DOUBLE,
163 0,
164 0,
165 MPI_COMM_WORLD,
166 MPI_STATUS_IGNORE);
167 }
168 else if (std::is_same<T, std::complex<double>>::value)
169 {
170 MPI_Recv(
171 psi_local,
172 npw_local[mypnum],
173 MPI_DOUBLE_COMPLEX,
174 0,
175 0,
176 MPI_COMM_WORLD,
177 MPI_STATUS_IGNORE);
178 }
179 else if (std::is_same<T, float>::value)
180 {
181 MPI_Recv(
182 psi_local,
183 npw_local[mypnum],
184 MPI_FLOAT,
185 0,
186 0,
187 MPI_COMM_WORLD,
188 MPI_STATUS_IGNORE);
189 }
190 else if (std::is_same<T, std::complex<float>>::value)
191 {
192 MPI_Recv(
193 psi_local,
194 npw_local[mypnum],
195 MPI_C_FLOAT_COMPLEX,
196 0,
197 0,
198 MPI_COMM_WORLD,
199 MPI_STATUS_IGNORE);
200 }
201#endif
202 }
203}
204template void divide_psi<double>(double *psi, double *psi_local);
205template void divide_psi<std::complex<double>>(std::complex<double> *psi, std::complex<double> *psi_local);
206template void divide_psi<std::complex<float>>(std::complex<float> *psi, std::complex<float> *psi_local);
207#ifdef __MPI
209{
210 int nprocs, mypnum;
211 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
212 MPI_Comm_rank(MPI_COMM_WORLD, &mypnum);
213 for (int i = 0; i < nprocs; i++)
214 {
215 if (i < npw % nprocs)
216 {
217 npw_local[i] = npw / nprocs + 1;
218 }
219 else
220 {
221 npw_local[i] = npw / nprocs;
222 }
223 }
224}
225
226template <typename T>
229 psi::Psi<T> &psi_local,
230 std::vector<T> &hmatrix,
231 std::vector<T> &hmatrix_local)
232{
233 int nprocs, mypnum;
234 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
235 MPI_Comm_rank(MPI_COMM_WORLD, &mypnum);
236
237 int npw = psi.get_nbasis();
238 int nbands = psi.get_nbands();
239 int nk = psi.get_nk();
240
241 hmatrix_local.resize(npw * npw_local[mypnum]);
242 h_nc = npw_local[mypnum];
243 h_nr = npw;
244 psi_local.resize(nk, nbands, npw_local[mypnum]);
245 for (int i = 0; i < npw; i++)
246 {
247 divide_psi<T>(&(hmatrix[i * npw]), &(hmatrix_local[i * npw_local[mypnum]]));
248 if (i < nbands)
249 {
250 for (int k = 0; k < nk; k++)
251 {
252 psi.fix_k(k);
253 psi_local.fix_k(k);
254 divide_psi<T>(psi.get_pointer(i), psi_local.get_pointer(i));
255 }
256 }
257 }
258}
259#endif
260} // namespace DIAGOTEST
261
262#include "source_base/macros.h"
263template <typename T> class HPsi
264{
283 using Real = typename GetTypeReal<T>::type;
284
285 public:
287 {
288 genhmatrix();
289 genpsi();
291 }
292 HPsi() {};
293 ~HPsi() { delete[] precondition; }
294
295 void create(int nbd, int npw, int sparsity = 7)
296 {
297 this->nband = nbd;
298 this->npw = npw;
299 this->sparsity = sparsity;
300 genhmatrix();
301 genpsi();
303 }
304
305 // return the matrix
306 Real *precond() { return precondition; }
307 std::vector<T> hamilt() { return hmatrix; }
308 // ModuleBase::ComplexMatrix psi() {return psimatrix;}
310 {
311 psi::Psi<T> psitmp(
312 1,
313 nband,
314 npw,
315 npw,
316 true);
317 for (int i = 0; i < nband; i++)
318 {
319 for (int j = 0; j < npw; j++)
320 {
321 psitmp(0, i, j) = psimatrix[i * npw + j];
322 }
323 }
324 return psitmp;
325 };
326
327 // generate the Hermite matrix
329
330 // generate the psi matrix
331 void genpsi();
332
333 // generate precondition
335 {
336 precondition = new Real[npw];
337 std::default_random_engine e(1000);
338 std::uniform_int_distribution<unsigned> u(min, max);
339 for (int i = 0; i < npw; i++)
340 {
341 precondition[i] = 1.0 + static_cast<Real>(u(e)) / max;
342 }
343 }
344
345 private:
346 int npw;
347 int nband;
348 std::vector<T> hmatrix;
349 std::vector<T> psimatrix;
351 int min = 0;
352 int max = 9999;
354};
355
356template <> void HPsi<double>::genhmatrix()
357{
358 hmatrix.resize(npw * npw);
361 std::default_random_engine e(100);
362 std::uniform_int_distribution<unsigned> u(min, max);
363 if (sparsity < 0)
364 {
365 sparsity = 0;
366 }
367 if (sparsity > 10)
368 {
369 sparsity = 10;
370 }
371 for (int i = 0; i < npw; i++)
372 {
373 for (int j = 0; j <= i; j++)
374 {
375 double mincoef = 0.0;
376 double realp = pow(-1.0, u(e) % 2) * static_cast<double>(u(e)) / max;
377 if (int(u(e) % 10) > int(sparsity - 1))
378 {
379 mincoef = 1.0;
380 }
381 if (i == j)
382 {
383 hmatrix[i * npw + j] = realp;
384 }
385 else
386 {
387 hmatrix[j * npw + i] = hmatrix[i * npw + j] = mincoef * realp;
388 }
389 }
390 }
391}
393{
394 hmatrix.resize(npw * npw);
397 std::default_random_engine e(100);
398 std::uniform_int_distribution<unsigned> u(min, max);
399 if (sparsity < 0)
400 {
401 sparsity = 0;
402 }
403 if (sparsity > 10)
404 {
405 sparsity = 10;
406 }
407 for (int i = 0; i < npw; i++)
408 {
409 for (int j = 0; j <= i; j++)
410 {
411 double mincoef = 0.0;
412 double realp = pow(-1.0, u(e) % 2) * static_cast<double>(u(e)) / max;
413 // double imagp= pow(-1.0,u(e)%2) * static_cast<double>(u(e))/max;
414 if (int(u(e) % 10) > int(sparsity - 1))
415 {
416 mincoef = 1.0;
417 }
418 if (i == j)
419 {
420 hmatrix[i * npw + j] = std::complex<double>{realp, 0.0};
421 }
422 else
423 {
424 // hmatrix(i,j) = mincoef*std::complex<double>{realp,imagp};
425 hmatrix[i * npw + j] = mincoef * std::complex<double>{realp, 0.0};
426 hmatrix[j * npw + i] = conj(hmatrix[i * npw + j]);
427 }
428 }
429 }
430}
432{
433 hmatrix.resize(npw * npw);
436 std::default_random_engine e(100);
437 std::uniform_int_distribution<unsigned> u(min, max);
438 if (sparsity < 0)
439 {
440 sparsity = 0;
441 }
442 if (sparsity > 10)
443 {
444 sparsity = 10;
445 }
446 for (int i = 0; i < npw; i++)
447 {
448 for (int j = 0; j <= i; j++)
449 {
450 float mincoef = 0.0;
451 float realp = pow(-1.0, u(e) % 2) * static_cast<float>(u(e)) / max;
452 if (int(u(e) % 10) > int(sparsity - 1))
453 {
454 mincoef = 1.0;
455 }
456 if (i == j)
457 {
458 hmatrix[i * npw + j] = std::complex<float>{realp, 0.0};
459 }
460 else
461 {
462 hmatrix[i * npw + j] = mincoef * std::complex<float>{realp, 0.0};
463 hmatrix[j * npw + i] = conj(hmatrix[i * npw + j]);
464 }
465 }
466 }
467}
468
469template <> void HPsi<double>::genpsi()
470{
471 {
472 psimatrix.resize(nband * npw);
473 std::default_random_engine e(10);
474 std::uniform_int_distribution<unsigned> u(min, max);
475 for (int i = 0; i < nband; i++)
476 {
477 for (int j = 0; j < npw; j++)
478 {
479 psimatrix[i * npw + j] = pow(-1.0, u(e) % 2) * static_cast<double>(u(e)) / max;
480 }
481 }
482 }
483}
485{
486 {
487 psimatrix.resize(nband * npw);
488 std::default_random_engine e(10);
489 std::uniform_int_distribution<unsigned> u(min, max);
490 for (int i = 0; i < nband; i++)
491 {
492 for (int j = 0; j < npw; j++)
493 {
494 double realp = pow(-1.0, u(e) % 2) * static_cast<double>(u(e)) / max;
495 // double imagp=pow(-1.0,u(e)%2) * static_cast<double>(u(e))/max;
496 double imagp = 0.0;
497 psimatrix[i * npw + j] = std::complex<double>{realp, imagp};
498 }
499 }
500 }
501}
503{
504 {
505 psimatrix.resize(nband * npw);
506 std::default_random_engine e(10);
507 std::uniform_int_distribution<unsigned> u(min, max);
508 for (int i = 0; i < nband; i++)
509 {
510 for (int j = 0; j < npw; j++)
511 {
512 float realp = pow(-1.0, u(e) % 2) * static_cast<float>(u(e)) / max;
513 float imagp = 0.0;
514 psimatrix[i * npw + j] = std::complex<float>{realp, imagp};
515 }
516 }
517 }
518}
519
520template class HPsi<double>;
521template class HPsi<std::complex<double>>;
522template class HPsi<std::complex<float>>;
523
524namespace DIAGOTEST
525{
527template <typename T> std::vector<T> &hmatrix_local_of();
528template <> std::vector<double> &hmatrix_local_of<double>() { return hmatrix_local_d; }
529template <> std::vector<std::complex<double>> &hmatrix_local_of<std::complex<double>>() { return hmatrix_local; }
530template <> std::vector<std::complex<float>> &hmatrix_local_of<std::complex<float>>() { return hmatrix_local_f; }
531} // namespace DIAGOTEST
532
541template <typename T> class HSOperatorMock : public hsolver::HSOperator<T, base_device::DEVICE_CPU>
542{
543 public:
544 void update_k(const int ik) override {}
545
546 void hpsi(
547 const T *x,
548 T *hx,
549 const int ld,
550 const int nvec) const override
551 {
552 int mypnum = 0;
553#ifdef __MPI
554 MPI_Comm_rank(MPI_COMM_WORLD, &mypnum);
555#endif
556 const std::vector<T> &hmat = DIAGOTEST::hmatrix_local_of<T>();
557 const int npw = DIAGOTEST::npw;
558 const int ncol = DIAGOTEST::npw_local[mypnum];
559 std::vector<T> hx_full(npw);
560 for (int m = 0; m < nvec; m++)
561 {
562 for (int i = 0; i < npw; i++)
563 {
564 hx_full[i] = T(0);
565 for (int j = 0; j < ncol; j++)
566 {
567 hx_full[i] += hmat[i * DIAGOTEST::h_nc + j] * x[m * ld + j];
568 }
569 }
570 Parallel_Reduce::reduce_pool(hx_full.data(), npw);
571 DIAGOTEST::divide_psi<T>(hx_full.data(), hx + m * ld);
572 }
573 }
574
575 void spsi(
576 const T *x,
577 T *sx,
578 const int ld,
579 const int nvec) const override
580 {
581 std::copy(x, x + static_cast<size_t>(ld) * nvec, sx);
582 }
583};
Definition diago_mock.h:264
int npw
Definition diago_mock.h:346
std::vector< T > psimatrix
Definition diago_mock.h:349
Real * precond()
Definition diago_mock.h:306
void genhmatrix()
std::vector< T > hamilt()
Definition diago_mock.h:307
HPsi()
Definition diago_mock.h:292
void create(int nbd, int npw, int sparsity=7)
Definition diago_mock.h:295
int nband
Definition diago_mock.h:347
int sparsity
Definition diago_mock.h:353
std::vector< T > hmatrix
Definition diago_mock.h:348
void genprecondition()
Definition diago_mock.h:334
HPsi(int nband, int npw, int sparsity=7)
Definition diago_mock.h:286
~HPsi()
Definition diago_mock.h:293
void genpsi()
int max
Definition diago_mock.h:352
typename GetTypeReal< T >::type Real
Definition diago_mock.h:283
Real * precondition
Definition diago_mock.h:350
int min
Definition diago_mock.h:351
psi::Psi< T > psi()
Definition diago_mock.h:309
Definition diago_mock.h:542
void hpsi(const T *x, T *hx, const int ld, const int nvec) const override
hx[:, 0:nvec) = H * x[:, 0:nvec), both with leading dimension ld
Definition diago_mock.h:546
void update_k(const int ik) override
switch H and S to k point ik; must precede hpsi()/spsi() for that k
Definition diago_mock.h:544
void spsi(const T *x, T *sx, const int ld, const int nvec) const override
sx[:, 0:nvec) = S * x[:, 0:nvec), both with leading dimension ld
Definition diago_mock.h:575
What an iterative eigensolver needs from the generalized eigenproblem H x = e S x: the ability to app...
Definition hs_operator.h:24
Definition psi.h:37
void resize(const int nks_in, const int nbands_in, const int nbasis_in)
Definition psi.cpp:249
T * get_pointer() const
Definition psi.cpp:267
void fix_k(const int ik) const
Definition psi.cpp:359
#define T
Definition exp.cpp:237
Definition diago_mock.h:14
std::vector< std::complex< float > > hmatrix_f
Definition diago_mock.h:19
std::vector< std::complex< double > > hmatrix
Definition diago_mock.h:17
int npw
Definition diago_mock.h:23
int h_nc
Definition diago_mock.h:22
void cal_division(int &npw)
Definition diago_mock.h:208
void divide_psi(T *psi, T *psi_local)
Definition diago_mock.h:93
void readh(std::ifstream &inf, std::vector< double > &hm)
Definition diago_mock.h:26
std::vector< double > hmatrix_d
Definition diago_mock.h:15
int * npw_local
Definition diago_mock.h:24
std::vector< std::complex< float > > hmatrix_local_f
Definition diago_mock.h:20
std::vector< double > hmatrix_local_d
Definition diago_mock.h:16
std::vector< double > & hmatrix_local_of< double >()
Definition diago_mock.h:528
std::vector< std::complex< double > > hmatrix_local
Definition diago_mock.h:18
template void divide_psi< double >(double *psi, double *psi_local)
int h_nr
Definition diago_mock.h:21
std::vector< T > & hmatrix_local_of()
the process-local slice of the test matrix for each scalar type
void divide_hpsi(psi::Psi< T > &psi, psi::Psi< T > &psi_local, std::vector< T > &hmatrix, std::vector< T > &hmatrix_local)
Definition diago_mock.h:227
void reduce_pool(T &object)
Definition depend_mock.cpp:15
Definition diag_lcao.h:14
double conj(double a)
Definition operator_lr_hxc.cpp:15
T type
Definition macros.h:8