ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
diago_bpcg.h
Go to the documentation of this file.
1#ifndef DIAGO_BPCG_H_
2#define DIAGO_BPCG_H_
3
12
13#include <ATen/core/tensor.h>
15#include <source_base/macros.h>
16
17namespace hsolver {
18
25template <typename T = std::complex<double>, typename Device = base_device::DEVICE_CPU>
27{
28 private:
29 // Note GetTypeReal<T>::type will
30 // return T if T is real type(float, double),
31 // otherwise return the real type of T(complex<float>, std::complex<double>)
32 using Real = typename GetTypeReal<T>::type;
33 // Column major psi in this class
34 public:
40 explicit DiagoBPCG(const Real* precondition);
41
45 ~DiagoBPCG();
46
58 void init_iter(const int nband, const int nband_l, const int nbasis, const int ndim);
59
60 using HPsiFunc = std::function<void(T*, T*, const int, const int)>;
61
72 void diag(const HPsiFunc& hpsi_func,
73 T* psi_in,
74 Real* eigenvalue_in,
75 const std::vector<double>& ethr_band);
76
77 private:
79 int n_band = 0;
81 int n_band_l = 0;
83 int n_basis = 0;
85 int n_dim = 0;
87 int nline = 4;
91
92
93 ct::DataType r_type = ct::DataType::DT_INVALID;
94 ct::DataType t_type = ct::DataType::DT_INVALID;
95 ct::DeviceType device_type = ct::DeviceType::UnKnown;
96
98
105
109 ct::Tensor psi = {}, hpsi = {};
110
112
115 ct::Tensor grad = {}, hgrad = {}, grad_old = {};
116
119
120 // These are for hsolver gemm_op use
122 Device * ctx = {};
123 // Pointer to objects of 1 and 0 for gemm
124 const T *one = nullptr, *zero = nullptr, *neg_one = nullptr;
125 const T one_ = static_cast<T>(1.0), zero_ = static_cast<T>(0.0), neg_one_ = static_cast<T>(-1.0);
126
143 void calc_prec();
144
163 const HPsiFunc& hpsi_func,
164 T *psi_in,
165 ct::Tensor& hpsi_out);
166
181 void diag_hsub(
182 const ct::Tensor& psi_in,
183 const ct::Tensor& hpsi_in,
184 ct::Tensor& hsub_out,
185 ct::Tensor& eigenvalue_out);
186
197 void rotate_wf(
198 const ct::Tensor& hsub_in,
199 ct::Tensor& psi_out,
200 ct::Tensor& workspace_in);
201
228 const ct::Tensor& prec_in,
229 ct::Tensor& err_out,
230 ct::Tensor& beta_out,
231 ct::Tensor& psi_in, ct::Tensor& hpsi_in,
232 ct::Tensor& grad_out, ct::Tensor& grad_old_out);
233
251 const HPsiFunc& hpsi_func,
252 T *psi_in,
253 ct::Tensor& psi_out, ct::Tensor& hpsi_out,
254 ct::Tensor& hsub_out, ct::Tensor& workspace_in,
255 ct::Tensor& eigenvalue_out);
256
274 ct::Tensor& psi_out,
275 ct::Tensor& hpsi_out,
276 ct::Tensor& hsub_out,
277 ct::Tensor& workspace_in,
278 ct::Tensor& eigenvalue_out);
279
290 void orth_projection(
291 const ct::Tensor& psi_in,
292 ct::Tensor& hsub_in,
293 ct::Tensor& grad_out);
294
308 void line_minimize(
309 ct::Tensor& grad_in,
310 ct::Tensor& hgrad_in,
311 ct::Tensor& psi_out,
312 ct::Tensor& hpsi_out);
313
322 void orth_cholesky(
323 ct::Tensor& workspace_in,
324 ct::Tensor& psi_out,
325 ct::Tensor& hpsi_out,
326 ct::Tensor& hsub_out);
327
335 bool test_error(const ct::Tensor& err_in, const std::vector<double>& ethr_band);
336
338 using setmem_var_op = ct::kernels::set_memory<Real, ct_Device>;
339 using resmem_var_op = ct::kernels::resize_memory<Real, ct_Device>;
340 using delmem_var_op = ct::kernels::delete_memory<Real, ct_Device>;
341 using syncmem_var_h2d_op = ct::kernels::synchronize_memory<Real, ct_Device, ct::DEVICE_CPU>;
342 using syncmem_var_d2h_op = ct::kernels::synchronize_memory<Real, ct::DEVICE_CPU, ct_Device>;
343
344 using setmem_complex_op = ct::kernels::set_memory<T, ct_Device>;
345 using delmem_complex_op = ct::kernels::delete_memory<T, ct_Device>;
346 using resmem_complex_op = ct::kernels::resize_memory<T, ct_Device>;
347 using syncmem_complex_op = ct::kernels::synchronize_memory<T, ct_Device, ct_Device>;
348
349 // note: these operators use template parameter base_device::Device_*
350 // defined in source_base/module_device/types.h
351 // different from ct_Device!
353
354};
355
356} // namespace hsolver
357#endif // DIAGO_BPCG_H_
this class is used to perform parallel matrix multiplication C = alpha * A^H * B + beta * C Here,...
Definition para_gemm.h:25
A multi-dimensional array of elements of a single data type.
Definition tensor.h:32
A class for diagonalization using the Blocked-PCG method.
Definition diago_bpcg.h:27
ct::Tensor hpsi
Definition diago_bpcg.h:109
int n_dim
valid dimension of psi
Definition diago_bpcg.h:85
ct::Tensor h_prec
Definition diago_bpcg.h:97
void init_iter(const int nband, const int nband_l, const int nbasis, const int ndim)
Initialize the class before diagonalization.
Definition diago_bpcg.cpp:37
void calc_hsub_with_block_exit(ct::Tensor &psi_out, ct::Tensor &hpsi_out, ct::Tensor &hsub_out, ct::Tensor &workspace_in, ct::Tensor &eigenvalue_out)
Apply the Hamiltonian operator to psi and obtain the hpsi matrix.
Definition diago_bpcg.cpp:243
ct::Tensor prec
Definition diago_bpcg.h:97
void calc_grad_with_block(const ct::Tensor &prec_in, ct::Tensor &err_out, ct::Tensor &beta_out, ct::Tensor &psi_in, ct::Tensor &hpsi_in, ct::Tensor &grad_out, ct::Tensor &grad_old_out)
Calculate the gradient for all bands used in CG method.
Definition diago_bpcg.cpp:137
int n_basis
the number of cols of the input psi
Definition diago_bpcg.h:83
void rotate_wf(const ct::Tensor &hsub_in, ct::Tensor &psi_out, ct::Tensor &workspace_in)
Inplace matrix multiplication to obtain the initial guessed wavefunction.
Definition diago_bpcg.cpp:180
void orth_projection(const ct::Tensor &psi_in, ct::Tensor &hsub_in, ct::Tensor &grad_out)
Orthogonalize column vectors in grad to column vectors in psi.
Definition diago_bpcg.cpp:165
const T zero_
Definition diago_bpcg.h:125
ct::kernels::set_memory< Real, ct_Device > setmem_var_op
Definition diago_bpcg.h:338
~DiagoBPCG()
Destructor for DiagoBPCG class.
Definition diago_bpcg.cpp:32
ct::kernels::synchronize_memory< Real, ct::DEVICE_CPU, ct_Device > syncmem_var_d2h_op
Definition diago_bpcg.h:342
void diag_hsub(const ct::Tensor &psi_in, const ct::Tensor &hpsi_in, ct::Tensor &hsub_out, ct::Tensor &eigenvalue_out)
Diagonalization of the subspace matrix.
Definition diago_bpcg.cpp:203
ct::kernels::synchronize_memory< T, ct_Device, ct_Device > syncmem_complex_op
Definition diago_bpcg.h:347
int nline
max iter steps for all-band cg loop
Definition diago_bpcg.h:87
void calc_hsub_with_block(const HPsiFunc &hpsi_func, T *psi_in, ct::Tensor &psi_out, ct::Tensor &hpsi_out, ct::Tensor &hsub_out, ct::Tensor &workspace_in, ct::Tensor &eigenvalue_out)
Apply the Hamiltonian operator to psi and obtain the hpsi matrix.
Definition diago_bpcg.cpp:218
const T * zero
Definition diago_bpcg.h:124
const T * one
Definition diago_bpcg.h:124
ct::Tensor grad
Definition diago_bpcg.h:115
int n_band
the number of bands of all processes
Definition diago_bpcg.h:79
ct::DataType t_type
Definition diago_bpcg.h:94
std::function< void(T *, T *, const int, const int)> HPsiFunc
Definition diago_bpcg.h:60
ct::kernels::resize_memory< Real, ct_Device > resmem_var_op
Definition diago_bpcg.h:339
ct::Tensor hgrad
Definition diago_bpcg.h:115
const T one_
Definition diago_bpcg.h:125
int n_band_l
the number of bands of current process
Definition diago_bpcg.h:81
Device * ctx
ctx is nothing but the devices used in gemm_op (Device * ctx = nullptr;),
Definition diago_bpcg.h:122
ct::kernels::set_memory< T, ct_Device > setmem_complex_op
Definition diago_bpcg.h:344
ModuleBase::PGemmCN< T, Device > pmmcn
parallel matrix multiplication
Definition diago_bpcg.h:89
typename ct::PsiToContainer< Device >::type ct_Device
Definition diago_bpcg.h:337
void diag(const HPsiFunc &hpsi_func, T *psi_in, Real *eigenvalue_in, const std::vector< double > &ethr_band)
Diagonalize the Hamiltonian using the BPCG method.
Definition diago_bpcg.cpp:261
ct::Tensor beta
The coefficient for mixing the current and previous step gradients, used in iterative methods.
Definition diago_bpcg.h:100
const T neg_one_
Definition diago_bpcg.h:125
void calc_hpsi_with_block(const HPsiFunc &hpsi_func, T *psi_in, ct::Tensor &hpsi_out)
Apply the H operator to psi and obtain the hpsi matrix.
Definition diago_bpcg.cpp:193
ct::kernels::synchronize_memory< Real, ct_Device, ct::DEVICE_CPU > syncmem_var_h2d_op
Definition diago_bpcg.h:341
void orth_cholesky(ct::Tensor &workspace_in, ct::Tensor &psi_out, ct::Tensor &hpsi_out, ct::Tensor &hsub_out)
Orthogonalize and normalize the column vectors in psi_out using Cholesky decomposition.
Definition diago_bpcg.cpp:114
void line_minimize(ct::Tensor &grad_in, ct::Tensor &hgrad_in, ct::Tensor &psi_out, ct::Tensor &hpsi_out)
Optimize psi as well as the hpsi.
Definition diago_bpcg.cpp:96
ct::Tensor err_st
Error state value, if it is smaller than the given threshold, then exit the iteration.
Definition diago_bpcg.h:102
ct::kernels::delete_memory< T, ct_Device > delmem_complex_op
Definition diago_bpcg.h:345
PLinearTransform< T, Device > plintrans
Definition diago_bpcg.h:90
ct::Tensor eigen
Calculated eigen.
Definition diago_bpcg.h:104
ct::Tensor grad_old
Definition diago_bpcg.h:115
ct::DataType r_type
Definition diago_bpcg.h:93
ct::Tensor hsub
Definition diago_bpcg.h:111
bool test_error(const ct::Tensor &err_in, const std::vector< double > &ethr_band)
Checks if the error satisfies the given threshold.
Definition diago_bpcg.cpp:70
const T * neg_one
Definition diago_bpcg.h:124
ct::kernels::resize_memory< T, ct_Device > resmem_complex_op
Definition diago_bpcg.h:346
void calc_prec()
Update the precondition array.
Definition diago_bpcg.cpp:159
typename GetTypeReal< T >::type Real
Definition diago_bpcg.h:32
ct::kernels::delete_memory< Real, ct_Device > delmem_var_op
Definition diago_bpcg.h:340
ct::Tensor work
work for some calculations within this class, including rotate_wf call
Definition diago_bpcg.h:118
ct::DeviceType device_type
Definition diago_bpcg.h:95
B = alpha * A * U + beta * B A and B are local matrice U can be a local matrix or a global matrix.
Definition para_linear_transform.h:22
#define T
Definition exp.cpp:237
DataType
Enumeration of data types for tensors. The DataType enum lists the supported data types for tensors....
Definition tensor_types.h:50
DeviceType
The type of memory used by an allocator.
Definition tensor_types.h:73
Definition diag_comm_info.h:9
Definition exx_lip.h:23
T type
Definition macros.h:8
Definition math_kernel_op.h:217
Definition tensor_types.h:113