44#include " source_hsolver/diago_iter_assist.h"
55#include " source_io/module_parameter/parameter.h"
66#include " spin_constrain.h"
7+ #include " deltaspin_pw_mi.h"
78#include " mi_tools.h"
89#include " source_pw/module_pwdft/onsite_proj.h"
910#include " source_base/parallel_reduce.h"
@@ -105,19 +106,19 @@ void spinconstrain::SpinConstrain<std::complex<double>>::cal_mw_from_lambda(
105106 PARAM .inp .nbands ,
106107 PARAM .inp .nelec ,
107108 PARAM .inp .device == " gpu" );
108- if (this ->nspin_ == 2 )
109+ if (this ->state_ . nspin_ == 2 )
109110 {
110111 dynamic_cast <hamilt::DeltaSpin<hamilt::OperatorLCAO<std::complex <double >, double >>*>(this ->p_operator )
111112 ->update_lambda ();
112113 }
113- else if (this ->nspin_ == 4 )
114+ else if (this ->state_ . nspin_ == 4 )
114115 {
115116 dynamic_cast <hamilt::DeltaSpin<hamilt::OperatorLCAO<std::complex <double >, std::complex <double >>>*>(
116117 this ->p_operator )
117118 ->update_lambda ();
118119 }
119120 // Diagonalization without updating charge density (last param = true means skip charge update)
120- hsolver_t .solve (hamilt_t , psi_t [0 ], this ->pelec , *this ->dm_ , *this ->pelec ->charge , this ->nspin_ , true );
121+ hsolver_t .solve (hamilt_t , psi_t [0 ], this ->pelec , *this ->dm_ , *this ->pelec ->charge , this ->state_ . nspin_ , true );
121122 elecstate::calculate_weights (this ->pelec ->ekb ,
122123 this ->pelec ->wg ,
123124 this ->pelec ->klist ,
@@ -164,23 +165,21 @@ void spinconstrain::SpinConstrain<std::complex<double>>::cal_mw_from_lambda(
164165 becp_tmp.resize (size_becp * nk);
165166 std::vector<std::complex <double >> h_tmp (nbands * nbands), s_tmp (nbands * nbands);
166167 int initial_hs = 0 ;
167- if (this ->sub_h_save == nullptr )
168+ if (! this ->pw_cache_ . allocated () )
168169 {
169170 // FIRST CALL: save subspace data for reuse across lambda steps
170171 initial_hs = 1 ;
171- this ->sub_h_save = new std::complex <double >[nbands * nbands * nk];
172- this ->sub_s_save = new std::complex <double >[nbands * nbands * nk];
173- this ->becp_save = new std::complex <double >[size_becp * nk];
174- this ->lambda_in_sub_ = this ->lambda_ ;
172+ this ->pw_cache_ .allocate_cpu (nbands, nk, size_becp);
173+ this ->pw_cache_ .lambda_in_sub () = this ->state_ .lambda_ ;
175174 }
176175 for (int ik = 0 ; ik < nk; ++ik)
177176 {
178177
179178 psi_t ->fix_k (ik);
180179
181- std::complex <double >* h_k = this ->sub_h_save + ik * nbands * nbands ;
182- std::complex <double >* s_k = this ->sub_s_save + ik * nbands * nbands ;
183- std::complex <double >* becp_k = this ->becp_save + ik * size_becp;
180+ std::complex <double >* h_k = this ->pw_cache_ . h_k (ik, nbands) ;
181+ std::complex <double >* s_k = this ->pw_cache_ . s_k (ik, nbands) ;
182+ std::complex <double >* becp_k = this ->pw_cache_ . becp_k (ik, size_becp) ;
184183 if (initial_hs)
185184 {
186185 // / Compute H(k) and extract subspace matrices for this k-point
@@ -191,7 +190,7 @@ void spinconstrain::SpinConstrain<std::complex<double>>::cal_mw_from_lambda(
191190 memcpy (h_tmp.data (), h_k, sizeof (std::complex <double >) * nbands * nbands);
192191 memcpy (s_tmp.data (), s_k, sizeof (std::complex <double >) * nbands * nbands);
193192 // Apply DeltaSpin correction (skip for initialization step i_step=-1)
194- if (i_step != -1 ) this ->calculate_delta_hcc ( h_tmp.data (), becp_k, this ->lambda_ .data (), nbands, nkb, nh_iat, ik, true );
193+ if (i_step != -1 ) pw::calculate_delta_hcc ( this ->state_ , this -> pw_cache_ , this -> pelec , h_tmp.data (), becp_k, this ->state_ . lambda_ .data (), nbands, nkb, nh_iat, ik, true );
195194
196195 // Diagonalize in subspace, update becp (response wavefunctions)
197196 hsolver::DiagoIterAssist<std::complex <double >>::diag_responce (h_tmp.data (),
@@ -224,23 +223,21 @@ void spinconstrain::SpinConstrain<std::complex<double>>::cal_mw_from_lambda(
224223 base_device::memory::resize_memory_op<std::complex <double >, base_device::DEVICE_GPU >()(h_tmp, nbands * nbands);
225224 base_device::memory::resize_memory_op<std::complex <double >, base_device::DEVICE_GPU >()(s_tmp, nbands * nbands);
226225 int initial_hs = 0 ;
227- if (this ->sub_h_save == nullptr )
226+ if (! this ->pw_cache_ . allocated () )
228227 {
229228 initial_hs = 1 ;
230- base_device::memory::resize_memory_op<std::complex <double >, base_device::DEVICE_GPU >()(this ->sub_h_save , nbands * nbands * nk);
231- base_device::memory::resize_memory_op<std::complex <double >, base_device::DEVICE_GPU >()(this ->sub_s_save , nbands * nbands * nk);
232- base_device::memory::resize_memory_op<std::complex <double >, base_device::DEVICE_GPU >()(this ->becp_save , size_becp * nk);
233- this ->lambda_in_sub_ = this ->lambda_ ;
229+ this ->pw_cache_ .allocate_gpu (nbands, nk, size_becp);
230+ this ->pw_cache_ .lambda_in_sub () = this ->state_ .lambda_ ;
234231 }
235232 std::complex <double >* becp_pointer = nullptr ;
236233 base_device::memory::resize_memory_op<std::complex <double >, base_device::DEVICE_GPU >()(becp_pointer, size_becp);
237234 for (int ik = 0 ; ik < nk; ++ik)
238235 {
239236 psi_t ->fix_k (ik);
240237
241- std::complex <double >* h_k = this ->sub_h_save + ik * nbands * nbands ;
242- std::complex <double >* s_k = this ->sub_s_save + ik * nbands * nbands ;
243- std::complex <double >* becp_k = this ->becp_save + ik * size_becp;
238+ std::complex <double >* h_k = this ->pw_cache_ . h_k (ik, nbands) ;
239+ std::complex <double >* s_k = this ->pw_cache_ . s_k (ik, nbands) ;
240+ std::complex <double >* becp_k = this ->pw_cache_ . becp_k (ik, size_becp) ;
244241 if (initial_hs)
245242 {
246243 hamilt_t ->updateHk (ik);
@@ -249,7 +246,7 @@ void spinconstrain::SpinConstrain<std::complex<double>>::cal_mw_from_lambda(
249246 }
250247 base_device::memory::synchronize_memory_op<std::complex <double >, base_device::DEVICE_GPU , base_device::DEVICE_GPU >()(h_tmp, h_k, nbands * nbands);
251248 base_device::memory::synchronize_memory_op<std::complex <double >, base_device::DEVICE_GPU , base_device::DEVICE_GPU >()(s_tmp, s_k, nbands * nbands);
252- if (i_step != -1 ) this ->calculate_delta_hcc ( h_tmp, becp_k, this ->lambda_ .data (), nbands, nkb, nh_iat, ik, true );
249+ if (i_step != -1 ) pw::calculate_delta_hcc ( this ->state_ , this -> pw_cache_ , this -> pelec , h_tmp, becp_k, this ->state_ . lambda_ .data (), nbands, nkb, nh_iat, ik, true );
253250
254251 hsolver::DiagoIterAssist<std::complex <double >, base_device::DEVICE_GPU >::diag_responce (h_tmp,
255252 s_tmp,
@@ -280,15 +277,15 @@ void spinconstrain::SpinConstrain<std::complex<double>>::cal_mw_from_lambda(
280277 for (int ik = 0 ; ik < nk; ik++)
281278 {
282279 const std::complex <double >* becp = &becp_tmp[ik * size_becp];
283- const int spin_sign = (this ->npol_ == 2 ) ? 1 : this ->get_spin_sign (ik);
284- accumulate_Mi_from_becp (becp, nkb, nbands, this ->npol_ , spin_sign,
285- &this ->pelec ->wg (ik, 0 ), nh_iat, this ->Mi_ );
280+ const int spin_sign = (this ->state_ . npol_ == 2 ) ? 1 : this ->get_spin_sign (ik);
281+ accumulate_Mi_from_becp (becp, nkb, nbands, this ->state_ . npol_ , spin_sign,
282+ &this ->pelec ->wg (ik, 0 ), nh_iat, this ->state_ . Mi_ );
286283 }
287284 // MPI reduction: sum Mi across all k-pool ranks
288285 Parallel_Reduce::reduce_double_allpool (PARAM .inp .kpar ,
289286 GlobalV::NPROC_IN_POOL ,
290- &(this ->Mi_ [0 ][0 ]),
291- 3 * this ->Mi_ .size ());
287+ &(this ->state_ . Mi_ [0 ][0 ]),
288+ 3 * this ->state_ . Mi_ .size ());
292289 }
293290 }
294291 ModuleBase::timer::end (" spinconstrain::SpinConstrain" , " cal_mw_from_lambda" );
@@ -328,12 +325,14 @@ void spinconstrain::SpinConstrain<std::complex<double>>::update_psi_charge(const
328325 {
329326 if (PARAM .inp .device == " cpu" )
330327 {
331- this ->update_psi_charge_pw_cpu (delta_lambda, pw_solve, full_update);
328+ pw::update_psi_charge_pw_cpu (this ->state_ , this ->pw_cache_ , this ->psi , this ->p_hamilt ,
329+ this ->pelec , this ->pw_wfc_ , delta_lambda, pw_solve, full_update);
332330 }
333331#if ((defined __CUDA) || (defined __ROCM))
334332 else
335333 {
336- this ->update_psi_charge_pw_gpu (delta_lambda, pw_solve, full_update);
334+ pw::update_psi_charge_pw_gpu (this ->state_ , this ->pw_cache_ , this ->psi , this ->p_hamilt ,
335+ this ->pelec , this ->pw_wfc_ , delta_lambda, pw_solve, full_update);
337336 }
338337#endif
339338 }
0 commit comments