From 37c19cc03434b5e5c929fa73c65c7984ab4c031b Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Mon, 5 Feb 2024 15:39:44 -0500 Subject: [PATCH 1/2] c: change the required shape of electric field to nloc * 3 A segfault sometimes appears in the tests after #3223. The reason is that the required shape of electric field is set to nall * 3 in the C interface, but a vector of nloc * 3 is actually given in the tests and lammps and used in the C++ interface. It's only found by sanitizer as the program doesn't write to the invalid address (only reading it won't cause segfault). Signed-off-by: Jinzhe Zeng --- source/api_c/include/deepmd.hpp | 6 +++--- source/api_c/src/c_api.cc | 2 +- source/api_cc/include/DataModifier.h | 6 +++--- source/api_cc/include/DataModifierTF.h | 6 +++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/source/api_c/include/deepmd.hpp b/source/api_c/include/deepmd.hpp index 966cc1f24e..16b8f08cad 100644 --- a/source/api_c/include/deepmd.hpp +++ b/source/api_c/include/deepmd.hpp @@ -1965,13 +1965,13 @@ class DipoleChargeModifier { * @param[out] dfcorr_ The force correction on each atom. * @param[out] dvcorr_ The virial correction. * @param[in] dcoord_ The coordinates of atoms. The array should be of size - *natoms x 3. - * @param[in] datype_ The atom types. The list should contain natoms ints. + *nall x 3. + * @param[in] datype_ The atom types. The list should contain nall ints. * @param[in] dbox The cell of the region. The array should be of size 9. * @param[in] pairs The pairs of atoms. The list should contain npairs pairs *of ints. * @param[in] delef_ The electric field on each atom. The array should be of - *size natoms x 3. + *size nghost x 3. * @param[in] nghost The number of ghost atoms. * @param[in] lmp_list The neighbor list. **/ diff --git a/source/api_c/src/c_api.cc b/source/api_c/src/c_api.cc index 029d020f45..79dc486e0d 100644 --- a/source/api_c/src/c_api.cc +++ b/source/api_c/src/c_api.cc @@ -785,7 +785,7 @@ inline void DP_DipoleChargeModifierComputeNList_variant( for (int i = 0; i < npairs; i++) { pairs_.push_back(std::make_pair(pairs[i * 2], pairs[i * 2 + 1])); } - std::vector delef_(delef, delef + natoms * 3); + std::vector delef_(delef, delef + (natoms - nghost) * 3); std::vector df, dv; DP_REQUIRES_OK(dcm, dcm->dcm.compute(df, dv, coord_, atype_, cell_, pairs_, diff --git a/source/api_cc/include/DataModifier.h b/source/api_cc/include/DataModifier.h index 6d443d9b9c..0f46b5e0f8 100644 --- a/source/api_cc/include/DataModifier.h +++ b/source/api_cc/include/DataModifier.h @@ -127,13 +127,13 @@ class DipoleChargeModifier { * @param[out] dfcorr_ The force correction on each atom. * @param[out] dvcorr_ The virial correction. * @param[in] dcoord_ The coordinates of atoms. The array should be of size - *natoms x 3. - * @param[in] datype_ The atom types. The list should contain natoms ints. + *nall x 3. + * @param[in] datype_ The atom types. The list should contain nall ints. * @param[in] dbox The cell of the region. The array should be of size 9. * @param[in] pairs The pairs of atoms. The list should contain npairs pairs *of ints. * @param[in] delef_ The electric field on each atom. The array should be of - *size natoms x 3. + *size nloc x 3. * @param[in] nghost The number of ghost atoms. * @param[in] lmp_list The neighbor list. **/ diff --git a/source/api_cc/include/DataModifierTF.h b/source/api_cc/include/DataModifierTF.h index cd1d696c3c..b2f610db3c 100644 --- a/source/api_cc/include/DataModifierTF.h +++ b/source/api_cc/include/DataModifierTF.h @@ -42,13 +42,13 @@ class DipoleChargeModifierTF : public DipoleChargeModifierBase { * @param[out] dfcorr_ The force correction on each atom. * @param[out] dvcorr_ The virial correction. * @param[in] dcoord_ The coordinates of atoms. The array should be of size - *natoms x 3. - * @param[in] datype_ The atom types. The list should contain natoms ints. + *nall x 3. + * @param[in] datype_ The atom types. The list should contain nall ints. * @param[in] dbox The cell of the region. The array should be of size 9. * @param[in] pairs The pairs of atoms. The list should contain npairs pairs *of ints. * @param[in] delef_ The electric field on each atom. The array should be of - *size natoms x 3. + *size nloc x 3. * @param[in] nghost The number of ghost atoms. * @param[in] lmp_list The neighbor list. **/ From f43ba1dc297d09e62693bbdf371f248eecaa7b91 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Mon, 5 Feb 2024 15:43:09 -0500 Subject: [PATCH 2/2] fix docs in c_api.h Signed-off-by: Jinzhe Zeng --- source/api_c/include/c_api.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/api_c/include/c_api.h b/source/api_c/include/c_api.h index 4baa7dd4a0..911813e428 100644 --- a/source/api_c/include/c_api.h +++ b/source/api_c/include/c_api.h @@ -1134,20 +1134,20 @@ extern void DP_DeleteDipoleChargeModifier(DP_DipoleChargeModifier* dcm); *modifier with the neighbor list. (double version) * @param[in] dcm The dipole charge modifier to use. * @param[in] natoms The number of atoms. - * @param[in] coord The coordinates of atoms. The array should be of size natoms + * @param[in] coord The coordinates of atoms. The array should be of size nall *x 3. - * @param[in] atype The atom types. The array should contain natoms ints. + * @param[in] atype The atom types. The array should contain nall ints. * @param[in] cell The cell of the region. The array should be of size 9. Pass *NULL if pbc is not used. * @param[in] pairs The pairs of atoms. The list should contain npairs pairs of *ints. * @param[in] npairs The number of pairs. * @param[in] delef_ The electric field on each atom. The array should be of - *size nframes x natoms x 3. + *size nframes x nloc x 3. * @param[in] nghost The number of ghost atoms. * @param[in] nlist The neighbor list. * @param[out] dfcorr_ Output force correction. The array should be of size - *natoms x 3. + *nall x 3. * @param[out] dvcorr_ Output virial correction. The array should be of size 9. * @warning The output arrays should be allocated before calling this function. *Pass NULL if not required.