Skip to content

Commit 4fcd312

Browse files
author
Han Wang
committed
refactor(nlist): move nprocs into InputNlist constructors
Replaces the ``set_nprocs(comm->nprocs)`` setter with a constructor parameter on both ``InputNlist`` overloads (lightweight and comm-aware) and the matching ``DP_NewNlist`` / ``DP_NewNlist_comm`` C entry points plus the hpp wrappers. ``nprocs`` is a conceptual sibling of ``world`` and ``nswap`` and lives more naturally in the initializer than as a post-construction setter. Default value 1 keeps direct C++ API consumers source-compatible — they need not pass anything. - ``InputNlist`` lightweight ctor: new trailing ``int nprocs_ = 1``. - ``InputNlist`` comm-aware ctor: new trailing ``int nprocs_ = 1``. - ``DP_NewNlist`` / ``DP_NewNlist_comm``: new trailing ``int nprocs`` arg (no default — C API). - ``deepmd::hpp::InputNlist`` ctors: new trailing ``int nprocs = 1``. - ``DP_NlistSetNprocs`` / ``set_nprocs`` removed (single-use in-tree). - ``pair_deepmd.cpp``, ``pair_deepspin.cpp``, ``fix_dplr.cpp`` now pass ``comm->nprocs`` directly in the constructor call. - Update stale comments in ``DeepPotPTExpt.cc`` and ``DeepSpinPTExpt.cc`` to reference the constructor parameter instead of the removed setter.
1 parent 818040f commit 4fcd312

9 files changed

Lines changed: 53 additions & 66 deletions

File tree

source/api_c/include/c_api.h

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ typedef struct DP_Nlist DP_Nlist;
2727
* @param[in] Array stores the core region atom's neighbor index
2828
* @returns A pointer to the neighbor list.
2929
**/
30-
extern DP_Nlist* DP_NewNlist(int inum_,
31-
int* ilist_,
32-
int* numneigh_,
33-
int** firstneigh_);
30+
extern DP_Nlist* DP_NewNlist(
31+
int inum_, int* ilist_, int* numneigh_, int** firstneigh_, int nprocs);
3432
/**
3533
* @brief Create a new neighbor list with communication capabilities.
3634
* @details This function extends DP_NewNlist by adding support for parallel
@@ -52,6 +50,9 @@ extern DP_Nlist* DP_NewNlist(int inum_,
5250
* each swap.
5351
* @param[in] world Pointer to the MPI communicator or similar communication
5452
* world used for the operation.
53+
* @param[in] nprocs Number of MPI ranks (1 = single-rank). Used by
54+
* ``DeepPotPTExpt`` / ``DeepSpinPTExpt`` to choose between the regular
55+
* and with-comm artifacts. Defaults to 1 if not supplied.
5556
* @returns A pointer to the initialized neighbor list with communication
5657
* capabilities.
5758
*/
@@ -66,7 +67,8 @@ extern DP_Nlist* DP_NewNlist_comm(int inum_,
6667
int** sendlist,
6768
int* sendproc,
6869
int* recvproc,
69-
void* world);
70+
void* world,
71+
int nprocs);
7072

7173
/**
7274
* @brief Set mask for a neighbor list.
@@ -88,16 +90,6 @@ extern void DP_NlistSetMask(DP_Nlist* nl, int mask);
8890
**/
8991
extern void DP_NlistSetMapping(DP_Nlist* nl, int* mapping);
9092

91-
/**
92-
* @brief Set the number of MPI ranks for a neighbor list.
93-
*
94-
* @param nl Neighbor list.
95-
* @param nprocs Number of MPI ranks (1 = single-rank).
96-
* @since API version 26
97-
*
98-
**/
99-
extern void DP_NlistSetNprocs(DP_Nlist* nl, int nprocs);
100-
10193
/**
10294
* @brief Delete a neighbor list.
10395
*

source/api_c/include/deepmd.hpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -809,15 +809,16 @@ struct InputNlist {
809809
ilist(nullptr),
810810
numneigh(nullptr),
811811
firstneigh(nullptr),
812-
nl(DP_NewNlist(0, nullptr, nullptr, nullptr)) {
812+
nl(DP_NewNlist(0, nullptr, nullptr, nullptr, 1)) {
813813
DP_CHECK_OK(DP_NlistCheckOK, nl);
814814
};
815-
InputNlist(int inum_, int* ilist_, int* numneigh_, int** firstneigh_)
815+
InputNlist(
816+
int inum_, int* ilist_, int* numneigh_, int** firstneigh_, int nprocs = 1)
816817
: inum(inum_),
817818
ilist(ilist_),
818819
numneigh(numneigh_),
819820
firstneigh(firstneigh_),
820-
nl(DP_NewNlist(inum_, ilist_, numneigh_, firstneigh_)) {
821+
nl(DP_NewNlist(inum_, ilist_, numneigh_, firstneigh_, nprocs)) {
821822
DP_CHECK_OK(DP_NlistCheckOK, nl);
822823
};
823824
InputNlist(int inum_,
@@ -831,7 +832,8 @@ struct InputNlist {
831832
int** sendlist,
832833
int* sendproc,
833834
int* recvproc,
834-
void* world)
835+
void* world,
836+
int nprocs = 1)
835837
: inum(inum_),
836838
ilist(ilist_),
837839
numneigh(numneigh_),
@@ -847,7 +849,8 @@ struct InputNlist {
847849
sendlist,
848850
sendproc,
849851
recvproc,
850-
world)) {};
852+
world,
853+
nprocs)) {};
851854
~InputNlist() { DP_DeleteNlist(nl); };
852855
/// @brief C API neighbor list.
853856
DP_Nlist* nl;
@@ -868,11 +871,6 @@ struct InputNlist {
868871
* @param mapping mapping from all atoms to real atoms, in size nall.
869872
*/
870873
void set_mapping(int* mapping) { DP_NlistSetMapping(nl, mapping); };
871-
/**
872-
* @brief Set the number of MPI ranks for this neighbor list.
873-
* @param nprocs Number of MPI ranks (1 = single-rank).
874-
*/
875-
void set_nprocs(int nprocs) { DP_NlistSetNprocs(nl, nprocs); };
876874
};
877875

878876
/**
@@ -900,7 +898,7 @@ void inline convert_nlist(InputNlist& to_nlist,
900898
// delete the original nl
901899
DP_DeleteNlist(to_nlist.nl);
902900
to_nlist.nl = DP_NewNlist(to_nlist.inum, to_nlist.ilist, to_nlist.numneigh,
903-
to_nlist.firstneigh);
901+
to_nlist.firstneigh, 1);
904902
}
905903
/**
906904
* @brief Deep Potential Base Model.

source/api_c/src/c_api.cc

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ extern "C" {
1616
DP_Nlist::DP_Nlist() {}
1717
DP_Nlist::DP_Nlist(deepmd::InputNlist& nl) : nl(nl) {}
1818

19-
DP_Nlist* DP_NewNlist(int inum_,
20-
int* ilist_,
21-
int* numneigh_,
22-
int** firstneigh_) {
23-
DP_NEW_OK(DP_Nlist,
24-
deepmd::InputNlist nl(inum_, ilist_, numneigh_, firstneigh_);
19+
DP_Nlist* DP_NewNlist(
20+
int inum_, int* ilist_, int* numneigh_, int** firstneigh_, int nprocs) {
21+
DP_NEW_OK(DP_Nlist, deepmd::InputNlist nl(inum_, ilist_, numneigh_,
22+
firstneigh_, nprocs);
2523
DP_Nlist* new_nl = new DP_Nlist(nl); return new_nl;)
2624
}
2725
DP_Nlist* DP_NewNlist_comm(int inum_,
@@ -35,18 +33,18 @@ DP_Nlist* DP_NewNlist_comm(int inum_,
3533
int** sendlist,
3634
int* sendproc,
3735
int* recvproc,
38-
void* world) {
36+
void* world,
37+
int nprocs) {
3938
deepmd::InputNlist nl(inum_, ilist_, numneigh_, firstneigh_, nswap, sendnum,
40-
recvnum, firstrecv, sendlist, sendproc, recvproc,
41-
world);
39+
recvnum, firstrecv, sendlist, sendproc, recvproc, world,
40+
nprocs);
4241
DP_Nlist* new_nl = new DP_Nlist(nl);
4342
return new_nl;
4443
}
4544
void DP_NlistSetMask(DP_Nlist* nl, int mask) { nl->nl.set_mask(mask); }
4645
void DP_NlistSetMapping(DP_Nlist* nl, int* mapping) {
4746
nl->nl.set_mapping(mapping);
4847
}
49-
void DP_NlistSetNprocs(DP_Nlist* nl, int nprocs) { nl->nl.set_nprocs(nprocs); }
5048
void DP_DeleteNlist(DP_Nlist* nl) { delete nl; }
5149

5250
// DP Base Model

source/api_cc/src/DeepPotPTExpt.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,11 @@ void DeepPotPTExpt::compute(ENERGYVTYPE& ener,
367367

368368
// Dispatch decision: use the with-comm artifact when LAMMPS is running
369369
// multi-rank. ``lmp_list.nprocs > 1`` is the direct predicate;
370-
// ``pair_deepmd.cpp`` populates it via ``set_nprocs(comm->nprocs)``.
371-
// Earlier drafts used ``nswap > 0`` as a proxy, but that breaks for
372-
// ``atom_style spin`` (which emits nswap > 0 even in single-rank to
373-
// propagate PBC ghost spins). ``nprocs`` is unambiguous.
370+
// LAMMPS pair styles populate it by passing ``comm->nprocs`` to the
371+
// ``InputNlist`` constructor. Earlier drafts used ``nswap > 0`` as a
372+
// proxy, but that breaks for ``atom_style spin`` (which emits
373+
// nswap > 0 even in single-rank to propagate PBC ghost spins).
374+
// ``nprocs`` is unambiguous.
374375
//
375376
// The regular artifact uses ``mapping`` to gather ghost-atom features
376377
// from local-atom embeddings (``index_select(node_ebd[1, nloc, dim],

source/api_cc/src/DeepSpinPTExpt.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,11 @@ void DeepSpinPTExpt::compute(ENERGYVTYPE& ener,
381381
// ghost→local mapping); multi-rank without a with-comm artifact cannot
382382
// drive border_op (no inter-rank exchange tensor). Both unsupported
383383
// combinations fail-fast for every caller.
384-
// ``nprocs > 1`` is the direct multi-rank predicate (set by
385-
// pair_deepspin via ``lmp_list.set_nprocs(comm->nprocs)``). Earlier
386-
// drafts used ``nswap > 0`` as a proxy, but atom_style spin emits
387-
// nswap > 0 even in single-rank, so the proxy is unsound.
384+
// ``nprocs > 1`` is the direct multi-rank predicate (LAMMPS pair
385+
// styles set it by passing ``comm->nprocs`` to the ``InputNlist``
386+
// constructor). Earlier drafts used ``nswap > 0`` as a proxy, but
387+
// atom_style spin emits nswap > 0 even in single-rank, so the proxy
388+
// is unsound.
388389
bool multi_rank = (lmp_list.nprocs > 1);
389390
bool atom_map_present = (lmp_list.mapping != nullptr);
390391
bool use_with_comm = has_comm_artifact_ && multi_rank;

source/lib/include/neighbor_list.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ struct InputNlist {
4646
int mask = 0xFFFFFFFF;
4747
/// mapping from all atoms to real atoms, in the size of nall
4848
int* mapping = nullptr;
49-
/// number of MPI ranks (1 = single-rank). Populated by LAMMPS pair
50-
/// styles from ``comm->nprocs``; defaults to 1 for direct C++
51-
/// callers that don't set it. Use this — NOT ``nswap > 0`` — as the
49+
/// number of MPI ranks (1 = single-rank). Passed by LAMMPS pair
50+
/// styles as the trailing ``nprocs_`` argument of the comm-aware
51+
/// constructor (sourced from ``comm->nprocs``); defaults to 1 for
52+
/// direct C++ callers and for the lightweight constructors that
53+
/// don't carry comm metadata. Use this — NOT ``nswap > 0`` — as the
5254
/// "is multi-rank?" predicate: ``atom_style spin`` and some other
5355
/// LAMMPS configurations populate ``nswap`` even in single-rank.
5456
int nprocs = 1;
@@ -65,7 +67,11 @@ struct InputNlist {
6567
sendproc(nullptr),
6668
recvproc(nullptr),
6769
world(0) {};
68-
InputNlist(int inum_, int* ilist_, int* numneigh_, int** firstneigh_)
70+
InputNlist(int inum_,
71+
int* ilist_,
72+
int* numneigh_,
73+
int** firstneigh_,
74+
int nprocs_ = 1)
6975
: inum(inum_),
7076
ilist(ilist_),
7177
numneigh(numneigh_),
@@ -77,7 +83,8 @@ struct InputNlist {
7783
sendlist(nullptr),
7884
sendproc(nullptr),
7985
recvproc(nullptr),
80-
world(0) {};
86+
world(0),
87+
nprocs(nprocs_) {};
8188
InputNlist(int inum_,
8289
int* ilist_,
8390
int* numneigh_,
@@ -89,7 +96,8 @@ struct InputNlist {
8996
int** sendlist,
9097
int* sendproc,
9198
int* recvproc,
92-
void* world)
99+
void* world,
100+
int nprocs_ = 1)
93101
: inum(inum_),
94102
ilist(ilist_),
95103
numneigh(numneigh_),
@@ -101,7 +109,8 @@ struct InputNlist {
101109
sendlist(sendlist),
102110
sendproc(sendproc),
103111
recvproc(recvproc),
104-
world(world) {};
112+
world(world),
113+
nprocs(nprocs_) {};
105114
~InputNlist() {};
106115
/**
107116
* @brief Set mask for this neighbor list.
@@ -111,15 +120,6 @@ struct InputNlist {
111120
* @brief Set mapping for this neighbor list.
112121
*/
113122
void set_mapping(int* mapping_) { mapping = mapping_; };
114-
/**
115-
* @brief Set the MPI rank count for this neighbor list.
116-
*
117-
* Used by ``DeepPotPTExpt`` / ``DeepSpinPTExpt`` to decide whether
118-
* the regular or with-comm artifact should run. Pair styles must
119-
* call this with ``comm->nprocs``; without it the C++ side will
120-
* treat the call as single-rank.
121-
*/
122-
void set_nprocs(int nprocs_) { nprocs = nprocs_; };
123123
};
124124

125125
/**

source/lmp/fix_dplr.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,9 +505,8 @@ void FixDPLR::pre_force(int vflag) {
505505
// get lammps nlist
506506
NeighList* list = pair_deepmd->list;
507507
deepmd_compat::InputNlist lmp_list(list->inum, list->ilist, list->numneigh,
508-
list->firstneigh);
508+
list->firstneigh, comm->nprocs);
509509
lmp_list.set_mask(NEIGHMASK);
510-
lmp_list.set_nprocs(comm->nprocs);
511510
if (comm->nprocs == 1 && atom->map_style != Atom::MAP_NONE) {
512511
lmp_list.set_mapping(mapping_vec.data());
513512
}

source/lmp/pair_deepmd.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,8 @@ void PairDeepMD::compute(int eflag, int vflag) {
237237
list->inum, list->ilist, list->numneigh, list->firstneigh,
238238
commdata_->nswap, commdata_->sendnum, commdata_->recvnum,
239239
commdata_->firstrecv, commdata_->sendlist, commdata_->sendproc,
240-
commdata_->recvproc, &world);
240+
commdata_->recvproc, &world, comm->nprocs);
241241
lmp_list.set_mask(NEIGHMASK);
242-
lmp_list.set_nprocs(comm->nprocs);
243242
if (comm->nprocs == 1 && atom->map_style != Atom::MAP_NONE) {
244243
lmp_list.set_mapping(mapping_vec.data());
245244
}

source/lmp/pair_deepspin.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,8 @@ void PairDeepSpin::compute(int eflag, int vflag) {
251251
list->inum, list->ilist, list->numneigh, list->firstneigh,
252252
commdata_->nswap, commdata_->sendnum, commdata_->recvnum,
253253
commdata_->firstrecv, commdata_->sendlist, commdata_->sendproc,
254-
commdata_->recvproc, &world);
254+
commdata_->recvproc, &world, comm->nprocs);
255255
lmp_list.set_mask(NEIGHMASK);
256-
lmp_list.set_nprocs(comm->nprocs);
257256
if (comm->nprocs == 1 && atom->map_style != Atom::MAP_NONE) {
258257
lmp_list.set_mapping(mapping_vec.data());
259258
}

0 commit comments

Comments
 (0)