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);
118 if (pmat.
size() > std::numeric_limits<int>::max())
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;
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());
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]));
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);
144 int local_flag = is_herm ? 1 : 0;
146 MPI_Allreduce(&local_flag, &global_flag, 1, MPI_INT, MPI_LAND, pmat.
comm());
147 is_herm = (global_flag != 0);
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]);
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);
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;
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);
183 if (pmat.
size() > std::numeric_limits<int>::max())
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;
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());
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]));
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;
208 MPI_Allreduce(&local_flag, &global_flag, 1, MPI_INT, MPI_LAND, pmat.
comm());
209 is_sym = (global_flag != 0);
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];
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);
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;
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,
370 const int row_offset,
const int col_offset,
const bool openshell,
371 const int my_rank,
const int nproc)
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)
383 assert(pX[is].nrglb() == nvirt[is]);
384 assert(pX[is].ncglb() == nocc[is]);
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);
393 std::vector<int> sbdispls(nproc, 0), rbdispls(nproc, 0);
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;
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)
408 int proc_col = (gb / nbpA) % pA.
ncproc();
409 for (
int pr = 0; pr < pA.
nrproc(); ++pr)
411 int ownerA = proc_col + pr * pA.
ncproc();
413 gblist[ownerA].push_back(gb);
416 const int lnband = lnbands[my_rank];
419 throw std::runtime_error(
"rank" + std::to_string(my_rank) +
"lnband = 0, please check the pA and pX distribution!");
421 const int lb_start = pA.
icloc(gblist[my_rank].front());
422 const int lb_end = lb_start + lnband;
424 for (
int is = 0; is < nspin_X; ++is)
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)
433 std::fill(send_head_counts.begin(), send_head_counts.end(), 0);
434 std::fill(send_buffer_counts.begin(), send_buffer_counts.end(), 0);
437 int irow_end = std::min(irow_start + nrow, grow_end);
438 for (
int irow = irow_start; irow < irow_end; ++irow)
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)
449 ++send_head_counts[ownerX];
452 for (
int p = 0; p < nproc; ++p)
454 std::size_t nbuffer = size_t(lnband) * send_head_counts[p];
455 if (nbuffer > std::numeric_limits<int>::max())
457 throw std::overflow_error(
"in pA2pX: overflow converting to int!");
459 send_buffer_counts[p] =
static_cast<int>(nbuffer);
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());
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)
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];
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); }
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;
493 for (
int irow = irow_start; irow < irow_end; ++irow)
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)
504 for (std::size_t lb = lb_start; lb < lb_end; ++lb)
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()];
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)
521 send_buffers[cursor_buffer[ownerX]++] = X_pA[
static_cast<std::size_t
>(lr) + lb * pA.
nrloc()];
525 for (
int p = 0; p < nproc; ++p)
527 assert(cursor_head[p] == shdispls[p] + send_head_counts[p]);
528 assert(cursor_buffer[p] == sbdispls[p] + send_buffer_counts[p]);
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,
539 for (
int src = 0; src < nproc; ++src)
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)
548 const BlockHead& rh = recv_heads[iblock];
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)
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];
560 buf_cursor += lnbands[src];
562 assert(buf_cursor == rbdispls[src] + recv_buffer_counts[src]);