From 04b3f958551fbcbdd6c82c46fce6811abe61ed04 Mon Sep 17 00:00:00 2001 From: ubuntu <3158793232@qq.com> Date: Wed, 28 Jan 2026 10:37:09 +0800 Subject: [PATCH 01/11] add parallel --- source/source_base/parallel_reduce.cpp | 64 +++++--------------------- source/source_base/parallel_reduce.h | 10 ++-- 2 files changed, 17 insertions(+), 57 deletions(-) diff --git a/source/source_base/parallel_reduce.cpp b/source/source_base/parallel_reduce.cpp index 03535573b7..cdd8105767 100644 --- a/source/source_base/parallel_reduce.cpp +++ b/source/source_base/parallel_reduce.cpp @@ -233,85 +233,45 @@ void Parallel_Reduce::gather_int_all(int& v, int* all) return; } -void Parallel_Reduce::gather_min_int_all(const int& nproc, int& v) +void Parallel_Reduce::reduce_min_int(int& v) { #ifdef __MPI - std::vector all(nproc, 0); - MPI_Allgather(&v, 1, MPI_INT, all.data(), 1, MPI_INT, MPI_COMM_WORLD); - for (int i = 0; i < nproc; i++) - { - if (v > all[i]) - { - v = all[i]; - } - } + MPI_Allreduce(MPI_IN_PLACE, &v, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD); #endif } -void Parallel_Reduce::gather_max_double_all(const int& nproc, double& v) +void Parallel_Reduce::reduce_max_double(double& v) { #ifdef __MPI - std::vector value(nproc, 0.0); - MPI_Allgather(&v, 1, MPI_DOUBLE, value.data(), 1, MPI_DOUBLE, MPI_COMM_WORLD); - for (int i = 0; i < nproc; i++) - { - if (v < value[i]) - { - v = value[i]; - } - } + MPI_Allreduce(MPI_IN_PLACE, &v, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD); #endif } -void Parallel_Reduce::gather_max_double_pool(const int& nproc_in_pool, double& v) +void Parallel_Reduce::reduce_min_double(double& v) { #ifdef __MPI - if (nproc_in_pool == 1) - { - return; - } - std::vector value(nproc_in_pool, 0.0); - MPI_Allgather(&v, 1, MPI_DOUBLE, value.data(), 1, MPI_DOUBLE, POOL_WORLD); - for (int i = 0; i < nproc_in_pool; i++) - { - if (v < value[i]) - { - v = value[i]; - } - } + MPI_Allreduce(MPI_IN_PLACE, &v, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD); #endif } -void Parallel_Reduce::gather_min_double_pool(const int& nproc_in_pool, double& v) +void Parallel_Reduce::reduce_max_double_pool(const int& nproc_in_pool, double& v) { #ifdef __MPI if (nproc_in_pool == 1) { return; } - std::vector value(nproc_in_pool, 0.0); - MPI_Allgather(&v, 1, MPI_DOUBLE, value.data(), 1, MPI_DOUBLE, POOL_WORLD); - for (int i = 0; i < nproc_in_pool; i++) - { - if (v > value[i]) - { - v = value[i]; - } - } + MPI_Allreduce(MPI_IN_PLACE, &v, 1, MPI_DOUBLE, MPI_MAX, POOL_WORLD); #endif } -void Parallel_Reduce::gather_min_double_all(const int& nproc, double& v) +void Parallel_Reduce::reduce_min_double_pool(const int& nproc_in_pool, double& v) { #ifdef __MPI - std::vector value(nproc, 0.0); - MPI_Allgather(&v, 1, MPI_DOUBLE, value.data(), 1, MPI_DOUBLE, MPI_COMM_WORLD); - for (int i = 0; i < nproc; i++) + if (nproc_in_pool == 1) { - if (v > value[i]) - { - v = value[i]; - } + return; } + MPI_Allreduce(MPI_IN_PLACE, &v, 1, MPI_DOUBLE, MPI_MIN, POOL_WORLD); #endif } \ No newline at end of file diff --git a/source/source_base/parallel_reduce.h b/source/source_base/parallel_reduce.h index 7ab85be1cb..0bfcbbacbc 100644 --- a/source/source_base/parallel_reduce.h +++ b/source/source_base/parallel_reduce.h @@ -34,11 +34,11 @@ void reduce_double_diag(double* object, const int n); void reduce_double_allpool(const int& npool, const int& nproc_in_pool, double& object); void reduce_double_allpool(const int& npool, const int& nproc_in_pool, double* object, const int n); -void gather_min_int_all(const int& nproc, int& v); -void gather_max_double_all(const int& nproc, double& v); -void gather_min_double_all(const int& nproc, double& v); -void gather_max_double_pool(const int& nproc_in_pool, double& v); -void gather_min_double_pool(const int& nproc_in_pool, double& v); +void reduce_min_int(int& v); +void reduce_max_double(double& v); +void reduce_min_double(double& v); +void reduce_max_double_pool(const int& nproc_in_pool, double& v); +void reduce_min_double_pool(const int& nproc_in_pool, double& v); // mohan add 2011-04-21 void gather_int_all(int& v, int* all); From 593b3c6dade9a5dacfbbd8e4d3993e4568f7debe Mon Sep 17 00:00:00 2001 From: ubuntu <3158793232@qq.com> Date: Wed, 28 Jan 2026 10:44:42 +0800 Subject: [PATCH 02/11] change function --- .../test_parallel/parallel_reduce_test.cpp | 16 ++++++++-------- source/source_cell/k_vector_utils.cpp | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/source/source_base/test_parallel/parallel_reduce_test.cpp b/source/source_base/test_parallel/parallel_reduce_test.cpp index 696de3b485..01d04c5796 100644 --- a/source/source_base/test_parallel/parallel_reduce_test.cpp +++ b/source/source_base/test_parallel/parallel_reduce_test.cpp @@ -30,9 +30,9 @@ * 3. ReduceComplexAll: * Tests two variations of reduce_complex_all() * 4. GatherIntAll: - * Tests gather_int_all() and gather_min_int_all() + * Tests gather_int_all() and reduce_min_int() * 5. GatherDoubleAll: - * Tests gather_min_double_all() and gather_max_double_all() + * Tests reduce_min_double() and reduce_max_double() * 6. ReduceIntDiag: * Tests reduce_int_diag() * 7. ReduceDoubleDiag: @@ -47,7 +47,7 @@ * 11.ReduceComplexPool: * Tests two variations of reduce_pool() * 12.GatherDoublePool: - * Tests gather_min_double_pool() and gather_max_double_pool() + * Tests reduce_min_double_pool() and reduce_max_double_pool() * * */ @@ -233,7 +233,7 @@ TEST_F(ParaReduce, GatherIntAll) EXPECT_EQ(local_number, array[my_rank]); // get minimum integer among all processes int min_number = local_number; - Parallel_Reduce::gather_min_int_all(nproc, min_number); + Parallel_Reduce::reduce_min_int(min_number); for (int i = 0; i < nproc; i++) { EXPECT_LE(min_number, array[i]); @@ -256,10 +256,10 @@ TEST_F(ParaReduce, GatherDoubleAll) EXPECT_EQ(local_number, array[my_rank]); // get minimum integer among all processes double min_number = local_number; - Parallel_Reduce::gather_min_double_all(nproc, min_number); + Parallel_Reduce::reduce_min_double(min_number); // get maximum integer among all processes double max_number = local_number; - Parallel_Reduce::gather_max_double_all(nproc, max_number); + Parallel_Reduce::reduce_max_double(max_number); for (int i = 0; i < nproc; i++) { EXPECT_LE(min_number, array[i]); @@ -587,10 +587,10 @@ TEST_F(ParaReduce, GatherDoublePool) EXPECT_EQ(local_number, array[mpiContext.rank_in_pool]); // get minimum integer among all processes double min_number = local_number; - Parallel_Reduce::gather_min_double_pool(mpiContext.nproc_in_pool, min_number); + Parallel_Reduce::reduce_min_double_pool(mpiContext.nproc_in_pool, min_number); // get maximum integer among all processes double max_number = local_number; - Parallel_Reduce::gather_max_double_pool(mpiContext.nproc_in_pool, max_number); + Parallel_Reduce::reduce_max_double_pool(mpiContext.nproc_in_pool, max_number); for (int i = 0; i < mpiContext.nproc_in_pool; i++) { EXPECT_LE(min_number, array[i]); diff --git a/source/source_cell/k_vector_utils.cpp b/source/source_cell/k_vector_utils.cpp index 3f5bef0b44..acb538fa55 100644 --- a/source/source_cell/k_vector_utils.cpp +++ b/source/source_cell/k_vector_utils.cpp @@ -245,7 +245,7 @@ void kvec_mpi_k(K_Vectors& kv) ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "Number of k-points in this process", kv.nks); int nks_minimum = kv.nks; - Parallel_Reduce::gather_min_int_all(GlobalV::NPROC, nks_minimum); + Parallel_Reduce::reduce_min_int(nks_minimum); if (nks_minimum == 0) { From 76c04eb3ddf5535a2134b7de92a3773c50f7ca09 Mon Sep 17 00:00:00 2001 From: ubuntu <3158793232@qq.com> Date: Wed, 28 Jan 2026 10:45:04 +0800 Subject: [PATCH 03/11] change use file --- source/source_basis/module_pw/pw_basis_big.h | 4 ++-- source/source_estate/elecstate_energy.cpp | 12 ++++++------ source/source_estate/occupy.cpp | 6 +++--- source/source_io/cal_dos.cpp | 4 ++-- .../source_lcao/module_operator_lcao/op_exx_lcao.hpp | 10 +++------- source/source_pw/module_pwdft/setup_pwwfc.cpp | 9 ++++----- source/source_pw/module_pwdft/vnl_pw.cpp | 3 --- source/source_pw/module_stodft/sto_iter.cpp | 4 ++-- source/source_pw/module_stodft/sto_tool.cpp | 7 +++---- 9 files changed, 25 insertions(+), 34 deletions(-) diff --git a/source/source_basis/module_pw/pw_basis_big.h b/source/source_basis/module_pw/pw_basis_big.h index 2a04720877..b0711fa99b 100644 --- a/source/source_basis/module_pw/pw_basis_big.h +++ b/source/source_basis/module_pw/pw_basis_big.h @@ -349,9 +349,9 @@ class PW_Basis_Big : public PW_Basis_Sup } } } -#ifdef __MPI + #ifdef __MPI MPI_Allreduce(MPI_IN_PLACE, &this->gridecut_lat, 1, MPI_DOUBLE, MPI_MIN , this->pool_world); -#endif + #endif this->gridecut_lat -= 1e-6; delete[] ibox; diff --git a/source/source_estate/elecstate_energy.cpp b/source/source_estate/elecstate_energy.cpp index ea3c88f4d9..1db0e5753b 100644 --- a/source/source_estate/elecstate_energy.cpp +++ b/source/source_estate/elecstate_energy.cpp @@ -39,8 +39,8 @@ void ElecState::cal_bandgap() } #ifdef __MPI - Parallel_Reduce::gather_max_double_all(GlobalV::NPROC, vbm); - Parallel_Reduce::gather_min_double_all(GlobalV::NPROC, cbm); + Parallel_Reduce::reduce_max_double(vbm); + Parallel_Reduce::reduce_min_double(cbm); #endif this->bandgap = cbm - vbm; @@ -94,10 +94,10 @@ void ElecState::cal_bandgap_updw() } #ifdef __MPI - Parallel_Reduce::gather_max_double_all(GlobalV::NPROC, vbm_up); - Parallel_Reduce::gather_min_double_all(GlobalV::NPROC, cbm_up); - Parallel_Reduce::gather_max_double_all(GlobalV::NPROC, vbm_dw); - Parallel_Reduce::gather_min_double_all(GlobalV::NPROC, cbm_dw); + Parallel_Reduce::reduce_max_double(vbm_up); + Parallel_Reduce::reduce_min_double(cbm_up); + Parallel_Reduce::reduce_max_double(vbm_dw); + Parallel_Reduce::reduce_min_double(cbm_dw); #endif this->bandgap_up = cbm_up - vbm_up; diff --git a/source/source_estate/occupy.cpp b/source/source_estate/occupy.cpp index d9d8bebf9b..4231f96e8d 100644 --- a/source/source_estate/occupy.cpp +++ b/source/source_estate/occupy.cpp @@ -180,7 +180,7 @@ void Occupy::iweights( } } #ifdef __MPI - Parallel_Reduce::gather_max_double_all(GlobalV::NPROC, ef); + Parallel_Reduce::reduce_max_double(ef); #endif return; @@ -309,8 +309,8 @@ void Occupy::efermig(const ModuleBase::matrix& ekb, #ifdef __MPI // find min and max across pools - Parallel_Reduce::gather_max_double_all(GlobalV::NPROC, eup); - Parallel_Reduce::gather_min_double_all(GlobalV::NPROC, elw); + Parallel_Reduce::reduce_max_double(eup); + Parallel_Reduce::reduce_min_double(elw); #endif //================= diff --git a/source/source_io/cal_dos.cpp b/source/source_io/cal_dos.cpp index 6eb53aac8f..af22a7af98 100644 --- a/source/source_io/cal_dos.cpp +++ b/source/source_io/cal_dos.cpp @@ -56,8 +56,8 @@ void ModuleIO::prepare_dos(std::ofstream& ofs_running, } #ifdef __MPI - Parallel_Reduce::gather_max_double_all(GlobalV::NPROC, emax); - Parallel_Reduce::gather_min_double_all(GlobalV::NPROC, emin); + Parallel_Reduce::reduce_max_double(emax); + Parallel_Reduce::reduce_min_double(emin); #endif emax *= ModuleBase::Ry_to_eV; diff --git a/source/source_lcao/module_operator_lcao/op_exx_lcao.hpp b/source/source_lcao/module_operator_lcao/op_exx_lcao.hpp index 1d7e61409f..7e1f97f4a3 100644 --- a/source/source_lcao/module_operator_lcao/op_exx_lcao.hpp +++ b/source/source_lcao/module_operator_lcao/op_exx_lcao.hpp @@ -3,6 +3,7 @@ #ifdef __EXX #include "op_exx_lcao.h" +#include "source_base/parallel_reduce.h" #include "source_io/module_parameter/parameter.h" #include "source_lcao/module_ri/RI_2D_Comm.h" #include "source_hamilt/module_xc/xc_functional.h" @@ -244,10 +245,7 @@ OperatorEXX>::OperatorEXX(HS_Matrix_K* hsk_in, if (!ifs) { all_exist = 0; break; } } // Add MPI communication to synchronize all_exist across processes -#ifdef __MPI - // don't read in any files if one of the processes doesn't have it - MPI_Allreduce(MPI_IN_PLACE, &all_exist, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD); -#endif + Parallel_Reduce::reduce_min_int(all_exist); if (all_exist) { // Read HexxR in CSR format @@ -264,9 +262,7 @@ OperatorEXX>::OperatorEXX(HS_Matrix_K* hsk_in, const std::string restart_HR_path_cereal = GlobalC::restart.folder + "HexxR_" + std::to_string(PARAM.globalv.myrank); std::ifstream ifs(restart_HR_path_cereal, std::ios::binary); int all_exist_cereal = ifs ? 1 : 0; -#ifdef __MPI - MPI_Allreduce(MPI_IN_PLACE, &all_exist_cereal, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD); -#endif + Parallel_Reduce::reduce_min_int(all_exist_cereal); if (!all_exist_cereal) { //no HexxR file in CSR or binary format diff --git a/source/source_pw/module_pwdft/setup_pwwfc.cpp b/source/source_pw/module_pwdft/setup_pwwfc.cpp index 759178638c..7ca7907367 100644 --- a/source/source_pw/module_pwdft/setup_pwwfc.cpp +++ b/source/source_pw/module_pwdft/setup_pwwfc.cpp @@ -1,5 +1,6 @@ #include "source_pw/module_pwdft/setup_pwwfc.h" // pw_wfc #include "source_base/parallel_comm.h" // POOL_WORLD +#include "source_base/parallel_reduce.h" // Parallel_Reduce #include "source_io/print_info.h" // print information void pw::teardown_pwwfc(ModulePW::PW_Basis_K* &pw_wfc) @@ -53,14 +54,12 @@ void pw::setup_pwwfc(const Input_para& inp, pw_wfc->initparameters(false, inp.ecutwfc, kv.get_nks(), kv.kvec_d.data()); -#ifdef __MPI if (inp.pw_seed > 0) { - MPI_Allreduce(MPI_IN_PLACE, &pw_wfc->ggecut, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD); + Parallel_Reduce::reduce_max_double( pw_wfc->ggecut); } - // qianrui add 2021-8-13 to make different kpar parameters can get the same - // results -#endif + // qianrui add 2021-8-13 to make different kpar parameters can get the same result + pw_wfc->fft_bundle.initfftmode(inp.fft_mode); pw_wfc->setuptransform(); diff --git a/source/source_pw/module_pwdft/vnl_pw.cpp b/source/source_pw/module_pwdft/vnl_pw.cpp index 52d3d447da..15fb7681c8 100644 --- a/source/source_pw/module_pwdft/vnl_pw.cpp +++ b/source/source_pw/module_pwdft/vnl_pw.cpp @@ -687,7 +687,6 @@ void pseudopot_cell_vnl::init_vnl(UnitCell& cell, const ModulePW::PW_Basis* rho_ MPI_Allreduce(MPI_IN_PLACE, this->qq_nt.ptr, this->qq_nt.getSize(), MPI_DOUBLE, MPI_SUM, POOL_WORLD); MPI_Allreduce(MPI_IN_PLACE, this->qq_so.ptr, this->qq_so.getSize(), MPI_DOUBLE_COMPLEX, MPI_SUM, POOL_WORLD); #endif - // set the atomic specific qq_at matrices for (int ia = 0; ia < cell.nat; ia++) { @@ -1509,11 +1508,9 @@ void pseudopot_cell_vnl::newq(const ModuleBase::matrix& veff, const ModulePW::PW } } } - #ifdef __MPI MPI_Allreduce(MPI_IN_PLACE, deeq.ptr, deeq.getSize(), MPI_DOUBLE, MPI_SUM, POOL_WORLD); #endif - delete[] qnorm; } diff --git a/source/source_pw/module_stodft/sto_iter.cpp b/source/source_pw/module_stodft/sto_iter.cpp index cd00e2f6f2..a7ca4158a9 100644 --- a/source/source_pw/module_stodft/sto_iter.cpp +++ b/source/source_pw/module_stodft/sto_iter.cpp @@ -202,9 +202,9 @@ void Stochastic_Iter::checkemm(const int& ik, } if (ik == nks - 1) { + Parallel_Reduce::reduce_max_double(*p_hamilt_sto->emax); + Parallel_Reduce::reduce_min_double(*p_hamilt_sto->emin); #ifdef __MPI - MPI_Allreduce(MPI_IN_PLACE, p_hamilt_sto->emax, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD); - MPI_Allreduce(MPI_IN_PLACE, p_hamilt_sto->emin, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD); MPI_Allreduce(MPI_IN_PLACE, &change, 1, MPI_CHAR, MPI_LOR, MPI_COMM_WORLD); #endif if (change) diff --git a/source/source_pw/module_stodft/sto_tool.cpp b/source/source_pw/module_stodft/sto_tool.cpp index de1e72e3f1..59f6898f33 100644 --- a/source/source_pw/module_stodft/sto_tool.cpp +++ b/source/source_pw/module_stodft/sto_tool.cpp @@ -2,6 +2,7 @@ #include "source_base/math_chebyshev.h" #include "source_base/parallel_device.h" +#include "source_base/parallel_reduce.h" #include "source_base/timer.h" #include "source_io/module_parameter/parameter.h" #ifdef __MPI @@ -102,10 +103,8 @@ void check_che_op::operator()(const int& nche_in, if (ik == nk - 1) { -#ifdef __MPI - MPI_Allreduce(MPI_IN_PLACE, p_hamilt_sto->emax, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD); - MPI_Allreduce(MPI_IN_PLACE, p_hamilt_sto->emin, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD); -#endif + Parallel_Reduce::reduce_max_double(*p_hamilt_sto->emax); + Parallel_Reduce::reduce_min_double(*p_hamilt_sto->emin); GlobalV::ofs_running << "New Emax " << *p_hamilt_sto->emax << " Ry; new Emin " << *p_hamilt_sto->emin << " Ry" << std::endl; change = false; From 979d31a0a673fb684b97ae85184691332b6366d6 Mon Sep 17 00:00:00 2001 From: ubuntu <3158793232@qq.com> Date: Wed, 28 Jan 2026 10:52:53 +0800 Subject: [PATCH 04/11] fix add __MPI --- source/source_estate/elecstate_energy.cpp | 4 ---- source/source_estate/occupy.cpp | 7 ------- source/source_pw/module_pwdft/vnl_pw.cpp | 3 +++ 3 files changed, 3 insertions(+), 11 deletions(-) diff --git a/source/source_estate/elecstate_energy.cpp b/source/source_estate/elecstate_energy.cpp index 1db0e5753b..0c612666b0 100644 --- a/source/source_estate/elecstate_energy.cpp +++ b/source/source_estate/elecstate_energy.cpp @@ -38,10 +38,8 @@ void ElecState::cal_bandgap() } } -#ifdef __MPI Parallel_Reduce::reduce_max_double(vbm); Parallel_Reduce::reduce_min_double(cbm); -#endif this->bandgap = cbm - vbm; } @@ -93,12 +91,10 @@ void ElecState::cal_bandgap_updw() } } -#ifdef __MPI Parallel_Reduce::reduce_max_double(vbm_up); Parallel_Reduce::reduce_min_double(cbm_up); Parallel_Reduce::reduce_max_double(vbm_dw); Parallel_Reduce::reduce_min_double(cbm_dw); -#endif this->bandgap_up = cbm_up - vbm_up; this->bandgap_dw = cbm_dw - vbm_dw; diff --git a/source/source_estate/occupy.cpp b/source/source_estate/occupy.cpp index 4231f96e8d..5356406850 100644 --- a/source/source_estate/occupy.cpp +++ b/source/source_estate/occupy.cpp @@ -179,10 +179,7 @@ void Occupy::iweights( } } } -#ifdef __MPI Parallel_Reduce::reduce_max_double(ef); -#endif - return; } @@ -306,13 +303,9 @@ void Occupy::efermig(const ModuleBase::matrix& ekb, eup += 2 * smearing_sigma; elw -= 2 * smearing_sigma; - -#ifdef __MPI // find min and max across pools Parallel_Reduce::reduce_max_double(eup); Parallel_Reduce::reduce_min_double(elw); - -#endif //================= // Bisection method //================= diff --git a/source/source_pw/module_pwdft/vnl_pw.cpp b/source/source_pw/module_pwdft/vnl_pw.cpp index 15fb7681c8..52d3d447da 100644 --- a/source/source_pw/module_pwdft/vnl_pw.cpp +++ b/source/source_pw/module_pwdft/vnl_pw.cpp @@ -687,6 +687,7 @@ void pseudopot_cell_vnl::init_vnl(UnitCell& cell, const ModulePW::PW_Basis* rho_ MPI_Allreduce(MPI_IN_PLACE, this->qq_nt.ptr, this->qq_nt.getSize(), MPI_DOUBLE, MPI_SUM, POOL_WORLD); MPI_Allreduce(MPI_IN_PLACE, this->qq_so.ptr, this->qq_so.getSize(), MPI_DOUBLE_COMPLEX, MPI_SUM, POOL_WORLD); #endif + // set the atomic specific qq_at matrices for (int ia = 0; ia < cell.nat; ia++) { @@ -1508,9 +1509,11 @@ void pseudopot_cell_vnl::newq(const ModuleBase::matrix& veff, const ModulePW::PW } } } + #ifdef __MPI MPI_Allreduce(MPI_IN_PLACE, deeq.ptr, deeq.getSize(), MPI_DOUBLE, MPI_SUM, POOL_WORLD); #endif + delete[] qnorm; } From d3ecaf2be793f75bd8df9dee2a355f1fe6f79489 Mon Sep 17 00:00:00 2001 From: ubuntu <3158793232@qq.com> Date: Wed, 28 Jan 2026 10:58:03 +0800 Subject: [PATCH 05/11] fix error --- source/source_estate/elecstate_energy.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/source/source_estate/elecstate_energy.cpp b/source/source_estate/elecstate_energy.cpp index 4f181c249f..cb1e8c9b7e 100644 --- a/source/source_estate/elecstate_energy.cpp +++ b/source/source_estate/elecstate_energy.cpp @@ -50,11 +50,6 @@ void ElecState::cal_bandgap() { vbm =this->eferm.ef; } -#ifdef __MPI - Parallel_Reduce::gather_max_double_all(GlobalV::NPROC, vbm); - Parallel_Reduce::gather_min_double_all(GlobalV::NPROC, cbm); -#endif - this->bandgap = cbm - vbm; } From a9241a384316f8146924b8b76d54abccb10f0f81 Mon Sep 17 00:00:00 2001 From: ubuntu <3158793232@qq.com> Date: Wed, 28 Jan 2026 11:18:43 +0800 Subject: [PATCH 06/11] fix unittest bug --- source/source_estate/elecstate_energy.cpp | 7 ++++--- source/source_estate/occupy.cpp | 4 ++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/source/source_estate/elecstate_energy.cpp b/source/source_estate/elecstate_energy.cpp index cb1e8c9b7e..217558dc47 100644 --- a/source/source_estate/elecstate_energy.cpp +++ b/source/source_estate/elecstate_energy.cpp @@ -37,9 +37,10 @@ void ElecState::cal_bandgap() } } } - + #ifdef __MPI Parallel_Reduce::reduce_max_double(vbm); Parallel_Reduce::reduce_min_double(cbm); + #endif // Assign fermi level to CBM if it's still infinity if(cbm == std::numeric_limits::infinity()) { @@ -117,12 +118,12 @@ void ElecState::cal_bandgap_updw() { vbm_dw =this->eferm.ef_dw; } - + #ifdef __MPI Parallel_Reduce::reduce_max_double(vbm_up); Parallel_Reduce::reduce_min_double(cbm_up); Parallel_Reduce::reduce_max_double(vbm_dw); Parallel_Reduce::reduce_min_double(cbm_dw); - + #endif this->bandgap_up = cbm_up - vbm_up; this->bandgap_dw = cbm_dw - vbm_dw; } diff --git a/source/source_estate/occupy.cpp b/source/source_estate/occupy.cpp index 3e7e9b8668..e71531c0f6 100644 --- a/source/source_estate/occupy.cpp +++ b/source/source_estate/occupy.cpp @@ -179,7 +179,9 @@ void Occupy::iweights( } } } + #ifdef __MPI Parallel_Reduce::reduce_max_double(ef); + #endif return; } @@ -304,8 +306,10 @@ void Occupy::efermig(const ModuleBase::matrix& ekb, eup += 2 * smearing_sigma; elw -= 2 * smearing_sigma; // find min and max across pools + #ifdef __MPI Parallel_Reduce::reduce_max_double(eup); Parallel_Reduce::reduce_min_double(elw); + #endif //================= // Bisection method //================= From 9bce1adba2c0d5dcdd93ea812c7837e155cade94 Mon Sep 17 00:00:00 2001 From: ubuntu <3158793232@qq.com> Date: Wed, 28 Jan 2026 11:28:30 +0800 Subject: [PATCH 07/11] fix format --- source/source_basis/module_pw/pw_basis_big.h | 4 ++-- source/source_estate/elecstate_energy.cpp | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/source/source_basis/module_pw/pw_basis_big.h b/source/source_basis/module_pw/pw_basis_big.h index b0711fa99b..2a04720877 100644 --- a/source/source_basis/module_pw/pw_basis_big.h +++ b/source/source_basis/module_pw/pw_basis_big.h @@ -349,9 +349,9 @@ class PW_Basis_Big : public PW_Basis_Sup } } } - #ifdef __MPI +#ifdef __MPI MPI_Allreduce(MPI_IN_PLACE, &this->gridecut_lat, 1, MPI_DOUBLE, MPI_MIN , this->pool_world); - #endif +#endif this->gridecut_lat -= 1e-6; delete[] ibox; diff --git a/source/source_estate/elecstate_energy.cpp b/source/source_estate/elecstate_energy.cpp index 217558dc47..ba54ee2e29 100644 --- a/source/source_estate/elecstate_energy.cpp +++ b/source/source_estate/elecstate_energy.cpp @@ -37,10 +37,6 @@ void ElecState::cal_bandgap() } } } - #ifdef __MPI - Parallel_Reduce::reduce_max_double(vbm); - Parallel_Reduce::reduce_min_double(cbm); - #endif // Assign fermi level to CBM if it's still infinity if(cbm == std::numeric_limits::infinity()) { @@ -51,6 +47,10 @@ void ElecState::cal_bandgap() { vbm =this->eferm.ef; } + #ifdef __MPI + Parallel_Reduce::reduce_max_double(vbm); + Parallel_Reduce::reduce_min_double(cbm); + #endif this->bandgap = cbm - vbm; } From 603e92f5c216289d1ee3c1be8165454bab065eb5 Mon Sep 17 00:00:00 2001 From: ubuntu <3158793232@qq.com> Date: Wed, 28 Jan 2026 11:38:21 +0800 Subject: [PATCH 08/11] add back MPI --- source/source_lcao/module_operator_lcao/op_exx_lcao.hpp | 4 ++++ source/source_pw/module_pwdft/setup_pwwfc.cpp | 4 ++-- source/source_pw/module_stodft/sto_iter.cpp | 2 +- source/source_pw/module_stodft/sto_tool.cpp | 2 ++ 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/source/source_lcao/module_operator_lcao/op_exx_lcao.hpp b/source/source_lcao/module_operator_lcao/op_exx_lcao.hpp index 7e1f97f4a3..973be0459c 100644 --- a/source/source_lcao/module_operator_lcao/op_exx_lcao.hpp +++ b/source/source_lcao/module_operator_lcao/op_exx_lcao.hpp @@ -245,7 +245,9 @@ OperatorEXX>::OperatorEXX(HS_Matrix_K* hsk_in, if (!ifs) { all_exist = 0; break; } } // Add MPI communication to synchronize all_exist across processes + #ifdef __MPI Parallel_Reduce::reduce_min_int(all_exist); + #endif if (all_exist) { // Read HexxR in CSR format @@ -262,7 +264,9 @@ OperatorEXX>::OperatorEXX(HS_Matrix_K* hsk_in, const std::string restart_HR_path_cereal = GlobalC::restart.folder + "HexxR_" + std::to_string(PARAM.globalv.myrank); std::ifstream ifs(restart_HR_path_cereal, std::ios::binary); int all_exist_cereal = ifs ? 1 : 0; + #ifdef __MPI Parallel_Reduce::reduce_min_int(all_exist_cereal); + #endif if (!all_exist_cereal) { //no HexxR file in CSR or binary format diff --git a/source/source_pw/module_pwdft/setup_pwwfc.cpp b/source/source_pw/module_pwdft/setup_pwwfc.cpp index 7ca7907367..d110f90b55 100644 --- a/source/source_pw/module_pwdft/setup_pwwfc.cpp +++ b/source/source_pw/module_pwdft/setup_pwwfc.cpp @@ -53,13 +53,13 @@ void pw::setup_pwwfc(const Input_para& inp, pw_rho.nz); pw_wfc->initparameters(false, inp.ecutwfc, kv.get_nks(), kv.kvec_d.data()); - +#ifdef __MPI if (inp.pw_seed > 0) { Parallel_Reduce::reduce_max_double( pw_wfc->ggecut); } // qianrui add 2021-8-13 to make different kpar parameters can get the same result - +#endif pw_wfc->fft_bundle.initfftmode(inp.fft_mode); pw_wfc->setuptransform(); diff --git a/source/source_pw/module_stodft/sto_iter.cpp b/source/source_pw/module_stodft/sto_iter.cpp index a7ca4158a9..1490987cba 100644 --- a/source/source_pw/module_stodft/sto_iter.cpp +++ b/source/source_pw/module_stodft/sto_iter.cpp @@ -202,9 +202,9 @@ void Stochastic_Iter::checkemm(const int& ik, } if (ik == nks - 1) { +#ifdef __MPI Parallel_Reduce::reduce_max_double(*p_hamilt_sto->emax); Parallel_Reduce::reduce_min_double(*p_hamilt_sto->emin); -#ifdef __MPI MPI_Allreduce(MPI_IN_PLACE, &change, 1, MPI_CHAR, MPI_LOR, MPI_COMM_WORLD); #endif if (change) diff --git a/source/source_pw/module_stodft/sto_tool.cpp b/source/source_pw/module_stodft/sto_tool.cpp index 59f6898f33..2174bed933 100644 --- a/source/source_pw/module_stodft/sto_tool.cpp +++ b/source/source_pw/module_stodft/sto_tool.cpp @@ -103,8 +103,10 @@ void check_che_op::operator()(const int& nche_in, if (ik == nk - 1) { +#ifdef __MPI Parallel_Reduce::reduce_max_double(*p_hamilt_sto->emax); Parallel_Reduce::reduce_min_double(*p_hamilt_sto->emin); +#endif GlobalV::ofs_running << "New Emax " << *p_hamilt_sto->emax << " Ry; new Emin " << *p_hamilt_sto->emin << " Ry" << std::endl; change = false; From 8c49f645203c3365a7237ae8bee5cfc7df891a15 Mon Sep 17 00:00:00 2001 From: ubuntu <3158793232@qq.com> Date: Wed, 28 Jan 2026 14:53:43 +0800 Subject: [PATCH 09/11] move back sdft --- source/source_pw/module_stodft/sto_iter.cpp | 4 ++-- source/source_pw/module_stodft/sto_tool.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/source/source_pw/module_stodft/sto_iter.cpp b/source/source_pw/module_stodft/sto_iter.cpp index 1490987cba..cd00e2f6f2 100644 --- a/source/source_pw/module_stodft/sto_iter.cpp +++ b/source/source_pw/module_stodft/sto_iter.cpp @@ -203,8 +203,8 @@ void Stochastic_Iter::checkemm(const int& ik, if (ik == nks - 1) { #ifdef __MPI - Parallel_Reduce::reduce_max_double(*p_hamilt_sto->emax); - Parallel_Reduce::reduce_min_double(*p_hamilt_sto->emin); + MPI_Allreduce(MPI_IN_PLACE, p_hamilt_sto->emax, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD); + MPI_Allreduce(MPI_IN_PLACE, p_hamilt_sto->emin, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD); MPI_Allreduce(MPI_IN_PLACE, &change, 1, MPI_CHAR, MPI_LOR, MPI_COMM_WORLD); #endif if (change) diff --git a/source/source_pw/module_stodft/sto_tool.cpp b/source/source_pw/module_stodft/sto_tool.cpp index 2174bed933..2e810abb8f 100644 --- a/source/source_pw/module_stodft/sto_tool.cpp +++ b/source/source_pw/module_stodft/sto_tool.cpp @@ -104,8 +104,8 @@ void check_che_op::operator()(const int& nche_in, if (ik == nk - 1) { #ifdef __MPI - Parallel_Reduce::reduce_max_double(*p_hamilt_sto->emax); - Parallel_Reduce::reduce_min_double(*p_hamilt_sto->emin); + MPI_Allreduce(MPI_IN_PLACE, p_hamilt_sto->emax, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD); + MPI_Allreduce(MPI_IN_PLACE, p_hamilt_sto->emin, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD); #endif GlobalV::ofs_running << "New Emax " << *p_hamilt_sto->emax << " Ry; new Emin " << *p_hamilt_sto->emin << " Ry" << std::endl; From 3de993c9f37f2c166ad4fbc1fb6a5c7ea678194d Mon Sep 17 00:00:00 2001 From: ubuntu <3158793232@qq.com> Date: Wed, 28 Jan 2026 16:26:57 +0800 Subject: [PATCH 10/11] add template --- source/source_base/parallel_reduce.cpp | 34 +++++++++++++++---- source/source_base/parallel_reduce.h | 15 ++++---- .../test_parallel/parallel_reduce_test.cpp | 14 ++++---- source/source_basis/module_pw/pw_basis_big.h | 3 +- source/source_basis/module_pw/pw_init.cpp | 2 +- .../module_pw/test/depend_mock.cpp | 11 +++--- .../source_basis/module_pw/test/depend_mock.h | 6 ++-- source/source_cell/k_vector_utils.cpp | 2 +- source/source_estate/elecstate_energy.cpp | 12 +++---- source/source_estate/occupy.cpp | 6 ++-- source/source_io/cal_dos.cpp | 4 +-- .../module_operator_lcao/op_exx_lcao.hpp | 4 +-- source/source_pw/module_pwdft/setup_pwwfc.cpp | 2 +- source/source_pw/module_stodft/sto_iter.cpp | 4 +-- source/source_pw/module_stodft/sto_tool.cpp | 4 +-- 15 files changed, 72 insertions(+), 51 deletions(-) diff --git a/source/source_base/parallel_reduce.cpp b/source/source_base/parallel_reduce.cpp index cdd8105767..ab5c8505b5 100644 --- a/source/source_base/parallel_reduce.cpp +++ b/source/source_base/parallel_reduce.cpp @@ -233,28 +233,48 @@ void Parallel_Reduce::gather_int_all(int& v, int* all) return; } -void Parallel_Reduce::reduce_min_int(int& v) +template <> +void Parallel_Reduce::reduce_min(int& v) { #ifdef __MPI MPI_Allreduce(MPI_IN_PLACE, &v, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD); #endif } -void Parallel_Reduce::reduce_max_double(double& v) +template <> +void Parallel_Reduce::reduce_min(float& v) { #ifdef __MPI - MPI_Allreduce(MPI_IN_PLACE, &v, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD); + MPI_Allreduce(MPI_IN_PLACE, &v, 1, MPI_FLOAT, MPI_MIN, MPI_COMM_WORLD); #endif } -void Parallel_Reduce::reduce_min_double(double& v) +template <> +void Parallel_Reduce::reduce_min(double& v) { #ifdef __MPI MPI_Allreduce(MPI_IN_PLACE, &v, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD); #endif } -void Parallel_Reduce::reduce_max_double_pool(const int& nproc_in_pool, double& v) +template <> +void Parallel_Reduce::reduce_max(float& v) +{ +#ifdef __MPI + MPI_Allreduce(MPI_IN_PLACE, &v, 1, MPI_FLOAT, MPI_MAX, MPI_COMM_WORLD); +#endif +} + +template <> +void Parallel_Reduce::reduce_max(double& v) +{ +#ifdef __MPI + MPI_Allreduce(MPI_IN_PLACE, &v, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD); +#endif +} + +template <> +void Parallel_Reduce::reduce_max_pool(const int& nproc_in_pool, double& v) { #ifdef __MPI if (nproc_in_pool == 1) @@ -264,8 +284,8 @@ void Parallel_Reduce::reduce_max_double_pool(const int& nproc_in_pool, double& v MPI_Allreduce(MPI_IN_PLACE, &v, 1, MPI_DOUBLE, MPI_MAX, POOL_WORLD); #endif } - -void Parallel_Reduce::reduce_min_double_pool(const int& nproc_in_pool, double& v) +template <> +void Parallel_Reduce::reduce_min_pool(const int& nproc_in_pool, double& v) { #ifdef __MPI if (nproc_in_pool == 1) diff --git a/source/source_base/parallel_reduce.h b/source/source_base/parallel_reduce.h index 0bfcbbacbc..a781989951 100644 --- a/source/source_base/parallel_reduce.h +++ b/source/source_base/parallel_reduce.h @@ -21,6 +21,14 @@ template void reduce_pool(T& object); template void reduce_pool(T* object, const int n); +template +void reduce_min(T& v); +template +void reduce_max(T& v); +template +void reduce_min_pool(const int& nproc_in_pool, T& v); +template +void reduce_max_pool(const int& nproc_in_pool, T& v); void reduce_int_diag(int& object); // mohan add 2012-01-12 @@ -34,13 +42,6 @@ void reduce_double_diag(double* object, const int n); void reduce_double_allpool(const int& npool, const int& nproc_in_pool, double& object); void reduce_double_allpool(const int& npool, const int& nproc_in_pool, double* object, const int n); -void reduce_min_int(int& v); -void reduce_max_double(double& v); -void reduce_min_double(double& v); -void reduce_max_double_pool(const int& nproc_in_pool, double& v); -void reduce_min_double_pool(const int& nproc_in_pool, double& v); - -// mohan add 2011-04-21 void gather_int_all(int& v, int* all); bool check_if_equal(double& v); // mohan add 2009-11-11 diff --git a/source/source_base/test_parallel/parallel_reduce_test.cpp b/source/source_base/test_parallel/parallel_reduce_test.cpp index 01d04c5796..ac980ba24d 100644 --- a/source/source_base/test_parallel/parallel_reduce_test.cpp +++ b/source/source_base/test_parallel/parallel_reduce_test.cpp @@ -30,7 +30,7 @@ * 3. ReduceComplexAll: * Tests two variations of reduce_complex_all() * 4. GatherIntAll: - * Tests gather_int_all() and reduce_min_int() + * Tests gather_int_all() and reduce_min() * 5. GatherDoubleAll: * Tests reduce_min_double() and reduce_max_double() * 6. ReduceIntDiag: @@ -47,7 +47,7 @@ * 11.ReduceComplexPool: * Tests two variations of reduce_pool() * 12.GatherDoublePool: - * Tests reduce_min_double_pool() and reduce_max_double_pool() + * Tests reduce_min_pool() and reduce_max_pool() * * */ @@ -233,7 +233,7 @@ TEST_F(ParaReduce, GatherIntAll) EXPECT_EQ(local_number, array[my_rank]); // get minimum integer among all processes int min_number = local_number; - Parallel_Reduce::reduce_min_int(min_number); + Parallel_Reduce::reduce_min(min_number); for (int i = 0; i < nproc; i++) { EXPECT_LE(min_number, array[i]); @@ -256,10 +256,10 @@ TEST_F(ParaReduce, GatherDoubleAll) EXPECT_EQ(local_number, array[my_rank]); // get minimum integer among all processes double min_number = local_number; - Parallel_Reduce::reduce_min_double(min_number); + Parallel_Reduce::reduce_min(min_number); // get maximum integer among all processes double max_number = local_number; - Parallel_Reduce::reduce_max_double(max_number); + Parallel_Reduce::reduce_max(max_number); for (int i = 0; i < nproc; i++) { EXPECT_LE(min_number, array[i]); @@ -587,10 +587,10 @@ TEST_F(ParaReduce, GatherDoublePool) EXPECT_EQ(local_number, array[mpiContext.rank_in_pool]); // get minimum integer among all processes double min_number = local_number; - Parallel_Reduce::reduce_min_double_pool(mpiContext.nproc_in_pool, min_number); + Parallel_Reduce::reduce_min_pool(mpiContext.nproc_in_pool, min_number); // get maximum integer among all processes double max_number = local_number; - Parallel_Reduce::reduce_max_double_pool(mpiContext.nproc_in_pool, max_number); + Parallel_Reduce::reduce_max_pool(mpiContext.nproc_in_pool, max_number); for (int i = 0; i < mpiContext.nproc_in_pool; i++) { EXPECT_LE(min_number, array[i]); diff --git a/source/source_basis/module_pw/pw_basis_big.h b/source/source_basis/module_pw/pw_basis_big.h index 2a04720877..8581c6b399 100644 --- a/source/source_basis/module_pw/pw_basis_big.h +++ b/source/source_basis/module_pw/pw_basis_big.h @@ -2,6 +2,7 @@ #define PW_BASIS_BIG_H #include "source_base/constants.h" #include "source_base/global_function.h" +#include "source_base/parallel_reduce.h" #ifdef __MPI #include "mpi.h" #endif @@ -350,7 +351,7 @@ class PW_Basis_Big : public PW_Basis_Sup } } #ifdef __MPI - MPI_Allreduce(MPI_IN_PLACE, &this->gridecut_lat, 1, MPI_DOUBLE, MPI_MIN , this->pool_world); + Parallel_Reduce::reduce_min_pool(this->poolnproc, this->gridecut_lat); #endif this->gridecut_lat -= 1e-6; diff --git a/source/source_basis/module_pw/pw_init.cpp b/source/source_basis/module_pw/pw_init.cpp index 08c676d39f..13ecb388ce 100644 --- a/source/source_basis/module_pw/pw_init.cpp +++ b/source/source_basis/module_pw/pw_init.cpp @@ -200,7 +200,7 @@ void PW_Basis:: initgrids( } } #ifdef __MPI - MPI_Allreduce(MPI_IN_PLACE, &this->gridecut_lat, 1, MPI_DOUBLE, MPI_MIN , this->pool_world); + Parallel_Reduce::reduce_min_pool(this->poolnproc, this->gridecut_lat); #endif this->gridecut_lat -= 1e-6; diff --git a/source/source_basis/module_pw/test/depend_mock.cpp b/source/source_basis/module_pw/test/depend_mock.cpp index 4fdcfd5f4a..afbc5d0a26 100644 --- a/source/source_basis/module_pw/test/depend_mock.cpp +++ b/source/source_basis/module_pw/test/depend_mock.cpp @@ -13,12 +13,11 @@ namespace Parallel_Reduce { template void reduce_all(T& object) { return; }; template void reduce_pool(T& object) { return; }; + template void reduce_min_pool(const int& nproc_in_pool, T& v) { return; }; - template<> - void reduce_all(double& object) { return; }; - template<> - void reduce_pool(double& object) { return; }; - template<> - void reduce_pool(float& object) { return; }; + template void reduce_all(double& object); + template void reduce_pool(double& object); + template void reduce_pool(float& object); + template void reduce_min_pool(const int& nproc_in_pool, double& v); } #endif \ No newline at end of file diff --git a/source/source_basis/module_pw/test/depend_mock.h b/source/source_basis/module_pw/test/depend_mock.h index 216233d7dc..f9d7715976 100644 --- a/source/source_basis/module_pw/test/depend_mock.h +++ b/source/source_basis/module_pw/test/depend_mock.h @@ -7,9 +7,9 @@ extern MPI_Comm POOL_WORLD; namespace Parallel_Reduce { - void reduce_all(double& object); - void reduce_pool(double& object); - void reduce_pool(float& object); + template void reduce_all(T& object); + template void reduce_pool(T& object); + template void reduce_min_pool(const int& nproc_in_pool, T& v); } #endif diff --git a/source/source_cell/k_vector_utils.cpp b/source/source_cell/k_vector_utils.cpp index acb538fa55..6af9e22835 100644 --- a/source/source_cell/k_vector_utils.cpp +++ b/source/source_cell/k_vector_utils.cpp @@ -245,7 +245,7 @@ void kvec_mpi_k(K_Vectors& kv) ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "Number of k-points in this process", kv.nks); int nks_minimum = kv.nks; - Parallel_Reduce::reduce_min_int(nks_minimum); + Parallel_Reduce::reduce_min(nks_minimum); if (nks_minimum == 0) { diff --git a/source/source_estate/elecstate_energy.cpp b/source/source_estate/elecstate_energy.cpp index ba54ee2e29..ea8687f853 100644 --- a/source/source_estate/elecstate_energy.cpp +++ b/source/source_estate/elecstate_energy.cpp @@ -48,8 +48,8 @@ void ElecState::cal_bandgap() vbm =this->eferm.ef; } #ifdef __MPI - Parallel_Reduce::reduce_max_double(vbm); - Parallel_Reduce::reduce_min_double(cbm); + Parallel_Reduce::reduce_max(vbm); + Parallel_Reduce::reduce_min(cbm); #endif this->bandgap = cbm - vbm; } @@ -119,10 +119,10 @@ void ElecState::cal_bandgap_updw() vbm_dw =this->eferm.ef_dw; } #ifdef __MPI - Parallel_Reduce::reduce_max_double(vbm_up); - Parallel_Reduce::reduce_min_double(cbm_up); - Parallel_Reduce::reduce_max_double(vbm_dw); - Parallel_Reduce::reduce_min_double(cbm_dw); + Parallel_Reduce::reduce_max(vbm_up); + Parallel_Reduce::reduce_min(cbm_up); + Parallel_Reduce::reduce_max(vbm_dw); + Parallel_Reduce::reduce_min(cbm_dw); #endif this->bandgap_up = cbm_up - vbm_up; this->bandgap_dw = cbm_dw - vbm_dw; diff --git a/source/source_estate/occupy.cpp b/source/source_estate/occupy.cpp index e71531c0f6..fa50d1520d 100644 --- a/source/source_estate/occupy.cpp +++ b/source/source_estate/occupy.cpp @@ -180,7 +180,7 @@ void Occupy::iweights( } } #ifdef __MPI - Parallel_Reduce::reduce_max_double(ef); + Parallel_Reduce::reduce_max(ef); #endif return; } @@ -307,8 +307,8 @@ void Occupy::efermig(const ModuleBase::matrix& ekb, elw -= 2 * smearing_sigma; // find min and max across pools #ifdef __MPI - Parallel_Reduce::reduce_max_double(eup); - Parallel_Reduce::reduce_min_double(elw); + Parallel_Reduce::reduce_max(eup); + Parallel_Reduce::reduce_min(elw); #endif //================= // Bisection method diff --git a/source/source_io/cal_dos.cpp b/source/source_io/cal_dos.cpp index af22a7af98..2e3e7f869c 100644 --- a/source/source_io/cal_dos.cpp +++ b/source/source_io/cal_dos.cpp @@ -56,8 +56,8 @@ void ModuleIO::prepare_dos(std::ofstream& ofs_running, } #ifdef __MPI - Parallel_Reduce::reduce_max_double(emax); - Parallel_Reduce::reduce_min_double(emin); + Parallel_Reduce::reduce_max(emax); + Parallel_Reduce::reduce_min(emin); #endif emax *= ModuleBase::Ry_to_eV; diff --git a/source/source_lcao/module_operator_lcao/op_exx_lcao.hpp b/source/source_lcao/module_operator_lcao/op_exx_lcao.hpp index 973be0459c..876b4f6af3 100644 --- a/source/source_lcao/module_operator_lcao/op_exx_lcao.hpp +++ b/source/source_lcao/module_operator_lcao/op_exx_lcao.hpp @@ -246,7 +246,7 @@ OperatorEXX>::OperatorEXX(HS_Matrix_K* hsk_in, } // Add MPI communication to synchronize all_exist across processes #ifdef __MPI - Parallel_Reduce::reduce_min_int(all_exist); + Parallel_Reduce::reduce_min(all_exist); #endif if (all_exist) { @@ -265,7 +265,7 @@ OperatorEXX>::OperatorEXX(HS_Matrix_K* hsk_in, std::ifstream ifs(restart_HR_path_cereal, std::ios::binary); int all_exist_cereal = ifs ? 1 : 0; #ifdef __MPI - Parallel_Reduce::reduce_min_int(all_exist_cereal); + Parallel_Reduce::reduce_min(all_exist_cereal); #endif if (!all_exist_cereal) { diff --git a/source/source_pw/module_pwdft/setup_pwwfc.cpp b/source/source_pw/module_pwdft/setup_pwwfc.cpp index d110f90b55..cd06c917fc 100644 --- a/source/source_pw/module_pwdft/setup_pwwfc.cpp +++ b/source/source_pw/module_pwdft/setup_pwwfc.cpp @@ -56,7 +56,7 @@ void pw::setup_pwwfc(const Input_para& inp, #ifdef __MPI if (inp.pw_seed > 0) { - Parallel_Reduce::reduce_max_double( pw_wfc->ggecut); + Parallel_Reduce::reduce_max( pw_wfc->ggecut); } // qianrui add 2021-8-13 to make different kpar parameters can get the same result #endif diff --git a/source/source_pw/module_stodft/sto_iter.cpp b/source/source_pw/module_stodft/sto_iter.cpp index cd00e2f6f2..260b0db5f5 100644 --- a/source/source_pw/module_stodft/sto_iter.cpp +++ b/source/source_pw/module_stodft/sto_iter.cpp @@ -203,8 +203,8 @@ void Stochastic_Iter::checkemm(const int& ik, if (ik == nks - 1) { #ifdef __MPI - MPI_Allreduce(MPI_IN_PLACE, p_hamilt_sto->emax, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD); - MPI_Allreduce(MPI_IN_PLACE, p_hamilt_sto->emin, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD); + Parallel_Reduce::reduce_max(*p_hamilt_sto->emax); + Parallel_Reduce::reduce_min(*p_hamilt_sto->emin); MPI_Allreduce(MPI_IN_PLACE, &change, 1, MPI_CHAR, MPI_LOR, MPI_COMM_WORLD); #endif if (change) diff --git a/source/source_pw/module_stodft/sto_tool.cpp b/source/source_pw/module_stodft/sto_tool.cpp index 2e810abb8f..4ba359310b 100644 --- a/source/source_pw/module_stodft/sto_tool.cpp +++ b/source/source_pw/module_stodft/sto_tool.cpp @@ -104,8 +104,8 @@ void check_che_op::operator()(const int& nche_in, if (ik == nk - 1) { #ifdef __MPI - MPI_Allreduce(MPI_IN_PLACE, p_hamilt_sto->emax, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD); - MPI_Allreduce(MPI_IN_PLACE, p_hamilt_sto->emin, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD); + Parallel_Reduce::reduce_max(*p_hamilt_sto->emax); + Parallel_Reduce::reduce_min(*p_hamilt_sto->emin); #endif GlobalV::ofs_running << "New Emax " << *p_hamilt_sto->emax << " Ry; new Emin " << *p_hamilt_sto->emin << " Ry" << std::endl; From 9538041d5f614dce9ac0a3ffeaeab909522dc3c3 Mon Sep 17 00:00:00 2001 From: ubuntu <3158793232@qq.com> Date: Wed, 28 Jan 2026 17:55:40 +0800 Subject: [PATCH 11/11] fix compile bug --- source/source_basis/module_pw/pw_basis_big.h | 4 ++-- source/source_basis/module_pw/pw_init.cpp | 2 +- source/source_basis/module_pw/test/depend_mock.cpp | 11 ++++++----- source/source_basis/module_pw/test/depend_mock.h | 6 +++--- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/source/source_basis/module_pw/pw_basis_big.h b/source/source_basis/module_pw/pw_basis_big.h index 8581c6b399..987af787b3 100644 --- a/source/source_basis/module_pw/pw_basis_big.h +++ b/source/source_basis/module_pw/pw_basis_big.h @@ -2,7 +2,7 @@ #define PW_BASIS_BIG_H #include "source_base/constants.h" #include "source_base/global_function.h" -#include "source_base/parallel_reduce.h" + #ifdef __MPI #include "mpi.h" #endif @@ -351,7 +351,7 @@ class PW_Basis_Big : public PW_Basis_Sup } } #ifdef __MPI - Parallel_Reduce::reduce_min_pool(this->poolnproc, this->gridecut_lat); + MPI_Allreduce(MPI_IN_PLACE, &this->gridecut_lat, 1, MPI_DOUBLE, MPI_MIN , this->pool_world); #endif this->gridecut_lat -= 1e-6; diff --git a/source/source_basis/module_pw/pw_init.cpp b/source/source_basis/module_pw/pw_init.cpp index 13ecb388ce..08c676d39f 100644 --- a/source/source_basis/module_pw/pw_init.cpp +++ b/source/source_basis/module_pw/pw_init.cpp @@ -200,7 +200,7 @@ void PW_Basis:: initgrids( } } #ifdef __MPI - Parallel_Reduce::reduce_min_pool(this->poolnproc, this->gridecut_lat); + MPI_Allreduce(MPI_IN_PLACE, &this->gridecut_lat, 1, MPI_DOUBLE, MPI_MIN , this->pool_world); #endif this->gridecut_lat -= 1e-6; diff --git a/source/source_basis/module_pw/test/depend_mock.cpp b/source/source_basis/module_pw/test/depend_mock.cpp index afbc5d0a26..4fdcfd5f4a 100644 --- a/source/source_basis/module_pw/test/depend_mock.cpp +++ b/source/source_basis/module_pw/test/depend_mock.cpp @@ -13,11 +13,12 @@ namespace Parallel_Reduce { template void reduce_all(T& object) { return; }; template void reduce_pool(T& object) { return; }; - template void reduce_min_pool(const int& nproc_in_pool, T& v) { return; }; - template void reduce_all(double& object); - template void reduce_pool(double& object); - template void reduce_pool(float& object); - template void reduce_min_pool(const int& nproc_in_pool, double& v); + template<> + void reduce_all(double& object) { return; }; + template<> + void reduce_pool(double& object) { return; }; + template<> + void reduce_pool(float& object) { return; }; } #endif \ No newline at end of file diff --git a/source/source_basis/module_pw/test/depend_mock.h b/source/source_basis/module_pw/test/depend_mock.h index f9d7715976..216233d7dc 100644 --- a/source/source_basis/module_pw/test/depend_mock.h +++ b/source/source_basis/module_pw/test/depend_mock.h @@ -7,9 +7,9 @@ extern MPI_Comm POOL_WORLD; namespace Parallel_Reduce { - template void reduce_all(T& object); - template void reduce_pool(T& object); - template void reduce_min_pool(const int& nproc_in_pool, T& v); + void reduce_all(double& object); + void reduce_pool(double& object); + void reduce_pool(float& object); } #endif