Skip to content

Commit 16a941f

Browse files
authored
fix:bug segment fault caused by pzhegvx() (#2038)
* fix: reduce memory when maxniter > 0 * fix leak of memories; leak in scalapack have not be solved yet * fix bug caused by merge * fix part of leak of memories in cblacs * fixbug of scalapack * update results * fix segment fault
1 parent 8db3b40 commit 16a941f

File tree

11 files changed

+78
-53
lines changed

11 files changed

+78
-53
lines changed

source/module_base/scalapack_connector.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,18 @@ class ScalapackConnector
140140
void getrf(
141141
const int M, const int N,
142142
std::complex<double> *A, const int IA, const int JA, const int *DESCA,
143-
int *ipiv, int info)
143+
int *ipiv, int *info) //fix a bug: info is output and we must use int*
144144
{
145-
pzgetrf_(&M, &N, A, &IA, &JA, DESCA, ipiv, &info);
145+
pzgetrf_(&M, &N, A, &IA, &JA, DESCA, ipiv, info);
146146
}
147147

148148
static inline
149149
void getri(
150150
const int n,
151151
const std::complex<double> *A, const int ia, const int ja, const int *desca, int *ipiv,
152-
const std::complex<double> *work, const int *lwork, const int *iwork, const int *liwork, const int info)
152+
const std::complex<double> *work, const int *lwork, const int *iwork, const int *liwork, int *info)
153153
{
154-
pzgetri_(&n, A, &ia, &ja, desca, ipiv, work, lwork, iwork, liwork, &info);
154+
pzgetri_(&n, A, &ia, &ja, desca, ipiv, work, lwork, iwork, liwork, info);
155155
}
156156

157157
static inline

source/module_cell/module_neighbor/sltk_grid.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ Grid::Grid(const int &test_grid_in):test_grid(test_grid_in)
5757
Grid::~Grid()
5858
{
5959
delete[] atomlink;
60+
if (this->init_cell_flag)
61+
{
62+
for (int i = 0;i < this->dx;i++)
63+
{
64+
for (int j = 0;j < this->dy;j++)
65+
{
66+
delete[] this->Cell[i][j];
67+
}
68+
}
69+
70+
for (int i = 0;i < this->dx;i++)
71+
{
72+
delete[] this->Cell[i];
73+
}
74+
75+
delete[] this->Cell;
76+
this->init_cell_flag = false;
77+
}
6078

6179
}
6280

source/module_cell/module_neighbor/sltk_grid.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class Grid
5656

5757
// Constructors and destructor
5858
Grid(const int &test_grid_in);
59-
~Grid();
59+
virtual ~Grid();
6060

6161
void init(
6262
std::ofstream &ofs,

source/module_esolver/esolver_ks_lcao_tddft.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,12 @@ void ESolver_KS_LCAO_TDDFT::updatepot(const int istep, const int iter)
305305
this->psi_laststep
306306
= new psi::Psi<std::complex<double>>(GlobalC::kv.nks, GlobalV::NBANDS, GlobalV::NLOCAL, nullptr);
307307
#endif
308-
for (int ik = 0; ik < GlobalC::kv.nks; ++ik)
308+
309+
std::complex<double> *p_psi = &psi[0](0,0,0);
310+
std::complex<double> *p_psi_laststep = &psi_laststep[0](0,0,0);
311+
for (int index = 0; index < psi[0].size(); ++index)
309312
{
310-
psi->fix_k(ik);
311-
for (int index = 0; index < psi[0].size(); ++index)
312-
psi_laststep[0].get_pointer()[index] = psi[0].get_pointer()[index];
313+
p_psi_laststep[index] = p_psi[index];
313314
}
314315
if (istep > 1 && ELEC_evolve::td_edm == 0)
315316
this->cal_edm_tddft();

source/module_gint/grid_technique.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -400,12 +400,19 @@ void Grid_Technique::cal_grid_integration_index(void)
400400
{
401401
// save the start
402402
delete[] this->bcell_start;
403-
this->bcell_start = new int[nbxx];
404-
ModuleBase::Memory::record("GT::bcell_start", sizeof(int) * nbxx);
405-
this->bcell_start[0] = 0;
406-
for(int i=1; i<nbxx; i++)
403+
if(nbxx > 0)
404+
{
405+
this->bcell_start = new int[nbxx];
406+
ModuleBase::Memory::record("GT::bcell_start", sizeof(int) * nbxx);
407+
this->bcell_start[0] = 0;
408+
for(int i=1; i<nbxx; i++)
409+
{
410+
this->bcell_start[i] = this->bcell_start[i-1] + this->how_many_atoms[i-1];
411+
}
412+
}
413+
else
407414
{
408-
this->bcell_start[i] = this->bcell_start[i-1] + this->how_many_atoms[i-1];
415+
this->bcell_start = nullptr;
409416
}
410417
// calculate which grid has the largest number of atoms,
411418
// and how many atoms.

source/module_hamilt_lcao/hamilt_lcaodft/local_orbital_wfc.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,17 @@ Local_Orbital_wfc::~Local_Orbital_wfc()
1919
{
2020

2121
// used for k-points.
22-
if(complex_flag && this->wfck_flag)
22+
if(this->complex_flag)
2323
{
24+
delete[] this->wfc_k_grid2;
25+
}
26+
if(this->wfck_flag)
27+
{
2428
for(int i=0; i<GlobalC::kv.nks; i++)
2529
{
26-
//for(int j=0; j<GlobalV::NBANDS; j++)
27-
//{
28-
// delete[] this->wfc_k_grid[i][j];
29-
//}
3030
delete[] this->wfc_k_grid[i];
31-
//std::cout<<"delete wfc_k_grid["<<i<<"] success"<<std::endl;
3231
}
3332
delete[] this->wfc_k_grid;
34-
//std::cout<<"delete wfc_k_grid success"<<std::endl;
35-
if(GlobalV::NLOCAL!= 0 )
36-
{
37-
delete[] this->wfc_k_grid2;
38-
//std::cout<<"delete wfc_k_grid2 success"<<std::endl;
39-
}
4033
}
4134

4235
}

source/module_hamilt_lcao/module_tddft/ELEC_evolve.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ void ELEC_evolve::evolve_psi(const int& istep,
4141
ModuleBase::timer::tick("Efficience", "evolve_k");
4242
Evolve_LCAO_Matrix ELM(lowf.ParaV);
4343
psi->fix_k(ik);
44+
psi_laststep->fix_k(ik);
4445
ELM.evolve_complex_matrix(ik, phm, psi, psi_laststep, &(ekb(ik, 0)));
4546
ModuleBase::timer::tick("Efficience", "evolve_k");
4647
} // end k

source/module_hamilt_lcao/module_tddft/LCAO_evolve.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ void Evolve_LCAO_Matrix::compute_U_operator(
505505
//->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
506506
// (3) Next, invert Denominator
507507
int* ipiv = new int[this->ParaV->nloc];
508-
int info;
508+
int info = 0;
509509
// (3.1) compute ipiv
510510
ScalapackConnector::getrf(
511511
nlocal,
@@ -515,7 +515,7 @@ void Evolve_LCAO_Matrix::compute_U_operator(
515515
1,
516516
this->ParaV->desc,
517517
ipiv,
518-
info
518+
&info
519519
);
520520
int lwork = -1;
521521
int liwotk = -1;
@@ -533,7 +533,7 @@ void Evolve_LCAO_Matrix::compute_U_operator(
533533
&lwork,
534534
iwork.data(),
535535
&liwotk,
536-
info
536+
&info
537537
);
538538
lwork = work[0].real();
539539
work.resize(lwork, 0);
@@ -551,7 +551,7 @@ void Evolve_LCAO_Matrix::compute_U_operator(
551551
&lwork,
552552
iwork.data(),
553553
&liwotk,
554-
info
554+
&info
555555
);
556556
assert(0 == info);
557557

source/module_hsolver/diago_blas.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ std::pair<int, std::vector<int>> DiagoBlas::pdsygvx_once(const int *const desc,
6161
const char jobz = 'V', range = 'I', uplo = 'U';
6262
const int itype = 1, il = 1, iu = GlobalV::NBANDS, one = 1;
6363
int M = 0, NZ = 0, lwork = -1, liwork = -1, info = 0;
64+
double vl = 0, vu = 0;
6465
const double abstol = 0, orfac = -1;
65-
std::vector<double> work(1, 0);
66+
std::vector<double> work(3, 0);
6667
std::vector<int> iwork(1, 0);
6768
std::vector<int> ifail(GlobalV::NLOCAL, 0);
6869
std::vector<int> iclustr(2 * GlobalV::DSIZE);
@@ -81,8 +82,8 @@ std::pair<int, std::vector<int>> DiagoBlas::pdsygvx_once(const int *const desc,
8182
&one,
8283
&one,
8384
desc,
84-
NULL,
85-
NULL,
85+
&vl,
86+
&vu,
8687
&il,
8788
&iu,
8889
&abstol,
@@ -109,7 +110,7 @@ std::pair<int, std::vector<int>> DiagoBlas::pdsygvx_once(const int *const desc,
109110

110111
// GlobalV::ofs_running<<"lwork="<<work[0]<<"\t"<<"liwork="<<iwork[0]<<std::endl;
111112
lwork = work[0];
112-
work.resize(lwork, 0);
113+
work.resize(std::max(lwork,3), 0);
113114
liwork = iwork[0];
114115
iwork.resize(liwork, 0);
115116

@@ -126,8 +127,8 @@ std::pair<int, std::vector<int>> DiagoBlas::pdsygvx_once(const int *const desc,
126127
&one,
127128
&one,
128129
desc,
129-
NULL,
130-
NULL,
130+
&vl,
131+
&vu,
131132
&il,
132133
&iu,
133134
&abstol,
@@ -184,8 +185,12 @@ std::pair<int, std::vector<int>> DiagoBlas::pzhegvx_once(const int *const desc,
184185
const int itype = 1, il = 1, iu = GlobalV::NBANDS, one = 1;
185186
int M = 0, NZ = 0, lwork = -1, lrwork = -1, liwork = -1, info = 0;
186187
const double abstol = 0, orfac = -1;
188+
//Note: pzhegvx_ has a bug
189+
// We must give vl,vu a value, although we do not use range 'V'
190+
// We must give rwork at least a memory of sizeof(double) * 3
191+
const double vl = 0, vu = 0;
187192
std::vector<std::complex<double>> work(1, 0);
188-
std::vector<double> rwork(1, 0);
193+
std::vector<double> rwork(3, 0);
189194
std::vector<int> iwork(1, 0);
190195
std::vector<int> ifail(GlobalV::NLOCAL, 0);
191196
std::vector<int> iclustr(2 * GlobalV::DSIZE);
@@ -204,8 +209,8 @@ std::pair<int, std::vector<int>> DiagoBlas::pzhegvx_once(const int *const desc,
204209
&one,
205210
&one,
206211
desc,
207-
NULL,
208-
NULL,
212+
&vl,
213+
&vu,
209214
&il,
210215
&iu,
211216
&abstol,
@@ -236,7 +241,8 @@ std::pair<int, std::vector<int>> DiagoBlas::pzhegvx_once(const int *const desc,
236241
lwork = work[0].real();
237242
work.resize(lwork, 0);
238243
lrwork = rwork[0] + this->degeneracy_max * GlobalV::NLOCAL;
239-
rwork.resize(lrwork, 0);
244+
int maxlrwork = std::max(lrwork,3);
245+
rwork.resize(maxlrwork, 0);
240246
liwork = iwork[0];
241247
iwork.resize(liwork, 0);
242248

@@ -253,8 +259,8 @@ std::pair<int, std::vector<int>> DiagoBlas::pzhegvx_once(const int *const desc,
253259
&one,
254260
&one,
255261
desc,
256-
NULL,
257-
NULL,
262+
&vl,
263+
&vu,
258264
&il,
259265
&iu,
260266
&abstol,
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
etotref -77.57538947510851
2-
etotperatomref -38.7876947376
3-
totalforceref 0.556100
4-
totalstressref 449.534598
5-
totaltimeref
6-
+3.8040
1+
etotref -77.57538889287758
2+
etotperatomref -38.7876944464
3+
totalforceref 0.554418
4+
totalstressref 449.531857
5+
totaltimeref +8.2720

0 commit comments

Comments
 (0)