Skip to content

Commit 0a799ba

Browse files
zgn-26714dyzheng
authored andcommitted
delete tem hRGintCd to reduce memory usage
1 parent 07a9a23 commit 0a799ba

File tree

4 files changed

+161
-71
lines changed

4 files changed

+161
-71
lines changed

source/module_hamilt_lcao/module_gint/gint.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
Gint::~Gint() {
2424

2525
delete this->hRGint;
26-
delete this->hRGintCd;
26+
//delete this->hRGintCd;
2727
// in gamma_only case, DMRGint.size()=0,
2828
// in multi-k case, DMRGint.size()=nspin
2929
for (int is = 0; is < this->DMRGint.size(); is++) {
@@ -155,11 +155,11 @@ void Gint::initialize_pvpR(const UnitCell& ucell_in, const Grid_Driver* gd, cons
155155
this->hRGint = new hamilt::HContainer<double>(ucell_in.nat);
156156
} else {
157157
npol = 2;
158-
if (this->hRGintCd != nullptr) {
159-
delete this->hRGintCd;
160-
}
161-
this->hRGintCd
162-
= new hamilt::HContainer<std::complex<double>>(ucell_in.nat);
158+
// if (this->hRGintCd != nullptr) {
159+
// delete this->hRGintCd;
160+
// }
161+
// this->hRGintCd
162+
// = new hamilt::HContainer<std::complex<double>>(ucell_in.nat);
163163
for (int is = 0; is < nspin; is++) {
164164
if (this->DMRGint[is] != nullptr) {
165165
delete this->DMRGint[is];
@@ -196,10 +196,10 @@ void Gint::initialize_pvpR(const UnitCell& ucell_in, const Grid_Driver* gd, cons
196196
this->DMRGint[0]->get_memory_size()
197197
* this->DMRGint.size());
198198
} else {
199-
this->hRGintCd->insert_ijrs(this->gridt->get_ijr_info(), ucell_in, npol);
200-
this->hRGintCd->allocate(nullptr, true);
201-
ModuleBase::Memory::record("Gint::hRGintCd",
202-
this->hRGintCd->get_memory_size());
199+
// this->hRGintCd->insert_ijrs(this->gridt->get_ijr_info(), ucell_in, npol);
200+
// this->hRGintCd->allocate(nullptr, true);
201+
// ModuleBase::Memory::record("Gint::hRGintCd",
202+
// this->hRGintCd->get_memory_size());
203203
for(int is = 0; is < nspin; is++) {
204204
this->hRGint_tmp[is]->insert_ijrs(this->gridt->get_ijr_info(), ucell_in);
205205
this->DMRGint[is]->insert_ijrs(this->gridt->get_ijr_info(), ucell_in);

source/module_hamilt_lcao/module_gint/gint.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ class Gint {
258258
std::vector<hamilt::HContainer<double>*> hRGint_tmp;
259259

260260
//! stores Hamiltonian in sparse format
261-
hamilt::HContainer<std::complex<double>>* hRGintCd = nullptr;
261+
//hamilt::HContainer<std::complex<double>>* hRGintCd = nullptr;
262262

263263
//! stores DMR in sparse format
264264
std::vector<hamilt::HContainer<double>*> DMRGint;

source/module_hamilt_lcao/module_gint/gint_k_pvpr.cpp

Lines changed: 148 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -78,86 +78,176 @@ void Gint_k::transfer_pvpR(hamilt::HContainer<std::complex<double>>* hR,
7878
ModuleBase::TITLE("Gint_k", "transfer_pvpR");
7979
ModuleBase::timer::tick("Gint_k", "transfer_pvpR");
8080

81-
this->hRGintCd->set_zero();
81+
//this->hRGintCd->set_zero();
8282

83-
for (int iap = 0; iap < this->hRGintCd->size_atom_pairs(); iap++)
84-
{
85-
auto* ap = &this->hRGintCd->get_atom_pair(iap);
86-
const int iat1 = ap->get_atom_i();
87-
const int iat2 = ap->get_atom_j();
88-
if (iat1 <= iat2)
89-
{
90-
hamilt::AtomPair<std::complex<double>>* upper_ap = ap;
91-
hamilt::AtomPair<std::complex<double>>* lower_ap = this->hRGintCd->find_pair(iat2, iat1);
92-
const hamilt::AtomPair<double>* ap_nspin_0 = this->hRGint_tmp[0]->find_pair(iat1, iat2);
93-
const hamilt::AtomPair<double>* ap_nspin_3 = this->hRGint_tmp[3]->find_pair(iat1, iat2);
94-
for (int ir = 0; ir < upper_ap->get_R_size(); ir++)
95-
{
96-
const auto R_index = upper_ap->get_R_index(ir);
97-
auto upper_mat = upper_ap->find_matrix(R_index);
98-
auto mat_nspin_0 = ap_nspin_0->find_matrix(R_index);
99-
auto mat_nspin_3 = ap_nspin_3->find_matrix(R_index);
83+
#ifdef __MPI
84+
int mg = hR->get_paraV()->get_global_row_size()/2;
85+
int ng = hR->get_paraV()->get_global_col_size()/2;
86+
int nb = hR->get_paraV()->get_block_size()/2;
87+
int blacs_ctxt = hR->get_paraV()->blacs_ctxt;
88+
int *iat2iwt = new int[ucell_in->nat];
89+
for (int iat = 0; iat < ucell_in->nat; iat++) {
90+
iat2iwt[iat] = ucell_in->get_iat2iwt()[iat]/2;
91+
}
92+
Parallel_Orbitals *pv = new Parallel_Orbitals();
93+
pv->set(mg, ng, nb, blacs_ctxt);
94+
pv->set_atomic_trace(iat2iwt, ucell_in->nat, mg);
95+
auto ijr_info = hR->get_ijr_info();
96+
97+
98+
hamilt::HContainer<double>* hR_tmp = new hamilt::HContainer<double>(pv, nullptr, &ijr_info);
99+
ModuleBase::Memory::record("Gint::hRGintCd", hR_tmp->get_memory_size());
100+
// std::cout<<"test1"<<std::endl;
101+
102+
// std::cout<<hRGint_tmp[0]->size_atom_pairs()<<std::endl;
103+
// std::cout<<hR_tmp->size_atom_pairs()<<std::endl;
104+
// for(int i =0; i < hRGint_tmp[0]->size_atom_pairs(); i++){
105+
// std::cout<<"hRGint_tmp"<<hRGint_tmp[0]->get_atom_pair(i).get_row_size()<<' '<<hRGint_tmp[0]->get_atom_pair(i).get_row_size()<<std::endl;
106+
// std::cout<<"hR_tmp"<<hR_tmp->get_atom_pair(i).get_row_size()<<' '<<hR_tmp->get_atom_pair(i).get_row_size()<<std::endl;
107+
// std::cout<<"pv1:" << pv->get_indexes_col(0).size()<< ' '<<pv->get_indexes_row(0).size()<<std::endl;
108+
// std::cout<<"pv2:" << pv->get_indexes_col().size()<< ' '<<pv->get_indexes_row().size()<<std::endl;
109+
// }
110+
100111

101-
// The row size and the col size of upper_matrix is double that of matrix_nspin_0
102-
for (int irow = 0; irow < mat_nspin_0->get_row_size(); ++irow)
112+
113+
for (int is = 0; is < 4; is++){
114+
hR_tmp->set_zero();
115+
//std::cout<<"is: "<<is<<std::endl;
116+
hamilt::transferSerials2Parallels( *(this->hRGint_tmp[is]), hR_tmp);
117+
for (int iap = 0; iap < hR->size_atom_pairs(); iap++)
118+
{
119+
//std::cout<<"iap: "<<iap<<std::endl;
120+
auto* ap = &hR->get_atom_pair(iap);
121+
const int iat1 = ap->get_atom_i();
122+
const int iat2 = ap->get_atom_j();
123+
const hamilt::AtomPair<double>* ap_nspin = nullptr;
124+
if (iat1 <= iat2)
125+
{
126+
hamilt::AtomPair<std::complex<double>>* upper_ap = ap;
127+
hamilt::AtomPair<std::complex<double>>* lower_ap = hR->find_pair(iat2, iat1);
128+
switch (is)
103129
{
104-
for (int icol = 0; icol < mat_nspin_0->get_col_size(); ++icol)
105-
{
106-
upper_mat->get_value(2*irow, 2*icol) = mat_nspin_0->get_value(irow, icol) + mat_nspin_3->get_value(irow, icol);
107-
upper_mat->get_value(2*irow+1, 2*icol+1) = mat_nspin_0->get_value(irow, icol) - mat_nspin_3->get_value(irow, icol);
108-
}
130+
case 0:
131+
ap_nspin = hR_tmp->find_pair(iat1, iat2);
132+
break;
133+
case 3:
134+
ap_nspin = hR_tmp->find_pair(iat1, iat2);
135+
break;
109136
}
137+
if(ap_nspin == nullptr) break;
138+
//const hamilt::AtomPair<double>* ap_nspin_0 = this->hR_tmp[0]->find_pair(iat1, iat2);
139+
//const hamilt::AtomPair<double>* ap_nspin_3 = this->hRGint_tmp[3]->find_pair(iat1, iat2);
140+
for (int ir = 0; ir < upper_ap->get_R_size(); ir++)
141+
{
142+
const auto R_index = upper_ap->get_R_index(ir);
143+
auto upper_mat = upper_ap->find_matrix(R_index);
144+
//auto mat_nspin_0 = ap_nspin_0->find_matrix(R_index);
145+
//auto mat_nspin_3 = ap_nspin_3->find_matrix(R_index);
146+
auto mat_nspin = ap_nspin->find_matrix(R_index);
110147

111-
if (PARAM.globalv.domag)
112-
{
113-
const hamilt::AtomPair<double>* ap_nspin_1 = this->hRGint_tmp[1]->find_pair(iat1, iat2);
114-
const hamilt::AtomPair<double>* ap_nspin_2 = this->hRGint_tmp[2]->find_pair(iat1, iat2);
115-
const auto mat_nspin_1 = ap_nspin_1->find_matrix(R_index);
116-
const auto mat_nspin_2 = ap_nspin_2->find_matrix(R_index);
117-
for (int irow = 0; irow < mat_nspin_1->get_row_size(); ++irow)
148+
// The row size and the col size of upper_matrix is double that of matrix_nspin_0
149+
for (int irow = 0; irow < mat_nspin->get_row_size(); ++irow)
118150
{
119-
for (int icol = 0; icol < mat_nspin_1->get_col_size(); ++icol)
151+
for (int icol = 0; icol < mat_nspin->get_col_size(); ++icol)
120152
{
121-
upper_mat->get_value(2*irow, 2*icol+1) = mat_nspin_1->get_value(irow, icol) + std::complex<double>(0.0, 1.0) * mat_nspin_2->get_value(irow, icol);
122-
upper_mat->get_value(2*irow+1, 2*icol) = mat_nspin_1->get_value(irow, icol) - std::complex<double>(0.0, 1.0) * mat_nspin_2->get_value(irow, icol);
153+
//upper_mat->get_value(2*irow, 2*icol) = mat_nspin_0->get_value(irow, icol) + mat_nspin_3->get_value(irow, icol);
154+
//upper_mat->get_value(2*irow+1, 2*icol+1) = mat_nspin_0->get_value(irow, icol) - mat_nspin_3->get_value(irow, icol);
155+
switch (is)
156+
{
157+
case 0:
158+
upper_mat->get_value(2*irow, 2*icol) = mat_nspin->get_value(irow, icol);
159+
upper_mat->get_value(2*irow+1, 2*icol+1) = mat_nspin->get_value(irow, icol);
160+
break;
161+
case 3:
162+
upper_mat->get_value(2*irow, 2*icol) += mat_nspin->get_value(irow, icol);
163+
upper_mat->get_value(2*irow+1, 2*icol+1) -= mat_nspin->get_value(irow, icol);
164+
break;
165+
}
123166
}
124167
}
125-
}
126168

127-
// fill the lower triangle matrix
128-
if (iat1 < iat2)
129-
{
130-
auto lower_mat = lower_ap->find_matrix(-R_index);
131-
for (int irow = 0; irow < upper_mat->get_row_size(); ++irow)
169+
if (PARAM.globalv.domag)
132170
{
133-
for (int icol = 0; icol < upper_mat->get_col_size(); ++icol)
171+
const hamilt::AtomPair<double>* ap_nspin = nullptr;
172+
switch (is)
173+
{
174+
case 1:
175+
ap_nspin = hR_tmp->find_pair(iat1, iat2);
176+
break;
177+
case 2:
178+
ap_nspin = hR_tmp->find_pair(iat1, iat2);
179+
break;
180+
}
181+
//const hamilt::AtomPair<double>* ap_nspin_1 = this->hRGint_tmp[1]->find_pair(iat1, iat2);
182+
//const hamilt::AtomPair<double>* ap_nspin_2 = this->hRGint_tmp[2]->find_pair(iat1, iat2);
183+
//const auto mat_nspin_1 = ap_nspin_1->find_matrix(R_index);
184+
//const auto mat_nspin_2 = ap_nspin_2->find_matrix(R_index);
185+
const auto mat_nspin = ap_nspin->find_matrix(R_index);
186+
for (int irow = 0; irow < mat_nspin->get_row_size(); ++irow)
134187
{
135-
lower_mat->get_value(icol, irow) = conj(upper_mat->get_value(irow, icol));
188+
for (int icol = 0; icol < mat_nspin->get_col_size(); ++icol)
189+
{
190+
switch(is)
191+
{
192+
case 1:
193+
upper_mat->get_value(2*irow, 2*icol+1) = mat_nspin->get_value(irow, icol);
194+
upper_mat->get_value(2*irow+1, 2*icol) = mat_nspin->get_value(irow, icol);
195+
break;
196+
case 2:
197+
upper_mat->get_value(2*irow, 2*icol+1) += std::complex<double>(0.0, 1.0) * mat_nspin->get_value(irow, icol);
198+
upper_mat->get_value(2*irow+1, 2*icol) -= std::complex<double>(0.0, 1.0) * mat_nspin->get_value(irow, icol);
199+
break;
200+
}
201+
//upper_mat->get_value(2*irow, 2*icol+1) = mat_nspin->get_value(irow, icol) + std::complex<double>(0.0, 1.0) * mat_nspin_2->get_value(irow, icol);
202+
//upper_mat->get_value(2*irow+1, 2*icol) = mat_nspin->get_value(irow, icol) - std::complex<double>(0.0, 1.0) * mat_nspin_2->get_value(irow, icol);
203+
}
136204
}
137205
}
206+
207+
// fill the lower triangle matrix
208+
if(is == 3){
209+
if (iat1 < iat2)
210+
{
211+
auto lower_mat = lower_ap->find_matrix(-R_index);
212+
for (int irow = 0; irow < upper_mat->get_row_size(); ++irow)
213+
{
214+
for (int icol = 0; icol < upper_mat->get_col_size(); ++icol)
215+
{
216+
lower_mat->get_value(icol, irow) = conj(upper_mat->get_value(irow, icol));
217+
}
218+
}
219+
}
220+
}
221+
// std::cout<<"end"<<std::endl;
138222
}
139223
}
140224
}
225+
141226
}
227+
delete[] iat2iwt;
228+
delete pv;
229+
delete hR_tmp;
230+
#else
231+
232+
#endif
142233

143234
// ===================================
144235
// transfer HR from Gint to Veff<OperatorLCAO<std::complex<double>, std::complex<double>>>
145236
// ===================================
146-
#ifdef __MPI
147-
int size;
148-
MPI_Comm_size(MPI_COMM_WORLD, &size);
149-
if (size == 1)
150-
{
151-
hR->add(*this->hRGintCd);
152-
}
153-
else
154-
{
155-
hamilt::transferSerials2Parallels<std::complex<double>>(*this->hRGintCd, hR);
156-
}
157-
#else
158-
hR->add(*this->hRGintCd);
159-
#endif
160-
237+
// #ifdef __MPI
238+
// int size;
239+
// MPI_Comm_size(MPI_COMM_WORLD, &size);
240+
// if (size == 1)
241+
// {
242+
// hR->add(*this->hRGintCd);
243+
// }
244+
// else
245+
// {
246+
// hamilt::transferSerials2Parallels<std::complex<double>>(*this->hRGintCd, hR);
247+
// }
248+
// #else
249+
// hR->add(*this->hRGintCd);
250+
// #endif
161251
ModuleBase::timer::tick("Gint_k", "transfer_pvpR");
162252
return;
163253
}

source/module_lr/utils/gint_move.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ Gint& Gint::operator=(Gint&& rhs)
4545
// move hR after refactor
4646
this->hRGint = rhs.hRGint;
4747
rhs.hRGint = nullptr;
48-
this->hRGintCd = rhs.hRGintCd;
49-
rhs.hRGintCd = nullptr;
48+
// this->hRGintCd = rhs.hRGintCd;
49+
// rhs.hRGintCd = nullptr;
5050
for (int i = 0; i < this->DMRGint.size(); i++)
5151
{
5252
delete this->DMRGint[i];

0 commit comments

Comments
 (0)