ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
zeq_solver.h
Go to the documentation of this file.
1#ifndef ABACUS_SOURCE_LCAO_MODULE_LR_ZEQ_SOLVER_H
2#define ABACUS_SOURCE_LCAO_MODULE_LR_ZEQ_SOLVER_H
3#include "gradient_inputs.h"
4#include <algorithm>
5#include <stdexcept>
6#include <vector>
9#include "zeqlin_solv.h"
10
11namespace LR
12{
13 void solve_Z_CG(double* const Z, double* R, const int& ld, const int& nstates,
14 std::function<void(const double* const, double* const)> f_LZ, const bool test_force);
15 void solve_Z_CG(std::complex<double>* const Z, std::complex<double>* R, const int& ld, const int& nstates,
16 std::function<void(const std::complex<double>* const, std::complex<double>* const)> f_LZ, const bool test_force);
19 template<typename THam>
20 inline int zvec_global_dim(THam& hm, const int nspin_x)
21 {
22 int n_global = 0;
23 for (int is = 0; is < nspin_x; ++is)
24 {
25 n_global += hm.nk * hm.nocc[is] * hm.nvirt[is];
26 }
27 return n_global;
28 }
29
30#ifdef __MPI
33 template<typename T, typename THam>
34 inline void zvec_local_to_full(THam& hm, const int nspin_x, const T* const local, T* const full)
35 {
36 const int n_global = zvec_global_dim(hm, nspin_x);
37 // `gather_2d_to_full` sums over the ranks, so the entries a rank does not own must be zero
38 std::fill(full, full + n_global, T(0));
39 int loffset = 0;
40 int goffset = 0;
41 for (int is = 0;is < nspin_x;++is)
42 {
43 const int npairs = hm.nocc[is] * hm.nvirt[is];
44 const int lsize = hm.pX[is].size();
45 for (int ik = 0;ik < hm.nk;++ik)
46 {
47 LR_Util::gather_2d_to_full(hm.pX[is], local + loffset + ik * lsize,
48 full + goffset + ik * npairs, false, hm.nvirt[is], hm.nocc[is]);
49 }
50 loffset += hm.nk * lsize;
51 goffset += hm.nk * npairs;
52 }
53 }
54
57 template<typename T, typename THam>
58 inline void zvec_full_to_local(THam& hm, const int nspin_x, const T* const full, T* const local)
59 {
60 int loffset = 0;
61 int goffset = 0;
62 for (int is = 0;is < nspin_x;++is)
63 {
64 const int npairs = hm.nocc[is] * hm.nvirt[is];
65 const int lsize = hm.pX[is].size();
66 for (int ik = 0;ik < hm.nk;++ik)
67 {
68 LR_Util::scatter_full_to_2d(hm.pX[is], full + goffset + ik * npairs,
69 local + loffset + ik * lsize, false);
70 }
71 loffset += hm.nk * lsize;
72 goffset += hm.nk * npairs;
73 }
74 }
75#endif
76
85 template<typename T, typename THam>
86 inline void solve_Z_lapack(T* const Z, const T* const R, const int& ld, const int& nstates,
87 THam& hm, const int nspin_x, const bool test_force)
88 {
89 ModuleBase::timer::start("Z_vector", "solve_Z_lapack");
90 ModuleBase::TITLE("Z_vector", "solve_Z_lapack");
91 const int n_global = zvec_global_dim(hm, nspin_x);
92 int ld_expect = 0;
93 for (int is = 0; is < nspin_x; ++is)
94 {
95 ld_expect += hm.nk * hm.pX[is].size();
96 }
97 assert(ld == ld_expect);
98
99 std::vector<T> hessian_full = hm.matrix(); // MO-hessian, A+B
100 std::vector<T> Z_full(static_cast<std::size_t>(n_global) * nstates, T(0.0));
101
102 // `hessian_full` is replicated, so the right-hand side must be global too.
103 // `R` is distributed over `pX` (length `ld` per state), so gather it first --
104 // the mirror image of the scatter of `Z_full` below. Reading `n_global` entries
105 // straight out of `R` would be an out-of-bounds read as soon as
106 // ld < n_global, and a plain segfault on a rank whose local size is 0.
107 std::vector<T> R_full(static_cast<std::size_t>(n_global) * nstates, T(0.0));
108#ifdef __MPI
109 for (int istate = 0; istate < nstates; ++istate)
110 {
111 zvec_local_to_full(hm, nspin_x, R + istate * ld, R_full.data() + istate * n_global);
112 }
113#else
114 std::copy(R, R + static_cast<std::size_t>(n_global) * nstates, R_full.begin());
115#endif
116
117 // use lapack to solve the linear equation
118 const int info = LR_Util::lapack_linear_solver(hessian_full.data(), Z_full.data(), R_full.data(), n_global, nstates);
119 if (info != 0)
120 {
121 ModuleBase::timer::end("Z_vector", "solve_Z_lapack");
122 throw std::runtime_error("LAPACK Z-vector solve failed (info=" + std::to_string(info)
123 + "); the response Hessian may be singular. Excited-state forces are not valid.");
124 }
125
126 // test: print full Z
127 if (test_force)
128 {
129 std::cout << "The full Z-vector solved by LAPACK:" << std::endl;
130 LR_Util::print_value(Z_full.data(), nstates, n_global);
131 }
132
133 // copy the local part of Z_full to Z
134#ifdef __MPI
135 for (int istate = 0; istate < nstates; ++istate)
136 {
137 zvec_full_to_local(hm, nspin_x, Z_full.data() + istate * n_global, Z + istate * ld);
138 }
139#else
140 std::copy(Z_full.begin(), Z_full.end(), Z);
141#endif
142 if (test_force)
143 {
144 std::cout << "The local Z-vector solved by LAPACK:" << std::endl;
145 LR_Util::print_value(Z, nstates, ld);
146 }
147 ModuleBase::timer::end("Z_vector", "solve_Z_lapack");
148 }
149
150#ifdef __MPI
156 template<typename T, typename THam>
157 std::vector<T> zvec_hessian_2d(THam& hm, const int nspin_x, const int ld, const Parallel_Blkcyclic& ph)
158 {
159 ModuleBase::TITLE("Z_vector", "zvec_hessian_2d");
160 ModuleBase::timer::start("Z_vector", "zvec_hessian_2d");
161 const int n_global = ph.nrglb();
162 std::vector<T> h_loc(ph.size(), T(0));
163 std::vector<T> e_full(n_global, T(0));
164 std::vector<T> e_loc(ld, T(0));
165 std::vector<T> he_loc(ld, T(0));
166 std::vector<T> he_full(n_global, T(0));
167 for (int gcol = 0; gcol < n_global; ++gcol)
168 {
169 e_full[gcol] = T(1);
170 zvec_full_to_local(hm, nspin_x, e_full.data(), e_loc.data());
171 e_full[gcol] = T(0);
172 hm.hPsi(e_loc.data(), he_loc.data(), ld, 1);
173 zvec_local_to_full(hm, nspin_x, he_loc.data(), he_full.data());
174 const int lcol = ph.icloc(gcol);
175 if (lcol < 0)
176 {
177 continue;
178 }
179 T* const h_col = h_loc.data() + static_cast<std::size_t>(lcol) * ph.nrloc();
180 for (int lrow = 0; lrow < ph.nrloc(); ++lrow)
181 {
182 h_col[lrow] = he_full[ph.irglb(lrow)];
183 }
184 }
185 ModuleBase::timer::end("Z_vector", "zvec_hessian_2d");
186 return h_loc;
187 }
188
193 template<typename T, typename THam>
194 void solve_Z_2d(T* const Z, const T* const R, const int ld, const int nstates,
195 THam& hm, const int nspin_x,
196 void (*linear_solver)(T*, T*, const Parallel_Blkcyclic&, const Parallel_Blkcyclic&))
197 {
198 ModuleBase::TITLE("Z_vector", "solve_Z_2d");
199 ModuleBase::timer::start("Z_vector", "solve_Z_2d");
200 const int n_global = zvec_global_dim(hm, nspin_x);
201 const Parallel_Blkcyclic& px0 = hm.pX[0];
202 // Shrink the ScaLAPACK block for small systems so no rank is left empty.
203 const int nproc_dim = std::max(px0.nrproc(), px0.ncproc());
204 const int nb = std::max(1, std::min(32, n_global / nproc_dim));
206 LR_Util::setup_2d_division(ph, nb, n_global, n_global, px0.ictxt());
208 LR_Util::setup_2d_division(pz, nb, n_global, nstates, px0.ictxt());
209
210 std::vector<T> h_loc = zvec_hessian_2d<T>(hm, nspin_x, ld, ph);
211
212 // right-hand side: pX layout -> 2D block-cyclic on pz
213 std::vector<T> z_loc(pz.size(), T(0));
214 std::vector<T> r_full(n_global, T(0));
215 for (int istate = 0; istate < nstates; ++istate)
216 {
217 zvec_local_to_full(hm, nspin_x, R + istate * ld, r_full.data());
218 const int lcol = pz.icloc(istate);
219 if (lcol < 0)
220 {
221 continue;
222 }
223 T* const z_col = z_loc.data() + static_cast<std::size_t>(lcol) * pz.nrloc();
224 for (int lrow = 0; lrow < pz.nrloc(); ++lrow)
225 {
226 z_col[lrow] = r_full[pz.irglb(lrow)];
227 }
228 }
229
230 linear_solver(h_loc.data(), z_loc.data(), ph, pz);
231
232 // solution: 2D block-cyclic on pz -> pX layout
233 std::vector<T> z_full(static_cast<std::size_t>(n_global) * nstates, T(0));
234 LR_Util::gather_2d_to_full(pz, z_loc.data(), z_full.data(), false, n_global, nstates);
235 for (int istate = 0; istate < nstates; ++istate)
236 {
237 zvec_full_to_local(hm, nspin_x, z_full.data() + static_cast<std::size_t>(istate) * n_global, Z + istate * ld);
238 }
239 ModuleBase::timer::end("Z_vector", "solve_Z_2d");
240 }
241#endif
242
245 template<typename T, typename THam>
246 inline void solve_Z_scalapack(T* const Z, const T* const R, const int ld, const int nstates,
247 THam& hm, const int nspin_x)
248 {
249#ifdef __MPI
250 solve_Z_2d(Z, R, ld, nstates, hm, nspin_x, &scalapack_linear_solver<T>);
251#else
252 throw std::runtime_error("Z-vector solver 'scalapack' needs an MPI build; use 'lapack' or 'cg'");
253#endif
254 }
255
259 template<typename T, typename THam>
260 inline void solve_Z_scalapack_chol(T* const Z, const T* const R, const int ld, const int nstates,
261 THam& hm, const int nspin_x)
262 {
263#ifdef __MPI
264 solve_Z_2d(Z, R, ld, nstates, hm, nspin_x, &scalapack_cholesky_linear_solver<T>);
265#else
266 throw std::runtime_error("Z-vector solver 'scalapack_chol' needs an MPI build; use 'lapack' or 'cg'");
267#endif
268 }
269
273 template<typename T, typename THamL>
274 inline void solve_zeq_with(T* const Z, T* const R, const int ld, const int nstates,
275 THamL& ops_L, const int nspin_x, const std::string& zvec_solver, const bool test_force)
276 {
277 for (int i = 0; i < nstates * ld; ++i)
278 {
279 Z[i] = T(0.0);
280 } // clear Z
281 if (zvec_solver == "cg")
282 {
284 Z,
285 R,
286 ld,
287 nstates,
288 [&ops_L, ld, nstates](const T* const in, T* const out) {
289 ops_L.hPsi(in, out, ld, nstates);
290 },
291 test_force);
292 }
293 else if (zvec_solver == "lapack")
294 {
295 solve_Z_lapack(Z, R, ld, nstates, ops_L, nspin_x, test_force);
296 }
297 else if (zvec_solver == "scalapack")
298 {
299 solve_Z_scalapack(Z, R, ld, nstates, ops_L, nspin_x);
300 }
301 else if (zvec_solver == "scalapack_chol")
302 {
303 solve_Z_scalapack_chol(Z, R, ld, nstates, ops_L, nspin_x);
304 }
305 else
306 {
307 throw std::runtime_error("Unsupported Z-vector solver: " + zvec_solver);
308 }
309 }
310
315 template<typename T, typename TOpsR, typename TOpsL>
316 void build_and_solve_zeq(TOpsR& ops_R, TOpsL& ops_L, const int nspin_x,
317 const T* const X, container::Tensor& R, T* const Z,
318 const int nloc_per_band, const int nstates, const std::string& zvec_solver, const bool test_force, const std::vector<std::vector<ct::Tensor>>& window_density)
319 {
320 ModuleBase::timer::start("Z_vector", "build_and_solve_zeq");
321 ops_R.hPsi(X, R.template data<T>(), nloc_per_band, nstates, window_density);
322 // std::cout << "The right side of the Z-vector equation:" << std::endl;
323 // LR_Util::print_value(R.template data<T>(), nstates, nloc_per_band);
324 solve_zeq_with(Z, R.template data<T>(), nloc_per_band, nstates, ops_L, nspin_x, zvec_solver, test_force);
325 ModuleBase::timer::end("Z_vector", "build_and_solve_zeq");
326 }
327
328 template<typename T>
329 void Z_vector_equation(const GradientInputs<T>& inputs,
330 const T* const X,
331 T* const Z,
332 const int& nstates,
333 std::weak_ptr<PotHxcLR> pot,
334 const std::string& spin_type,
335 const std::string& in_dir,
336 const bool openshell,
337 const std::string& zvec_solver, const std::vector<std::vector<ct::Tensor>>& window_density);
338}
339#endif
static void start(void)
Start total time calculation.
Definition timer.cpp:44
static void end(const std::string &class_name_in, const std::string &name_in)
Definition timer.cpp:109
Parallel block-cyclic matrix distribution.
Definition parallel_blkcyc.h:25
int irglb(const int irloc) const
Definition parallel_blkcyc.h:88
int ncproc() const
Definition parallel_blkcyc.h:63
int nrproc() const
Definition parallel_blkcyc.h:58
int nrloc() const
Definition parallel_blkcyc.h:35
int ictxt() const
Definition parallel_blkcyc.h:130
std::size_t size() const
Definition parallel_blkcyc.h:48
int nrglb() const
Definition parallel_blkcyc.cpp:75
int icloc(const int icglb) const
Definition parallel_blkcyc.h:83
A multi-dimensional array of elements of a single data type.
Definition tensor.h:32
#define T
Definition exp.cpp:237
void scatter_full_to_2d(const Parallel_Blkcyclic &pv, const T *fullmat, T *submat, const bool col_first=false)
scatter full matrix to 2d block-cyclic distributed matrix
Definition lr_util.hpp:593
int lapack_linear_solver(const T *A, T *x, const T *b, const int n, const int nrhs)
solve linear equations Ax=b using LAPACK
void setup_2d_division(Parallel_Blkcyclic &pv, int nb, int gr, int gc)
=================2D-block Parallel===============
Definition lr_util.cpp:436
void gather_2d_to_full(const Parallel_Blkcyclic &pv, const T *submat, T *fullmat, const bool row_major, const std::size_t global_nrow, const std::size_t global_ncol)
gather 2d matrix to full matrix the defination of row and col is consistent with setup_2d_division
Definition lr_util.hpp:571
int print_value(const T *ptr, const int &size)
Definition lr_util_print.h:91
Definition esolver_lr_lcao_tddft.h:28
void build_and_solve_zeq(TOpsR &ops_R, TOpsL &ops_L, const int nspin_x, const T *const X, container::Tensor &R, T *const Z, const int nloc_per_band, const int nstates, const std::string &zvec_solver, const bool test_force, const std::vector< std::vector< ct::Tensor > > &window_density)
Definition zeq_solver.h:316
void solve_zeq_with(T *const Z, T *const R, const int ld, const int nstates, THamL &ops_L, const int nspin_x, const std::string &zvec_solver, const bool test_force)
Run the configured solver (and then, for testing, every supported one). Shared by the closed- and ope...
Definition zeq_solver.h:274
void solve_Z_2d(T *const Z, const T *const R, const int ld, const int nstates, THam &hm, const int nspin_x, void(*linear_solver)(T *, T *, const Parallel_Blkcyclic &, const Parallel_Blkcyclic &))
Distributed dense Z-vector solve: the Hessian (zvec_hessian_2d) and the right-hand side are laid out ...
Definition zeq_solver.h:194
void scalapack_linear_solver(T *A, T *B, const Parallel_Blkcyclic &pA, const Parallel_Blkcyclic &pB)
Solve A Z = B with ScaLAPACK LU (p?gesv), the distributed counterpart of LR_Util::lapack_linear_solve...
Definition zeqlin_solv.cpp:35
void zvec_full_to_local(THam &hm, const int nspin_x, const T *const full, T *const local)
Inverse of zvec_local_to_full: pick this rank's entries of the global vector full into the hm....
Definition zeq_solver.h:58
int zvec_global_dim(THam &hm, const int nspin_x)
Global length of one state's Z-vector: $\sum_\sigma n_k n_{occ,\sigma} n_{virt,\sigma}...
Definition zeq_solver.h:20
void zvec_local_to_full(THam &hm, const int nspin_x, const T *const local, T *const full)
Gather one state's Z-vector from the hm.pX layout (local, length ld) into the global vector full (len...
Definition zeq_solver.h:34
void solve_Z_scalapack_chol(T *const Z, const T *const R, const int ld, const int nstates, THam &hm, const int nspin_x)
Distributed dense Z-vector solve with a ScaLAPACK Cholesky factorization (p?potrf),...
Definition zeq_solver.h:260
void scalapack_cholesky_linear_solver(T *A, T *B, const Parallel_Blkcyclic &pA, const Parallel_Blkcyclic &pB)
Solve A Z = B for a Hermitian positive-definite A with ScaLAPACK alone: Cholesky p?...
Definition zeqlin_solv.cpp:58
void Z_vector_equation(const GradientInputs< T > &inputs, const T *const X, T *const Z, const int &nstates, std::weak_ptr< PotHxcLR > pot, const std::string &spin_type, const std::string &in_dir, const bool openshell, const std::string &zvec_solver, const std::vector< std::vector< ct::Tensor > > &window_density)
Definition zeq_solver.cpp:7
void solve_Z_scalapack(T *const Z, const T *const R, const int ld, const int nstates, THam &hm, const int nspin_x)
Distributed dense Z-vector solve with ScaLAPACK LU (p?gesv). Same system as solve_Z_lapack,...
Definition zeq_solver.h:246
std::vector< T > zvec_hessian_2d(THam &hm, const int nspin_x, const int ld, const Parallel_Blkcyclic &ph)
Build this rank's block of the Z-vector Hessian on the 2D block-cyclic layout ph (n_global x n_global...
Definition zeq_solver.h:157
void solve_Z_CG(double *const Z, double *R, const int &ld, const int &nstates, std::function< void(const double *const, double *const)> f_LZ, const bool test_force)
Definition zeq_cg.cpp:8
void solve_Z_lapack(T *const Z, const T *const R, const int &ld, const int &nstates, THam &hm, const int nspin_x, const bool test_force)
Solve the Z-vector equation with a dense LAPACK solve.
Definition zeq_solver.h:86
void TITLE(const std::string &class_name, const std::string &function_name, const bool disable)
Definition tool_title.cpp:18