Skip to content

Commit 6224a17

Browse files
authored
Fix: Fix ELF for kpar > 1 (#7307)
* Fix: Fix ELF for kpar > 1 * Fix: add #ifdef __MPN * Test: Update source/source_estate/test/CMakeLists.txt * Test: Add a unit test for kin_r_mpi
1 parent 295f3e1 commit 6224a17

5 files changed

Lines changed: 88 additions & 1 deletion

File tree

source/source_estate/elecstate_pw_cal_tau.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ void ElecStatePW<T, Device>::cal_tau(const psi::Psi<T, Device>& psi)
5454
castmem_var_d2h_op()(this->charge->kin_r[ii], this->kin_r[ii], this->charge->nrxx);
5555
}
5656
}
57-
this->parallelK();
57+
#ifdef __MPI
58+
this->charge->kin_r_mpi();
59+
#endif
5860
ModuleBase::TITLE("ElecStatePW", "cal_tau");
5961
}
6062

source/source_estate/module_charge/charge.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ class Charge
136136
*/
137137
void rho_mpi();
138138

139+
/**
140+
* @brief Sum kin_r at different pools (k-point/band parallelism).
141+
* Only used when GlobalV::KPAR * bndpar > 1
142+
*/
143+
void kin_r_mpi();
144+
139145
/**
140146
* @brief Reduce among different pools
141147
* If NPROC_IN_POOLs are all the same, use GlobalV::KP_WORLD

source/source_estate/module_charge/charge_mpi.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,25 @@ void Charge::rho_mpi()
137137
ModuleBase::timer::end("Charge", "rho_mpi");
138138
return;
139139
}
140+
141+
void Charge::kin_r_mpi()
142+
{
143+
ModuleBase::TITLE("Charge", "kin_r_mpi");
144+
if (GlobalV::KPAR * PARAM.inp.bndpar <= 1)
145+
{
146+
return;
147+
}
148+
ModuleBase::timer::start("Charge", "kin_r_mpi");
149+
150+
if (XC_Functional::get_ked_flag() || PARAM.inp.out_elf[0] > 0)
151+
{
152+
for (int is = 0; is < PARAM.inp.nspin; ++is)
153+
{
154+
reduce_diff_pools(this->kin_r[is]);
155+
}
156+
}
157+
158+
ModuleBase::timer::end("Charge", "kin_r_mpi");
159+
return;
160+
}
140161
#endif

source/source_estate/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ AddTest(
5656
../elecstate_pw_cal_tau.cpp
5757
../elecstate.cpp
5858
../occupy.cpp
59+
../module_charge/charge_mpi.cpp
5960
../../source_psi/psi.cpp
6061
# ../../source_psi/kernels/psi_memory_op.cpp
6162
../../source_base/module_device/memory_op.cpp

source/source_estate/test_mpi/charge_mpi_test.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,63 @@ TEST_F(ChargeMpiTest, rho_mpi)
201201
charge->rho_mpi();
202202
}
203203

204+
TEST_F(ChargeMpiTest, kin_r_mpi)
205+
{
206+
if (GlobalV::NPROC >= 2 && GlobalV::NPROC % 2 == 0)
207+
{
208+
const bool ked_flag_old = XC_Functional::ked_flag;
209+
XC_Functional::ked_flag = true;
210+
PARAM.input.nspin = 1;
211+
PARAM.input.bndpar = 1;
212+
GlobalV::KPAR = 2;
213+
214+
Parallel_Global::divide_pools(GlobalV::NPROC,
215+
GlobalV::MY_RANK,
216+
PARAM.input.bndpar,
217+
GlobalV::KPAR,
218+
GlobalV::NPROC_IN_BNDGROUP,
219+
GlobalV::RANK_IN_BPGROUP,
220+
GlobalV::MY_BNDGROUP,
221+
GlobalV::NPROC_IN_POOL,
222+
GlobalV::RANK_IN_POOL,
223+
GlobalV::MY_POOL);
224+
ModulePW::PW_Basis* rhopw = new ModulePW::PW_Basis();
225+
rhopw->initmpi(GlobalV::NPROC_IN_POOL, GlobalV::RANK_IN_POOL, POOL_WORLD);
226+
rhopw->initgrids(lat0, latvec, 40);
227+
rhopw->initparameters(false, 10);
228+
rhopw->setuptransform();
229+
charge->rhopw = rhopw;
230+
231+
const int nz = rhopw->nz;
232+
const int nrxx = rhopw->nrxx;
233+
const int nxy = rhopw->nxy;
234+
const int nplane = rhopw->nplane;
235+
charge->nrxx = nrxx;
236+
charge->kin_r = new double*[1];
237+
charge->kin_r[0] = new double[nrxx];
238+
239+
for (int ir = 0; ir < nxy; ++ir)
240+
{
241+
for (int iz = 0; iz < nplane; ++iz)
242+
{
243+
charge->kin_r[0][nplane * ir + iz]
244+
= (rhopw->startz_current + iz + ir * nz) / double(nxy * nz);
245+
}
246+
}
247+
const double refsum = sum_array(charge->kin_r[0], nrxx);
248+
249+
charge->init_chgmpi();
250+
charge->kin_r_mpi();
251+
const double sum = sum_array(charge->kin_r[0], nrxx);
252+
EXPECT_EQ(sum, refsum * GlobalV::KPAR);
253+
254+
delete[] charge->kin_r[0];
255+
delete[] charge->kin_r;
256+
delete rhopw;
257+
XC_Functional::ked_flag = ked_flag_old;
258+
}
259+
}
260+
204261
int main(int argc, char** argv)
205262
{
206263
MPI_Init(&argc, &argv);

0 commit comments

Comments
 (0)