FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Split some large files (fs_nonlocal_tools.cpp, onsite_proj.cpp, to_wannier90_lcao.cpp, etc.) into smaller ones by mohanchen · Pull Request #7873 · deepmodeling/abacus-develop · GitHub

Split some large files (fs_nonlocal_tools.cpp, onsite_proj.cpp, to_wannier90_lcao.cpp, etc.) into smaller ones - #7873

Open
mohanchen wants to merge 36 commits into
deepmodeling:developfrom
mohanchen:2026-08-28-2
Open

Split some large files (fs_nonlocal_tools.cpp, onsite_proj.cpp, to_wannier90_lcao.cpp, etc.) into smaller ones#7873
mohanchen wants to merge 36 commits into
deepmodeling:developfrom
mohanchen:2026-08-28-2

Conversation

Copy link
Copy Markdown
Collaborator

Split some large files into smaller ones

abacus_fixer added 8 commits August 28, 2026 14:13
…te_proj_init.cpp

Step 1 of file split. Move init(), init_proj(), read_abacus_orb() from
onsite_proj.cpp to a new onsite_proj_init.cpp. Switch the main file
from whole-class 'template class' instantiation to method-level
explicit instantiation so the moved methods are emitted from
onsite_proj_init.cpp only. README comment block moved along with init.

CMakeLists.txt updated to compile the new file. Verified by
'make -j 30' in build_max_para_test (built abacus_max_para successfully).
…upations into onsite_proj_overlap.cpp

Step 2 of file split. Move the three overlap/occupation methods to
onsite_proj_overlap.cpp and emit them via method-level instantiation.
Slim the main onsite_proj.cpp includes (drop onsite_proj_print.h,
cell_tools.h, parallel_reduce.h, parameter.h, timer.h, <vector>) that
were only needed by the moved methods.

Verified by 'make -j 30' in build_max_para_test (built abacus_max_para).
…orce_stress.cpp

Step 3 of file split. Move the four cal_{force,stress}_onsite_{dftu,dspin}
wrappers to onsite_proj_force_stress.cpp. Main onsite_proj.cpp now
contains only the singleton accessor and destructor, with method-level
instantiation for those two. Drop the now-unused dftu_base.h and
<complex> includes from the main file.

Verified by 'make -j 30' in build_max_para_test (built abacus_max_para).
…tools_becp.cpp

Move the cal_becp method definition from onsite_proj_tools.cpp to a new
file onsite_proj_tools_becp.cpp with method-level explicit instantiation
for the CPU specialization. Add required includes (math_polyint.h,
math_ylmreal.h) for Nonlocal_maths dependencies. Update CMakeLists.txt.

Build verified: make -j 30 in build_max_para_test succeeds.
…roj_tools.cpp

Move cal_dbecp_s, cal_dbecp_f, save_vkb, revert_vkb, and transfer_gcar
method definitions from onsite_proj_tools.cpp to a new file
onsite_proj_tools_dbecp.cpp with method-level explicit instantiation
for the CPU specialization. Add required includes (math_polyint.h,
math_ylmreal.h, kernels/force_op.h for cal_vkb1_nl_op). Update CMakeLists.

Build verified: make -j 30 in build_max_para_test succeeds.
Move cal_force_dftu and cal_force_dspin to onsite_proj_tools_force.cpp,
and cal_stress_dftu and cal_stress_dspin to onsite_proj_tools_stress.cpp.
Both new files use method-level explicit instantiation for the CPU
specialization. Update CMakeLists.txt.

Build verified: make -j 30 in build_max_para_test succeeds.
Split the 827-line fs_nonlocal_tools.cpp into four functionally-cohesive
files:
- fs_nonlocal_tools.cpp (core): constructor, destructor, allocate_memory,
  delete_memory, whole-class template instantiation
- fs_nonlocal_tools_vkb.cpp: cal_vkb, cal_becp, reduce_pool_becp
- fs_nonlocal_tools_stress.cpp: cal_vkb_deri_s, cal_dbecp_s, cal_stress
- fs_nonlocal_tools_force.cpp: cal_vkb_deri_f, cal_dbecp_f, save_vkb,
  revert_vkb, transfer_gcar, cal_force

Each new file uses method-level explicit instantiation for the CPU
specialization. Update CMakeLists.txt.

Build verified: make -j 30 in build_max_para_test succeeds.
Tests verified: MODULE_PW_pw_test, MODULE_PW_pwdft_soc, onsite_op_test,
dftu_pw/core/operator, deltaspin_pw/core all pass.
Add a bullet to the Required Baseline section: for multi-step refactors
such as splitting a large .cpp into several files, build and commit after
each step rather than batching all changes before verification.
mohanchen added Refactor Refactor ABACUS codes The Absolute Zero Reduce the "entropy" of the code to 0 labels Aug 28, 2026
mohanchen requested a review from Critsium-xy August 28, 2026 08:13
mohanchen changed the title Split some large files into smaller ones Split some large files (fs_nonlocal_tools.cpp and onsite_proj.cpp) into smaller ones Aug 28, 2026
Split the 1202-line to_wannier90_lcao.cpp into four functionally-cohesive
files (non-template class, no explicit instantiation needed):
- to_wannier90_lcao.cpp (core): constructor, destructor, calculate
- to_wannier90_lcao_io.cpp: cal_Mmn, cal_Amn, out_unk (file output)
- to_wannier90_lcao_setup.cpp: initialize_orb_table, set_R_coor,
  count_delta_k, unkdotkb, produce_basis_orb, produce_trial_in_lcao,
  construct_overlap_table_project
- to_wannier90_lcao_overlap.cpp: cal_orbA_overlap_R, unkdotA

All files wrapped in #ifdef __LCAO. Update CMakeLists.txt.

Build verified: make -j 30 in build_max_para_test succeeds.
mohanchen changed the title Split some large files (fs_nonlocal_tools.cpp and onsite_proj.cpp) into smaller ones Split some large files (fs_nonlocal_tools.cpp, onsite_proj.cpp, to_wannier90_lcao.cpp) into smaller ones Aug 28, 2026
abacus_fixer added 16 commits August 28, 2026 16:33
Split the 1051-line xc_grad.cpp into three files:
- xc_grad.cpp (gradcorr): the monolithic gradient correction method
  (single 798-line method, cannot be mechanically split further)
- xc_grad_wfc.cpp: grad_wfc template method + explicit instantiation
- xc_grad_utils.cpp: grad_rho, grad_dot, laplacian_rho, noncolin_rho

Update CMakeLists.txt for xc_ library and test targets that compile
xc_grad.cpp directly (test_xc3/5/7, surchem cal_vcav/cal_vel).

Build verified: make -j 30 in build_max_para_test succeeds, including
all test executables.
Extract the rho preparation phase (FFT, array allocation, gradient and
laplacian computation) from the 798-line gradcorr method into a separate
gradcorr_prepare_rho function (26 parameters, all by reference).

gradcorr shrinks from 826 to 657 lines. The orchestrator now declares
the shared arrays, calls prepare_rho, then proceeds to the XC kernel
and potential assembly (to be extracted in steps 2-3).

Update xc_functional.h, CMakeLists.txt, and test CMakeLists that
compile xc_grad.cpp directly.

Build verified: make -j 30 in build_max_para_test succeeds.
Extract the main XC computation loop (#pragma omp parallel + #ifdef __LIBXC
nspin branches + OpenMP reduction) from gradcorr into a separate
gradcorr_xc_kernel function (27 parameters, all by reference/pointer).

gradcorr shrinks from 657 to 355 lines. The orchestrator now:
1. Guards + setup
2. Calls prepare_rho (step 1)
3. Calls xc_kernel (this step)
4. [Laplacian stress + assembly — to be extracted in step 3]
5. Cleanup

Update xc_functional.h, CMakeLists.txt, and test CMakeLists.

Build verified: make -j 30 in build_max_para_test succeeds.
Extract the potential assembly phase (Laplacian stress contribution,
vxc from vtxcgc + dh + laplacian, noncolinear rotation back) from
gradcorr into a separate gradcorr_assemble_vxc function (24 parameters).

gradcorr is now a 183-line orchestrator that:
1. Guards + early returns
2. Setup (nspin0, fac, need_laplacian, stress_gga init)
3. Variable declarations
4. Calls prepare_rho (step 1)
5. Calls xc_kernel (step 2)
6. Calls assemble_vxc (this step)
7. Cleanup (delete arrays)

The original 798-line monolith is now split across:
- xc_grad.cpp (183 lines, orchestrator)
- xc_grad_prepare.cpp (222 lines, rho preparation)
- xc_grad_kernel.cpp (351 lines, XC computation loop)
- xc_grad_assemble.cpp (222 lines, potential assembly)

Build verified: make -j 30 in build_max_para_test succeeds.
- Replace 9 raw pointer allocations (rhotmp1/2, rhogsum1/2, gdr1/2, h1/2, neg) with std::vector, removing all manual delete[] in the orchestrator
- Flatten 2D arrays vsave/vgg (double**) into 1D vectors (vsave[is*nrxx+ir] index pattern), eliminating nested heap allocations
- prepare_rho now resizes vectors instead of new; kernel receives raw pointers via .data() (signature unchanged)
- assemble_vxc signature updated: double** vsave/vgg -> double* (flattened 1D)
- Build verified: make -j 30, 5 surchem tests passed
…iles

After splitting fs_nonlocal_tools.cpp and onsite_proj_tools.cpp into
multiple .cpp files with method-level explicit instantiation, only CPU
instantiations were added to the split files. The whole-class explicit
instantiation in the main file does not instantiate methods defined in
other translation units, causing undefined reference errors for
DEVICE_GPU specializations during GPU linking.

Add #if ((defined __CUDA) || (defined __ROCM)) guarded GPU method-level
explicit instantiations mirroring the existing CPU ones in:
- fs_nonlocal_tools_vkb.cpp (cal_vkb/cal_becp/reduce_pool_becp)
- fs_nonlocal_tools_stress.cpp (cal_vkb_deri_s/cal_dbecp_s/cal_stress)
- fs_nonlocal_tools_force.cpp (cal_vkb_deri_f/cal_dbecp_f/save_vkb/
  revert_vkb/transfer_gcar/cal_force)
- onsite_proj_tools_dbecp.cpp (cal_dbecp_s/cal_dbecp_f/save_vkb/
  revert_vkb/transfer_gcar)
- onsite_proj_tools_force.cpp (cal_force_dftu/cal_force_dspin)
- onsite_proj_tools_stress.cpp (cal_stress_dftu/cal_stress_dspin)

Verified with local GPU build (USE_CUDA=ON): abacus_pw_gpu links cleanly.
- xc_grad_assemble.cpp: replace 'double* dh = new double[]' with
  'std::vector<double> dh', pass .data() to grad_dot
- xc_grad_utils.cpp (grad_rho): replace gdrtmp raw pointer with
  std::vector<std::complex<double>>, use .data() for recip2real
- xc_grad_utils.cpp (grad_dot): replace aux and gaux raw pointers
  with std::vector<std::complex<double>>, use .data() for FFT calls
- xc_functional.cpp: minor comment typo fix (GGA, plane-wave)

All replacements are zero-overhead (vector uses contiguous memory
identical to new[]), exception-safe (auto-dealloc on scope exit), and
consistent with the previous RAII migration in xc_grad.cpp.

Verified: make -j 30 passes; ctest -R 'GRADCORR|VXC|PBE|SCAN|LAPL|HSE' 7/7 passed.
…al/get_trial/integral to to_wannier90_pw_setup.cpp
…o to_wannier90_pw_overlap.cpp, reduce original to 77 lines (ctor/dtor/calculate/set_tpiba)
abacus_fixer added 11 commits August 28, 2026 18:38
…CMake lists for deepks/gint/operator/dftu/lcao/dm/hcontainer tests to include new atom_pair split files
…lue_union/shape_synchron to hcontainer_math.cpp
…/Sq/V+cal_flq+cal_ylm+cal_gpow to numerical_basis_overlap.cpp
…ing) to force_pw_driver.cpp; each TU instantiates Forces template class
….cpp; each TU instantiates Forces template class
… assign_dmk_ptr + ctor/dtor to force_stress_lcao_driver.cpp
…ress_lcao_pw.cpp; each TU instantiates Force_Stress_LCAO template class
…lex<double>> to force_stress_lcao_integral.cpp
…metry to force_stress_lcao_utility.cpp; base .cpp now is a thin header-forward file (31 lines includes only). Total across 5 files ~1098 lines (original 1080).
mohanchen changed the title Split some large files (fs_nonlocal_tools.cpp, onsite_proj.cpp, to_wannier90_lcao.cpp) into smaller ones Split some large files (fs_nonlocal_tools.cpp, onsite_proj.cpp, to_wannier90_lcao.cpp, etc.) into smaller ones Aug 28, 2026

Critsium-xy left a comment
edited
Loading

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I pulled this PR locally and mechanically checked each of the 12 split groups: for every group I stripped comments, normalized whitespace, dropped #include lines, and sorted the content of the original file and of the union of the resulting files, then diffed them to see whether any code was actually added or removed. The splits are faithful overall — 11 of the 12 groups are token-for-token equivalent to the original, with the only differences being duplicated includes / #ifdef guards and template instantiation lines. Three things worth raising.

1. Duplicated whole-class explicit template instantiations

After the split, the same specialization is explicitly instantiated in multiple TUs:

  • atom_pair.cpp / atom_pair_kernels.cpp / atom_pair_output.cpp: each file carries 3 lines of template class AtomPair<float/double/std::complex<double>>;
  • hcontainer.cpp / hcontainer_insert.cpp / hcontainer_math.cpp: likewise 3 lines each of template class HContainer<...>;
  • force_pw.cpp / force_pw_driver.cpp / force_pw_ew.cpp: 2 lines each of template class Forces<double, DEVICE_CPU/DEVICE_GPU>;
  • force_stress_lcao_driver.cpp / _integral.cpp / _pw.cpp / _utility.cpp: 2 lines each of template class Force_Stress_LCAO<double/std::complex<double>>;

Per [temp.explicit]/13, a given specialization may have at most one explicit instantiation definition in a program; violating this is IFNDR. GCC/Clang link it fine via COMDAT weak symbols, which is why CI is green — but the practical effect is that every TU re-instantiates all members visible from the headers, inflating object size and link time. That works against the compile-time speedup the split is meant to deliver.

onsite_proj.cpp and onsite_proj_tools.cpp in this same PR use per-member instantiation (template void X<...>::f(...);), which is the correct form. I checked all 12 out-of-line members of OnsiteProjector (including get_instance and the destructor) and none are missing. Suggest switching the hcontainer / force groups to per-member instantiation as well.

2. force_stress_lcao lost 118 lines of explanatory comments

While moving code from force_stress_lcao.cpp into force_stress_lcao_driver.cpp and friends, 118 comment lines were not carried over. These are not commented-out dead code — they carry real information, e.g.:

// The legacy dft_plus_u==2 force/stress path is currently broken.
// With symmetry switched on, the forces assembled above are built from IBZ-reduced ...
// Force symmetrization is linear, so it commutes with the removal of a ...
// Force contribution from DFT+U, Quxin add on 20201029
// Background: Plus_U::force_stress relies on ForceStressArrays

I confirmed with git grep that these no longer exist anywhere on the PR branch. The first one is the most costly to lose — it flags a known defect. Suggest restoring this batch of comments.

Comment loss in the other groups is minor (11 lines in force_pw, 15 in numerical_basis) and is almost entirely commented-out dead code, which is fine to drop.

3. xc_grad is not a pure split — worth calling out in the PR description

Beyond splitting files, the xc_grad group contains a substantive refactor: raw new[]/delete[] pointers were converted to std::vector (removing 20 delete[] calls), and double** vgg / double** vsave became flat arrays, changing indexing from vgg[is][ir] to vgg[is*nrxx+ir].

I checked the prepare / kernel / assemble sections one by one and the semantics are equivalent: vgg is allocated as nspin0*nrxx and vsave as nspin*nrxx, matching the indexing; the 310-line kernel section is a verbatim move (diff -w shows only the function header/footer); and passing vtxcgc by value, accumulating into it, then doing vtxc += vtxcgc behaves the same as the original local variable. The change itself is a good direction.

But the PR description currently only says "Split some large files into smaller ones", so a reader will assume this is a purely mechanical move and skip the xc_grad diff. Suggest stating explicitly in the description that this group also rewrites memory management and indexing, or splitting it into its own PR to keep future bisects clean.

Separately, the three new public static functions added to xc_functional.h take 26 / 27 / 24 parameters, all bool, double and raw pointers — getting the argument order wrong would not be caught by the compiler. That header is also widely included, so touching it forces a full rebuild, which offsets the benefit of the split. Suggest packing the parameters into a struct and moving these declarations to an internal header (e.g. xc_grad_internal.h) rather than the public section of xc_functional.h.

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

Labels

Refactor Refactor ABACUS codes The Absolute Zero Reduce the "entropy" of the code to 0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL