1- #include < algorithm>
2- #include < type_traits>
3-
41#include " source_base/global_function.h"
52#include " gint_rho.h"
63#include " gint_common.h"
@@ -30,15 +27,14 @@ template<typename Real>
3027void Gint_rho::cal_gint_impl_ ()
3128{
3229 std::vector<HContainer<Real>> dm_gint_vec = init_dm_gint_<Real>();
33- std::vector<std::vector<Real>> rho_cache (nspin_);
34- std::vector<Real *> rho_data (nspin_);
30+ // rho_[is] is always double; phi_dot_phi accumulates into it directly.
31+ std::vector<double *> rho_data (nspin_);
3532 for (int is = 0 ; is < nspin_; ++is)
3633 {
37- rho_data[is] = get_rho_data_<Real>(is, rho_cache) ;
34+ rho_data[is] = rho_[is] ;
3835 }
3936 dm_2d_to_gint (*gint_info_, dm_vec_, dm_gint_vec);
4037 cal_rho_ (dm_gint_vec, rho_data);
41- transfer_rho_cache_<Real>(rho_cache);
4238}
4339
4440template <typename Real>
@@ -52,64 +48,19 @@ std::vector<HContainer<Real>> Gint_rho::init_dm_gint_() const
5248 return dm_gint_vec;
5349}
5450
55- // Overloaded helpers (C++11-compatible alternative to if constexpr).
56- // The double overload is preferred by overload resolution when Real=double.
57-
58- inline double * get_rho_data (double * const * rho, int is, int /* local_mgrid_num*/ ,
59- std::vector<std::vector<double >>& /* rho_cache*/ )
60- {
61- return rho[is];
62- }
63-
64- template <typename Real>
65- Real* get_rho_data (double * const * rho, int is, int local_mgrid_num,
66- std::vector<std::vector<Real>>& rho_cache)
67- {
68- rho_cache[is].resize (local_mgrid_num);
69- std::transform (rho[is], rho[is] + local_mgrid_num, rho_cache[is].begin (), [](const double value) {
70- return static_cast <Real>(value);
71- });
72- return rho_cache[is].data ();
73- }
74-
75- inline void transfer_rho_back (double * const * /* rho*/ , int /* nspin*/ , int /* local_mgrid_num*/ ,
76- const std::vector<std::vector<double >>& /* rho_cache*/ )
77- {
78- // Nothing to do: double rho was written directly.
79- }
80-
81- template <typename Real>
82- void transfer_rho_back (double * const * rho, int nspin, int local_mgrid_num,
83- const std::vector<std::vector<Real>>& rho_cache)
84- {
85- for (int is = 0 ; is < nspin; ++is)
86- {
87- for (int ir = 0 ; ir < local_mgrid_num; ++ir)
88- {
89- rho[is][ir] = static_cast <double >(rho_cache[is][ir]);
90- }
91- }
92- }
93-
94-
95-
96- template <typename Real>
97- Real* Gint_rho::get_rho_data_ (int is, std::vector<std::vector<Real>>& rho_cache) const
98- {
99- return get_rho_data (
100- rho_, is, gint_info_->get_local_mgrid_num (), rho_cache);
101- }
102-
10351template <typename Real>
10452void Gint_rho::cal_rho_ (
10553 const std::vector<HContainer<Real>>& dm_gint_vec,
106- const std::vector<Real *>& rho_data) const
54+ const std::vector<double *>& rho_data) const
10755{
10856#pragma omp parallel
10957 {
11058 PhiOperator phi_op;
11159 std::vector<Real> phi;
112- std::vector<Real> phi_dm;
60+ // phi_dm is always double: phi_mul_dm writes the cast-to-double result
61+ // into it, and phi_dot_phi reads it as fp64 (so the rho reduction's
62+ // right-hand side is uniformly fp64 even on the fp32 path).
63+ std::vector<double > phi_dm;
11364#pragma omp for schedule(dynamic)
11465 for (int i = 0 ; i < gint_info_->get_bgrids_num (); i++)
11566 {
@@ -132,11 +83,4 @@ void Gint_rho::cal_rho_(
13283 }
13384}
13485
135- template <typename Real>
136- void Gint_rho::transfer_rho_cache_ (const std::vector<std::vector<Real>>& rho_cache) const
137- {
138- transfer_rho_back (
139- rho_, nspin_, gint_info_->get_local_mgrid_num (), rho_cache);
140- }
141-
14286}
0 commit comments