Skip to content

Commit 39564a1

Browse files
Critsium-xyclaude
andauthored
Refactor hsolver: drop dead global includes and GlobalV::DSIZE (#7865)
Step 1 — remove includes of global_variable.h / parameter.h from files that no longer reference any symbol from them: diago_cusolver.cpp, diago_pexsi.cpp, diago_elpa_native.cpp and module_pexsi/simple_pexsi.cpp. Step 2 — remove GlobalV::DSIZE from the ScaLAPACK/LAPACK solvers: * diago_scalapack.cpp: p?sygvx needs iclustr(2*NPROW*NPCOL) and gap(NPROW*NPCOL) of the grid the descriptor lives on, which is not necessarily the size of the diag world. Query the grid via Cblacs_gridinfo() on desc[1] instead. This also fixes a latent out-of-bounds read in post_processing() whenever the BLACS grid is smaller than DSIZE; the loop now derives its bound from iclustr. * diago_lapack.cpp: the serial LAPACK ?sygvx/?hegvx take no iclustr/gap at all, so gap is dropped and iclustr becomes a single-process placeholder. No functional change for the existing single-grid runs. Verified with MODULE_HSOLVER_LCAO (serial and 4 MPI ranks) and MODULE_HSOLVER_diago_hs_parallel. Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
1 parent 0aa8d8b commit 39564a1

6 files changed

Lines changed: 31 additions & 15 deletions

File tree

source/source_hsolver/diago_cusolver.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include "source_base/module_external/blas_connector.h"
44
#include "source_base/module_external/blacs_connector.h"
5-
#include "source_base/global_variable.h"
65
#include "source_base/module_external/scalapack_connector.h"
76
#include "source_base/tool_title.h"
87
#include "source_base/timer.h"

source/source_hsolver/diago_elpa_native.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "source_base/global_function.h"
44
#include "source_base/module_external/blas_connector.h"
55
#include "source_base/module_external/blacs_connector.h"
6-
#include "source_base/global_variable.h"
76
#include "source_base/timer.h"
87
#include "source_base/tool_quit.h"
98
#include "source_hsolver/module_genelpa/elpa_new.h"

source/source_hsolver/diago_lapack.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,10 @@ std::pair<int, std::vector<int>> DiagoLapack<T>::dsygvx_once(const int ncol,
120120
std::vector<double> work(3, 0);
121121
std::vector<int> iwork(1, 0);
122122
std::vector<int> ifail(this->nlocal, 0);
123-
std::vector<int> iclustr(2 * GlobalV::DSIZE);
124-
std::vector<double> gap(GlobalV::DSIZE);
123+
// iclustr/gap are ScaLAPACK-only outputs; the serial LAPACK call below never
124+
// writes them, so gap is dropped and iclustr is kept merely as a zero-filled
125+
// single-process placeholder for the info & 2 branch of post_processing().
126+
std::vector<int> iclustr(2, 0);
125127

126128
// LAPACK dsygvx signature:
127129
// (ITYPE, JOBZ, RANGE, UPLO, N, A, LDA, B, LDB, VL, VU, IL, IU,
@@ -231,8 +233,10 @@ std::pair<int, std::vector<int>> DiagoLapack<T>::zhegvx_once(const int ncol,
231233
std::vector<double> rwork(3, 0);
232234
std::vector<int> iwork(1, 0);
233235
std::vector<int> ifail(this->nlocal, 0);
234-
std::vector<int> iclustr(2 * GlobalV::DSIZE);
235-
std::vector<double> gap(GlobalV::DSIZE);
236+
// iclustr/gap are ScaLAPACK-only outputs; the serial LAPACK call below never
237+
// writes them, so gap is dropped and iclustr is kept merely as a zero-filled
238+
// single-process placeholder for the info & 2 branch of post_processing().
239+
std::vector<int> iclustr(2, 0);
236240

237241
// LAPACK zhegvx signature:
238242
// (ITYPE, JOBZ, RANGE, UPLO, N, A, LDA, B, LDB, VL, VU, IL, IU,
@@ -393,7 +397,8 @@ void DiagoLapack<T>::post_processing(const int info, const std::vector<int>& vec
393397
else if (info / 2 % 2)
394398
{
395399
int degeneracy_need = 0;
396-
for (int irank = 0; irank < GlobalV::DSIZE; ++irank) {
400+
// `vec` is iclustr, holding one [begin, end] pair per process
401+
for (std::size_t irank = 0; 2 * irank + 1 < vec.size(); ++irank) {
397402
degeneracy_need = std::max(degeneracy_need, vec[2 * irank + 1] - vec[2 * irank]);
398403
}
399404
const std::string str_need = "degeneracy_need = " + ModuleBase::GlobalFunc::TO_STRING(degeneracy_need) + ".\n";

source/source_hsolver/diago_pexsi.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#ifdef __PEXSI
55
#include "diago_pexsi.h"
66
#include "source_base/tool_title.h"
7-
#include "source_base/global_variable.h"
87
#include "source_base/tool_quit.h"
98
#include "source_basis/module_ao/parallel_orbitals.h"
109
#include "module_pexsi/pexsi_solver.h"

source/source_hsolver/diago_scalapack.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "source_base/global_function.h"
1414
#include "source_base/global_variable.h"
15+
#include "source_base/module_external/blacs_connector.h"
1516
#include "source_base/module_external/scalapack_connector.h"
1617
#include "source_hamilt/matrixblock.h"
1718

@@ -20,6 +21,18 @@ typedef hamilt::MatrixBlock<std::complex<double>> matcd;
2021

2122
namespace hsolver
2223
{
24+
namespace
25+
{
26+
/// Number of processes in the BLACS process grid that `desc` lives on.
27+
/// p?sygvx requires iclustr(2*NPROW*NPCOL) and gap(NPROW*NPCOL).
28+
int blacs_grid_size(const int* const desc)
29+
{
30+
int nprow = 0, npcol = 0, myprow = 0, mypcol = 0;
31+
Cblacs_gridinfo(desc[1], &nprow, &npcol, &myprow, &mypcol);
32+
return nprow * npcol;
33+
}
34+
} // namespace
35+
2336
template<>
2437
void DiagoScalapack<double>::diag(hamilt::Hamilt<double>* phm_in, psi::Psi<double>& psi, Real* eigenvalue_in)
2538
{
@@ -98,8 +111,9 @@ namespace hsolver
98111
std::vector<double> work(3, 0);
99112
std::vector<int> iwork(1, 0);
100113
std::vector<int> ifail(this->nlocal, 0);
101-
std::vector<int> iclustr(2 * GlobalV::DSIZE);
102-
std::vector<double> gap(GlobalV::DSIZE);
114+
const int ngrid = blacs_grid_size(desc);
115+
std::vector<int> iclustr(2 * ngrid);
116+
std::vector<double> gap(ngrid);
103117

104118
pdsygvx_(&itype,
105119
&jobz,
@@ -227,8 +241,9 @@ namespace hsolver
227241
std::vector<double> rwork(3, 0);
228242
std::vector<int> iwork(1, 0);
229243
std::vector<int> ifail(this->nlocal, 0);
230-
std::vector<int> iclustr(2 * GlobalV::DSIZE);
231-
std::vector<double> gap(GlobalV::DSIZE);
244+
const int ngrid = blacs_grid_size(desc);
245+
std::vector<int> iclustr(2 * ngrid);
246+
std::vector<double> gap(ngrid);
232247

233248
pzhegvx_(&itype,
234249
&jobz,
@@ -408,7 +423,8 @@ namespace hsolver
408423
else if (info / 2 % 2)
409424
{
410425
int degeneracy_need = 0;
411-
for (int irank = 0; irank < GlobalV::DSIZE; ++irank) {
426+
// `vec` is iclustr, sized 2*NPROW*NPCOL by the caller
427+
for (std::size_t irank = 0; 2 * irank + 1 < vec.size(); ++irank) {
412428
degeneracy_need = std::max(degeneracy_need, vec[2 * irank + 1] - vec[2 * irank]);
413429
}
414430
const std::string str_need = "degeneracy_need = " + ModuleBase::GlobalFunc::TO_STRING(degeneracy_need) + ".\n";

source/source_hsolver/module_pexsi/simple_pexsi.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// use PEXSI to solve a Kohn-Sham equation
22
// the H and S matrices are given by 2D block cyclic distribution
3-
#include "source_io/module_parameter/parameter.h"
43
// the Density Matrix and Energy Density Matrix calculated by PEXSI are transformed to 2D block cyclic distribution
54
// #include "mpi.h"
65
#ifdef __PEXSI
@@ -20,7 +19,6 @@
2019
#include "source_base/module_external/lapack_connector.h"
2120
#include "source_base/timer.h"
2221
#include "source_base/tool_quit.h"
23-
#include "source_base/global_variable.h"
2422
#include "source_hsolver/diago_pexsi.h"
2523

2624
namespace pexsi

0 commit comments

Comments
 (0)