ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
lr_util.hpp
Go to the documentation of this file.
1#ifndef ABACUS_SOURCE_LCAO_MODULE_LR_UTILS_LR_UTIL_HPP
2#define ABACUS_SOURCE_LCAO_MODULE_LR_UTILS_LR_UTIL_HPP
3
4#include "lr_util.h"
10#include <algorithm>
11#include <cstddef>
12#include <fstream>
13#include <iomanip>
14#include <limits>
15#include <stdexcept>
16#include <string>
17namespace LR_Util
18{
20
21 template <typename TCell>
22 int cal_nelec(const TCell& ucell) {
23 int nelec = 0;
24 for (int it = 0; it < ucell.ntype; ++it) {
25 nelec += ucell.atoms[it].ncpp.zv * ucell.atoms[it].na;
26 }
27 return nelec;
28 }
29
32 template<typename T, typename... Args>
33 std::unique_ptr<T> make_unique(Args &&... args)
34 {
35 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
36 }
37
38 //====== newers and deleters========
43 template <typename T>
44 void _allocate_2order_nested_ptr(T**& p2, size_t size1, size_t size2)
45 {
46 p2 = new T * [size1];
47 for (size_t i = 0; i < size1; ++i)
48 {
49 p2[i] = new T[size2];
50 }
51 };
52
57 template <typename T>
58 void _deallocate_2order_nested_ptr(T** p2, size_t size)
59 {
60 if (p2 != nullptr)
61 {
62 for (size_t i = 0; i < size; ++i)
63 {
64 if (p2[i] != nullptr) { delete[] p2[i]; }
65 }
66 delete[] p2;
67 }
68 };
69
70 inline double get_conj(const double& x)
71 {
72 return x;
73 }
74 inline std::complex<double> get_conj(const std::complex<double>& x)
75 {
76 return std::conj(x);
77 }
78 template <typename T>
79 void matsym(const T* in, const int n, T* out)
80 {
81 for (int i = 0; i < n; ++i) {
82 out[i * n + i] = 0.5 * in[i * n + i] + 0.5 * get_conj(in[i * n + i]);
83 }
84 for (int i = 0;i < n;++i) {
85 for (int j = i + 1;j < n;++j)
86 {
87 out[i * n + j] = 0.5 * (in[i * n + j] + get_conj(in[j * n + i]));
88 out[j * n + i] = get_conj(out[i * n + j]);
89 }
90 }
91 }
92 template <typename T>
93 void matsym(T* inout, const int n)
94 {
95 for (int i = 0; i < n; ++i) {
96 inout[i * n + i] = 0.5 * (inout[i * n + i] + get_conj(inout[i * n + i]));
97 }
98 for (int i = 0;i < n;++i) {
99 for (int j = i + 1;j < n;++j)
100 {
101 inout[i * n + j] = 0.5 * (inout[i * n + j] + get_conj(inout[j * n + i]));
102 inout[j * n + i] = get_conj(inout[i * n + j]);
103 }
104 }
105 }
106 template<typename T>
107 bool is_hermitian(const T* mat, const Parallel_Blkcyclic& pmat, const double threshold, const int my_rank){
108 bool is_herm = true;
109 int n = pmat.nrglb();
110 assert(n == pmat.ncglb());
111 double work_dummy = 0.0;
112 const char norm_type = 'F';
113 double norm1(0.0), norm2(0.0);
114#ifdef __MPI
115 const int one = 1;
116 T alpha = 1.0;
117 T beta = 0.0;
118 if (pmat.size() > std::numeric_limits<int>::max())
119 {
120 std::cerr<< "Warning: pmat in RANK " + std::to_string(my_rank) + " has "
121 + std::to_string(pmat.size()) + " elements, which may overflow during `tranc`, please use more mpi." << std::endl;
122 }
123 std::vector<T> herm_mat(pmat.size());
125 alpha, const_cast<T*>(mat), one, one, pmat.desc(),
126 beta, herm_mat.data(), one, one, pmat.desc());
127 ModuleBase::TITLE("LR_Util", "is_hermitian: tranc");
129 T diff;
130 for (std::size_t i = 0;i < pmat.size();++i) {
131 diff = mat[i] - herm_mat[i];
132 if (std::abs(diff) > threshold) { is_herm = false; }
133 norm1 += static_cast<double>(std::norm(diff));
134 norm2 += static_cast<double>(std::norm(mat[i] + herm_mat[i]));
135 }
136 MPI_Allreduce(MPI_IN_PLACE, &norm1, 1, MPI_DOUBLE, MPI_SUM, pmat.comm());
137 MPI_Allreduce(MPI_IN_PLACE, &norm2, 1, MPI_DOUBLE, MPI_SUM, pmat.comm());
138 norm1 = std::sqrt(norm1);
139 norm2 = std::sqrt(norm2);
140 // We don't use `lange` here because minus_mat and sum_mat are memory-consuming.
141 // double norm1 = ScalapackConnector::lange(norm_type, n, n, minus_mat.data(), one, one, pmat.desc, &work_dummy);
142 // double norm2 = ScalapackConnector::lange(norm_type, n, n, sum_mat.data(), one, one, pmat.desc, &work_dummy);
144 int local_flag = is_herm ? 1 : 0;
145 int global_flag = 0;
146 MPI_Allreduce(&local_flag, &global_flag, 1, MPI_INT, MPI_LAND, pmat.comm());
147 is_herm = (global_flag != 0);
148#else
149 assert(pmat.is_serial());
150 std::vector<T> minus_mat(pmat.size());
151 std::vector<T> sum_mat(pmat.size());
152 for (std::size_t i = 0;i < n;++i) {
153 for (std::size_t j = i;j < n;++j) {
154 minus_mat[i * n + j] = mat[i * n + j] - get_conj(mat[j * n + i]);
155 minus_mat[j * n + i] = -get_conj(minus_mat[i * n + j]);
156 if (std::abs(minus_mat[i * n + j]) > threshold) { is_herm = false; }
157 sum_mat[i * n + j] = mat[i * n + j] + get_conj(mat[j * n + i]);
158 sum_mat[j * n + i] = get_conj(sum_mat[i * n + j]);
159 }
160 }
161 norm1 = LapackConnector::lange(norm_type, n, n, minus_mat.data(), n, &work_dummy);
162 norm2 = LapackConnector::lange(norm_type, n, n, sum_mat.data(), n, &work_dummy);
163#endif
164 if (my_rank == 0) {
165 std::cout << "| Hermitian check: ||A - A^H||_F = " << norm1 << ", ||A + A^H||_F = " << norm2 << std::endl;
166 std::cout << "| ||A - A^H||_F / ||A + A^H||_F = " << norm1 / norm2 << std::endl;
167 }
168 return is_herm;
169 }
170
171 template<typename T>
172 bool is_symmetric(const T* mat, const Parallel_Blkcyclic& pmat, const double threshold, const int my_rank){
173 bool is_sym = true;
174 int n = pmat.nrglb();
175 assert(n == pmat.ncglb());
176 double work_dummy = 0.0;
177 const char norm_type = 'F';
178 double norm1(0.0), norm2(0.0);
179#ifdef __MPI
180 const int one = 1;
181 T alpha = 1.0;
182 T beta = 0.0;
183 if (pmat.size() > std::numeric_limits<int>::max())
184 {
185 std::cerr<< "Warning: pmat in RANK " + std::to_string(my_rank) + " has "
186 + std::to_string(pmat.size()) + " elements, which may overflow during `tranu`, please use more mpi." << std::endl;
187 }
188 std::vector<T> trans_mat(pmat.size());
190 alpha, const_cast<T*>(mat), one, one, pmat.desc(),
191 beta, trans_mat.data(), one, one, pmat.desc());
192 ModuleBase::TITLE("LR_Util", "is_symmetric: tranu");
194 T diff;
195 for (std::size_t i = 0;i < pmat.size();++i) {
196 diff = mat[i] - trans_mat[i];
197 if (std::abs(diff) > threshold) { is_sym = false; }
198 norm1 += static_cast<double>(std::norm(diff));
199 norm2 += static_cast<double>(std::norm(mat[i] + trans_mat[i]));
200 }
201 MPI_Allreduce(MPI_IN_PLACE, &norm1, 1, MPI_DOUBLE, MPI_SUM, pmat.comm());
202 MPI_Allreduce(MPI_IN_PLACE, &norm2, 1, MPI_DOUBLE, MPI_SUM, pmat.comm());
203 norm1 = std::sqrt(norm1);
204 norm2 = std::sqrt(norm2);
206 int local_flag = is_sym ? 1 : 0;
207 int global_flag = 0;
208 MPI_Allreduce(&local_flag, &global_flag, 1, MPI_INT, MPI_LAND, pmat.comm());
209 is_sym = (global_flag != 0);
210#else
211 assert(pmat.is_serial());
212 std::vector<T> minus_mat(pmat.size());
213 std::vector<T> sum_mat(pmat.size());
214 for (std::size_t i = 0;i < n;++i) {
215 for (std::size_t j = i;j < n;++j) {
216 minus_mat[i * n + j] = mat[i * n + j] - mat[j * n + i];
217 minus_mat[j * n + i] = -minus_mat[i * n + j];
218 if (std::abs(minus_mat[i * n + j]) > threshold) {is_sym = false; }
219 sum_mat[i * n + j] = mat[i * n + j] + mat[j * n + i];
220 sum_mat[j * n + i] = sum_mat[i * n + j];
221 }
222 }
223 norm1 = LapackConnector::lange(norm_type, n, n, minus_mat.data(), n, &work_dummy);
224 norm2 = LapackConnector::lange(norm_type, n, n, sum_mat.data(), n, &work_dummy);
225#endif
226 if (my_rank == 0) {
227 std::cout << "| Symmetric check: ||B - B^T||_F = " << norm1 << ", ||B + B^T||_F = " << norm2 << std::endl;
228 std::cout << "| ||B - B^T||_F / ||B + B^T||_F = " << norm1 / norm2 << std::endl;
229 }
230 return is_sym;
231 }
232
234 template<typename T>
235 psi::Psi<T> get_psi_spin(const psi::Psi<T>& psi_in, const int& is, const int& nk)
236 {
237 // Empty coefficient ranks have no (band=0, AO=0) element to address.
238 // The pointer is only a view; the PBLAS descriptors retain zero ownership.
239 if (psi_in.get_nbands() == 0 || psi_in.get_nbasis() == 0)
240 {
241 return psi::Psi<T>(psi_in.get_pointer(), nk, psi_in.get_nbands(), psi_in.get_nbasis(), true);
242 }
243 return psi::Psi<T>(&psi_in(is * nk, 0, 0),
244 nk,
245 psi_in.get_nbands(),
246 psi_in.get_nbasis(),
247 true);
248 }
249
251 template<typename T, typename Device>
252 psi::Psi<T, Device> k1_to_bfirst_wrapper(const psi::Psi<T, Device>& psi_kfirst, int nk_in, int nbasis_in)
253 {
254 assert(psi_kfirst.get_nk() == 1);
255 assert(nk_in * nbasis_in == psi_kfirst.get_nbasis());
256
257 int ib_now = psi_kfirst.get_current_b();
258 psi_kfirst.fix_b(0); // for get_pointer() to get the head pointer
259 psi::Psi<T, Device> psi_bfirst(psi_kfirst.get_pointer(),
260 nk_in,
261 psi_kfirst.get_nbands(),
262 nbasis_in,
263 nbasis_in,
264 false);
265 psi_kfirst.fix_b(ib_now);
266 return psi_bfirst;
267 }
268
270 template<typename T, typename Device>
272 {
273 int ib_now = psi_bfirst.get_current_b();
274 int ik_now = psi_bfirst.get_current_k();
275
276 psi_bfirst.fix_kb(0, 0); // for get_pointer() to get the head pointer
277 psi::Psi<T, Device> psi_kfirst(psi_bfirst.get_pointer(),
278 1,
279 psi_bfirst.get_nbands(),
280 psi_bfirst.get_nk() * psi_bfirst.get_nbasis(),
281 psi_bfirst.get_nk() * psi_bfirst.get_nbasis(),
282 true);
283 psi_bfirst.fix_kb(ik_now, ib_now);
284 return psi_kfirst;
285 }
286//=================2D-block Parallel===============
287
291 template <typename T>
292 void global2local_X(T* local_X, const T* global_X, const int nband, const int nk,
293 const std::vector<int>& nocc, const std::vector<int>& nvirt, const std::vector<Parallel_Blkcyclic>& pX,
294 const bool openshell)
295 {
296 const int nspin_X = openshell ? 2 : 1;
297 const std::vector<int> npairs = { nocc[0] * nvirt[0], nocc[1] * nvirt[1] };
298 const int gdim = openshell ? nk * (npairs[0] + npairs[1] ) : nk * npairs[0];
299 const int ldim = openshell ? nk * (pX[0].size() + pX[1].size()) : nk * pX[0].size();
300
301 for (int ib = 0;ib < nband;++ib)
302 {
303 const int loffset_b = ib * ldim;
304 const int goffset_b = ib * gdim;
305 for (int is = 0;is < nspin_X;++is)
306 {
307 const int loffset_bs = loffset_b + is * nk * pX[0].size();
308 const int goffset_bs = goffset_b + is * nk * npairs[0];
309 for (int ik = 0;ik < nk;++ik)
310 {
311 const int loffset = loffset_bs + ik * pX[is].size();
312 const int goffset = goffset_bs + ik * npairs[is];
313 for (int lo = 0; lo < pX[is].ncloc(); ++lo)
314 {
315 const int go = pX[is].icglb(lo);
316 for (int lv = 0; lv < pX[is].nrloc(); ++lv)
317 {
318 const int gv = pX[is].irglb(lv);
319 local_X[loffset + lo * pX[is].nrloc() + lv] = global_X[goffset + go * nvirt[is] + gv];
320 }
321 }
322 }
323 }
324 }
326 }
327#ifdef __MPI
329 {
330 int ivirt; // {nvirt, nocc} global row
331 int iocc; // {nvirt, nocc} global col
332 int ik;
334 void set(int iv_, int io_, int ik_, int src_rank_)
335 {
336 ivirt = iv_;
337 iocc = io_;
338 ik = ik_;
339 src_rank = src_rank_;
340 }
341 };
342 inline MPI_Datatype mpi_type_blockhead()
343 {
344 static MPI_Datatype dt = MPI_DATATYPE_NULL;
345 static bool committed = false;
346 if (!committed)
347 {
348 int blen[4] = {1, 1, 1, 1};
349 MPI_Aint disp[4];
350 MPI_Datatype types[4] = {MPI_INT, MPI_INT, MPI_INT, MPI_INT};
351
352 disp[0] = static_cast<MPI_Aint>(offsetof(BlockHead, ivirt));
353 disp[1] = static_cast<MPI_Aint>(offsetof(BlockHead, iocc));
354 disp[2] = static_cast<MPI_Aint>(offsetof(BlockHead, ik));
355 disp[3] = static_cast<MPI_Aint>(offsetof(BlockHead, src_rank));
356
357 MPI_Type_create_struct(4, blen, disp, types, &dt);
358 MPI_Type_commit(&dt);
359 committed = true;
360 }
361 return dt;
362 }
363
366 template <typename T>
367 void pA2pX(T* X_pX, const T* X_pA, const int nband, const int nk,
368 const std::vector<int>& nocc, const std::vector<int>& nvirt,
369 const std::vector<Parallel_Blkcyclic>& pX, const Parallel_Blkcyclic& pA,
370 const int row_offset, const int col_offset, const bool openshell,
371 const int my_rank, const int nproc)
372 {
373 ModuleBase::TITLE("LR_Util", "pA2pX");
374 ModuleBase::timer::start("LR_Util", "pA2pX");
375 const int nspin_X = openshell ? 2 : 1;
376 const std::vector<int> npairs = { nocc[0] * nvirt[0], nocc[1] * nvirt[1] };
377 const std::size_t gdim = openshell ? nk * (npairs[0] + npairs[1] ) : nk * npairs[0];
378 const std::size_t ldim = openshell ? nk * (pX[0].size() + pX[1].size()) : nk * pX[0].size();
379 assert(pA.nrglb() == gdim || pA.nrglb() == 2 * gdim);
380 assert(pA.ncglb() == gdim || pA.ncglb() == 2 * gdim);
381 for (int is = 0;is < nspin_X;++is)
382 {
383 assert(pX[is].nrglb() == nvirt[is]);
384 assert(pX[is].ncglb() == nocc[is]);
385 }
386 MPI_Datatype mpitype_blockhead = mpi_type_blockhead();
387
388 // 0. outer loop of row, communicate per 64 kpoints
389 constexpr int comm_nk = 64;
390 std::vector<int> send_head_counts(nproc, 0), recv_head_counts(nproc, 0);
391 std::vector<int> send_buffer_counts(nproc, 0), recv_buffer_counts(nproc, 0);
392 std::vector<int> shdispls(nproc, 0), rhdispls(nproc, 0); // displacements of block heads
393 std::vector<int> sbdispls(nproc, 0), rbdispls(nproc, 0); // displacements of buffer
394 std::vector<int> cursor_head(nproc, 0), cursor_buffer(nproc, 0);
395 std::vector<BlockHead> send_heads, recv_heads;
396 std::vector<T> send_buffers, recv_buffers;
397 int max_send_head_total = 0, max_recv_head_total = 0;
398 int max_send_buffer_total = 0, max_recv_buffer_total = 0;
399
400 // 1. pre-calculate local bands and global band list on each processor
401 const int gb_start = col_offset;
402 const int gb_end = col_offset + nband;
403 std::vector<int> lnbands(nproc, 0);
404 std::vector<std::vector<int>> gblist(nproc);
405 const int nbpA = pA.nb();
406 for (int gb = gb_start; gb < gb_end; ++gb)
407 {
408 int proc_col = (gb / nbpA) % pA.ncproc();
409 for (int pr = 0; pr < pA.nrproc(); ++pr)
410 {
411 int ownerA = proc_col + pr * pA.ncproc();
412 ++lnbands[ownerA];
413 gblist[ownerA].push_back(gb);
414 }
415 }
416 const int lnband = lnbands[my_rank];
417 if (lnband == 0)
418 {
419 throw std::runtime_error("rank" + std::to_string(my_rank) + "lnband = 0, please check the pA and pX distribution!");
420 }
421 const int lb_start = pA.icloc(gblist[my_rank].front());
422 const int lb_end = lb_start + lnband;
423
424 for (int is = 0; is < nspin_X; ++is)
425 {
426 const std::size_t gspin_base = is * nk * npairs[0];
427 const std::size_t lspin_base = is * nk * pX[0].size();
428 const std::size_t nrow = comm_nk * npairs[is];
429 const std::size_t grow_start = row_offset + gspin_base;
430 const std::size_t grow_end = grow_start + nk * npairs[is];
431 for (int irow_start = grow_start; irow_start < grow_end; irow_start += nrow)
432 {
433 std::fill(send_head_counts.begin(), send_head_counts.end(), 0);
434 std::fill(send_buffer_counts.begin(), send_buffer_counts.end(), 0);
435
436 // 2. calculate and coummunicate block counts, then calculate block displacements
437 int irow_end = std::min(irow_start + nrow, grow_end);
438 for (int irow = irow_start; irow < irow_end; ++irow)
439 {
440 const int lrpA = pA.irloc(irow);
441 if (lrpA == -1) continue;
442 const int ir = irow - row_offset - gspin_base;
443 const int ik = ir / npairs[is];
444 const int io = (ir-ik*npairs[is]) / nvirt[is];
445 const int iv = ir % nvirt[is];
446 int ownerX = pX[is].owner_processor(iv, io);
447 if (ownerX != my_rank)
448 {
449 ++send_head_counts[ownerX];
450 }
451 }
452 for (int p = 0; p < nproc; ++p)
453 {
454 std::size_t nbuffer = size_t(lnband) * send_head_counts[p];
455 if (nbuffer > std::numeric_limits<int>::max())
456 {
457 throw std::overflow_error("in pA2pX: overflow converting to int!");
458 }
459 send_buffer_counts[p] = static_cast<int>(nbuffer);
460 }
461
462 assert(send_head_counts.at(my_rank) == 0);
463 assert(send_buffer_counts.at(my_rank) == 0);
464 MPI_Alltoall(send_head_counts.data(), 1, MPI_INT, recv_head_counts.data(), 1, MPI_INT, pA.comm());
465 MPI_Alltoall(send_buffer_counts.data(), 1, MPI_INT,
466 recv_buffer_counts.data(), 1, MPI_INT, pA.comm());
467
468 int send_head_total = 0, recv_head_total = 0, send_buffer_total = 0, recv_buffer_total = 0;
469 for (int p = 0; p < nproc; ++p)
470 {
471 shdispls[p] = send_head_total; send_head_total += send_head_counts[p];
472 rhdispls[p] = recv_head_total; recv_head_total += recv_head_counts[p];
473 sbdispls[p] = send_buffer_total; send_buffer_total += send_buffer_counts[p];
474 rbdispls[p] = recv_buffer_total; recv_buffer_total += recv_buffer_counts[p];
475 }
476 if (send_head_total > max_send_head_total)
477 { max_send_head_total = send_head_total; send_heads.reserve(max_send_head_total); }
478 if (recv_head_total > max_recv_head_total)
479 { max_recv_head_total = recv_head_total; recv_heads.reserve(max_recv_head_total); }
480 if (send_buffer_total > max_send_buffer_total)
481 { max_send_buffer_total = send_buffer_total; send_buffers.reserve(max_send_buffer_total); }
482 if (recv_buffer_total > max_recv_buffer_total)
483 { max_recv_buffer_total = recv_buffer_total; recv_buffers.reserve(max_recv_buffer_total); }
484
485 // 3. prepare block heads and buffers
486 send_heads.resize(send_head_total);
487 recv_heads.resize(recv_head_total);
488 send_buffers.resize(send_buffer_total);
489 recv_buffers.resize(recv_buffer_total);
490 cursor_head = shdispls;
491 cursor_buffer = sbdispls;
492
493 for (int irow = irow_start; irow < irow_end; ++irow)
494 {
495 const int lr = pA.irloc(irow);
496 if (lr == -1) continue;
497 const std::size_t ir = irow - row_offset - gspin_base;
498 const std::size_t ik = ir / npairs[is];
499 const int io = (ir-ik*npairs[is]) / nvirt[is];
500 const int iv = ir % nvirt[is];
501 const int ownerX = pX[is].owner_processor(iv, io);
502 if (ownerX == my_rank)
503 {
504 for (std::size_t lb = lb_start; lb < lb_end; ++lb)
505 {
506 const int gb = pA.icglb(lb);
507 const std::size_t lb_base = std::size_t(gb - col_offset) * std::size_t(ldim);
508 const std::size_t lrowX = pX[is].irloc(iv);
509 const std::size_t lcolX = pX[is].icloc(io);
510 const std::size_t lX_indx = lrowX + lcolX * pX[is].nrloc() + lb_base
511 + lspin_base + ik * pX[is].size();
512 X_pX[lX_indx] = X_pA[static_cast<std::size_t>(lr) + lb * pA.nrloc()];
513 }
514 }
515 else
516 {
517 BlockHead& head = send_heads[cursor_head[ownerX]++];
518 head.set(iv, io, ik, my_rank);
519 for (std::size_t lb = lb_start; lb < lb_end; ++lb)
520 {
521 send_buffers[cursor_buffer[ownerX]++] = X_pA[static_cast<std::size_t>(lr) + lb * pA.nrloc()];
522 }
523 }
524 }
525 for (int p = 0; p < nproc; ++p)
526 {
527 assert(cursor_head[p] == shdispls[p] + send_head_counts[p]);
528 assert(cursor_buffer[p] == sbdispls[p] + send_buffer_counts[p]);
529 }
530 // 4. communicate block heads and buffers
531 MPI_Alltoallv(send_heads.data(), send_head_counts.data(), shdispls.data(), mpitype_blockhead,
532 recv_heads.data(), recv_head_counts.data(), rhdispls.data(), mpitype_blockhead,
533 pA.comm());
534 MPI_Alltoallv(send_buffers.data(), send_buffer_counts.data(), sbdispls.data(), LR_Util::MPIType<T>::value(),
535 recv_buffers.data(), recv_buffer_counts.data(), rbdispls.data(), LR_Util::MPIType<T>::value(),
536 pA.comm());
537
538 // 5. unpack received buffers to pX
539 for (int src = 0; src < nproc; ++src)
540 {
541 const int nh = recv_head_counts[src];
542 if (nh == 0) continue;
543 int buf_cursor = rbdispls[src];
544 const int ib_start = rhdispls[src];
545 const int ib_end = ib_start + nh;
546 for (int iblock = ib_start; iblock < ib_end; ++iblock)
547 {
548 const BlockHead& rh = recv_heads[iblock];
549 assert(rh.src_rank == src);
550 const std::size_t ik = rh.ik;
551 const std::size_t lr = pX[is].irloc(rh.ivirt);
552 const std::size_t lc = pX[is].icloc(rh.iocc);
553 for (int ib = 0; ib < lnbands[src]; ++ib)
554 {
555 const std::size_t lb_base = std::size_t(gblist[src][ib] - col_offset) * std::size_t(ldim);
556 const std::size_t lX_indx = lr + lc * pX[is].nrloc() + lb_base
557 + lspin_base + ik * pX[is].size();
558 X_pX[lX_indx] = recv_buffers[ib + buf_cursor];
559 }
560 buf_cursor += lnbands[src];
561 }
562 assert(buf_cursor == rbdispls[src] + recv_buffer_counts[src]);
563 }
564 } // end of irow_start
565 } // end of is
567 ModuleBase::timer::end("LR_Util", "pA2pX");
568 }
569
570 template <typename T>
571 void gather_2d_to_full(const Parallel_Blkcyclic& pv, const T* submat, T* fullmat,
572 const bool row_major, const std::size_t global_nrow, const std::size_t global_ncol)
573 {
574 assert(pv.nrglb() == global_nrow);
575 assert(pv.ncglb() == global_ncol);
576 // copy
577#ifdef _OPENMP
578#pragma omp parallel for collapse(2)
579#endif
580 for (int j = 0; j < pv.ncloc(); ++j) {
581 for (int i = 0; i < pv.nrloc(); ++i) {
582 if (row_major) {
583 fullmat[pv.irglb(i) * global_ncol + pv.icglb(j)] = submat[i * pv.ncloc() + j];
584 } else {
585 fullmat[pv.icglb(j) * global_nrow + pv.irglb(i)] = submat[j * pv.nrloc() + i];
586 }
587 }
588 }
589 MPI_Allreduce(MPI_IN_PLACE, fullmat, global_nrow * global_ncol, LR_Util::MPIType<T>::value(), MPI_SUM, pv.comm());
590 };
591
592 template <typename T>
593 void scatter_full_to_2d(const Parallel_Blkcyclic& pv, const T* fullmat, T* submat, const bool col_first)
594 {
595 ModuleBase::TITLE("LR_Util", "scatter_full_to_2d");
596 const int global_nrow = pv.nrglb();
597 const int global_ncol = pv.ncglb();
598 for (int i = 0; i < pv.nrloc(); ++i)
599 for (int j = 0; j < pv.ncloc(); ++j)
600 if (col_first)
601 submat[i * pv.ncloc() + j] = fullmat[pv.irglb(i) * global_ncol + pv.icglb(j)];
602 else
603 submat[j * pv.nrloc() + i] = fullmat[pv.icglb(j) * global_nrow + pv.irglb(i)];
604 }
605#endif
606}
607
608#endif // ABACUS_SOURCE_LCAO_MODULE_LR_UTILS_LR_UTIL_HPP
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 ncloc() const
Definition parallel_blkcyc.h:40
int ncproc() const
Definition parallel_blkcyc.h:63
int nrproc() const
Definition parallel_blkcyc.h:58
const int * desc() const
Definition parallel_blkcyc.h:125
int irloc(const int irglb) const
Definition parallel_blkcyc.h:78
int ncglb() const
Definition parallel_blkcyc.cpp:86
int nrloc() const
Definition parallel_blkcyc.h:35
int icglb(const int icloc) const
Definition parallel_blkcyc.h:93
int nb() const
Definition parallel_blkcyc.h:53
std::size_t size() const
Definition parallel_blkcyc.h:48
MPI_Comm comm() const
Definition parallel_blkcyc.cpp:98
int nrglb() const
Definition parallel_blkcyc.cpp:75
int icloc(const int icglb) const
Definition parallel_blkcyc.h:83
bool is_serial() const
Definition parallel_blkcyc.h:112
static void tranu(const int m, const int n, const double alpha, const double *a, const int ia, const int ja, const int *desca, const double beta, double *c, const int ic, const int jc, const int *descc)
Definition scalapack_connector.h:465
static void tranc(const int m, const int n, const double alpha, const double *a, const int ia, const int ja, const int *desca, const double beta, double *c, const int ic, const int jc, const int *descc)
Definition scalapack_connector.h:481
Definition psi.h:37
const int & get_nbands() const
Definition psi.cpp:337
int get_current_b() const
Definition psi.cpp:466
const int & get_nk() const
Definition psi.cpp:331
void fix_kb(const int ik, const int ib) const
Definition psi.cpp:416
const int & get_nbasis() const
Definition psi.cpp:343
int get_current_k() const
Definition psi.cpp:460
T * get_pointer() const
Definition psi.cpp:267
void fix_b(const int ib) const
Definition psi.cpp:391
#define T
Definition exp.cpp:237
This is a wrapper of some LAPACK routines. Row-Major version.
std::ofstream ofs_running
Definition global_variable.cpp:38
Definition exciton_plotter.cpp:368
psi::Psi< T > get_psi_spin(const psi::Psi< T > &psi_in, const int &is, const int &nk)
get the Psi wrapper of the selected spin from the Psi object
Definition lr_util.hpp:235
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
void global2local_X(T *local_X, const T *global_X, const int nband, const int nk, const std::vector< int > &nocc, const std::vector< int > &nvirt, const std::vector< Parallel_Blkcyclic > &pX, const bool openshell)
assign global X to 2d-matrix, its col is band(excition state), and row is { spin, k-point,...
Definition lr_util.hpp:292
int cal_nelec(const TCell &ucell)
calculate the number of electrons
Definition lr_util.hpp:22
std::unique_ptr< T > make_unique(Args &&... args)
Definition lr_util.hpp:33
void matsym(const T *in, const int n, T *out)
Definition lr_util.hpp:79
bool is_hermitian(const T *mat, const Parallel_Blkcyclic &pmat, const double threshold, const int my_rank)
Definition lr_util.hpp:107
double get_conj(const double &x)
Definition lr_util.hpp:70
MPI_Datatype mpi_type_blockhead()
Definition lr_util.hpp:342
psi::Psi< T, Device > bfirst_to_k1_wrapper(const psi::Psi< T, Device > &psi_bfirst)
psi(nb, nk, nbasis) -> psi(nk=1, nbands=nb, nk * nbasis) without memory copy
Definition lr_util.hpp:271
psi::Psi< T, Device > k1_to_bfirst_wrapper(const psi::Psi< T, Device > &psi_kfirst, int nk_in, int nbasis_in)
psi(nk=1, nbands=nb, nk * nbasis) -> psi(nb, nk, nbasis) without memory copy
Definition lr_util.hpp:252
void _allocate_2order_nested_ptr(T **&p2, size_t size1, size_t size2)
new 2d pointer
Definition lr_util.hpp:44
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
void _deallocate_2order_nested_ptr(T **p2, size_t size)
delete 2d pointer
Definition lr_util.hpp:58
void pA2pX(T *X_pX, const T *X_pA, const int nband, const int nk, const std::vector< int > &nocc, const std::vector< int > &nvirt, const std::vector< Parallel_Blkcyclic > &pX, const Parallel_Blkcyclic &pA, const int row_offset, const int col_offset, const bool openshell, const int my_rank, const int nproc)
assign X in pA to X in pX
Definition lr_util.hpp:367
bool is_symmetric(const T *mat, const Parallel_Blkcyclic &pmat, const double threshold, const int my_rank)
Definition lr_util.hpp:172
void DONE(std::ofstream &ofs, const std::string &description, const bool only_rank0)
Definition global_function.cpp:78
void TITLE(const std::string &class_name, const std::string &function_name, const bool disable)
Definition tool_title.cpp:18
#define threshold
Definition sph_bessel_rec_test.cpp:4
Definition lr_util.hpp:329
int ivirt
Definition lr_util.hpp:330
int ik
Definition lr_util.hpp:332
void set(int iv_, int io_, int ik_, int src_rank_)
Definition lr_util.hpp:334
int iocc
Definition lr_util.hpp:331
int src_rank
Definition lr_util.hpp:333
static MPI_Datatype value()
Definition lr_util.h:213