Skip to content

Commit bad686a

Browse files
authored
Merge branch 'develop' into perf/buffer-csr-output
2 parents 54d9e82 + 27be187 commit bad686a

40 files changed

Lines changed: 254 additions & 352 deletions

source/source_cell/mdcell.cpp

Lines changed: 49 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ void MDCell::sync_backing_unitcell_owned_atoms_()
8282
}
8383
}
8484

85-
#ifdef __MPI
86-
void MDCell::initialize_from_ucell_(UnitCell& ucell, MPI_Comm comm, double cutoff, double skin)
85+
void MDCell::initialize_from_unitcell(UnitCell& ucell,
86+
double skin,
87+
const ModuleBase::CommunicationDomain& comm_domain)
8788
{
8889
backing_unitcell_ = &ucell;
8990
nat_ = ucell.nat;
@@ -100,55 +101,21 @@ void MDCell::initialize_from_ucell_(UnitCell& ucell, MPI_Comm comm, double cutof
100101
type_masses_[static_cast<std::size_t>(it)] = ucell.atoms[it].mass;
101102
type_atom_counts_[static_cast<std::size_t>(it)] = ucell.atoms[it].na;
102103
}
103-
comm_ = comm;
104-
cutoff_ = cutoff;
104+
cutoff_ = 0.0;
105105
skin_ = skin;
106-
106+
neighbor_search_.reset();
107+
neighbor_layout_valid_ = false;
107108
owned_atoms_.clear();
108109
ghost_atoms_.clear();
109-
MPI_Comm_rank(comm_, &rank_);
110-
MPI_Comm_size(comm_, &size_);
111110

112-
decomp_.init(comm_, latvec_, lat0_, cutoff_, skin_);
113-
decomp_.split_owned_atoms_from_ucell(ucell, owned_atoms_);
114-
clear_forces_(owned_atoms_);
115-
exchange_ghost_atoms();
116-
}
117-
118-
void MDCell::initialize_from_owned_atoms_(MPI_Comm comm, double cutoff, double skin)
119-
{
120-
comm_ = comm;
121-
cutoff_ = cutoff;
122-
skin_ = skin;
111+
#ifdef __MPI
112+
comm_ = comm_domain.communicator();
123113
MPI_Comm_rank(comm_, &rank_);
124114
MPI_Comm_size(comm_, &size_);
125-
decomp_.init(comm_, latvec_, lat0_, cutoff_, skin_);
126-
clear_forces_(owned_atoms_);
127-
exchange_ghost_atoms();
128-
}
115+
decomp_.init(comm_, latvec_, lat0_, 0.0, 0.0);
116+
decomp_.split_owned_atoms_from_ucell(ucell, owned_atoms_);
129117
#else
130-
void MDCell::initialize_from_ucell_(UnitCell& ucell, double cutoff, double skin)
131-
{
132-
backing_unitcell_ = &ucell;
133-
nat_ = ucell.nat;
134-
lat0_ = ucell.lat0;
135-
omega_ = ucell.omega;
136-
latvec_ = ucell.latvec;
137-
gt_ = ucell.GT;
138-
type_labels_.resize(static_cast<std::size_t>(ucell.ntype));
139-
type_masses_.resize(static_cast<std::size_t>(ucell.ntype));
140-
type_atom_counts_.resize(static_cast<std::size_t>(ucell.ntype));
141-
for (int it = 0; it < ucell.ntype; ++it)
142-
{
143-
type_labels_[static_cast<std::size_t>(it)] = ucell.atoms[it].label;
144-
type_masses_[static_cast<std::size_t>(it)] = ucell.atoms[it].mass;
145-
type_atom_counts_[static_cast<std::size_t>(it)] = ucell.atoms[it].na;
146-
}
147-
cutoff_ = cutoff;
148-
skin_ = skin;
149-
owned_atoms_.clear();
150-
ghost_atoms_.clear();
151-
118+
static_cast<void>(comm_domain);
152119
for (int it = 0; it < ucell.ntype; ++it)
153120
{
154121
for (int ia = 0; ia < ucell.atoms[it].na; ++ia)
@@ -164,30 +131,9 @@ void MDCell::initialize_from_ucell_(UnitCell& ucell, double cutoff, double skin)
164131
0));
165132
}
166133
}
167-
exchange_ghost_atoms();
168-
}
169-
170-
void MDCell::initialize_from_owned_atoms_(double cutoff, double skin)
171-
{
172-
cutoff_ = cutoff;
173-
skin_ = skin;
174-
clear_forces_(owned_atoms_);
175-
exchange_ghost_atoms();
176-
}
177134
#endif
178135

179-
180-
void MDCell::initialize_from_unitcell(UnitCell& ucell,
181-
double cutoff,
182-
double skin,
183-
const ModuleBase::CommunicationDomain& comm_domain)
184-
{
185-
#ifdef __MPI
186-
initialize_from_ucell_(ucell, comm_domain.communicator(), cutoff, skin);
187-
#else
188-
static_cast<void>(comm_domain);
189-
initialize_from_ucell_(ucell, cutoff, skin);
190-
#endif
136+
clear_forces_(owned_atoms_);
191137
}
192138

193139
void MDCell::initialize_from_owned_atoms(const ModuleBase::Matrix3& latvec,
@@ -199,7 +145,6 @@ void MDCell::initialize_from_owned_atoms(const ModuleBase::Matrix3& latvec,
199145
const std::vector<std::string>& type_labels,
200146
const std::vector<double>& type_masses,
201147
const std::vector<std::int64_t>& type_atom_counts,
202-
double cutoff,
203148
double skin,
204149
const ModuleBase::CommunicationDomain& comm_domain)
205150
{
@@ -212,12 +157,42 @@ void MDCell::initialize_from_owned_atoms(const ModuleBase::Matrix3& latvec,
212157
type_labels_ = type_labels;
213158
type_masses_ = type_masses;
214159
type_atom_counts_ = type_atom_counts;
160+
backing_unitcell_ = nullptr;
161+
cutoff_ = 0.0;
162+
skin_ = skin;
163+
neighbor_search_.reset();
164+
neighbor_layout_valid_ = false;
165+
ghost_atoms_.clear();
215166
#ifdef __MPI
216-
initialize_from_owned_atoms_(comm_domain.communicator(), cutoff, skin);
167+
comm_ = comm_domain.communicator();
168+
MPI_Comm_rank(comm_, &rank_);
169+
MPI_Comm_size(comm_, &size_);
217170
#else
218171
static_cast<void>(comm_domain);
219-
initialize_from_owned_atoms_(cutoff, skin);
220172
#endif
173+
clear_forces_(owned_atoms_);
174+
}
175+
176+
void MDCell::initialize_neighbors(double cutoff)
177+
{
178+
if (cutoff <= 0.0)
179+
{
180+
throw std::runtime_error("MDCell neighbor cutoff must be positive.");
181+
}
182+
183+
cutoff_ = cutoff;
184+
neighbor_search_.reset();
185+
neighbor_layout_valid_ = false;
186+
187+
#ifdef __MPI
188+
if (comm_ == MPI_COMM_NULL)
189+
{
190+
throw std::runtime_error("MDCell communication domain is not initialized.");
191+
}
192+
decomp_.init(comm_, latvec_, lat0_, cutoff_, skin_);
193+
#endif
194+
195+
migrate_owned_atoms();
221196
}
222197

223198
#ifdef __MPI
@@ -341,6 +316,11 @@ void MDCell::migrate_owned_atoms()
341316

342317
void MDCell::prepare_neighbors()
343318
{
319+
if (cutoff_ <= 0.0)
320+
{
321+
throw std::runtime_error("MDCell neighbors must be initialized before use.");
322+
}
323+
344324
bool rebuild = !neighbor_layout_valid_ || neighbor_reference_frac_.size() != owned_atoms_.size();
345325
double local_max_displacement = 0.0;
346326
if (!rebuild)

source/source_cell/mdcell.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class MDCell : public BaseCell
3232
MDCell& operator=(MDCell&&);
3333

3434
void initialize_from_unitcell(UnitCell& ucell,
35-
double cutoff,
3635
double skin,
3736
const ModuleBase::CommunicationDomain& comm_domain);
3837
void initialize_from_owned_atoms(const ModuleBase::Matrix3& latvec,
@@ -44,10 +43,11 @@ class MDCell : public BaseCell
4443
const std::vector<std::string>& type_labels,
4544
const std::vector<double>& type_masses,
4645
const std::vector<std::int64_t>& type_atom_counts,
47-
double cutoff,
4846
double skin,
4947
const ModuleBase::CommunicationDomain& comm_domain);
5048

49+
void initialize_neighbors(double cutoff);
50+
5151
#ifdef __MPI
5252
int mpi_rank() const;
5353
int mpi_size() const;
@@ -74,7 +74,7 @@ class MDCell : public BaseCell
7474
std::vector<LocalAtom>& mutable_owned_atoms();
7575
std::vector<LocalAtom>& mutable_ghost_atoms();
7676

77-
int nlocal() const { return static_cast<int>(owned_atoms_.size()); }
77+
int nowned_atoms() const { return static_cast<int>(owned_atoms_.size()); }
7878
int nghost() const { return static_cast<int>(ghost_atoms_.size()); }
7979
double cutoff() const;
8080
bool has_backing_unitcell() const;
@@ -90,14 +90,6 @@ class MDCell : public BaseCell
9090
const ModuleBase::Matrix3& get_latvec() const override;
9191
const ModuleBase::Matrix3& get_GT() const override;
9292

93-
#ifdef __MPI
94-
void initialize_from_ucell_(UnitCell& ucell, MPI_Comm comm, double cutoff, double skin);
95-
void initialize_from_owned_atoms_(MPI_Comm comm, double cutoff, double skin);
96-
#else
97-
void initialize_from_ucell_(UnitCell& ucell, double cutoff, double skin);
98-
void initialize_from_owned_atoms_(double cutoff, double skin);
99-
#endif
100-
10193
void sync_backing_unitcell_geometry_();
10294
void sync_backing_unitcell_owned_atoms_();
10395
void clear_forces_(std::vector<LocalAtom>& atoms);

source/source_cell/mdcell_reader.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,13 @@ std::vector<LocalAtom> read_owned_atoms(std::ifstream& ifs,
187187
const ModuleBase::Matrix3& primitive_latvec,
188188
const ModuleBase::Matrix3& primitive_gt,
189189
const std::vector<int>& cell_replica,
190-
double cutoff,
191-
double skin,
192190
std::int64_t& nat,
193191
const ModuleBase::CommunicationDomain& comm_domain)
194192
{
195193
int rank = 0;
196194
#ifdef __MPI
197195
DomainDecomposition decomposition;
198-
decomposition.init(comm_domain.communicator(), metadata.latvec, metadata.lat0, cutoff, skin);
196+
decomposition.init(comm_domain.communicator(), metadata.latvec, metadata.lat0, 0.0, 0.0);
199197
rank = comm_domain.rank();
200198
#endif
201199

@@ -324,16 +322,10 @@ std::vector<LocalAtom> read_owned_atoms(std::ifstream& ifs,
324322
} // namespace
325323

326324
MDCell MDCellReader::read_stru(const std::string& stru_file,
327-
const std::vector<int>& cell_replica,
328-
double cutoff,
329-
double skin,
330-
const ModuleBase::CommunicationDomain& comm_domain)
325+
const std::vector<int>& cell_replica,
326+
double skin,
327+
const ModuleBase::CommunicationDomain& comm_domain)
331328
{
332-
if (cutoff <= 0.0)
333-
{
334-
throw std::runtime_error("MDCell requires a positive cutoff.");
335-
}
336-
337329
std::ifstream ifs(stru_file.c_str(), std::ios::in);
338330
if (!ifs)
339331
{
@@ -354,7 +346,7 @@ MDCell MDCellReader::read_stru(const std::string& stru_file,
354346
metadata.omega = std::abs(metadata.latvec.Det()) * metadata.lat0 * metadata.lat0 * metadata.lat0;
355347
std::int64_t nat = 0;
356348
const std::vector<LocalAtom> owned_atoms = read_owned_atoms(ifs, metadata, primitive_latvec, primitive_gt,
357-
cell_replica, cutoff, skin, nat, comm_domain);
349+
cell_replica, nat, comm_domain);
358350
MDCell mdcell;
359351
mdcell.initialize_from_owned_atoms(metadata.latvec,
360352
metadata.gt,
@@ -365,7 +357,6 @@ MDCell MDCellReader::read_stru(const std::string& stru_file,
365357
metadata.labels,
366358
metadata.masses,
367359
metadata.type_atom_counts,
368-
cutoff,
369360
skin,
370361
comm_domain);
371362
mdcell.mutable_stru_meta() = metadata.stru_meta;

source/source_cell/mdcell_reader.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class MDCellReader
1515
public:
1616
static MDCell read_stru(const std::string& stru_file,
1717
const std::vector<int>& cell_replica,
18-
double cutoff,
1918
double skin,
2019
const ModuleBase::CommunicationDomain& comm_domain);
2120
};

source/source_cell/module_neighlist/bin_manager.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,16 @@ void BinManager::build_atom_neighbors(
182182
const std::vector<NeighborAtom>& binned_atoms
183183
)
184184
{
185-
assert(atoms.size() == static_cast<size_t>(neighbor_list.get_nlocal()));
185+
assert(atoms.size() == static_cast<size_t>(neighbor_list.get_ncentral_atoms()));
186186

187187
double sradius2 = sradius_ * sradius_;
188188

189189
neighbor_list.reset();
190190

191191
std::vector<int> neigh_tmp;
192192

193-
const int nlocal = neighbor_list.get_nlocal();
194-
for (int i = 0; i < nlocal; i++)
193+
const int ncentral_atoms = neighbor_list.get_ncentral_atoms();
194+
for (int i = 0; i < ncentral_atoms; i++)
195195
{
196196
neigh_tmp.clear();
197197
const NeighborAtom& atom = atoms[i];

source/source_cell/module_neighlist/neighbor_list.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ class NeighborList
1212
NeighborList() = default;
1313
~NeighborList() = default;
1414

15-
void initialize(std::size_t nlocal, std::size_t pgsize)
15+
void initialize(std::size_t ncentral_atoms, std::size_t pgsize)
1616
{
17-
nlocal_ = ModuleNeighList::checked_int_size(nlocal, "NeighborList local atom count");
17+
ncentral_atoms_ = ModuleNeighList::checked_int_size(ncentral_atoms, "NeighborList central atom count");
1818
allocator_.initialize(ModuleNeighList::checked_int_size(pgsize, "NeighborList page size"));
19-
numneigh_.assign(nlocal, 0);
20-
firstneigh_.assign(nlocal, nullptr);
19+
numneigh_.assign(ncentral_atoms, 0);
20+
firstneigh_.assign(ncentral_atoms, nullptr);
2121
}
2222

2323
void reset()
2424
{
2525
allocator_.reset();
2626
}
2727

28-
int get_nlocal() const { return nlocal_; }
28+
int get_ncentral_atoms() const { return ncentral_atoms_; }
2929
int get_numneigh(int i) const { return numneigh_[i]; }
3030
int* get_firstneigh(int i) { return firstneigh_[i]; }
3131
const int* get_firstneigh(int i) const { return firstneigh_[i]; }
@@ -43,7 +43,7 @@ class NeighborList
4343
}
4444

4545
private:
46-
int nlocal_ = 0;
46+
int ncentral_atoms_ = 0;
4747
std::vector<int> numneigh_;
4848
std::vector<int*> firstneigh_;
4949
PageAllocator allocator_;

source/source_cell/module_neighlist/neighbor_search.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ void NeighborSearch::filter_candidate_neighbors_(double cutoff, double lat0)
202202
const double cutoff2 = cutoff * cutoff;
203203
neighbor_list_.reset();
204204
std::vector<int> active;
205-
for (int i = 0; i < candidate_neighbor_list_.get_nlocal(); ++i)
205+
for (int i = 0; i < candidate_neighbor_list_.get_ncentral_atoms(); ++i)
206206
{
207207
active.clear();
208208
const NeighborAtom& center = all_atoms_[static_cast<std::size_t>(i)];

source/source_cell/module_neighlist/test/bin_manager_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ TEST(BinManagerUnit, EmptyAtomsBuildNeighbors)
8282
nl.initialize(0, 16);
8383

8484
bm.build_atom_neighbors(nl, atoms, atoms);
85-
EXPECT_EQ(nl.get_nlocal(), 0);
85+
EXPECT_EQ(nl.get_ncentral_atoms(), 0);
8686
}
8787

8888
TEST(BinManagerUnit, BoundaryAndExactRadius)
@@ -166,7 +166,7 @@ TEST(BinManagerUnit, GhostAtomsAreCounted)
166166

167167
bm.build_atom_neighbors(nl, inside, all_atoms);
168168

169-
EXPECT_EQ(nl.get_nlocal(), 1);
169+
EXPECT_EQ(nl.get_ncentral_atoms(), 1);
170170
EXPECT_EQ(nl.get_numneigh(0), 1);
171171
bool found = false;
172172
if (nl.get_numneigh(0) > 0 && nl.get_firstneigh(0) != nullptr) {

0 commit comments

Comments
 (0)