11#include " hcontainer.h"
2+ #include " source_base/memory.h"
23
34namespace hamilt
45{
@@ -12,12 +13,16 @@ HContainer<T>::~HContainer()
1213{
1314 if (this ->allocated )
1415 {
16+ if (this ->allocated_size > 0 )
17+ {
18+ ModuleBase::Memory::record (" HContainer" , -(long long )this ->allocated_size , true );
19+ }
1520 delete[] this ->wrapper_pointer ;
1621 }
1722}
1823
1924template <typename T>
20- HContainer<T>::HContainer() {}
25+ HContainer<T>::HContainer() : allocated_size( 0 ) {}
2126
2227// copy constructor
2328template <typename T>
@@ -30,6 +35,7 @@ HContainer<T>::HContainer(const HContainer<T>& HR_in, T* data_array)
3035 this ->current_R = -1 ;
3136 this ->wrapper_pointer = data_array;
3237 this ->allocated = false ;
38+ this ->allocated_size = 0 ;
3339 this ->atom_pairs = HR_in.atom_pairs ;
3440 // data of HR_in will not be copied, please call add() after this constructor to copy data.
3541 this ->allocate (this ->wrapper_pointer , true );
@@ -47,8 +53,11 @@ HContainer<T>::HContainer(HContainer<T>&& HR_in) noexcept
4753 this ->gamma_only = HR_in.gamma_only ;
4854 this ->paraV = HR_in.paraV ;
4955 this ->allocated = HR_in.allocated ;
56+ this ->allocated_size = HR_in.allocated_size ;
5057 this ->current_R = -1 ;
5158 HR_in.wrapper_pointer = nullptr ;
59+ HR_in.allocated = false ;
60+ HR_in.allocated_size = 0 ;
5261 // tmp terms not moved
5362}
5463
@@ -65,9 +74,12 @@ HContainer<T>& HContainer<T>::operator=(HContainer<T>&& HR_in) noexcept
6574 this ->gamma_only = HR_in.gamma_only ;
6675 this ->paraV = HR_in.paraV ;
6776 this ->allocated = HR_in.allocated ;
77+ this ->allocated_size = HR_in.allocated_size ;
6878 this ->current_R = -1 ;
6979
7080 HR_in.wrapper_pointer = nullptr ;
81+ HR_in.allocated = false ;
82+ HR_in.allocated_size = 0 ;
7183 }
7284 return *this ;
7385}
@@ -80,6 +92,7 @@ HContainer<T>::HContainer(int natom)
8092 this ->current_R = -1 ;
8193 this ->sparse_ap .resize (natom);
8294 this ->sparse_ap_index .resize (natom);
95+ this ->allocated_size = 0 ;
8396}
8497
8598// use unitcell to initialize atom_pairs
@@ -88,6 +101,7 @@ HContainer<T>::HContainer(const UnitCell& ucell_, const Parallel_Orbitals* paraV
88101{
89102 this ->gamma_only = false ;
90103 this ->current_R = -1 ;
104+ this ->allocated_size = 0 ;
91105 std::vector<int > atom_begin_row (ucell_.nat +1 , 0 );
92106 std::vector<int > atom_begin_col (ucell_.nat +1 , 0 );
93107 int begin = 0 ;
@@ -148,6 +162,7 @@ template <typename T>
148162HContainer<T>::HContainer(const Parallel_Orbitals* paraV_in, T* data_pointer, const std::vector<int >* ijr_info)
149163{
150164 this ->current_R = -1 ;
165+ this ->allocated_size = 0 ;
151166
152167 // use HContainer as a wrapper(!nullptr) or container(nullptr)
153168 this ->wrapper_pointer = data_pointer;
@@ -177,13 +192,23 @@ void HContainer<T>::allocate(T* data_array, bool is_zero)
177192 size_t nnr = this ->get_nnr ();
178193 if (this ->allocated )
179194 {// delete existed memory of this->wrapper_pointer
195+ if (this ->allocated_size > 0 )
196+ {
197+ ModuleBase::Memory::record (" HContainer" , -(long long )this ->allocated_size , true );
198+ }
180199 delete[] this ->wrapper_pointer ;
181200 this ->allocated = false ;
201+ this ->allocated_size = 0 ;
182202 }
183203 if (data_array == nullptr )
184204 {
185205 // use this->wrapper_pointer as data_array
186206 this ->allocated = true ;
207+ this ->allocated_size = nnr * sizeof (T);
208+ if (this ->allocated_size > 0 )
209+ {
210+ ModuleBase::Memory::record (" HContainer" , (long long )this ->allocated_size , true );
211+ }
187212 this ->wrapper_pointer = new T[nnr];
188213 ModuleBase::GlobalFunc::ZEROS (this ->wrapper_pointer , nnr);
189214 data_array = this ->wrapper_pointer ;
0 commit comments