From 86efe3136cebe9aaf00b8370079ab78f2708a5e5 Mon Sep 17 00:00:00 2001 From: Liangxuan <1351620715@qq.com> Date: Mon, 5 May 2025 21:30:28 +0800 Subject: [PATCH 1/2] Fix: if restart but can't find Hexx files, warning and restart from the non-exx loop --- .../operator_lcao/op_exx_lcao.hpp | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/op_exx_lcao.hpp b/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/op_exx_lcao.hpp index 5b756cdec3..691a0d7552 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/op_exx_lcao.hpp +++ b/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/op_exx_lcao.hpp @@ -207,13 +207,18 @@ OperatorEXX>::OperatorEXX(HS_Matrix_K* hsk_in, else if (this->add_hexx_type == Add_Hexx_Type::R) { // read in Hexx(R) - const std::string restart_HR_path = PARAM.globalv.global_readin_dir + "HexxR" + std::to_string(GlobalV::MY_RANK); - bool all_exist = true; + const std::string restart_HR_path = GlobalC::restart.folder + "HexxR" + std::to_string(GlobalV::MY_RANK); + int all_exist = 1; for (int is = 0; is < PARAM.inp.nspin; ++is) { std::ifstream ifs(restart_HR_path + "_" + std::to_string(is) + ".csr"); - if (!ifs) { all_exist = false; break; } + 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 if (all_exist) { // Read HexxR in CSR format @@ -228,11 +233,24 @@ OperatorEXX>::OperatorEXX(HS_Matrix_K* hsk_in, { // Read HexxR in binary format (old version) const std::string restart_HR_path_cereal = GlobalC::restart.folder + "HexxR_" + std::to_string(GlobalV::MY_RANK); - if (GlobalC::exx_info.info_ri.real_number) { - ModuleIO::read_Hexxs_cereal(restart_HR_path_cereal, *Hexxd); + 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 + if (!all_exist_cereal) + { + //no HexxR file in CSR or binary format + this->restart = false; } - else { - ModuleIO::read_Hexxs_cereal(restart_HR_path_cereal, *Hexxc); + else + { + if (GlobalC::exx_info.info_ri.real_number) { + ModuleIO::read_Hexxs_cereal(restart_HR_path_cereal, *Hexxd); + } + else { + ModuleIO::read_Hexxs_cereal(restart_HR_path_cereal, *Hexxc); + } } } } From 2136cdfb261165d67b9f1daa08a826d1b3633e26 Mon Sep 17 00:00:00 2001 From: Liangxuan <1351620715@qq.com> Date: Thu, 8 May 2025 22:10:36 +0800 Subject: [PATCH 2/2] Fix : InputParaTest was aborted due to EXPECT_EXIT being incompatible with MPI. Replace the EXPECT_EXIT with try-catch. --- source/module_io/test/read_input_ptest.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/source/module_io/test/read_input_ptest.cpp b/source/module_io/test/read_input_ptest.cpp index f775aa7aca..f6d8528ec5 100644 --- a/source/module_io/test/read_input_ptest.cpp +++ b/source/module_io/test/read_input_ptest.cpp @@ -460,9 +460,24 @@ TEST_F(InputParaTest, Check) ModuleIO::ReadInput readinput(GlobalV::MY_RANK); Parameter param; testing::internal::CaptureStdout(); - EXPECT_EXIT(readinput.read_parameters(param, "./empty_INPUT"), ::testing::ExitedWithCode(0), ""); - std::string output = testing::internal::GetCapturedStdout(); - EXPECT_THAT(output, testing::HasSubstr("INPUT parameters have been successfully checked!")); + try { + readinput.read_parameters(param, "./empty_INPUT"); + + // if exit normally with exit(0) + std::string output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("INPUT parameters have been successfully checked!")); + + } catch (const std::exception& e) { + // if exit with error, then the test is failed + FAIL() << "read_parameters threw an exception: " << e.what(); + } catch (...) { + // if exit with unknown error, then the test is failed + FAIL() << "read_parameters threw an unknown exception."; + } + // Note : the EXPECT_EXIT is not working with MPI, so we use try-catch to test the exit + // EXPECT_EXIT(readinput.read_parameters(param, "./empty_INPUT"), ::testing::ExitedWithCode(0), ""); + // std::string output = testing::internal::GetCapturedStdout(); + // EXPECT_THAT(output, testing::HasSubstr("INPUT parameters have been successfully checked!")); if (GlobalV::MY_RANK == 0) { EXPECT_TRUE(std::remove("./empty_INPUT") == 0);