Skip to content

Commit 33577ba

Browse files
caic99pplabdyzheng
authored
Fix: use new genelpa interface (#1213)
* Add new GenELPA package, which support both new and legacy API of ELPA, to ABACUS. Now ABACUS can use all versions of ELPA packages instead of whose who are newer than 2017. Also, the program can choose a better (if not the best) kernel to solve Kohn-Shan equation, which makes it run (much) more faster than the one with the default kernel. More information about GenELPA: <https://github.com/pplab/GenELPA> * Fix: only support elpa2016+ * Fix: remove legacy support in genelpa. Co-authored-by: Shen Yu <[email protected]> Co-authored-by: dyzheng <[email protected]>
1 parent 64e3118 commit 33577ba

26 files changed

+2826
-158
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ target_link_libraries(${ABACUS_BIN_NAME}
336336
driver
337337
xc_
338338
hsolver
339+
genelpa
339340
elecstate
340341
hamilt
341342
psi

modules/FindELPA.cmake

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,4 @@ if(ELPA_FOUND)
3838
endif()
3939

4040
set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES} ${ELPA_INCLUDE_DIR})
41-
include(CheckCXXSourceCompiles)
42-
check_cxx_source_compiles("
43-
#include <elpa/elpa_version.h>
44-
#if ELPA_API_VERSION < 20210430
45-
#error ELPA version is too old.
46-
#endif
47-
int main(){}
48-
"
49-
ELPA_VERSION_SATISFIES
50-
)
51-
if(NOT ELPA_VERSION_SATISFIES)
52-
message(FATAL_ERROR "ELPA version is too old. We support version 2017 or higher.")
53-
endif()
5441
mark_as_advanced(ELPA_INCLUDE_DIR ELPA_LIBRARY)

source/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ VPATH=./src_global\
2020
:./module_xc\
2121
:./module_esolver\
2222
:./module_hsolver\
23+
:./module_hsolver/genelpa\
2324
:./module_elecstate\
2425
:./module_psi\
2526
:./module_hamilt\

source/Makefile.Objects

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,11 @@ hsolver_lcao.o\
277277
hsolver_pw.o\
278278
hsolver_pw_sdft.o
279279

280+
OBJ_GENELPA=elpa_new_complex.o\
281+
elpa_new_real.o\
282+
elpa_new.o\
283+
utils.o
284+
280285
OBJ_ELECSTATES=elecstate.o\
281286
dm2d_to_grid.o\
282287
elecstate_lcao.o\
@@ -304,6 +309,7 @@ $(OBJ_HSOLVER)\
304309
$(OBJ_ELECSTATES)\
305310
$(OBJ_PSI)\
306311
${OBJ_OPERATOR}\
312+
${OBJ_GENELPA}\
307313
charge.o \
308314
charge_mixing.o \
309315
charge_pulay.o \

source/module_deepks/test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ target_link_libraries(
88
test_deepks
99
base cell symmetry md surchem xc_
1010
neighbor orb io relax gint lcao parallel mrrr pdiag pw ri driver esolver hsolver psi elecstate hamilt planewave
11-
pthread
11+
pthread genelpa
1212
deepks
1313
${ABACUS_LINK_LIBRARIES}
1414
)

source/module_hamilt/hamilt_lcao.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ template <> void HamiltLCAO<double>::updateHk(const int ik)
134134
}
135135
const int inc = 1;
136136
BlasConnector::copy(this->LM->Sloc.size(), this->LM->Sloc.data(), inc, this->smatrix_k, inc);
137-
hsolver::DiagoElpa::is_already_decomposed = false;
137+
hsolver::DiagoElpa::DecomposedState = 0;
138138
}
139139
ModuleBase::timer::tick("HamiltLCAO", "updateHk");
140140
return;
@@ -303,4 +303,4 @@ template <> void HamiltLCAO<std::complex<double>>::constructHamilt()
303303
#endif
304304
}
305305

306-
} // namespace hamilt
306+
} // namespace hamilt

source/module_hsolver/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ add_library(
1212
diago_lapack.cpp
1313
)
1414

15+
add_subdirectory(genelpa)
16+
1517
IF (BUILD_TESTING)
1618
add_subdirectory(test)
1719
endif()

source/module_hsolver/diago_elpa.cpp

Lines changed: 15 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,16 @@
77
extern "C"
88
{
99
#include "module_base/blacs_connector.h"
10-
#include "my_elpa.h"
1110
#include "module_base/scalapack_connector.h"
1211
}
12+
#include "genelpa/elpa_solver.h"
1313

1414
typedef hamilt::MatrixBlock<double> matd;
1515
typedef hamilt::MatrixBlock<std::complex<double>> matcd;
1616

1717
namespace hsolver
1818
{
19-
bool DiagoElpa::is_already_decomposed = false;
20-
#ifdef __MPI
21-
inline int set_elpahandle(elpa_t &handle,
22-
const int *desc,
23-
const int local_nrows,
24-
const int local_ncols,
25-
const int nbands)
26-
{
27-
int error;
28-
int nprows, npcols, myprow, mypcol;
29-
Cblacs_gridinfo(desc[1], &nprows, &npcols, &myprow, &mypcol);
30-
elpa_init(20210430);
31-
handle = elpa_allocate(&error);
32-
elpa_set_integer(handle, "na", desc[2], &error);
33-
elpa_set_integer(handle, "nev", nbands, &error);
34-
35-
elpa_set_integer(handle, "local_nrows", local_nrows, &error);
36-
37-
elpa_set_integer(handle, "local_ncols", local_ncols, &error);
38-
39-
elpa_set_integer(handle, "nblk", desc[4], &error);
40-
41-
elpa_set_integer(handle, "mpi_comm_parent", MPI_Comm_c2f(MPI_COMM_WORLD), &error);
42-
43-
elpa_set_integer(handle, "process_row", myprow, &error);
44-
45-
elpa_set_integer(handle, "process_col", mypcol, &error);
46-
47-
elpa_set_integer(handle, "blacs_context", desc[1], &error);
48-
49-
elpa_set_integer(handle, "cannon_for_generalized", 0, &error);
50-
/* Setup */
51-
elpa_setup(handle); /* Set tunables */
52-
return 0;
53-
}
54-
#endif
55-
19+
int DiagoElpa::DecomposedState = 0;
5620
void DiagoElpa::diag(hamilt::Hamilt *phm_in, psi::Psi<std::complex<double>> &psi, double *eigenvalue_in)
5721
{
5822
ModuleBase::TITLE("DiagoElpa", "diag");
@@ -62,31 +26,15 @@ void DiagoElpa::diag(hamilt::Hamilt *phm_in, psi::Psi<std::complex<double>> &psi
6226

6327
std::vector<double> eigen(GlobalV::NLOCAL, 0.0);
6428

65-
static elpa_t handle;
66-
static bool has_set_elpa_handle = false;
67-
if (!has_set_elpa_handle)
68-
{
69-
set_elpahandle(handle, h_mat.desc, h_mat.row, h_mat.col, GlobalV::NBANDS);
70-
has_set_elpa_handle = true;
71-
}
72-
73-
// compare to old code from pplab, there is no need to copy Sloc2 to another memory,
74-
// just change Sloc2, which is a temporary matrix
75-
// size_t nloc = h_mat.col * h_mat.row,
76-
// BlasConnector::copy(nloc, s_mat, inc, Stmp, inc);
77-
29+
bool isReal=false;
30+
const MPI_Comm COMM_DIAG=MPI_COMM_WORLD; // use all processes
31+
ELPA_Solver es((const bool)isReal, COMM_DIAG, (const int)GlobalV::NBANDS, (const int)h_mat.row, (const int)h_mat.col, (const int*)h_mat.desc);
32+
this->DecomposedState=0; // for k pointer, the decomposed s_mat can not be reused
7833
ModuleBase::timer::tick("DiagoElpa", "elpa_solve");
79-
int elpa_derror;
80-
elpa_generalized_eigenvectors_dc(handle,
81-
reinterpret_cast<double _Complex *>(h_mat.p),
82-
reinterpret_cast<double _Complex *>(s_mat.p),
83-
eigen.data(),
84-
reinterpret_cast<double _Complex *>(psi.get_pointer()),
85-
0,
86-
&elpa_derror);
34+
es.generalized_eigenvector(h_mat.p, s_mat.p, this->DecomposedState, eigen.data(), psi.get_pointer());
8735
ModuleBase::timer::tick("DiagoElpa", "elpa_solve");
36+
es.exit();
8837

89-
// the eigenvalues.
9038
const int inc = 1;
9139
BlasConnector::copy(GlobalV::NBANDS, eigen.data(), inc, eigenvalue_in, inc);
9240
#else
@@ -103,43 +51,14 @@ void DiagoElpa::diag(hamilt::Hamilt *phm_in, psi::Psi<double> &psi, double *eige
10351

10452
std::vector<double> eigen(GlobalV::NLOCAL, 0.0);
10553

106-
static elpa_t handle;
107-
static bool has_set_elpa_handle = false;
108-
if (!has_set_elpa_handle)
109-
{
110-
set_elpahandle(handle, h_mat.desc, h_mat.row, h_mat.col, GlobalV::NBANDS);
111-
has_set_elpa_handle = true;
112-
}
113-
114-
// compare to old code from pplab, there is no need to copy Sloc2 to another memory,
115-
// just change Sloc2, which is a temporary matrix
116-
// change this judgement to HamiltLCAO
117-
/*int is_already_decomposed;
118-
if(ifElpaHandle(GlobalC::CHR.get_new_e_iteration(), (GlobalV::CALCULATION=="nscf")))
119-
{
120-
ModuleBase::timer::tick("DiagoElpa","decompose_S");
121-
BlasConnector::copy(pv->nloc, s_mat, inc, Stmp, inc);
122-
is_already_decomposed=0;
123-
ModuleBase::timer::tick("DiagoElpa","decompose_S");
124-
}
125-
else
126-
{
127-
is_already_decomposed=1;
128-
}*/
129-
54+
bool isReal=true;
55+
MPI_Comm COMM_DIAG=MPI_COMM_WORLD; // use all processes
56+
//ELPA_Solver es(isReal, COMM_DIAG, GlobalV::NBANDS, h_mat.row, h_mat.col, h_mat.desc);
57+
ELPA_Solver es((const bool)isReal, COMM_DIAG, (const int)GlobalV::NBANDS, (const int)h_mat.row, (const int)h_mat.col, (const int*)h_mat.desc);
13058
ModuleBase::timer::tick("DiagoElpa", "elpa_solve");
131-
int elpa_error;
132-
elpa_generalized_eigenvectors_d(handle,
133-
h_mat.p,
134-
s_mat.p,
135-
eigen.data(),
136-
psi.get_pointer(),
137-
DiagoElpa::is_already_decomposed,
138-
&elpa_error);
59+
es.generalized_eigenvector(h_mat.p, s_mat.p, this->DecomposedState, eigen.data(), psi.get_pointer());
13960
ModuleBase::timer::tick("DiagoElpa", "elpa_solve");
140-
141-
//S matrix has been decomposed
142-
DiagoElpa::is_already_decomposed = true;
61+
es.exit();
14362

14463
const int inc = 1;
14564
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "K-S equation was solved by genelpa2");
@@ -162,4 +81,4 @@ bool DiagoElpa::ifElpaHandle(const bool &newIteration, const bool &ifNSCF)
16281
}
16382
#endif
16483

165-
} // namespace hsolver
84+
} // namespace hsolver

source/module_hsolver/diago_elpa.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class DiagoElpa : public DiagH
1414
void diag(hamilt::Hamilt* phm_in, psi::Psi<double>& psi, double* eigenvalue_in) override;
1515

1616
void diag(hamilt::Hamilt* phm_in, psi::Psi<std::complex<double>>& psi, double* eigenvalue_in) override;
17-
18-
static bool is_already_decomposed;
1917

18+
static int DecomposedState;
19+
2020
private:
2121
#ifdef __MPI
2222
bool ifElpaHandle(const bool& newIteration, const bool& ifNSCF);
@@ -25,4 +25,4 @@ class DiagoElpa : public DiagH
2525

2626
} // namespace hsolver
2727

28-
#endif
28+
#endif
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_library(genelpa OBJECT elpa_new.cpp elpa_new_real.cpp elpa_new_complex.cpp utils.cpp)

0 commit comments

Comments
 (0)