Skip to content

Commit 2a40059

Browse files
authored
Perf: minor optimization in module_lr (#5435)
* fix the waste of memory in matsym * output to running_nscf.log for lr * remove a useless line in op_lr_diag
1 parent 05de419 commit 2a40059

File tree

6 files changed

+23
-39
lines changed

6 files changed

+23
-39
lines changed

source/module_io/read_input_item_system.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ void ReadInput::item_system()
107107
}
108108
{
109109
Input_Item item("esolver_type");
110-
item.annotation = "the energy solver: ksdft, sdft, ofdft, tddft, lj, dp";
110+
item.annotation = "the energy solver: ksdft, sdft, ofdft, tddft, lj, dp, ks-lr, lr";
111111
read_sync_string(input.esolver_type);
112112
item.check_value = [](const Input_Item& item, const Parameter& para) {
113113
const std::vector<std::string> esolver_types = { "ksdft", "sdft", "ofdft", "tddft", "lj", "dp", "lr", "ks-lr" };
@@ -124,6 +124,12 @@ void ReadInput::item_system()
124124
}
125125
}
126126
};
127+
item.reset_value = [](const Input_Item& item, Parameter& para) {
128+
if (para.input.esolver_type == "lr" && para.input.calculation == "scf")
129+
{ // for LR-only calculation based on the ground-state, set calculation to "nscf"
130+
para.input.calculation = "nscf";
131+
}
132+
};
127133
this->add_item(item);
128134
}
129135
{

source/module_io/test_serial/read_input_item_test.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ TEST_F(InputTest, Item_test)
7979
EXPECT_EXIT(it->second.check_value(it->second, param), ::testing::ExitedWithCode(0), "");
8080
output = testing::internal::GetCapturedStdout();
8181
EXPECT_THAT(output, testing::HasSubstr("NOTICE"));
82+
83+
param.input.esolver_type = "lr";
84+
param.input.calculation = "scf";
85+
it = find_label("esolver_type", readinput.input_lists);
86+
it->second.reset_value(it->second, param);
87+
EXPECT_EQ(param.input.calculation, "nscf");
8288
}
8389
{ // nspin
8490
auto it = find_label("nspin", readinput.input_lists);

source/module_lr/esolver_lrtd_lcao.cpp

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,6 @@ void LR::ESolver_LR<double>::move_exx_lri(std::shared_ptr<Exx_LRI<std::complex<d
4646
template<>void LR::ESolver_LR<double>::set_gint() { this->gint_ = &this->gint_g_;this->gint_g_.gridt = &this->gt_; }
4747
template<>void LR::ESolver_LR<std::complex<double>>::set_gint() { this->gint_ = &this->gint_k_; this->gint_k_.gridt = &this->gt_; }
4848

49-
inline double getreal(std::complex<double> x) { return x.real(); }
50-
inline double getreal(double x) { return x; }
51-
52-
inline void redirect_log(const bool& out_alllog)
53-
{
54-
GlobalV::ofs_running.close();
55-
std::stringstream ss;
56-
if (out_alllog)
57-
{
58-
ss << PARAM.globalv.global_out_dir << "running_lr_" << GlobalV::MY_RANK + 1 << ".log";
59-
GlobalV::ofs_running.open(ss.str());
60-
}
61-
else
62-
{
63-
if (GlobalV::MY_RANK == 0)
64-
{
65-
ss << PARAM.globalv.global_out_dir << "running_lr.log";
66-
GlobalV::ofs_running.open(ss.str());
67-
}
68-
}
69-
}
70-
7149
inline int cal_nupdown_form_occ(const ModuleBase::matrix& wg)
7250
{ // only for nspin=2
7351
const int& nk = wg.nr / 2;
@@ -155,7 +133,6 @@ LR::ESolver_LR<T, TR>::ESolver_LR(ModuleESolver::ESolver_KS_LCAO<T, TR>&& ks_sol
155133
, exx_info(GlobalC::exx_info)
156134
#endif
157135
{
158-
redirect_log(inp.out_alllog);
159136
ModuleBase::TITLE("ESolver_LR", "ESolver_LR(KS)");
160137

161138
if (this->input.lr_solver == "spectrum") {
@@ -259,7 +236,6 @@ LR::ESolver_LR<T, TR>::ESolver_LR(const Input_para& inp, UnitCell& ucell) : inpu
259236
, exx_info(GlobalC::exx_info)
260237
#endif
261238
{
262-
redirect_log(inp.out_alllog);
263239
ModuleBase::TITLE("ESolver_LR", "ESolver_LR(from scratch)");
264240
// xc kernel
265241
this->xc_kernel = inp.xc_kernel;
@@ -431,6 +407,7 @@ template <typename T, typename TR>
431407
void LR::ESolver_LR<T, TR>::runner(int istep, UnitCell& cell)
432408
{
433409
ModuleBase::TITLE("ESolver_LR", "runner");
410+
ModuleBase::timer::tick("ESolver_LR", "runner");
434411
//allocate 2-particle state and setup 2d division
435412
this->setup_eigenvectors_X();
436413
this->pelec->ekb.create(nspin, this->nstates);
@@ -494,6 +471,7 @@ void LR::ESolver_LR<T, TR>::runner(int istep, UnitCell& cell)
494471
for (int is = 0;is < nspin;++is) { read_states(spin_types[is], this->pelec->ekb.c + is * nstates, this->X[is].template data<T>(), nloc_per_band, nstates); }
495472
}
496473
}
474+
ModuleBase::timer::tick("ESolver_LR", "runner");
497475
return;
498476
}
499477

source/module_lr/operator_casida/operator_lr_diag.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ namespace LR
4646
const bool is_first_node = false)const override
4747
{
4848
ModuleBase::TITLE("OperatorLRDiag", "act");
49-
const int nlocal_ph = nk * pX.get_local_size(); // local size of particle-hole basis
5049
hsolver::vector_mul_vector_op<T, Device>()(this->ctx,
51-
nk * pX.get_local_size(),
50+
nk * pX.get_local_size(), // local size of particle-hole basis
5251
hpsi,
5352
psi_in,
5453
this->eig_ks_diff.c);

source/module_lr/utils/lr_util.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,37 +62,33 @@ namespace LR_Util
6262
template<>
6363
void matsym<double>(const double* in, const int n, const Parallel_2D& pmat, double* out)
6464
{
65-
for (int i = 0;i < pmat.get_local_size();++i) {out[i] = in[i];
66-
}
65+
for (int i = 0;i < pmat.get_local_size();++i) { out[i] = in[i]; }
6766
const double alpha = 0.5, beta = 0.5;
6867
const int i1 = 1;
6968
pdtran_(&n, &n, &alpha, in, &i1, &i1, pmat.desc, &beta, out, &i1, &i1, pmat.desc);
7069
}
7170
template<>
7271
void matsym<double>(double* inout, const int n, const Parallel_2D& pmat)
7372
{
74-
std::vector<double> tmp(n * n);
75-
for (int i = 0;i < pmat.get_local_size();++i) {tmp[i] = inout[i];
76-
}
73+
std::vector<double> tmp(pmat.get_local_size());
74+
std::copy(inout, inout + pmat.get_local_size(), tmp.begin());
7775
const double alpha = 0.5, beta = 0.5;
7876
const int i1 = 1;
7977
pdtran_(&n, &n, &alpha, tmp.data(), &i1, &i1, pmat.desc, &beta, inout, &i1, &i1, pmat.desc);
8078
}
8179
template<>
8280
void matsym<std::complex<double>>(const std::complex<double>* in, const int n, const Parallel_2D& pmat, std::complex<double>* out)
8381
{
84-
for (int i = 0;i < pmat.get_local_size();++i) {out[i] = in[i];
85-
}
82+
for (int i = 0;i < pmat.get_local_size();++i) { out[i] = in[i]; }
8683
const std::complex<double> alpha(0.5, 0.0), beta(0.5, 0.0);
8784
const int i1 = 1;
8885
pztranc_(&n, &n, &alpha, in, &i1, &i1, pmat.desc, &beta, out, &i1, &i1, pmat.desc);
8986
}
9087
template<>
9188
void matsym<std::complex<double>>(std::complex<double>* inout, const int n, const Parallel_2D& pmat)
9289
{
93-
std::vector<std::complex<double>> tmp(n * n);
94-
for (int i = 0;i < pmat.get_local_size();++i) {tmp[i] = inout[i];
95-
}
90+
std::vector<std::complex<double>> tmp(pmat.get_local_size());
91+
std::copy(inout, inout + pmat.get_local_size(), tmp.begin());
9692
const std::complex<double> alpha(0.5, 0.0), beta(0.5, 0.0);
9793
const int i1 = 1;
9894
pztranc_(&n, &n, &alpha, tmp.data(), &i1, &i1, pmat.desc, &beta, inout, &i1, &i1, pmat.desc);

tests/integrate/tools/catch_properties.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,10 +536,9 @@ if ! test -z "$out_current" && [ $out_current ]; then
536536
fi
537537

538538
if [ $is_lr == 1 ]; then
539-
lr_path=OUT.autotest/running_lr.log
540539
lrns=$(get_input_key_value "lr_nstates" "INPUT")
541540
lrns1=`echo "$lrns + 1" |bc`
542-
grep -A$lrns1 "Excitation Energy" $lr_path | awk 'NR > 2 && $2 ~ /^[0-9]+\.[0-9]+$/ {print $2}' > lr_eig.txt
541+
grep -A$lrns1 "Excitation Energy" $running_path | awk 'NR > 2 && $2 ~ /^[0-9]+\.[0-9]+$/ {print $2}' > lr_eig.txt
543542
lreig_tot=`sum_file lr_eig.txt`
544543
echo "totexcitationenergyref $lreig_tot" >>$1
545544
fi

0 commit comments

Comments
 (0)