diff --git a/source/source_io/module_parameter/read_inp_sys.cpp b/source/source_io/module_parameter/read_inp_sys.cpp index 9eb89882e9..7a8d450f93 100644 --- a/source/source_io/module_parameter/read_inp_sys.cpp +++ b/source/source_io/module_parameter/read_inp_sys.cpp @@ -493,28 +493,49 @@ In socket_driver mode, this flag controls whether the returned frame advertises // the number of MPI processes. Other configurations make the vendor // routines return inconsistent results across ranks and the SCF // silently diverges, so reject them at input time. See issue #8228. - // bndpar <= 0 is rejected by the bndpar input check; skip here so - // the diagnostic below never divides by zero. + // + // Skip conditions: + // 1) bndpar <= 0 is rejected by the bndpar input check; skipping + // here avoids a division by zero in the diagnostic below. + // 2) esolver_type = dp / nep never runs an eigensolver and never + // touches mtblas/C_PACK, so the kpar/bndpar layout is not + // consumed and must not trigger a diagnostic. See issue #8228. if (para.input.bndpar <= 0) { return; } + if (!ModuleIO::dsp_layout_check_needed(para.input.esolver_type)) + { + return; + } const int nproc = para.sys.nproc; - const bool layout_valid = dsp_kpar_layout_valid(nproc, - para.input.kpar, - para.sys.kpar_lcao, - para.input.bndpar); + const bool layout_valid = ModuleIO::dsp_kpar_layout_valid( + nproc, + para.input.basis_type, + para.input.kpar, + para.sys.kpar_lcao, + para.input.bndpar, + para.input.esolver_type); if (!layout_valid) { - const std::string msg = - "On DSP hardware, the mtblas/C_PACK libraries require kpar (kpar_lcao for LCAO) * bndpar" - " to equal the number of MPI processes, but got NPROC=" + std::to_string(nproc) - + ", kpar=" + std::to_string(para.input.kpar) - + ", kpar_lcao=" + std::to_string(para.sys.kpar_lcao) + const int kpar_eff = ModuleIO::dsp_effective_kpar( + para.input.basis_type, para.input.kpar, para.sys.kpar_lcao); + const std::string basis_str + = (para.input.basis_type == "lcao") ? "kpar_lcao" : "kpar"; + const std::string msg + = "On DSP hardware with esolver_type=" + para.input.esolver_type + + " and basis_type=" + para.input.basis_type + + ", the mtblas/C_PACK libraries require " + basis_str + + " * bndpar to equal the number of MPI processes, but got" + + " NPROC=" + std::to_string(nproc) + + ", " + basis_str + "=" + std::to_string(kpar_eff) + ", bndpar=" + std::to_string(para.input.bndpar) - + ". Please set kpar=" + std::to_string(nproc / para.input.bndpar) + + ". Please set " + basis_str + "=" + + std::to_string(nproc / para.input.bndpar) + " or adjust the process count."; - ModuleBase::WARNING_QUIT("ReadInput", msg); + std::cout << " === Note: kpar * bndpar != NPROC ===" << std::endl; + std::cout << msg << std::endl; + std::cout << "=== end ===" << std::endl; } }; #endif diff --git a/source/source_io/module_parameter/read_input_tool.h b/source/source_io/module_parameter/read_input_tool.h index 4a733644dc..7a82d1b4fe 100644 --- a/source/source_io/module_parameter/read_input_tool.h +++ b/source/source_io/module_parameter/read_input_tool.h @@ -245,20 +245,69 @@ void reset_vector(std::vector& vec, int size, T default_value) namespace ModuleIO { + +/** + * @brief Whether the DSP kpar/bndpar layout constraint applies. + * + * esolver_type = dp / nep are machine-learning interatomic-potential + * solvers: they never build a PW/LCAO Hamiltonian, never construct + * k-point pools or band groups, and never call the mtblas/C_PACK + * vendor libraries, so the layout constraint is inapplicable. + * Comparison is case-insensitive. See issue #8228. + */ +inline bool dsp_layout_check_needed(const std::string& esolver_type) +{ + std::string lower = esolver_type; + std::transform(lower.begin(), lower.end(), lower.begin(), + [](unsigned char c) { return std::tolower(c); }); + return !(lower == "dp" || lower == "nep"); +} + +/** + * @brief The k-point pool count actually consumed by the current basis. + * + * After ReadInput::reset_value, exactly one of (kpar, kpar_lcao) carries + * the user value and the other is 1. Select explicitly by basis_type + * instead of std::max so the invariant is documented and enforced. + */ +inline int dsp_effective_kpar(const std::string& basis_type, + const int kpar, + const int kpar_lcao) +{ + return (basis_type == "lcao") ? kpar_lcao : kpar; +} + /** - * @brief Check whether a k-point/band parallel layout satisfies the DSP vendor - * library constraint. + * @brief Check whether a k-point/band parallel layout satisfies the DSP + * vendor library constraint. * - * On DSP hardware, the mtblas/C_PACK libraries require exactly one MPI process - * per k-point pool: the effective pool count (kpar for PW, kpar_lcao for LCAO, - * the other being 1) times bndpar must equal nproc exactly. Defined inline here - * so that both the __DSP input check and non-DSP unit tests can use it. - * See issue #8228. + * On DSP hardware, the mtblas/C_PACK libraries require exactly one MPI + * process per k-point pool: the effective pool count (kpar for PW, + * kpar_lcao for LCAO) times bndpar must equal nproc exactly. Non-positive + * nproc / pool count / bndpar are invalid layouts. Always returns true + * when esolver_type is dp or nep. See issue #8228. */ -inline bool dsp_kpar_layout_valid(const int nproc, const int kpar, const int kpar_lcao, const int bndpar) +inline bool dsp_kpar_layout_valid(const int nproc, + const std::string& basis_type, + const int kpar, + const int kpar_lcao, + const int bndpar, + const std::string& esolver_type) { - const int kpar_eff = std::max(kpar, kpar_lcao); - const int expected_nproc = kpar_eff * bndpar; - return nproc == expected_nproc; + if (!dsp_layout_check_needed(esolver_type)) + { + return true; + } + if (nproc <= 0) + { + return false; + } + const int kpar_eff = dsp_effective_kpar(basis_type, kpar, kpar_lcao); + if (kpar_eff <= 0 || bndpar <= 0) + { + return false; + } + return nproc == kpar_eff * bndpar; } + } // namespace ModuleIO diff --git a/source/source_io/unittests/test_read_inp_sys.cpp b/source/source_io/unittests/test_read_inp_sys.cpp index b6ee67b76c..2e07bd9e33 100644 --- a/source/source_io/unittests/test_read_inp_sys.cpp +++ b/source/source_io/unittests/test_read_inp_sys.cpp @@ -14,37 +14,107 @@ TEST(DspKparLayoutValidTest, PWKparPools) { // PW: kpar keeps the user value, kpar_lcao stays 1 - EXPECT_TRUE(ModuleIO::dsp_kpar_layout_valid(4, 4, 1, 1)); + EXPECT_TRUE(ModuleIO::dsp_kpar_layout_valid(4, "pw", 4, 1, 1, "ksdft")); // default kpar=1 on 4 processes: the silently diverging case of #8228 - EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(4, 1, 1, 1)); + EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(4, "pw", 1, 1, 1, "ksdft")); // serial run is always fine - EXPECT_TRUE(ModuleIO::dsp_kpar_layout_valid(1, 1, 1, 1)); + EXPECT_TRUE(ModuleIO::dsp_kpar_layout_valid(1, "pw", 1, 1, 1, "ksdft")); // more processes than pools - EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(8, 4, 1, 1)); + EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(8, "pw", 4, 1, 1, "ksdft")); } TEST(DspKparLayoutValidTest, LCAOKparReset) { // LCAO moves the user kpar into kpar_lcao and forces kpar to 1 - EXPECT_TRUE(ModuleIO::dsp_kpar_layout_valid(4, 1, 4, 1)); - EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(4, 1, 2, 1)); - EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(4, 1, 1, 1)); + EXPECT_TRUE(ModuleIO::dsp_kpar_layout_valid(4, "lcao", 1, 4, 1, "ksdft")); + EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(4, "lcao", 1, 2, 1, "ksdft")); + EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(4, "lcao", 1, 1, 1, "ksdft")); +} + +TEST(DspKparLayoutValidTest, EffectiveKparSelection) +{ + // Regression guard for the removed std::max(kpar, kpar_lcao): + // the field NOT used by the current basis_type must be ignored, + // even when it is larger than the effective one. + + // LCAO with a stale kpar=8: old std::max took 8 and rejected a + // perfectly valid LCAO layout; the explicit selection accepts it. + EXPECT_TRUE(ModuleIO::dsp_kpar_layout_valid(4, "lcao", 8, 4, 1, "ksdft")); + + // PW with a stale kpar_lcao=8: old std::max took 8 and wrongly + // accepted an invalid PW layout (4 pools on 8 processes). + EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(8, "pw", 4, 8, 1, "ksdft")); + + // unknown basis_type falls back to the PW field + EXPECT_TRUE(ModuleIO::dsp_kpar_layout_valid(4, "lcao_in_pw", 4, 1, 1, "ksdft")); } TEST(DspKparLayoutValidTest, BndparExemption) { // the bndpar+kpar layout that #7420 intended to allow - EXPECT_TRUE(ModuleIO::dsp_kpar_layout_valid(4, 2, 1, 2)); - EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(4, 1, 1, 2)); - EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(8, 2, 1, 2)); + EXPECT_TRUE(ModuleIO::dsp_kpar_layout_valid(4, "pw", 2, 1, 2, "ksdft")); + EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(4, "pw", 1, 1, 2, "ksdft")); + EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(8, "pw", 2, 1, 2, "ksdft")); + // LCAO variant: kpar_lcao * bndpar == nproc + EXPECT_TRUE(ModuleIO::dsp_kpar_layout_valid(4, "lcao", 1, 2, 2, "ksdft")); + EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(4, "lcao", 1, 1, 2, "ksdft")); +} + +TEST(DspKparLayoutValidTest, EsolverExemption) +{ + // dp / nep never run an eigensolver and never touch mtblas/C_PACK: + // any layout must be accepted, however mismatched + EXPECT_TRUE(ModuleIO::dsp_kpar_layout_valid(4, "pw", 2, 1, 3, "dp")); + EXPECT_TRUE(ModuleIO::dsp_kpar_layout_valid(7, "lcao", 1, 2, 3, "nep")); + EXPECT_TRUE(ModuleIO::dsp_kpar_layout_valid(0, "pw", 0, 0, 0, "dp")); + // case-insensitive exemption + EXPECT_TRUE(ModuleIO::dsp_kpar_layout_valid(4, "pw", 2, 1, 3, "DP")); + EXPECT_TRUE(ModuleIO::dsp_kpar_layout_valid(4, "pw", 2, 1, 3, "Nep")); + // non-ML esolvers are still checked + EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(4, "pw", 1, 1, 2, "ksdft")); + EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(4, "pw", 1, 1, 2, "sdft")); } TEST(DspKparLayoutValidTest, DegenerateValues) { - // non-positive factors are invalid layouts, and must be rejected by plain - // arithmetic without any division so no undefined behavior can occur - EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(4, 0, 0, 1)); - EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(4, 1, 1, 0)); - EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(4, -1, -1, 1)); - EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(0, 1, 1, 1)); + // non-positive factors are invalid layouts, rejected by plain + // comparisons without any division so no UB can occur + EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(4, "pw", 0, 0, 1, "ksdft")); + EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(4, "pw", 1, 1, 0, "ksdft")); + EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(4, "pw", -1, -1, 1, "ksdft")); + EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(0, "pw", 1, 1, 1, "ksdft")); + // the 0*1 == 0 hole: nproc=0 with all-zero factors must NOT pass + EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(0, "pw", 0, 0, 1, "ksdft")); + EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(0, "lcao", 0, 0, 0, "ksdft")); + // negative product accidentally matching a negative nproc must not pass + EXPECT_FALSE(ModuleIO::dsp_kpar_layout_valid(-4, "pw", -2, 1, 2, "ksdft")); +} + +//--------------------------------------------------------------- +// sub-helpers: unit-level coverage of the two extracted decisions +//--------------------------------------------------------------- + +TEST(DspLayoutCheckNeededTest, EsolverType) +{ + EXPECT_FALSE(ModuleIO::dsp_layout_check_needed("dp")); + EXPECT_FALSE(ModuleIO::dsp_layout_check_needed("nep")); + EXPECT_FALSE(ModuleIO::dsp_layout_check_needed("DP")); + EXPECT_FALSE(ModuleIO::dsp_layout_check_needed("NEP")); + EXPECT_TRUE(ModuleIO::dsp_layout_check_needed("ksdft")); + EXPECT_TRUE(ModuleIO::dsp_layout_check_needed("sdft")); + EXPECT_TRUE(ModuleIO::dsp_layout_check_needed("")); + // trailing whitespace is NOT normalized: input parsing lowercases + // esolver_type upstream; an unnormalized value is checked strictly + EXPECT_TRUE(ModuleIO::dsp_layout_check_needed("dp ")); +} + +TEST(DspEffectiveKparTest, BasisTypeSelection) +{ + EXPECT_EQ(ModuleIO::dsp_effective_kpar("pw", 4, 1), 4); + EXPECT_EQ(ModuleIO::dsp_effective_kpar("lcao", 1, 4), 4); + // invariant after reset_value: exactly one of the two is 1 + EXPECT_EQ(ModuleIO::dsp_effective_kpar("lcao", 1, 1), 1); + // unknown basis types take the PW field, matching reset_value, + // which only reroutes kpar when basis_type == "lcao" + EXPECT_EQ(ModuleIO::dsp_effective_kpar("", 3, 1), 3); }