ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
molecular_lri_comm.hpp
Go to the documentation of this file.
1//=======================
2// AUTHOR : Ziqing Guan
3// DATE : 2026-03-22
4//=======================
5#include "molecular_lri.h"
6#include <algorithm>
7#include <cstddef> // offsetof
8#include <limits>
9#ifdef _OPENMP
10#include <omp.h>
11#endif
12
13#ifdef __MPI
14#include <mpi.h>
15#endif
16namespace BSE
17{
18
20{
21 int gr0; // global row start
22 int gc0; // global col start
23 int nr; // rows in block
24 int nc; // cols in block
25};
26
27#ifdef __MPI
28inline MPI_Datatype mpi_type_blockhead()
29{
30 static MPI_Datatype dt = MPI_DATATYPE_NULL;
31 static bool committed = false;
32 if (!committed)
33 {
34 int blen[4] = {1, 1, 1, 1};
35 MPI_Aint disp[4];
36 MPI_Datatype types[4] = {MPI_INT, MPI_INT, MPI_INT, MPI_INT};
37
38 disp[0] = static_cast<MPI_Aint>(offsetof(BlockHead, gr0));
39 disp[1] = static_cast<MPI_Aint>(offsetof(BlockHead, gc0));
40 disp[2] = static_cast<MPI_Aint>(offsetof(BlockHead, nr));
41 disp[3] = static_cast<MPI_Aint>(offsetof(BlockHead, nc));
42
43 MPI_Type_create_struct(4, blen, disp, types, &dt);
44 MPI_Type_commit(&dt);
45 committed = true;
46 }
47 return dt;
48}
49#endif
50
52template <typename T>
53void MolecularLRI<T>::transform_k_2dlocal(std::vector<T>& m_2d,
54 const std::map<int, std::map<int, RI::Tensor<T>>>& m_lri,
55 const Parallel_Blkcyclic& pm_2d, const double factor)
56{
57 ModuleBase::TITLE("MolecularLRI", "transform_k_2dlocal");
58 ModuleBase::timer::start("MolecularLRI", "transform_k_2dlocal");
59 const int npair = this->nocc * this->nvirt;
60 const double fac_base = factor * 2.0; // factor 2 for Ha → Ry; k-point weight multiplies below
61 const int nb = pm_2d.nb();
62#ifdef __MPI
63 MPI_Datatype mpitype_blockhead = mpi_type_blockhead();
64 // 0. outer loop of k1: communicate per 64 k1
65 constexpr int comm_nk1 = 64;
66 std::vector<int> send_head_counts(this->nproc, 0), recv_head_counts(this->nproc, 0);
67 std::vector<int> send_buffer_counts(this->nproc, 0), recv_buffer_counts(this->nproc, 0);
68 std::vector<std::size_t> send_buffer_counts_c(this->nproc, 0);
69 std::vector<int> shdispls(this->nproc, 0), rhdispls(this->nproc, 0); //displacements of block heads
70 std::vector<int> sbdispls(this->nproc, 0), rbdispls(this->nproc, 0); //displacements of buffer
71 std::vector<int> cursor_head(this->nproc, 0), cursor_buffer(this->nproc, 0);
72 std::vector<BlockHead> send_heads, recv_heads;
73 std::vector<T> send_buffers, recv_buffers;
74 int max_send_head_total = 0, max_recv_head_total = 0;
75 int max_send_buffer_total = 0, max_recv_buffer_total = 0;
76 for (int k1_start = 0; k1_start < this->nk; k1_start += comm_nk1)
77 {
78 std::fill(send_head_counts.begin(), send_head_counts.end(), 0);
79 std::fill(send_buffer_counts.begin(), send_buffer_counts.end(), 0);
80 std::fill(send_buffer_counts_c.begin(), send_buffer_counts_c.end(), 0);
81 std::fill(recv_head_counts.begin(), recv_head_counts.end(), 0);
82 std::fill(recv_buffer_counts.begin(), recv_buffer_counts.end(), 0);
83 const int k1_end = std::min(k1_start+comm_nk1, this->nk);
84 // 1. calculate and coummunicate block counts, then calculate block displacements
85 for (int kai = k1_start; kai < k1_end; ++kai)
86 {
87 if ( !this->is_local_k1[kai] ) continue;
88 auto it_k1 = m_lri.find(kai);
89 if (it_k1 == m_lri.end()) { continue; }
90 const int row_base = kai * npair;
91 for (const int kbj : this->LR_lri.k2_indices)
92 {
93 auto it_k2 = it_k1->second.find(kbj);
94 if (it_k2 == it_k1->second.end()) { continue; }
95 const int col_base = kbj * npair;
96 for (int j = 0; j < npair; )
97 {
98 const int global_col = col_base + j;
99 const int j_next = std::min(global_col/nb*nb + nb - col_base, npair);
100 for (int i = 0; i < npair; )
101 {
102 const int global_row = row_base + i;
103 const int i_next = std::min(global_row/nb*nb + nb - row_base, npair);
104 if (!pm_2d.in_this_processor(global_row, global_col))
105 {
106 const int owner = pm_2d.owner_processor(global_row, global_col);
107 ++send_head_counts[owner];
108 send_buffer_counts_c[owner] += std::size_t(i_next - i) * std::size_t(j_next - j);
109 }
110 i = i_next;
111 }
112 j = j_next;
113 }
114 }
115 }
116 for (int p = 0; p < this->nproc; ++p)
117 {
118 if (send_buffer_counts_c[p] > std::numeric_limits<int>::max())
119 {
120 throw std::overflow_error("in transform_k_2dlocal: overflow converting to int!");
121 }
122 send_buffer_counts[p] = static_cast<int>(send_buffer_counts_c[p]);
123 }
124 assert(send_head_counts.at(this->my_rank) == 0);
125 assert(send_buffer_counts.at(this->my_rank) == 0);
126 MPI_Alltoall(send_head_counts.data(), 1, MPI_INT, recv_head_counts.data(), 1, MPI_INT, pm_2d.comm());
127 MPI_Alltoall(send_buffer_counts.data(), 1, MPI_INT,
128 recv_buffer_counts.data(), 1, MPI_INT, pm_2d.comm());
129
130 int send_head_total = 0, recv_head_total = 0, send_buffer_total = 0, recv_buffer_total = 0;
131 for (int p = 0; p < this->nproc; ++p)
132 {
133 shdispls[p] = send_head_total; send_head_total += send_head_counts[p];
134 rhdispls[p] = recv_head_total; recv_head_total += recv_head_counts[p];
135 sbdispls[p] = send_buffer_total; send_buffer_total += send_buffer_counts[p];
136 rbdispls[p] = recv_buffer_total; recv_buffer_total += recv_buffer_counts[p];
137 }
138 if (send_head_total > max_send_head_total)
139 { max_send_head_total = send_head_total; send_heads.reserve(max_send_head_total); }
140 if (recv_head_total > max_recv_head_total)
141 { max_recv_head_total = recv_head_total; recv_heads.reserve(max_recv_head_total); }
142 if (send_buffer_total > max_send_buffer_total)
143 { max_send_buffer_total = send_buffer_total; send_buffers.reserve(max_send_buffer_total); }
144 if (recv_buffer_total > max_recv_buffer_total)
145 { max_recv_buffer_total = recv_buffer_total; recv_buffers.reserve(max_recv_buffer_total); }
146
147 // 2. prepare block heads and buffers for send and recv
148 send_heads.resize(send_head_total);
149 recv_heads.resize(recv_head_total);
150 send_buffers.resize(send_buffer_total);
151 recv_buffers.resize(recv_buffer_total);
152 cursor_head = shdispls;
153 cursor_buffer = sbdispls;
154 for (int kai = k1_start; kai < k1_end; ++kai)
155 {
156 if (!this->is_local_k1[kai] ) continue;
157 const int row_base = kai * npair;
158 auto it_k1 = m_lri.find(kai);
159 if (it_k1 == m_lri.end()) { continue; }
160 const double fac = fac_base * this->kv.wk[kai]; // k-point weight, normalized as sum = 1
161 for (const int kbj : this->LR_lri.k2_indices)
162 {
163 const int col_base = kbj * npair;
164 auto it_k2 = it_k1->second.find(kbj);
165 if (it_k2 == it_k1->second.end()) { continue; }
166 const RI::Tensor<T>& m_kai_kbj = it_k2->second;
167 for (int j = 0; j < npair; )
168 {
169 const int global_col = col_base + j;
170 const int j_next = std::min(global_col/nb*nb + nb - col_base, npair);
171 for (int i = 0; i < npair; )
172 {
173 const int global_row = row_base + i;
174 const int i_next = std::min(global_row/nb*nb + nb - row_base, npair);
175 const int owner = pm_2d.owner_processor(global_row, global_col);
176 if (owner == this->my_rank)
177 {
178 const int lr0 = pm_2d.irloc(global_row);
179 const int lc0 = pm_2d.icloc(global_col);
180 const int lld = pm_2d.nrloc();
181 for(int jj = j; jj < j_next; ++jj)
182 {
183 for(int ii = i; ii < i_next; ++ii)
184 {
185 const int lr = lr0 + (ii - i);
186 const int lc = lc0 + (jj - j);
187 const std::size_t idx_2d = lr + std::size_t(lc) * std::size_t(lld);
188 m_2d[idx_2d] += (*m_kai_kbj.data)[ii + jj * npair] * fac;
189 }
190 }
191 }
192 else
193 {
194 BlockHead& head = send_heads[cursor_head[owner]++];
195 head.gr0 = global_row;
196 head.gc0 = global_col;
197 head.nr = i_next - i;
198 head.nc = j_next - j;
199 for(int jj = j; jj < j_next; ++jj)
200 {
201 for (int ii = i; ii < i_next; ++ii)
202 {
203 send_buffers[cursor_buffer[owner]++] = (*m_kai_kbj.data)[ii + jj * npair] * fac;
204 }
205 }
206 }
207 i = i_next;
208 }
209 j = j_next;
210 }
211 }
212 }
213 for (int p = 0; p < this->nproc; ++p)
214 {
215 assert(cursor_head[p] == shdispls[p] + send_head_counts[p]);
216 assert(cursor_buffer[p] == sbdispls[p] + send_buffer_counts[p]);
217 }
218 // 3. communicate block heads and buffers
219 MPI_Alltoallv(send_heads.data(), send_head_counts.data(), shdispls.data(), mpitype_blockhead,
220 recv_heads.data(), recv_head_counts.data(), rhdispls.data(), mpitype_blockhead,
221 pm_2d.comm());
222 MPI_Alltoallv(send_buffers.data(), send_buffer_counts.data(), sbdispls.data(), LR_Util::MPIType<T>::value(),
223 recv_buffers.data(), recv_buffer_counts.data(), rbdispls.data(), LR_Util::MPIType<T>::value(),
224 pm_2d.comm());
225 // 4. unpack recv head and buffer
226 int buf_cursor = 0;
227 for (int iblock = 0; iblock < recv_head_total; ++iblock)
228 {
229 const BlockHead& rh = recv_heads[iblock];
230 const int nr = rh.nr;
231 const int nc = rh.nc;
232 const int lr = pm_2d.irloc(rh.gr0);
233 const int lc = pm_2d.icloc(rh.gc0);
234 const int lld = pm_2d.nrloc();
235 for (int j = 0; j < nc; ++j)
236 {
237 for (int i = 0; i < nr; ++i)
238 {
239 const int idx_buffer = i + j * nr;
240 const std::size_t idx_2d = (lr + i) + std::size_t(lc + j) * std::size_t(lld);
241 m_2d[idx_2d] += recv_buffers[idx_buffer + buf_cursor];
242 }
243 }
244 buf_cursor += nr * nc;
245 }
246 assert(buf_cursor == recv_buffer_total);
247 }
248#else
249 // gather all Wk
250 auto gather_matrix = [&](std::vector<T>& target,
251 const std::valarray<T>& value,
252 int k1_step,
253 int k2_step,
254 double factor) -> void
255 {
256 for (int j = 0; j < npair; ++j)
257 {
258 for (int i = 0; i < npair; ++i)
259 {
260 const std::size_t idx_target = (k1_step + i) + std::size_t(k2_step + j) * std::size_t(this->ndim);
261 const int idx_value = i + j * npair;
262 target[idx_target] += value[idx_value] * factor;
263 }
264 }
265 };
266
267 #ifdef _OPENMP
268 #pragma omp parallel for schedule(static) collapse(2)
269 #endif
270 for (const int kai : this->k1_indices)
271 {
272 const int k1_step = kai * npair;
273 for (const int kbj : this->k2_indices)
274 {
275 const int k2_step = kbj * npair;
276 auto it_k1 = m_lri.find(kai);
277 if (it_k1 == m_lri.end()) { continue; }
278 auto it_k2 = it_k1->second.find(kbj);
279 if (it_k2 == it_k1->second.end()) { continue; }
280 const RI::Tensor<T>& m_kai_kbj = it_k2->second;
281 gather_matrix(m_2d, *m_kai_kbj.data, k1_step, k2_step, fac_base * this->kv.wk[kai]);
282 }
283 }
284#endif
286 ModuleBase::timer::end("MolecularLRI", "transform_k_2dlocal");
287}
288}
const int ndim
Definition molecular_lri.h:166
RI::LR< int, int, 3, T > LR_lri
Definition molecular_lri.h:38
const int nvirt
Definition molecular_lri.h:165
const int nocc
Definition molecular_lri.h:164
const int my_rank
Definition molecular_lri.h:172
std::vector< bool > is_local_k1
Definition molecular_lri.h:174
const Parallel_Blkcyclic pm_2d
Definition molecular_lri.h:163
const K_Vectors & kv
Definition molecular_lri.h:161
const int nproc
Definition molecular_lri.h:173
void transform_k_2dlocal(std::vector< T > &m_2d, const std::map< int, std::map< int, RI::Tensor< T > > > &m_lri, const Parallel_Blkcyclic &pm_2d, const double factor)
=============== inner function ====================
Definition molecular_lri_comm.hpp:53
const int nk
Definition molecular_lri.h:160
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
Definition rpa_lri_coeff_v1.h:11
Definition hamilt_bse.cpp:10
MPI_Datatype mpi_type_blockhead()
Definition molecular_lri_comm.hpp:28
std::ofstream ofs_running
Definition global_variable.cpp:38
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
Definition molecular_lri_comm.hpp:20
int gr0
Definition molecular_lri_comm.hpp:21
int gc0
Definition molecular_lri_comm.hpp:22
int nr
Definition molecular_lri_comm.hpp:23
int nc
Definition molecular_lri_comm.hpp:24
static MPI_Datatype value()
Definition lr_util.h:213