From a5da988f725645a29130c85b4ee82f07579833b5 Mon Sep 17 00:00:00 2001 From: MrLi000001 <1417558386@qq.com> Date: Sat, 10 Oct 2026 11:32:47 +0800 Subject: [PATCH 1/2] refactor(parallel_grid): pass KPAR into z_distribution as a parameter z_distribution() read GlobalV::KPAR directly, so its behaviour depended on global state. Take kpar as an explicit parameter, with Parallel_Grid::init() supplying GlobalV::KPAR, so the function body no longer reads any global variable. The added assert(kpar > 0) replaces the protection the caller assert(GlobalV::KPAR > 0) used to give startp[0]. --- source/source_base/parallel_grid.cpp | 9 +++++---- source/source_base/parallel_grid.h | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/source/source_base/parallel_grid.cpp b/source/source_base/parallel_grid.cpp index 1c258ca266c..6d23b2f6981 100644 --- a/source/source_base/parallel_grid.cpp +++ b/source/source_base/parallel_grid.cpp @@ -94,18 +94,19 @@ void Parallel_Grid::init(const int& ncx_in, this->whichpro_loc[ip].assign(this->ncz, 0); } - this->z_distribution(); + this->z_distribution(GlobalV::KPAR); return; } -void Parallel_Grid::z_distribution() +void Parallel_Grid::z_distribution(const int kpar) { assert(!this->numz.empty()); + assert(kpar > 0); - std::vector startp(GlobalV::KPAR); + std::vector startp(kpar); startp[0] = 0; - for (int ip = 0; ip < GlobalV::KPAR; ip++) + for (int ip = 0; ip < kpar; ip++) { const int nproc = nproc_in_pool[ip]; diff --git a/source/source_base/parallel_grid.h b/source/source_base/parallel_grid.h index 8cc7592384c..929ff94854d 100644 --- a/source/source_base/parallel_grid.h +++ b/source/source_base/parallel_grid.h @@ -45,7 +45,7 @@ class Parallel_Grid private: - void z_distribution(void); + void z_distribution(const int kpar); #ifdef __MPI void zpiece_distribute(double* zpiece, const int& iz, double* rho, const bool is_sdft) const; From 7918076f257ace18c02f7a37b9bd9165013fbbc0 Mon Sep 17 00:00:00 2001 From: MrLi000001 <77618365+MrLi000001@users.noreply.github.com> Date: Sat, 10 Oct 2026 16:00:57 +0800 Subject: [PATCH 2/2] Refactor z_distribution function placement --- source/source_base/parallel_grid.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/source/source_base/parallel_grid.cpp b/source/source_base/parallel_grid.cpp index cc0e162f978..86c7e90aced 100644 --- a/source/source_base/parallel_grid.cpp +++ b/source/source_base/parallel_grid.cpp @@ -96,7 +96,6 @@ void Parallel_Grid::init_parallel(const int nprocgroup) this->z_distribution(GlobalV::KPAR); - return; } #endif void Parallel_Grid::init(const int& ncx_in, @@ -109,16 +108,15 @@ void Parallel_Grid::init(const int& ncx_in, const int nprocgroup) { this->init_serial(ncx_in, ncy_in, ncz_in, nczp_in, nrxx_in, nbz_in, bz_in); - -void Parallel_Grid::z_distribution(const int kpar) #ifdef __MPI this->init_parallel(nprocgroup); #endif } +void Parallel_Grid::z_distribution(const int kpar) { assert(!this->numz.empty()); assert(kpar > 0); - + std::vector startp(kpar); startp[0] = 0; for (int ip = 0; ip < kpar; ip++)