Refactor hsolver: move MatrixBlock to source_base, drop dead hamilt include - #7920
Merged
mohanchen merged 6 commits intoSep 9, 2026
Merged
Conversation
MatrixBlock only records a matrix memory layout -- data pointer, local row/column counts and the BLACS descriptor -- so it carries no physics and does not belong to the Hamiltonian module. Move the definition to source_base/matrix_block.h as ModuleBase::MatrixBlock and leave a `using` alias in source_hamilt/matrixblock.h, so the ~30 existing hamilt::MatrixBlock spellings outside source_hsolver keep compiling unchanged. With that, three source_hsolver files stop reaching into source_hamilt altogether: - diago_bpcg.h included source_hamilt/hamilt.h without using a single symbol from it; DiagoBPCG::diag() has taken HPsiFunc/SPsiFunc closures for a while. - diago_cusolver.h only needed the matrix descriptor, not the whole Hamiltonian interface. psi.h is now included explicitly, it used to arrive through hamilt.h. - diago_scalapack.cpp likewise only needed the descriptor. The source_hsolver files that still need hamilt::Hamilt now spell the type ModuleBase::MatrixBlock and include source_base/matrix_block.h directly, so dropping their source_hamilt/hamilt.h include later is a pure signature change with no name churn. source_hsolver -> source_hamilt includes: 16 files / 17 lines -> 13 files / 13 lines. No behavior change. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Follow-up review fixes on top of the MatrixBlock move: - parallel_k2d.h swapped one include for another, but the header itself never names MatrixBlock -- only parallel_k2d.cpp does. Drop it from the header and include source_base/matrix_block.h in the .cpp instead, so the header's dependency set actually shrinks rather than shifts. - diago_cusolver.h includes source_basis/module_ao/parallel_orbitals.h, but Parallel_Orbitals appears in neither the header nor the .cpp. Same class of dead include as the source_hamilt/hamilt.h one already removed from diago_bpcg.h. - source_hamilt/matrixblock.h is now a one-line alias kept only for the historical spelling. Say so with a TODO, so it has a stated end state instead of quietly becoming permanent. No behavior change; no type or signature is touched. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NxDLsUHq81NAbsh2rpCwKB
mohanchen
approved these changes
Sep 9, 2026
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reminder
AGENTS.mdanddocs/developers_guide/agent_governance.md.source/changes.Linked Issue
No issue. This is the first, purely mechanical step of an incremental effort to
turn
source_hsolverinto a self-contained numerical module. It changes nobehavior and is small enough to review on its own, so it seemed better to send
it standalone than bundled with the interface changes that have to follow.
Unit Tests and/or Case Tests for my changes
Commands run (Linux, gcc, cmake 3.31 + ninja, 15 cores):
Full-feature build from scratch:
diago_cusolver.{h,cpp}are guarded byUSE_CUDAand are the files this PRchanges most, so they were built explicitly:
Whole unit-test suite, serially, on this branch and on the base commit
fb9ce1da4built with the same flags:Governance check:
Result summary
3301/3301targets, 0 errorsdiag_cusolver(CUDA)fb9ce1da4The two failing sets are identical test-for-test —
diffof the sortedThe following tests FAILEDlists is empty. They are pre-existing failures ofthe machine I have access to (mostly
mpirun-launched*_para/*_parallelwrappers, plus
MODULE_BASE_math_sphbes,MODULE_BASE_cubic_spline,MODULE_PSI_init_test,MODULE_HAMILT_vdwTest,MODULE_RELAX_relax_new_relax) and are unrelated to this change.Checks not run, with reason
-DENABLE_CUSOLVERMP=ON(diago_cusolvermp.cpp): cuSOLVERMp is not installedon that machine, so CMake refuses to configure (
cuSOLVERMp not found. Set CUSOLVERMP_PATH or NVHPC_ROOT_DIR.). The change in that file is onehamilt::MatrixBlock→ModuleBase::MatrixBlockrename plus the matchinginclude; after the alias both names denote the same type.
tests/: this PR changes only which header adeclaration comes from and how a type is spelled — no code path, no
arithmetic — so numerical results cannot move. CI's integration suites cover
it.
What's changed?
source_hsolveris meant to be the numerical linear-algebra layer, but 16 of itsfiles reach into
source_hamilt. Two of those reasons turn out not to be real:diago_bpcg.hincludedsource_hamilt/hamilt.hand used nothing fromit.
DiagoBPCG::diag()has takenHPsiFunc/SPsiFuncclosures for a while;the include is left over from before that.
hamilt::MatrixBlockis a four-field aggregate that records where a matrixlives —
T* p, localrow/col, and the BLACSdesc. It carries nophysics, but because it lived in
source_hamilt, a solver that only wanted amatrix descriptor had to include the whole Hamiltonian interface to get it.
So this PR:
source/source_base/matrix_block.hasModuleBase::MatrixBlock, field-for-field unchanged (still an aggregate,still no default member initializers, so the existing
MatrixBlock<T>{p, row, col, desc}brace initializations keep working);using ModuleBase::MatrixBlock;insource_hamilt/matrixblock.h, soevery
hamilt::MatrixBlockspelling insource_estate,source_lcao,source_io,source_esolverand the hsolver tests compiles untouched. Nofile outside
source_hsolverand those two headers is modified;source_hsolverto the canonicalModuleBase::MatrixBlock, withsource_base/matrix_block.hincluded directly.Net effect:
source_hsolverfiles includingsource_hamilt#include "source_hamilt/..."lines therehamilt::MatrixBlockinsource_hsolver(non-test)hamilt::MatrixBlockelsewhere in the treediago_bpcg.h,diago_cusolver.handdiago_scalapack.cppno longer referencesource_hamiltat all. Each remaining include ishamilt.hpulled in forhamilt::Hamilt(plus onemodule_xc/general_exx_info.hinhsolver_lcaopw.cpp) — exactly the set a follow-up has to address. Becausethose files now already spell the type canonically and include the base header
directly, dropping
hamilt.hfrom them later becomes a pure signature changewith no name churn.
For context on the direction:
DiagoCusolver::diagandDiagoScalapack::diag_poolalready takeMatrixBlockrather than ahamilt::Hamilt*, andDiagoCG/DiagoDavid/DiagoDavSubspace/DiagoBPCGalready take
std::functionclosures instead of the Hamiltonian. The intent isto make the rest of
source_hsolverconsistent with the parts that are alreadydecoupled, one reviewable step at a time.
Governance Notes
Input_Item, no user-visible behavior, nooutput format is touched, so
docs/parameters.yamlanddocs/advanced/input_files/input-main.mdare unaffected.hamilt::MatrixBlock<T>andModuleBase::MatrixBlock<T>name the sametype, so every existing signature still matches — including
hamilt::Hamilt<T>::matrix(MatrixBlock<T>&, MatrixBlock<T>&)and the mockoverrides in
source_hsolver/test, which are deliberately left as they are.There is no ABI or numerical change; object code differs only by the removed
hamilt.hinclude in three translation units.agent_governance_check.pywarnings areexpected and intentional:
is
source_base/matrix_block.h, strictly narrower than thesource_hamilt/hamilt.hit replaces or sits beside. Adding it explicitlyeven where
hamilt.hstill supplies it transitively is deliberate — it iswhat makes the later removal of
hamilt.hsafe.type-alias refactor with no behavior change, covered by the existing
MODULE_HSOLVER_*suite, which is unchanged and shown above to have thesame pass/fail set as the base commit. A new test for a type alias would not
test anything.
🤖 Generated with Claude Code