Skip to content

Commit 05b44ce

Browse files
19helloFei Yang
andauthored
Restore default constructor (deepmodeling#7896)
* refactor(neighbor): make page allocation explicit * refactor(parallel): initialize communication domains explicitly * refactor(md): initialize MDCell explicitly --------- Co-authored-by: Fei Yang <2501213217@stu.pku.edu.cn>
1 parent c567031 commit 05b44ce

19 files changed

Lines changed: 134 additions & 100 deletions

source/source_base/parallel_cell.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
namespace ModuleBase
44
{
5-
CommunicationDomain::CommunicationDomain()
6-
{
7-
}
8-
95
#ifdef __MPI
10-
CommunicationDomain::CommunicationDomain(MPI_Comm communicator) : communicator_(communicator)
6+
void CommunicationDomain::initialize(MPI_Comm communicator)
117
{
8+
communicator_ = communicator;
9+
rank_ = 0;
1210
if (communicator_ != MPI_COMM_NULL)
1311
{
1412
MPI_Comm_rank(communicator_, &rank_);
@@ -28,10 +26,10 @@ int CommunicationDomain::rank() const
2826

2927
CommunicationDomain world_communication_domain()
3028
{
29+
CommunicationDomain communication_domain;
3130
#ifdef __MPI
32-
return CommunicationDomain(MPI_COMM_WORLD);
33-
#else
34-
return CommunicationDomain();
31+
communication_domain.initialize(MPI_COMM_WORLD);
3532
#endif
33+
return communication_domain;
3634
}
3735
} // namespace ModuleBase

source/source_base/parallel_cell.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ namespace ModuleBase
1010
class CommunicationDomain
1111
{
1212
public:
13-
CommunicationDomain();
13+
CommunicationDomain() = default;
1414
#ifdef __MPI
15-
explicit CommunicationDomain(MPI_Comm communicator);
15+
void initialize(MPI_Comm communicator);
1616
MPI_Comm communicator() const;
1717
#endif
1818
int rank() const;

source/source_cell/distributed_mdcell_reader.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -356,18 +356,19 @@ MDCell DistributedMDCellReader::read_stru(const std::string& stru_file,
356356
std::int64_t nat = 0;
357357
const std::vector<LocalAtom> owned_atoms = read_owned_atoms(ifs, metadata, primitive_latvec, primitive_gt,
358358
cell_replica, cutoff, skin, nat, communication_domain);
359-
MDCell mdcell(metadata.latvec,
360-
metadata.gt,
361-
metadata.lat0,
362-
metadata.omega,
363-
nat,
364-
owned_atoms,
365-
metadata.labels,
366-
metadata.masses,
367-
metadata.type_atom_counts,
368-
cutoff,
369-
skin,
370-
communication_domain);
359+
MDCell mdcell;
360+
mdcell.initialize_from_owned_atoms(metadata.latvec,
361+
metadata.gt,
362+
metadata.lat0,
363+
metadata.omega,
364+
nat,
365+
owned_atoms,
366+
metadata.labels,
367+
metadata.masses,
368+
metadata.type_atom_counts,
369+
cutoff,
370+
skin,
371+
communication_domain);
371372
stru_metadata = metadata.stru_file_metadata;
372373
return mdcell;
373374
}

source/source_cell/md_cell.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <cmath>
99
#include <stdexcept>
1010

11+
MDCell::MDCell() = default;
1112
MDCell::~MDCell() = default;
1213
MDCell::MDCell(MDCell&&) = default;
1314
MDCell& MDCell::operator=(MDCell&&) = default;
@@ -176,10 +177,10 @@ void MDCell::initialize_from_owned_atoms_(double cutoff, double skin)
176177
#endif
177178

178179

179-
MDCell::MDCell(UnitCell& ucell,
180-
double cutoff,
181-
double skin,
182-
const ModuleBase::CommunicationDomain& communication_domain)
180+
void MDCell::initialize_from_unitcell(UnitCell& ucell,
181+
double cutoff,
182+
double skin,
183+
const ModuleBase::CommunicationDomain& communication_domain)
183184
{
184185
#ifdef __MPI
185186
initialize_from_ucell_(ucell, communication_domain.communicator(), cutoff, skin);
@@ -189,18 +190,18 @@ MDCell::MDCell(UnitCell& ucell,
189190
#endif
190191
}
191192

192-
MDCell::MDCell(const ModuleBase::Matrix3& latvec,
193-
const ModuleBase::Matrix3& gt,
194-
double lat0,
195-
double omega,
196-
std::int64_t nat,
197-
const std::vector<LocalAtom>& owned_atoms,
198-
const std::vector<std::string>& type_labels,
199-
const std::vector<double>& type_masses,
200-
const std::vector<std::int64_t>& type_atom_counts,
201-
double cutoff,
202-
double skin,
203-
const ModuleBase::CommunicationDomain& communication_domain)
193+
void MDCell::initialize_from_owned_atoms(const ModuleBase::Matrix3& latvec,
194+
const ModuleBase::Matrix3& gt,
195+
double lat0,
196+
double omega,
197+
std::int64_t nat,
198+
const std::vector<LocalAtom>& owned_atoms,
199+
const std::vector<std::string>& type_labels,
200+
const std::vector<double>& type_masses,
201+
const std::vector<std::int64_t>& type_atom_counts,
202+
double cutoff,
203+
double skin,
204+
const ModuleBase::CommunicationDomain& communication_domain)
204205
{
205206
latvec_ = latvec;
206207
gt_ = gt;

source/source_cell/md_cell.h

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,29 @@ class CommunicationDomain;
2323
class MDCell : public BaseCell
2424
{
2525
public:
26+
MDCell();
2627
~MDCell();
2728
MDCell(const MDCell&) = delete;
2829
MDCell& operator=(const MDCell&) = delete;
2930
MDCell(MDCell&&);
3031
MDCell& operator=(MDCell&&);
3132

32-
MDCell(UnitCell& ucell,
33-
double cutoff,
34-
double skin,
35-
const ModuleBase::CommunicationDomain& communication_domain);
36-
MDCell(const ModuleBase::Matrix3& latvec,
37-
const ModuleBase::Matrix3& gt,
38-
double lat0,
39-
double omega,
40-
std::int64_t nat,
41-
const std::vector<LocalAtom>& owned_atoms,
42-
const std::vector<std::string>& type_labels,
43-
const std::vector<double>& type_masses,
44-
const std::vector<std::int64_t>& type_atom_counts,
45-
double cutoff,
46-
double skin,
47-
const ModuleBase::CommunicationDomain& communication_domain);
33+
void initialize_from_unitcell(UnitCell& ucell,
34+
double cutoff,
35+
double skin,
36+
const ModuleBase::CommunicationDomain& communication_domain);
37+
void initialize_from_owned_atoms(const ModuleBase::Matrix3& latvec,
38+
const ModuleBase::Matrix3& gt,
39+
double lat0,
40+
double omega,
41+
std::int64_t nat,
42+
const std::vector<LocalAtom>& owned_atoms,
43+
const std::vector<std::string>& type_labels,
44+
const std::vector<double>& type_masses,
45+
const std::vector<std::int64_t>& type_atom_counts,
46+
double cutoff,
47+
double skin,
48+
const ModuleBase::CommunicationDomain& communication_domain);
4849

4950
#ifdef __MPI
5051
int mpi_rank() const;

source/source_cell/module_neighlist/neighbor_list.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class NeighborList
1515
void initialize(std::size_t nlocal, std::size_t pgsize)
1616
{
1717
nlocal_ = ModuleNeighList::checked_int_size(nlocal, "NeighborList local atom count");
18-
allocator_ = PageAllocator(ModuleNeighList::checked_int_size(pgsize, "NeighborList page size"));
18+
allocator_.initialize(ModuleNeighList::checked_int_size(pgsize, "NeighborList page size"));
1919
numneigh_.assign(nlocal, 0);
2020
firstneigh_.assign(nlocal, nullptr);
2121
}

source/source_cell/module_neighlist/page_allocator.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,16 @@
44
#include <stdexcept>
55
#include <utility>
66

7-
PageAllocator::PageAllocator() : pgsize_(default_pgsize)
7+
void PageAllocator::initialize(int pgsize)
88
{
9-
new_page_();
10-
}
11-
12-
PageAllocator::PageAllocator(int pgsize) : pgsize_(pgsize)
13-
{
14-
if (pgsize_ <= 0)
9+
if (pgsize <= 0)
1510
{
1611
throw std::invalid_argument("PageAllocator page size must be positive.");
1712
}
18-
new_page_();
19-
}
2013

21-
PageAllocator::~PageAllocator() = default;
14+
pgsize_ = pgsize;
15+
pages_.clear();
16+
}
2217

2318
int* PageAllocator::allocate(int n)
2419
{
@@ -56,6 +51,11 @@ int* PageAllocator::allocate(int n)
5651

5752
void PageAllocator::reset()
5853
{
54+
if (pages_.empty())
55+
{
56+
return;
57+
}
58+
5959
pages_.resize(1);
6060
pages_[0].offset = 0;
6161
}

source/source_cell/module_neighlist/page_allocator.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ class PageAllocator
88
public:
99
enum { default_pgsize = 1024 };
1010

11-
PageAllocator();
12-
explicit PageAllocator(int pgsize);
13-
~PageAllocator();
14-
11+
PageAllocator() = default;
1512
PageAllocator(const PageAllocator&) = delete;
1613
PageAllocator& operator=(const PageAllocator&) = delete;
1714
PageAllocator(PageAllocator&&) = default;
1815
PageAllocator& operator=(PageAllocator&&) = default;
1916

17+
void initialize(int pgsize);
2018
int* allocate(int n);
2119
void reset();
2220
int get_pgsize() const;
@@ -30,9 +28,9 @@ class PageAllocator
3028
};
3129

3230
std::vector<Page> pages_;
33-
int pgsize_ = 0;
31+
int pgsize_ = default_pgsize;
3432

3533
void new_page_();
3634
};
3735

38-
#endif // PAGE_ALLOCATOR_H
36+
#endif // PAGE_ALLOCATOR_H

source/source_cell/module_neighlist/test/distributed_mdcell_reader_test.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
static_assert(!std::is_copy_constructible<MDCell>::value, "MDCell must not be copy constructible.");
2121
static_assert(!std::is_copy_assignable<MDCell>::value, "MDCell must not be copy assignable.");
22+
static_assert(std::is_default_constructible<MDCell>::value, "MDCell must be default constructible.");
2223
static_assert(std::is_move_constructible<MDCell>::value, "MDCell must be move constructible.");
2324

2425
namespace
@@ -75,7 +76,8 @@ TEST(DistributedMDCellReaderTest, ReadOwnedAtomsFromSTRUWithoutUnitCell)
7576

7677
MPI_Comm md_comm = MPI_COMM_NULL;
7778
MPI_Comm_split(MPI_COMM_WORLD, world_rank % 2, world_rank, &md_comm);
78-
const ModuleBase::CommunicationDomain communication_domain(md_comm);
79+
ModuleBase::CommunicationDomain communication_domain;
80+
communication_domain.initialize(md_comm);
7981

8082
MdStruFileMetadata stru_metadata;
8183
MDCell mdcell = DistributedMDCellReader::read_stru(stru_file,
@@ -212,7 +214,8 @@ TEST(DistributedMDCellReaderTest, RestartStruPreservesAtomRecordsAcrossRanks)
212214
lattice.e11 = 20.0;
213215
lattice.e22 = 20.0;
214216
lattice.e33 = 20.0;
215-
MDCell mdcell(lattice,
217+
MDCell mdcell;
218+
mdcell.initialize_from_owned_atoms(lattice,
216219
lattice.Inverse(),
217220
1.0,
218221
1.0,

source/source_cell/module_neighlist/test/md_cell_migrate_mpi_test.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ TEST(MdCellMigrateMpiTest, AtomCrossingDomainMigratesToNewOwner)
5656
rank,
5757
rank));
5858
}
59-
MDCell mdcell(latvec,
59+
MDCell mdcell;
60+
mdcell.initialize_from_owned_atoms(latvec,
6061
latvec.Inverse(),
6162
1.0,
6263
1.0,
@@ -123,7 +124,8 @@ TEST(MdCellMigrateMpiTest, GhostForcesReturnToOwners)
123124
0,
124125
rank,
125126
rank));
126-
MDCell mdcell(latvec,
127+
MDCell mdcell;
128+
mdcell.initialize_from_owned_atoms(latvec,
127129
latvec.Inverse(),
128130
1.0,
129131
1.0,
@@ -178,7 +180,8 @@ TEST(MdCellMigrateMpiTest, SkinUpdatesFixedGhostLayoutBeforeRebuild)
178180
0,
179181
rank,
180182
rank));
181-
MDCell mdcell(latvec,
183+
MDCell mdcell;
184+
mdcell.initialize_from_owned_atoms(latvec,
182185
latvec.Inverse(),
183186
1.0,
184187
1.0,

0 commit comments

Comments
 (0)