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
11
12#include <ATen/core/tensor.h>
14#include <source_base/macros.h>
15
16namespace hsolver {
17
24template <typename T = std::complex<double>, typename Device = base_device::DEVICE_CPU>
26{
27 private:
28 // Note GetTypeReal<T>::type will
29 // return T if T is real type(float, double),
30 // otherwise return the real type of T(complex<float>, std::complex<double>)
31 using Real = typename GetTypeReal<T>::type;
32 // Column major psi in this class
33 public:
39 explicit DiagoBPCG(const Real* precondition);
40
44 ~DiagoBPCG();
45
57 void init_iter(const int nband, const int nband_l, const int nbasis, const int ndim);
58
68 void diag(const HSOperator<T, Device>& op,
69 T* psi_in,
70 Real* eigenvalue_in,
71 const std::vector<double>& ethr_band);
72
73 private:
75 int n_band = 0;
77 int n_band_l = 0;
79 int n_basis = 0;
81 int n_dim = 0;
83 int nline = 4;
87
88
92
94
101
108
110
117
120
121 // These are for hsolver gemm_op use
123 Device * ctx = {};
124 // Pointer to objects of 1 and 0 for gemm
125 const T *one = nullptr, *zero = nullptr, *neg_one = nullptr;
126 const T one_ = static_cast<T>(1.0), zero_ = static_cast<T>(0.0), neg_one_ = static_cast<T>(-1.0);
127
144 void calc_prec();
145
163 const HSOperator<T, Device>& op,
164 const T *psi_in,
165 ct::Tensor& hpsi_out);
166
174 void calc_spsi_with_block(const HSOperator<T, Device>& op, const T* psi_in, ct::Tensor& spsi_out);
175
190 void diag_hsub(
191 const ct::Tensor& psi_in,
192 const ct::Tensor& hpsi_in,
193 ct::Tensor& hsub_out,
194 ct::Tensor& eigenvalue_out);
195
206 void rotate_wf(
207 const ct::Tensor& hsub_in,
208 ct::Tensor& psi_out,
209 ct::Tensor& workspace_in);
210
237 const ct::Tensor& prec_in,
238 ct::Tensor& err_out,
239 ct::Tensor& beta_out,
240 ct::Tensor& psi_in, ct::Tensor& hpsi_in, ct::Tensor& spsi_in,
241 ct::Tensor& grad_out, ct::Tensor& grad_old_out);
242
260 const HSOperator<T, Device>& op,
261 T *psi_in,
262 ct::Tensor& psi_out, ct::Tensor& hpsi_out, ct::Tensor& spsi_out,
263 ct::Tensor& hsub_out, ct::Tensor& workspace_in,
264 ct::Tensor& eigenvalue_out);
265
283 ct::Tensor& psi_out,
284 ct::Tensor& hpsi_out,
285 ct::Tensor& hsub_out,
286 ct::Tensor& workspace_in,
287 ct::Tensor& eigenvalue_out);
288
299 void orth_projection(
300 const ct::Tensor& psi_in,
301 const ct::Tensor& spsi_in,
302 ct::Tensor& hsub_in,
303 ct::Tensor& grad_out,
304 ct::Tensor& sgrad_out);
305
319 void line_minimize(
320 ct::Tensor& grad_in,
321 ct::Tensor& hgrad_in,
322 ct::Tensor& sgrad_in,
323 ct::Tensor& psi_out,
324 ct::Tensor& hpsi_out,
325 ct::Tensor& spsi_out);
326
335 void orth_cholesky(
336 ct::Tensor& workspace_in,
337 ct::Tensor& psi_out,
338 ct::Tensor& hpsi_out,
339 ct::Tensor& spsi_out,
340 ct::Tensor& hsub_out);
341
349 bool test_error(const ct::Tensor& err_in, const std::vector<double>& ethr_band);
350
357
363
364 // note: these operators use template parameter base_device::Device_*
365 // defined in source_base/module_device/types.h
366 // different from ct_Device!
368
369};
370
371} // namespace hsolver
372#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
void orth_projection(const ct::Tensor &psi_in, const ct::Tensor &spsi_in, ct::Tensor &hsub_in, ct::Tensor &grad_out, ct::Tensor &sgrad_out)
Orthogonalize column vectors in grad to column vectors in psi.
Definition diago_bpcg.cpp:175
ct::Tensor hpsi
Definition diago_bpcg.h:106
int n_dim
valid dimension of psi
Definition diago_bpcg.h:81
ct::Tensor h_prec
Definition diago_bpcg.h:93
ct::Tensor spsi
Definition diago_bpcg.h:107
void calc_hsub_with_block(const HSOperator< T, Device > &op, T *psi_in, ct::Tensor &psi_out, ct::Tensor &hpsi_out, ct::Tensor &spsi_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:246
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:277
ct::Tensor prec
Definition diago_bpcg.h:93
int n_basis
the number of cols of the input psi
Definition diago_bpcg.h:79
ModuleBase::gemm_op< T, Device > gemm_op
Definition diago_bpcg.h:367
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:193
const T zero_
Definition diago_bpcg.h:126
ct::kernels::set_memory< Real, ct_Device > setmem_var_op
Definition diago_bpcg.h:352
~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:356
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:230
ct::kernels::synchronize_memory< T, ct_Device, ct_Device > syncmem_complex_op
Definition diago_bpcg.h:361
int nline
max iter steps for all-band cg loop
Definition diago_bpcg.h:83
const T * zero
Definition diago_bpcg.h:125
const T * one
Definition diago_bpcg.h:125
ct::Tensor grad
Definition diago_bpcg.h:113
int n_band
the number of bands of all processes
Definition diago_bpcg.h:75
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 &spsi_in, ct::Tensor &grad_out, ct::Tensor &grad_old_out)
Calculate the gradient for all bands used in CG method.
Definition diago_bpcg.cpp:145
ct::DataType t_type
Definition diago_bpcg.h:90
ct::Tensor sgrad
Definition diago_bpcg.h:115
void line_minimize(ct::Tensor &grad_in, ct::Tensor &hgrad_in, ct::Tensor &sgrad_in, ct::Tensor &psi_out, ct::Tensor &hpsi_out, ct::Tensor &spsi_out)
Optimize psi together with hpsi and spsi.
Definition diago_bpcg.cpp:98
ct::kernels::resize_memory< Real, ct_Device > resmem_var_op
Definition diago_bpcg.h:353
ct::Tensor hgrad
Definition diago_bpcg.h:114
const T one_
Definition diago_bpcg.h:126
void diag(const HSOperator< T, Device > &op, T *psi_in, Real *eigenvalue_in, const std::vector< double > &ethr_band)
Diagonalize the Hamiltonian using the BPCG method.
Definition diago_bpcg.cpp:295
int n_band_l
the number of bands of current process
Definition diago_bpcg.h:77
Device * ctx
ctx is nothing but the devices used in gemm_op (Device * ctx = nullptr;),
Definition diago_bpcg.h:123
ct::kernels::set_memory< T, ct_Device > setmem_complex_op
Definition diago_bpcg.h:358
ModuleBase::PGemmCN< T, Device > pmmcn
parallel matrix multiplication
Definition diago_bpcg.h:85
typename ct::PsiToContainer< Device >::type ct_Device
Definition diago_bpcg.h:351
ct::Tensor beta
The coefficient for mixing the current and previous step gradients, used in iterative methods.
Definition diago_bpcg.h:96
const T neg_one_
Definition diago_bpcg.h:126
ct::kernels::synchronize_memory< Real, ct_Device, ct::DEVICE_CPU > syncmem_var_h2d_op
Definition diago_bpcg.h:355
DiagoBPCG(const Real *precondition)
Constructor for DiagoBPCG class.
Definition diago_bpcg.cpp:18
ct::Tensor err_st
Error state value, if it is smaller than the given threshold, then exit the iteration.
Definition diago_bpcg.h:98
ct::kernels::delete_memory< T, ct_Device > delmem_complex_op
Definition diago_bpcg.h:359
PLinearTransform< T, Device > plintrans
Definition diago_bpcg.h:86
void calc_hpsi_with_block(const HSOperator< T, Device > &op, const T *psi_in, ct::Tensor &hpsi_out)
Apply the H operator to psi and obtain the hpsi matrix.
Definition diago_bpcg.cpp:211
ct::Tensor eigen
Calculated eigen.
Definition diago_bpcg.h:100
ct::Tensor grad_old
Definition diago_bpcg.h:116
void calc_spsi_with_block(const HSOperator< T, Device > &op, const T *psi_in, ct::Tensor &spsi_out)
Apply the overlap operator to a wavefunction block.
Definition diago_bpcg.cpp:221
ct::DataType r_type
Definition diago_bpcg.h:89
ct::Tensor hsub
Definition diago_bpcg.h:109
void orth_cholesky(ct::Tensor &workspace_in, ct::Tensor &psi_out, ct::Tensor &hpsi_out, ct::Tensor &spsi_out, ct::Tensor &hsub_out)
S-orthogonalize and normalize the column vectors in psi_out using Cholesky decomposition.
Definition diago_bpcg.cpp:120
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:72
const T * neg_one
Definition diago_bpcg.h:125
ct::kernels::resize_memory< T, ct_Device > resmem_complex_op
Definition diago_bpcg.h:360
void calc_prec()
Update the precondition array.
Definition diago_bpcg.cpp:169
typename GetTypeReal< T >::type Real
Definition diago_bpcg.h:31
ct::kernels::delete_memory< Real, ct_Device > delmem_var_op
Definition diago_bpcg.h:354
ct::Tensor work
work for some calculations within this class, including rotate_wf call
Definition diago_bpcg.h:119
ct::DeviceType device_type
Definition diago_bpcg.h:91
base_device::memory::synchronize_memory_2d_op< T, Device, Device > syncmem_complex_2d_op
Definition diago_bpcg.h:362
What an iterative eigensolver needs from the generalized eigenproblem H x = e S x: the ability to app...
Definition hs_operator.h:24
B = alpha * A * U + beta * B A and B are local matrice U can be a local matrix or a global matrix.
Definition para_lin_tf.h:20
#define T
Definition exp.cpp:237
DataType
Definition tensor_enums.h:15
@ DT_INVALID
Definition tensor_enums.h:16
DeviceType
Definition tensor_enums.h:28
@ UnKnown
Definition tensor_enums.h:29
Definition dense_eigensolver.cpp:23
Definition diag_lcao.h:14
T type
Definition macros.h:8
Definition math_kernel_op.h:298
T type
Definition tensor_types.h:76
Deletes memory on a device.
Definition memory.h:135
A functor to resize memory allocation.
Definition memory.h:19
A functor to set memory to a constant value.
Definition memory.h:37
Synchronizes memory between devices.
Definition memory.h:58