Skip to content

Commit 4640fbf

Browse files
zgn-26714dyzheng
authored andcommitted
fix parallel bug
1 parent 0a799ba commit 4640fbf

File tree

4 files changed

+82
-104
lines changed

4 files changed

+82
-104
lines changed

source/module_hamilt_lcao/module_gint/gint.cpp

Lines changed: 4 additions & 6 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->hR_tmp;
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,9 @@ 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->hR_tmp != nullptr) {
159+
delete this->hR_tmp;
160+
}
163161
for (int is = 0; is < nspin; is++) {
164162
if (this->DMRGint[is] != nullptr) {
165163
delete this->DMRGint[is];

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>>* hR_tmp = 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: 75 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,10 @@ 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();
82-
83-
#ifdef __MPI
8481
int mg = hR->get_paraV()->get_global_row_size()/2;
8582
int ng = hR->get_paraV()->get_global_col_size()/2;
8683
int nb = hR->get_paraV()->get_block_size()/2;
84+
#ifdef __MPI
8785
int blacs_ctxt = hR->get_paraV()->blacs_ctxt;
8886
int *iat2iwt = new int[ucell_in->nat];
8987
for (int iat = 0; iat < ucell_in->nat; iat++) {
@@ -93,119 +91,64 @@ void Gint_k::transfer_pvpR(hamilt::HContainer<std::complex<double>>* hR,
9391
pv->set(mg, ng, nb, blacs_ctxt);
9492
pv->set_atomic_trace(iat2iwt, ucell_in->nat, mg);
9593
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-
11194

95+
this->hR_tmp = new hamilt::HContainer<std::complex<double>>(pv, nullptr, &ijr_info);
96+
ModuleBase::Memory::record("Gint::hRGintCd", this->hR_tmp->get_memory_size());
11297

98+
//0,3;1,2;1,2;0,3
99+
std::vector<int> first = {0, 1, 1, 0};
100+
std::vector<int> second= {3, 2, 2, 3};
113101
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++)
102+
this->hR_tmp->set_zero();
103+
hamilt::HContainer<std::complex<double>>* hRGint_tmpCd = new hamilt::HContainer<std::complex<double>>(this->ucell->nat);
104+
hRGint_tmpCd->insert_ijrs(this->gridt->get_ijr_info(), *(this->ucell));
105+
hRGint_tmpCd->allocate(nullptr, true);
106+
hRGint_tmpCd->set_zero();
107+
for (int iap = 0; iap < hRGint_tmpCd->size_atom_pairs(); iap++)
118108
{
119109
//std::cout<<"iap: "<<iap<<std::endl;
120-
auto* ap = &hR->get_atom_pair(iap);
110+
auto* ap = &hRGint_tmpCd->get_atom_pair(iap);
121111
const int iat1 = ap->get_atom_i();
122112
const int iat2 = ap->get_atom_j();
123-
const hamilt::AtomPair<double>* ap_nspin = nullptr;
124113
if (iat1 <= iat2)
125114
{
126115
hamilt::AtomPair<std::complex<double>>* upper_ap = ap;
127-
hamilt::AtomPair<std::complex<double>>* lower_ap = hR->find_pair(iat2, iat1);
128-
switch (is)
129-
{
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;
136-
}
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);
116+
hamilt::AtomPair<std::complex<double>>* lower_ap = hRGint_tmpCd->find_pair(iat2, iat1);
117+
const hamilt::AtomPair<double>* ap_nspin1 = this->hRGint_tmp[first[is]] ->find_pair(iat1, iat2);
118+
const hamilt::AtomPair<double>* ap_nspin2 = this->hRGint_tmp[second[is]] ->find_pair(iat1, iat2);
140119
for (int ir = 0; ir < upper_ap->get_R_size(); ir++)
141120
{
121+
//std::cout<<"ir"<<ir<<std::endl;
142122
const auto R_index = upper_ap->get_R_index(ir);
143123
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);
147-
124+
auto mat_nspin1 = ap_nspin1->find_matrix(R_index);
125+
auto mat_nspin2 = ap_nspin2->find_matrix(R_index);
148126
// 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)
127+
for (int irow = 0; irow < mat_nspin1->get_row_size(); ++irow)
150128
{
151-
for (int icol = 0; icol < mat_nspin->get_col_size(); ++icol)
129+
for (int icol = 0; icol < mat_nspin1->get_col_size(); ++icol)
152130
{
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);
155131
switch (is)
156132
{
157133
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);
134+
upper_mat->get_value(irow, icol) = mat_nspin1->get_value(irow, icol) + mat_nspin2->get_value(irow, icol);
135+
break;
136+
case 1:
137+
upper_mat->get_value(irow, icol) = mat_nspin1->get_value(irow, icol) +
138+
std::complex<double>(0.0, 1.0) * mat_nspin2->get_value(irow, icol);
139+
break;
140+
case 2:
141+
upper_mat->get_value(irow, icol) = mat_nspin1->get_value(irow, icol) -
142+
std::complex<double>(0.0, 1.0) * mat_nspin2->get_value(irow, icol);
160143
break;
161144
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);
145+
upper_mat->get_value(irow, icol) = mat_nspin1->get_value(irow, icol) - mat_nspin2->get_value(irow, icol);
164146
break;
165147
}
166148
}
167149
}
168-
169-
if (PARAM.globalv.domag)
170-
{
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)
187-
{
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-
}
204-
}
205-
}
206-
207-
// fill the lower triangle matrix
208-
if(is == 3){
150+
//fill the lower triangle matrix
151+
if (PARAM.globalv.domag){
209152
if (iat1 < iat2)
210153
{
211154
auto lower_mat = lower_ap->find_matrix(-R_index);
@@ -218,15 +161,52 @@ void Gint_k::transfer_pvpR(hamilt::HContainer<std::complex<double>>* hR,
218161
}
219162
}
220163
}
221-
// std::cout<<"end"<<std::endl;
222164
}
223165
}
224166
}
225-
167+
168+
//std::cout<<"success"<<std::endl;
169+
170+
hamilt::transferSerials2Parallels( *hRGint_tmpCd, this->hR_tmp);
171+
for (int iap = 0; iap < hR->size_atom_pairs(); iap++)
172+
{
173+
//std::cout<<"iap: "<<iap<<std::endl;
174+
auto* ap = &hR->get_atom_pair(iap);
175+
const int iat1 = ap->get_atom_i();
176+
const int iat2 = ap->get_atom_j();
177+
auto* ap_nspin = this->hR_tmp ->find_pair(iat1, iat2);
178+
for (int ir = 0; ir < ap->get_R_size(); ir++)
179+
{
180+
const auto R_index = ap->get_R_index(ir);
181+
auto upper_mat = ap->find_matrix(R_index);
182+
auto mat_nspin = ap_nspin->find_matrix(R_index);
183+
184+
// The row size and the col size of upper_matrix is double that of matrix_nspin_0
185+
for (int irow = 0; irow < mat_nspin->get_row_size(); ++irow)
186+
{
187+
for (int icol = 0; icol < mat_nspin->get_col_size(); ++icol)
188+
{
189+
switch (is)
190+
{
191+
case 0:
192+
upper_mat->get_value(2*irow, 2*icol) += mat_nspin->get_value(irow, icol);
193+
break;
194+
case 1:
195+
upper_mat->get_value(2*irow, 2*icol+1) += mat_nspin->get_value(irow, icol);
196+
break;
197+
case 2:
198+
upper_mat->get_value(2*irow+1, 2*icol) += mat_nspin->get_value(irow, icol);
199+
break;
200+
case 3:
201+
upper_mat->get_value(2*irow+1, 2*icol+1) += mat_nspin->get_value(irow, icol);
202+
break;
203+
}
204+
}
205+
}
206+
}
207+
}
226208
}
227209
delete[] iat2iwt;
228-
delete pv;
229-
delete hR_tmp;
230210
#else
231211

232212
#endif
@@ -246,7 +226,7 @@ void Gint_k::transfer_pvpR(hamilt::HContainer<std::complex<double>>* hR,
246226
// hamilt::transferSerials2Parallels<std::complex<double>>(*this->hRGintCd, hR);
247227
// }
248228
// #else
249-
// hR->add(*this->hRGintCd);
229+
// hR->add(*this->hRGintCd);
250230
// #endif
251231
ModuleBase::timer::tick("Gint_k", "transfer_pvpR");
252232
return;

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->hR_tmp = rhs.hR_tmp;
49+
rhs.hR_tmp = nullptr;
5050
for (int i = 0; i < this->DMRGint.size(); i++)
5151
{
5252
delete this->DMRGint[i];

0 commit comments

Comments
 (0)