Skip to content

Commit f9a265a

Browse files
authored
Fix memory recorder (deepmodeling#6952)
1 parent be3b24c commit f9a265a

8 files changed

Lines changed: 51 additions & 3 deletions

File tree

source/source_base/memory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ double Memory::record
159159
void Memory::record
160160
(
161161
const std::string &name_in,
162-
const size_t &n_in,
162+
const long &n_in,
163163
const bool accumulate
164164
)
165165
{

source/source_base/memory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Memory
4545
*/
4646
static void record(
4747
const std::string &name_in,
48-
const size_t &n_in,
48+
const long &n_in,
4949
const bool accumulate = false
5050
);
5151

source/source_lcao/module_gint/biggrid_info.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "biggrid_info.h"
22
#include "gint_helper.h"
33
#include "gint_type.h"
4+
#include "source_base/memory.h"
45

56
namespace ModuleGint
67
{
@@ -44,6 +45,12 @@ BigGridInfo::BigGridInfo(
4445
meshgrid_coords_[index_1d] =
4546
meshgrid_info_->get_cartesian_coord(mgrid_idx_1Dto3D(index_1d));
4647
}
48+
ModuleBase::Memory::record("BigGridInfo::meshgrid_coords", (long long)nmxyz_ * sizeof(Vec3d), true);
49+
}
50+
51+
BigGridInfo::~BigGridInfo()
52+
{
53+
ModuleBase::Memory::record("BigGridInfo::meshgrid_coords", -(long long)nmxyz_ * sizeof(Vec3d), true);
4754
}
4855

4956
Vec3i BigGridInfo::max_ext_bgrid_num(double r) const

source/source_lcao/module_gint/biggrid_info.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class BigGridInfo
2121
Vec3d biggrid_vec2,
2222
Vec3d biggrid_vec3,
2323
int nmx, int nmy, int nmz);
24+
25+
~BigGridInfo();
2426

2527
Vec3d get_cartesian_coord(const Vec3d& index_3d) const { return index_3d * biggrid_latvec0_; }
2628
Vec3d get_cartesian_coord(const Vec3i& index_3d) const { return index_3d * biggrid_latvec0_; }

source/source_lcao/module_gint/gint_info.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "source_base/timer.h"
55
#include "gint_info.h"
66
#include "gint_type.h"
7+
#include "source_base/memory.h"
78

89
namespace ModuleGint
910
{
@@ -62,6 +63,12 @@ GintInfo::GintInfo(
6263
#endif
6364
}
6465

66+
GintInfo::~GintInfo()
67+
{
68+
ModuleBase::Memory::record("GintInfo::trace_lo_", -(long long)(sizeof(int) * trace_lo_.size()), true);
69+
ModuleBase::Memory::record("GintInfo::ijr_info_", -(long long)(sizeof(int) * ijr_info_.size()), true);
70+
}
71+
6572
template <typename T>
6673
HContainer<T> GintInfo::get_hr(int npol) const
6774
{
@@ -194,6 +201,7 @@ void GintInfo::init_trace_lo_(const UnitCell& ucell, const int nspin)
194201
++iat;
195202
}
196203
}
204+
ModuleBase::Memory::record("GintInfo::trace_lo_", (long long)(sizeof(int) * trace_lo_.size()), true);
197205
}
198206

199207
void GintInfo::init_ijr_info_(const UnitCell& ucell, Grid_Driver& gd)
@@ -260,6 +268,7 @@ void GintInfo::init_ijr_info_(const UnitCell& ucell, Grid_Driver& gd)
260268
}
261269
}
262270
this->ijr_info_ = hr_gint_local.get_ijr_info();
271+
ModuleBase::Memory::record("GintInfo::ijr_info_", (long long)(sizeof(int) * ijr_info_.size()), true);
263272
return;
264273
}
265274

source/source_lcao/module_gint/gint_info.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class GintInfo
3333
const Numerical_Orbital* Phi,
3434
const UnitCell& ucell, Grid_Driver& gd);
3535

36+
~GintInfo();
37+
3638
// getter functions
3739
const std::vector<std::shared_ptr<BigGrid>>& get_biggrids() { return biggrids_; }
3840
int get_bgrids_num() const { return static_cast<int>(biggrids_.size()); }

source/source_lcao/module_hcontainer/hcontainer.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "hcontainer.h"
2+
#include "source_base/memory.h"
23

34
namespace 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

1924
template <typename T>
20-
HContainer<T>::HContainer() {}
25+
HContainer<T>::HContainer() : allocated_size(0) {}
2126

2227
// copy constructor
2328
template <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>
148162
HContainer<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;

source/source_lcao/module_hcontainer/hcontainer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,9 @@ class HContainer
475475
// sparse table for (atom_i, atom_j)->index of atom_pairs
476476
std::vector<std::vector<int>> sparse_ap;
477477
std::vector<std::vector<int>> sparse_ap_index;
478+
479+
// record allocated memory size
480+
size_t allocated_size = 0;
478481

479482
/**
480483
* @brief temporary atom-pair lists to loop selected R index

0 commit comments

Comments
 (0)