Skip to content

Commit 315a164

Browse files
authored
Fix: if restart but can't find Hexx files, warning and run as usual (automatically initialized the density) (#6194)
* Fix: if restart but can't find Hexx files, warning and restart from the non-exx loop * Fix : InputParaTest was aborted due to EXPECT_EXIT being incompatible with MPI. Replace the EXPECT_EXIT with try-catch. * Fix: error related to MPI in InputParaTest * Fix : store and recover check_mode to prevent problems when multiple unittests need to be run continuously * comment out the input_test_para_4
1 parent 691b2ed commit 315a164

File tree

2 files changed

+51
-12
lines changed

2 files changed

+51
-12
lines changed

source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/op_exx_lcao.hpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,18 @@ OperatorEXX<OperatorLCAO<TK, TR>>::OperatorEXX(HS_Matrix_K<TK>* hsk_in,
207207
else if (this->add_hexx_type == Add_Hexx_Type::R)
208208
{
209209
// read in Hexx(R)
210-
const std::string restart_HR_path = PARAM.globalv.global_readin_dir + "HexxR" + std::to_string(GlobalV::MY_RANK);
211-
bool all_exist = true;
210+
const std::string restart_HR_path = GlobalC::restart.folder + "HexxR" + std::to_string(GlobalV::MY_RANK);
211+
int all_exist = 1;
212212
for (int is = 0; is < PARAM.inp.nspin; ++is)
213213
{
214214
std::ifstream ifs(restart_HR_path + "_" + std::to_string(is) + ".csr");
215-
if (!ifs) { all_exist = false; break; }
215+
if (!ifs) { all_exist = 0; break; }
216216
}
217+
// Add MPI communication to synchronize all_exist across processes
218+
#ifdef __MPI
219+
// don't read in any files if one of the processes doesn't have it
220+
MPI_Allreduce(MPI_IN_PLACE, &all_exist, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
221+
#endif
217222
if (all_exist)
218223
{
219224
// Read HexxR in CSR format
@@ -228,11 +233,24 @@ OperatorEXX<OperatorLCAO<TK, TR>>::OperatorEXX(HS_Matrix_K<TK>* hsk_in,
228233
{
229234
// Read HexxR in binary format (old version)
230235
const std::string restart_HR_path_cereal = GlobalC::restart.folder + "HexxR_" + std::to_string(GlobalV::MY_RANK);
231-
if (GlobalC::exx_info.info_ri.real_number) {
232-
ModuleIO::read_Hexxs_cereal(restart_HR_path_cereal, *Hexxd);
236+
std::ifstream ifs(restart_HR_path_cereal, std::ios::binary);
237+
int all_exist_cereal = ifs ? 1 : 0;
238+
#ifdef __MPI
239+
MPI_Allreduce(MPI_IN_PLACE, &all_exist_cereal, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
240+
#endif
241+
if (!all_exist_cereal)
242+
{
243+
//no HexxR file in CSR or binary format
244+
this->restart = false;
233245
}
234-
else {
235-
ModuleIO::read_Hexxs_cereal(restart_HR_path_cereal, *Hexxc);
246+
else
247+
{
248+
if (GlobalC::exx_info.info_ri.real_number) {
249+
ModuleIO::read_Hexxs_cereal(restart_HR_path_cereal, *Hexxd);
250+
}
251+
else {
252+
ModuleIO::read_Hexxs_cereal(restart_HR_path_cereal, *Hexxc);
253+
}
236254
}
237255
}
238256
}

source/module_io/test/read_input_ptest.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ TEST_F(InputParaTest, ParaRead)
448448

449449
// comment out this part of tests, since Parameter is in another directory now, mohan 2025-05-18
450450
// besides, the following tests will cause strange error in MPI_Finalize()
451+
// I tried the following modification, it worked well in my own environment, but not in the Github test, Xinyuan 2025-05-25
451452
/*
452453
TEST_F(InputParaTest, Check)
453454
{
@@ -458,20 +459,40 @@ TEST_F(InputParaTest, Check)
458459
emptyfile << "stru_file ./support/STRU \n";
459460
emptyfile.close();
460461
}
462+
MPI_Barrier(MPI_COMM_WORLD);
463+
464+
bool original_check_mode = ModuleIO::ReadInput::check_mode;
461465
ModuleIO::ReadInput::check_mode = true;
462466
ModuleIO::ReadInput readinput(GlobalV::MY_RANK);
463467
464-
//
465468
Parameter param;
466469
testing::internal::CaptureStdout();
467-
EXPECT_EXIT(readinput.read_parameters(param, "./empty_INPUT"), ::testing::ExitedWithCode(0), "");
468-
std::string output = testing::internal::GetCapturedStdout();
469-
EXPECT_THAT(output, testing::HasSubstr("INPUT parameters have been successfully checked!"));
470-
//
470+
try {
471+
readinput.read_parameters(param, "./empty_INPUT");
472+
473+
// if exit normally with exit(0)
474+
std::string output = testing::internal::GetCapturedStdout();
475+
EXPECT_THAT(output, testing::HasSubstr("INPUT parameters have been successfully checked!"));
476+
477+
} catch (const std::exception& e) {
478+
// if exit with error, then the test is failed
479+
std::cerr << "Rank " << GlobalV::MY_RANK << " error: " << e.what() << std::endl;
480+
MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
481+
} catch (...) {
482+
// if exit with unknown error, then the test is failed
483+
std::cerr << "Rank " << GlobalV::MY_RANK << " unknown error." << std::endl;
484+
MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
485+
}
486+
// Note : the EXPECT_EXIT is not working with MPI, so we use try-catch to test the exit
487+
// EXPECT_EXIT(readinput.read_parameters(param, "./empty_INPUT"), ::testing::ExitedWithCode(0), "");
488+
// std::string output = testing::internal::GetCapturedStdout();
489+
// EXPECT_THAT(output, testing::HasSubstr("INPUT parameters have been successfully checked!"));
490+
MPI_Barrier(MPI_COMM_WORLD);
471491
if (GlobalV::MY_RANK == 0)
472492
{
473493
EXPECT_TRUE(std::remove("./empty_INPUT") == 0);
474494
}
495+
ModuleIO::ReadInput::check_mode = original_check_mode;
475496
}
476497
*/
477498

0 commit comments

Comments
 (0)