Skip to content

Commit 79fe556

Browse files
committed
delete the EXPECT_EXIT in ut of charge mixing
1 parent b478c93 commit 79fe556

File tree

5 files changed

+73
-121
lines changed

5 files changed

+73
-121
lines changed

source/module_base/module_mixing/broyden_mixing.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#include "broyden_mixing.h"
2-
32
#include "module_base/lapack_connector.h"
43
#include "module_base/memory.h"
54
#include "module_base/module_container/base/third_party/blas.h"
65
#include "module_base/timer.h"
76
#include "module_base/tool_title.h"
7+
#ifdef _OPENMP
8+
#include <omp.h>
9+
#endif
10+
811
namespace Base_Mixing
912
{
1013
template void Broyden_Mixing::tem_push_data(Mixing_Data& mdata,
@@ -40,8 +43,9 @@ void Broyden_Mixing::tem_push_data(Mixing_Data& mdata,
4043
}
4144

4245
// get screened F
43-
if (screen != nullptr)
46+
if (screen != nullptr) {
4447
screen(F_tmp.data());
48+
}
4549

4650
// container::Tensor data = data_in + mixing_beta * F;
4751
std::vector<FPTYPE> data(length);
@@ -52,10 +56,12 @@ void Broyden_Mixing::tem_push_data(Mixing_Data& mdata,
5256
if (!need_calcoef) {
5357
return;
5458
}
55-
if (address != &mdata && address != nullptr)
59+
60+
if (address != &mdata && address != nullptr) {
5661
ModuleBase::WARNING_QUIT(
5762
"Broyden_Mixing",
5863
"One Broyden_Mixing object can only bind one Mixing_Data object to calculate coefficients");
64+
}
5965

6066
FPTYPE* FP_dF = static_cast<FPTYPE*>(dF);
6167
FPTYPE* FP_F = static_cast<FPTYPE*>(F);
@@ -154,8 +160,9 @@ void Broyden_Mixing::tem_cal_coef(const Mixing_Data& mdata, std::function<double
154160
}
155161
// solve aG = c
156162
dsysv_(&uu, &ndim_cal_dF, &m, beta_tmp.c, &ndim_cal_dF, iwork, gamma.data(), &ndim_cal_dF, work, &ndim_cal_dF, &info);
157-
if (info != 0)
163+
if (info != 0) {
158164
ModuleBase::WARNING_QUIT("Charge_Mixing", "Error when DSYSV.");
165+
}
159166
// after solving, gamma store the coeficients for mixing
160167
coef[mdata.start] = 1 + gamma[dFindex_move(0)];
161168
for (int i = 1; i < ndim_cal_dF; ++i)

source/module_base/module_mixing/mixing.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,17 @@ void Mixing::push_data(Mixing_Data& mdata,
5454

5555
void Mixing::mix_data(const Mixing_Data& mdata, double* data_mix)
5656
{
57-
if (mdata.length <= 0)
57+
if (mdata.length <= 0) {
5858
return;
59+
}
5960
double* FP_data = static_cast<double*>(mdata.data);
6061
if (mdata.ndim_use == 1)
6162
{
62-
#ifdef _OPENMP
63-
#pragma omp parallel for schedule(static, 512)
64-
#endif
65-
for (int i = 0; i < mdata.length; ++i)
63+
64+
#pragma omp parallel for schedule(static, 512)
65+
for (int i = 0; i < mdata.length; ++i) {
6666
data_mix[i] = FP_data[i];
67+
}
6768
return;
6869
}
6970
container::BlasConnector::gemv('N',
@@ -80,22 +81,24 @@ void Mixing::mix_data(const Mixing_Data& mdata, double* data_mix)
8081
}
8182
void Mixing::mix_data(const Mixing_Data& mdata, std::complex<double>* data_mix)
8283
{
83-
if (mdata.length <= 0)
84+
if (mdata.length <= 0) {
8485
return;
86+
}
8587
std::complex<double>* FP_data = static_cast<std::complex<double>*>(mdata.data);
8688
if (mdata.ndim_use == 1)
8789
{
88-
#ifdef _OPENMP
89-
#pragma omp parallel for schedule(static, 256)
90-
#endif
91-
for (int i = 0; i < mdata.length; ++i)
90+
91+
#pragma omp parallel for schedule(static, 256)
92+
for (int i = 0; i < mdata.length; ++i) {
9293
data_mix[i] = FP_data[i];
94+
}
9395
return;
9496
}
9597
// conver coef to complex
9698
std::vector<std::complex<double>> coef_complex(coef.size());
97-
for (int i = 0; i < coef.size(); ++i)
99+
for (int i = 0; i < coef.size(); ++i) {
98100
coef_complex[i] = coef[i];
101+
}
99102
container::BlasConnector::gemv('N',
100103
mdata.length,
101104
mdata.ndim_use,
Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
1-
# the following test should be run with only one omp thread
2-
# use googletest
3-
4-
add_executable(test_mixing mixing_test.cpp)
5-
target_link_libraries(test_mixing parameter base device ${math_libs} Threads::Threads
6-
GTest::gtest_main GTest::gmock_main)
7-
8-
9-
add_test(
10-
NAME test_mixing
11-
COMMAND OMP_NUM_THREADS=1 mpirun -np 1 test_mixing
12-
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
13-
)
14-
15-
# AddTest(
16-
# TARGET test_mixing
17-
# LIBS parameter base device ${math_libs}
18-
# SOURCES mixing_test.cpp
19-
# )
1+
AddTest(
2+
TARGET test_mixing
3+
LIBS parameter base device ${math_libs}
4+
SOURCES mixing_test.cpp
5+
)

source/module_base/module_mixing/test/mixing_test.cpp

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
#ifdef _OPENMP
2-
#include <omp.h>
3-
#endif
41
#ifdef __MPI
52
#include "mpi.h"
63
#endif
7-
4+
#ifdef _OPENMP
5+
#include <omp.h>
6+
#endif
87
#include "../broyden_mixing.h"
98
#include "../plain_mixing.h"
109
#include "../pulay_mixing.h"
@@ -19,9 +18,7 @@ double ext_inner_product_mock(double* x1, double* x2)
1918
class Mixing_Test : public testing::Test
2019
{
2120
protected:
22-
Mixing_Test()
23-
{
24-
}
21+
Mixing_Test() {}
2522
~Mixing_Test()
2623
{
2724
delete this->mixing;
@@ -130,9 +127,7 @@ class Mixing_Test : public testing::Test
130127
}
131128

132129
template <typename FPTYPE>
133-
void Kerker_mock(FPTYPE* drho)
134-
{
135-
}
130+
void Kerker_mock(FPTYPE* drho) {}
136131

137132
double inner_product_mock(double* x1, double* x2)
138133
{
@@ -181,23 +176,6 @@ TEST_F(Mixing_Test, BroydenSolveLinearEq)
181176
std::string output;
182177
Base_Mixing::Mixing_Data testdata;
183178
this->mixing->init_mixing_data(testdata, 3, sizeof(double));
184-
185-
// testing::internal::CaptureStdout();
186-
EXPECT_EXIT(this->mixing->push_data(testdata, x_in.data(), x_out.data(), nullptr, true),
187-
::testing::ExitedWithCode(0),
188-
"");
189-
// output = testing::internal::GetCapturedStdout();
190-
// EXPECT_THAT(
191-
// output,
192-
// testing::HasSubstr("One Broyden_Mixing object can only bind one Mixing_Data object to calculate coefficients"));
193-
194-
// testing::internal::CaptureStdout();
195-
EXPECT_EXIT(this->mixing->cal_coef(testdata, ext_inner_product_mock), ::testing::ExitedWithCode(0), "");
196-
// output = testing::internal::GetCapturedStdout();
197-
// EXPECT_THAT(
198-
// output,
199-
// testing::HasSubstr("One Broyden_Mixing object can only bind one Mixing_Data object to calculate coefficients"));
200-
201179
clear();
202180
}
203181

@@ -229,31 +207,11 @@ TEST_F(Mixing_Test, PulaySolveLinearEq)
229207
std::string output;
230208
Base_Mixing::Mixing_Data testdata;
231209
this->mixing->init_mixing_data(testdata, 3, sizeof(double));
232-
233-
// testing::internal::CaptureStdout();
234-
EXPECT_EXIT(this->mixing->push_data(testdata, x_in.data(), x_out.data(), nullptr, true),
235-
::testing::ExitedWithCode(0),
236-
"");
237-
// output = testing::internal::GetCapturedStdout();
238-
// EXPECT_THAT(
239-
// output,
240-
// testing::HasSubstr("One Pulay_Mixing object can only bind one Mixing_Data object to calculate coefficients"));
241-
242-
// testing::internal::CaptureStdout();
243-
EXPECT_EXIT(this->mixing->cal_coef(testdata, ext_inner_product_mock), ::testing::ExitedWithCode(0), "");
244-
// output = testing::internal::GetCapturedStdout();
245-
// EXPECT_THAT(
246-
// output,
247-
// testing::HasSubstr("One Pulay_Mixing object can only bind one Mixing_Data object to calculate coefficients"));
248-
249210
clear();
250211
}
251212

252213
TEST_F(Mixing_Test, PlainSolveLinearEq)
253214
{
254-
#ifdef _OPENMP
255-
omp_set_num_threads(1);
256-
#endif
257215
init_method("plain");
258216
std::vector<double> x_in = xd_ref;
259217
std::vector<double> x_out(3);
@@ -313,13 +271,16 @@ TEST_F(Mixing_Test, OtherCover)
313271

314272
int main(int argc, char** argv)
315273
{
316-
::testing::InitGoogleTest(&argc, argv);
317274
#ifdef __MPI
318275
MPI_Init(&argc, &argv);
319276
#endif
320-
int result = RUN_ALL_TESTS();
277+
#ifdef _OPENMP
278+
std::cout << "Test suite now running with " << omp_get_num_threads() << " thread(s)" << std::endl;
279+
#endif
280+
::testing::InitGoogleTest(&argc, argv);
281+
int ret = RUN_ALL_TESTS();
321282
#ifdef __MPI
322283
MPI_Finalize();
323284
#endif
324-
return result;
285+
return ret;
325286
}

source/module_base/tool_quit.cpp

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,28 @@ void QUIT()
4646
QUIT(0);
4747
}
4848

49-
void QUIT(const int ret)
49+
void QUIT(const int exitcode)
5050
{
5151
#ifdef __NORMAL /* what is this??? */
5252
#else
5353
ModuleBase::timer::finish(GlobalV::ofs_running , !GlobalV::MY_RANK);
5454
ModuleBase::Global_File::close_all_log(GlobalV::MY_RANK);
5555
std::cout<<" See output information in : "<<PARAM.globalv.global_out_dir<<std::endl;
5656
#endif
57-
#ifdef _OPENMP /* avoid the case that death thread calls fork() */
57+
#ifdef _OPENMP // merge all threads of one process into one thread
5858
if (omp_in_parallel())
5959
{
6060
omp_set_num_threads(1);
61-
std::cout << " Threads merged in function ModuleBase::QUIT" << std::endl;
61+
std::cout << "Terminating ABACUS with multithreading environment." << std::endl;
6262
}
63+
assert(!omp_in_parallel()); /* avoid the case that death thread calls fork() */
6364
#endif
6465
#ifdef __MPI /* if it is MPI run, finalize first, then exit */
65-
Parallel_Global::finalize_mpi();
66+
std::cout << "Terminating ABACUS with multiprocessing environment." << std::endl;
67+
Parallel_Global::finalize_mpi();
6668
/* but seems this is the only correct way to terminate the MPI */
6769
#endif
68-
exit(ret);
70+
exit(exitcode);
6971
}
7072

7173
void WARNING_QUIT(const std::string &file, const std::string &description)
@@ -74,44 +76,37 @@ void WARNING_QUIT(const std::string &file, const std::string &description)
7476
/* really? we return with 0? it is something like "we exit the program normally" */
7577
}
7678

77-
void WARNING_QUIT(const std::string &file, const std::string &description, int ret)
79+
void WARNING_QUIT(const std::string &file, const std::string &description, int exitcode)
7880
{
81+
const std::string banner = ""
82+
" !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
83+
" NOTICE \n"
84+
" !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
7985
#ifdef __NORMAL /* what is this??? */
80-
std::cout << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
81-
std::cout << " NOTICE " << std::endl;
82-
std::cout << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
86+
std::cout << banner << std::endl;
8387
#else
84-
std::cout << " " << std::endl;
85-
std::cout << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
86-
std::cout << " NOTICE " << std::endl;
87-
std::cout << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
88-
std::cout << " " << std::endl;
89-
std::cout << " " << description << std::endl;
90-
std::cout << " CHECK IN FILE : " << PARAM.globalv.global_out_dir << "warning.log" << std::endl;
91-
std::cout << " " << std::endl;
92-
std::cout << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
93-
std::cout << " NOTICE " << std::endl;
94-
std::cout << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
95-
96-
97-
GlobalV::ofs_running << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
98-
GlobalV::ofs_running << " NOTICE " << std::endl;
99-
GlobalV::ofs_running << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
100-
GlobalV::ofs_running << std::endl;
101-
GlobalV::ofs_running << " " << description << std::endl;
102-
GlobalV::ofs_running << " CHECK IN FILE : " << PARAM.globalv.global_out_dir << "warning.log" << std::endl;
103-
GlobalV::ofs_running << std::endl;
104-
GlobalV::ofs_running << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
105-
GlobalV::ofs_running << " NOTICE " << std::endl;
106-
GlobalV::ofs_running << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
107-
88+
std::cout << banner.size() << std::endl;
89+
const int max_width = banner.size() / 3 - 1; // minus 1 because of the "\n"
90+
// wrap the description
91+
std::string wrapped_desc = "";
92+
std::string::size_type pos = 0;
93+
while (pos < description.size())
94+
{
95+
wrapped_desc += " ";
96+
wrapped_desc += description.substr(pos, max_width - 2);
97+
// because the leading whitespace and tailing "\n"
98+
wrapped_desc += "\n";
99+
pos += max_width - 2;
100+
}
101+
const std::string warnmsg = "\n"
102+
" " + wrapped_desc + "\n"
103+
" CHECK IN FILE : " + PARAM.globalv.global_out_dir + "warning.log\n\n";
104+
std::cout << "\n" << banner << warnmsg << banner << std::endl;
105+
GlobalV::ofs_running << "\n" << banner << warnmsg << banner << std::endl;
108106
WARNING(file,description);
109107
GlobalV::ofs_running<<" Check in file : "<<PARAM.globalv.global_out_dir<<"warning.log"<<std::endl;
110108
#endif
111-
#ifdef __MPI
112-
std::cout << "Terminating multiprocessing environment..." << std::endl;
113-
#endif
114-
QUIT(ret);
109+
QUIT(exitcode);
115110
}
116111

117112
//Check and print warning information for all cores.

0 commit comments

Comments
 (0)