59 const int npair = this->
nocc * this->
nvirt;
60 const double fac_base = factor * 2.0;
61 const int nb =
pm_2d.nb();
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);
70 std::vector<int> sbdispls(this->
nproc, 0), rbdispls(this->
nproc, 0);
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)
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);
85 for (
int kai = k1_start; kai < k1_end; ++kai)
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)
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; )
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; )
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))
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);
116 for (
int p = 0; p < this->
nproc; ++p)
118 if (send_buffer_counts_c[p] > std::numeric_limits<int>::max())
120 throw std::overflow_error(
"in transform_k_2dlocal: overflow converting to int!");
122 send_buffer_counts[p] =
static_cast<int>(send_buffer_counts_c[p]);
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());
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)
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];
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); }
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)
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];
161 for (
const int kbj : this->
LR_lri.k2_indices)
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; }
167 for (
int j = 0; j < npair; )
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; )
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);
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)
183 for(
int ii = i; ii < i_next; ++ii)
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;
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)
201 for (
int ii = i; ii < i_next; ++ii)
203 send_buffers[cursor_buffer[owner]++] = (*m_kai_kbj.data)[ii + jj * npair] * fac;
213 for (
int p = 0; p < this->nproc; ++p)
215 assert(cursor_head[p] == shdispls[p] + send_head_counts[p]);
216 assert(cursor_buffer[p] == sbdispls[p] + send_buffer_counts[p]);
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,
227 for (
int iblock = 0; iblock < recv_head_total; ++iblock)
229 const BlockHead& rh = recv_heads[iblock];
230 const int nr = rh.
nr;
231 const int nc = rh.
nc;
234 const int lld =
pm_2d.nrloc();
235 for (
int j = 0; j < nc; ++j)
237 for (
int i = 0; i < nr; ++i)
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];
244 buf_cursor += nr * nc;
246 assert(buf_cursor == recv_buffer_total);
250 auto gather_matrix = [&](std::vector<T>& target,
251 const std::valarray<T>& value,
254 double factor) ->
void
256 for (
int j = 0; j < npair; ++j)
258 for (
int i = 0; i < npair; ++i)
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;
268 #pragma omp parallel for schedule(static) collapse(2)
270 for (
const int kai : this->k1_indices)
272 const int k1_step = kai * npair;
273 for (
const int kbj : this->k2_indices)
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; }
281 gather_matrix(m_2d, *m_kai_kbj.data, k1_step, k2_step, fac_base * this->kv.wk[kai]);