From eb9114bc307f71229ac0a3abb6027f8f2b770646 Mon Sep 17 00:00:00 2001 From: chengleizheng Date: Fri, 29 May 2026 16:51:16 +0800 Subject: [PATCH 1/2] Replaced ia-it nested loops with flat iat loops using ucell.iat2it/iat2ia lookup arrays and added #pragma omp parallel for guarded by #ifdef _OPENMP in runner() coord building, runner() force assignment, and type_map() atype assignment. --- source/source_esolver/esolver_dp.cpp | 46 ++++++++++++++++------------ 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/source/source_esolver/esolver_dp.cpp b/source/source_esolver/esolver_dp.cpp index 879193e668b..2c524511ddb 100644 --- a/source/source_esolver/esolver_dp.cpp +++ b/source/source_esolver/esolver_dp.cpp @@ -71,18 +71,17 @@ void ESolver_DP::runner(UnitCell& ucell, const int istep) cell[8] = ucell.latvec.e33 * ucell.lat0_angstrom; std::vector coord(3 * ucell.nat, 0.0); - int iat = 0; - for (int it = 0; it < ucell.ntype; ++it) +#ifdef _OPENMP +#pragma omp parallel for +#endif + for (int iat = 0; iat < ucell.nat; ++iat) { - for (int ia = 0; ia < ucell.atoms[it].na; ++ia) - { - coord[3 * iat] = ucell.atoms[it].tau[ia].x * ucell.lat0_angstrom; - coord[3 * iat + 1] = ucell.atoms[it].tau[ia].y * ucell.lat0_angstrom; - coord[3 * iat + 2] = ucell.atoms[it].tau[ia].z * ucell.lat0_angstrom; - iat++; - } + int it = ucell.iat2it[iat]; + int ia = ucell.iat2ia[iat]; + coord[3 * iat] = ucell.atoms[it].tau[ia].x * ucell.lat0_angstrom; + coord[3 * iat + 1] = ucell.atoms[it].tau[ia].y * ucell.lat0_angstrom; + coord[3 * iat + 2] = ucell.atoms[it].tau[ia].z * ucell.lat0_angstrom; } - assert(ucell.nat == iat); #ifdef __DPMD std::vector f, v; @@ -101,6 +100,9 @@ void ESolver_DP::runner(UnitCell& ucell, const int istep) GlobalV::ofs_running << " #TOTAL ENERGY# " << std::setprecision(11) << dp_potential * ModuleBase::Ry_to_eV << " eV" << std::endl; +#ifdef _OPENMP +#pragma omp parallel for +#endif for (int i = 0; i < ucell.nat; ++i) { dp_force(i, 0) = f[3 * i] * fact_f; @@ -186,20 +188,24 @@ void ESolver_DP::type_map(const UnitCell& ucell) } std::cout << "\n -----------------------------------------------------------------" << std::endl; - int iat = 0; + // validate labels exist in DP model type map for (int it = 0; it < ucell.ntype; ++it) { - for (int ia = 0; ia < ucell.atoms[it].na; ++ia) + if (label.find(ucell.atoms[it].label) == label.end()) { - if (label.find(ucell.atoms[it].label) == label.end()) - { - ModuleBase::WARNING_QUIT("ESolver_DP", - "The label " + ucell.atoms[it].label + " is not found in the type map."); - } - atype[iat] = label[ucell.atoms[it].label]; - iat++; + ModuleBase::WARNING_QUIT("ESolver_DP", + "The label " + ucell.atoms[it].label + " is not found in the type map."); } } - assert(ucell.nat == iat); + + // assign atype for each atom +#ifdef _OPENMP +#pragma omp parallel for +#endif + for (int iat = 0; iat < ucell.nat; ++iat) + { + int it = ucell.iat2it[iat]; + atype[iat] = label[ucell.atoms[it].label]; + } } #endif From 006d439db0e3a8d66845a4004f059d52c4c83954 Mon Sep 17 00:00:00 2001 From: chengleizheng Date: Sun, 31 May 2026 19:18:26 +0800 Subject: [PATCH 2/2] Add default(none) to explicitly distinguish private variables from shared variables. --- source/source_esolver/esolver_dp.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/source_esolver/esolver_dp.cpp b/source/source_esolver/esolver_dp.cpp index 2c524511ddb..baced2bbeac 100644 --- a/source/source_esolver/esolver_dp.cpp +++ b/source/source_esolver/esolver_dp.cpp @@ -72,7 +72,7 @@ void ESolver_DP::runner(UnitCell& ucell, const int istep) std::vector coord(3 * ucell.nat, 0.0); #ifdef _OPENMP -#pragma omp parallel for +#pragma omp parallel for default(none) shared(ucell, coord) #endif for (int iat = 0; iat < ucell.nat; ++iat) { @@ -101,7 +101,7 @@ void ESolver_DP::runner(UnitCell& ucell, const int istep) << std::endl; #ifdef _OPENMP -#pragma omp parallel for +#pragma omp parallel for default(none) shared(ucell, f, fact_f) #endif for (int i = 0; i < ucell.nat; ++i) { @@ -200,7 +200,7 @@ void ESolver_DP::type_map(const UnitCell& ucell) // assign atype for each atom #ifdef _OPENMP -#pragma omp parallel for +#pragma omp parallel for default(none) shared(ucell, label) #endif for (int iat = 0; iat < ucell.nat; ++iat) {