From cd10c4a6192b8a1ecd2a58b78cd5959aee82a829 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Fri, 8 Dec 2023 15:21:36 -0500 Subject: [PATCH 01/17] test with -fsanitize=leak Signed-off-by: Jinzhe Zeng --- .github/workflows/test_cc.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test_cc.yml b/.github/workflows/test_cc.yml index a98afa7a94..687f67c974 100644 --- a/.github/workflows/test_cc.yml +++ b/.github/workflows/test_cc.yml @@ -25,6 +25,7 @@ jobs: TF_INTER_OP_PARALLELISM_THREADS: 1 LMP_CXX11_ABI_0: 1 CMAKE_GENERATOR: Ninja + CXXFLAGS: "-fsanitize=leak" # test lammps # ASE issue: https://gitlab.com/ase/ase/-/merge_requests/2843 # TODO: remove ase version when ase has new release From db67db3530c28f3ea87c8fad040da7d0a94f8e5f Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Wed, 13 Dec 2023 18:35:24 -0500 Subject: [PATCH 02/17] fix memory leak of DeepPot Signed-off-by: Jinzhe Zeng --- source/api_c/include/c_api.h | 7 +++++++ source/api_c/include/deepmd.hpp | 2 +- source/api_c/src/c_api.cc | 2 ++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/source/api_c/include/c_api.h b/source/api_c/include/c_api.h index b0c030962a..306fcbec7a 100644 --- a/source/api_c/include/c_api.h +++ b/source/api_c/include/c_api.h @@ -72,6 +72,13 @@ extern DP_DeepPot* DP_NewDeepPotWithParam2(const char* c_model, const char* c_file_content, const int size_file_content); +/** + * @brief Delete a DP. + * + * @param dp Deep Potential to delete. + */ +extern void DP_DeleteDeepPot(DP_DeepPot* dp); + /** * @brief Evaluate the energy, force and virial by using a DP. (double version) * @attention The number of frames is assumed to be 1. diff --git a/source/api_c/include/deepmd.hpp b/source/api_c/include/deepmd.hpp index 90c1c1c918..e5e6c757c4 100644 --- a/source/api_c/include/deepmd.hpp +++ b/source/api_c/include/deepmd.hpp @@ -564,7 +564,7 @@ class DeepPot { * @brief DP constructor without initialization. **/ DeepPot() : dp(nullptr){}; - ~DeepPot(){}; + ~DeepPot() { DP_DeleteDeepPot(dp); }; /** * @brief DP constructor with initialization. * @param[in] model The name of the frozen model file. diff --git a/source/api_c/src/c_api.cc b/source/api_c/src/c_api.cc index 9d1ed7d323..71a898474b 100644 --- a/source/api_c/src/c_api.cc +++ b/source/api_c/src/c_api.cc @@ -61,6 +61,8 @@ DP_DeepPot* DP_NewDeepPotWithParam2(const char* c_model, DP_DeepPot* new_dp = new DP_DeepPot(dp); return new_dp;) } +void DP_DeleteDeepPot(DP_DeepPot* dp) { delete dp; } + DP_DeepPotModelDevi::DP_DeepPotModelDevi() {} DP_DeepPotModelDevi::DP_DeepPotModelDevi(deepmd::DeepPotModelDevi& dp) : dp(dp) { From 24835774088fabfe4da9f2b1884de8b7d784694e Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Fri, 15 Dec 2023 00:58:14 -0500 Subject: [PATCH 03/17] delete dp in the tests Signed-off-by: Jinzhe Zeng --- source/api_c/tests/test_deeppot_a.cc | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/source/api_c/tests/test_deeppot_a.cc b/source/api_c/tests/test_deeppot_a.cc index 63f53e16e9..3118d8e545 100644 --- a/source/api_c/tests/test_deeppot_a.cc +++ b/source/api_c/tests/test_deeppot_a.cc @@ -86,7 +86,10 @@ class TestInferDeepPotA : public ::testing::Test { } }; - void TearDown() override { remove("deeppot.pb"); }; + void TearDown() override { + remove("deeppot.pb"); + DP_DeleteDeepPot(dp); + }; }; TEST_F(TestInferDeepPotA, double_infer) { @@ -253,7 +256,11 @@ class TestInferDeepPotANoPBC : public ::testing::Test { } }; - void TearDown() override { remove("deeppot.pb"); }; + void TearDown() override { + remove("deeppot.pb"); + + DP_DeleteDeepPot(dp); + }; }; TEST_F(TestInferDeepPotANoPBC, double_infer) { From 58d61e6a98c99c074fd4133d990c41354385f753 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 3 Feb 2024 04:54:32 -0500 Subject: [PATCH 04/17] resolve memory leaks Signed-off-by: Jinzhe Zeng --- doc/inference/cxx.md | 1 + examples/infer_water/infer_water.c | 1 + source/api_c/include/c_api.h | 23 ++++++++++++++++++++++- source/api_c/include/deepmd.hpp | 6 +++--- source/api_c/src/c_api.cc | 6 ++++++ source/api_c/tests/test_deeppot_a.cc | 10 ++++++++++ 6 files changed, 43 insertions(+), 4 deletions(-) diff --git a/doc/inference/cxx.md b/doc/inference/cxx.md index 6188daba4c..b6cc525e30 100644 --- a/doc/inference/cxx.md +++ b/doc/inference/cxx.md @@ -63,6 +63,7 @@ int main(){ free(ae); free(av); free(dp); + DP_DeleteDeepPot(dp); } ``` diff --git a/examples/infer_water/infer_water.c b/examples/infer_water/infer_water.c index f4eeae147f..22abf3d52b 100644 --- a/examples/infer_water/infer_water.c +++ b/examples/infer_water/infer_water.c @@ -33,4 +33,5 @@ int main() { free(ae); free(av); free(dp); + DP_DeleteDeepPot(dp); } diff --git a/source/api_c/include/c_api.h b/source/api_c/include/c_api.h index 7f4e35148b..604005180a 100644 --- a/source/api_c/include/c_api.h +++ b/source/api_c/include/c_api.h @@ -73,12 +73,33 @@ extern DP_DeepPot* DP_NewDeepPotWithParam2(const char* c_model, const int size_file_content); /** - * @brief Delete a DP. + * @brief Delete a Deep Potential. * * @param dp Deep Potential to delete. */ extern void DP_DeleteDeepPot(DP_DeepPot* dp); +/** + * @brief Delete a Deep Potential Model Deviation. + * + * @param dp Deep Potential to delete. + */ +extern void DP_DeleteDeepPotModelDevi(DP_DeepPotModelDevi* dp); + +/** + * @brief Delete a Deep Tensor. + * + * @param dp Deep Tensor to delete. + */ +extern void DP_DeleteDeepTensor(DP_DeepTensor* dt); + +/** + * @brief Delete a Dipole Charge Modifier. + * + * @param dp Dipole Charge Modifier to delete. + */ +extern void DP_DeleteDipoleChargeModifier(DP_DipoleChargeModifier* dcm); + /** * @brief Evaluate the energy, force and virial by using a DP. (double version) * @attention The number of frames is assumed to be 1. diff --git a/source/api_c/include/deepmd.hpp b/source/api_c/include/deepmd.hpp index 2d172cc557..570dadd820 100644 --- a/source/api_c/include/deepmd.hpp +++ b/source/api_c/include/deepmd.hpp @@ -1100,7 +1100,7 @@ class DeepPotModelDevi { * @brief DP model deviation constructor without initialization. **/ DeepPotModelDevi() : dp(nullptr){}; - ~DeepPotModelDevi(){}; + ~DeepPotModelDevi() { DP_DeleteDeepPotModelDevi(dp); }; /** * @brief DP model deviation constructor with initialization. * @param[in] models The names of the frozen model file. @@ -1523,7 +1523,7 @@ class DeepTensor { * @brief Deep Tensor constructor without initialization. **/ DeepTensor() : dt(nullptr){}; - ~DeepTensor(){}; + ~DeepTensor() { DP_DeleteDeepTensor(dt); }; /** * @brief DeepTensor constructor with initialization. * @param[in] model The name of the frozen model file. @@ -1891,7 +1891,7 @@ class DipoleChargeModifier { * @brief DipoleChargeModifier constructor without initialization. **/ DipoleChargeModifier() : dcm(nullptr){}; - ~DipoleChargeModifier(){}; + ~DipoleChargeModifier() { DP_DeleteDipoleChargeModifier(dcm); }; /** * @brief DipoleChargeModifier constructor with initialization. * @param[in] model The name of the frozen model file. diff --git a/source/api_c/src/c_api.cc b/source/api_c/src/c_api.cc index 7c90d30869..75c4f727e6 100644 --- a/source/api_c/src/c_api.cc +++ b/source/api_c/src/c_api.cc @@ -99,6 +99,8 @@ DP_DeepPotModelDevi* DP_NewDeepPotModelDeviWithParam( return new_dp;) } +void DP_DeleteDeepPotModelDevi(DP_DeepPotModelDevi* dp) { delete dp; } + DP_DeepTensor::DP_DeepTensor() {} DP_DeepTensor::DP_DeepTensor(deepmd::DeepTensor& dt) : dt(dt) {} @@ -117,6 +119,8 @@ DP_DeepTensor* DP_NewDeepTensorWithParam(const char* c_model, DP_DeepTensor* new_dt = new DP_DeepTensor(dt); return new_dt;) } +void DP_DeleteDeepTensor(DP_DeepTensor* dt) { delete dt; } + DP_DipoleChargeModifier::DP_DipoleChargeModifier() {} DP_DipoleChargeModifier::DP_DipoleChargeModifier( deepmd::DipoleChargeModifier& dcm) @@ -139,6 +143,8 @@ DP_DipoleChargeModifier* DP_NewDipoleChargeModifierWithParam( return new_dcm;) } +void DP_DeleteDipoleChargeModifier(DP_DipoleChargeModifier* dcm) { delete dcm; } + } // extern "C" template diff --git a/source/api_c/tests/test_deeppot_a.cc b/source/api_c/tests/test_deeppot_a.cc index 3118d8e545..3acf6f09a2 100644 --- a/source/api_c/tests/test_deeppot_a.cc +++ b/source/api_c/tests/test_deeppot_a.cc @@ -293,6 +293,11 @@ TEST_F(TestInferDeepPotANoPBC, double_infer) { for (int ii = 0; ii < natoms * 9; ++ii) { EXPECT_LT(fabs(atomic_virial[ii] - expected_v[ii]), 1e-10); } + delete ener_; + delete[] force_; + delete[] virial_; + delete[] atomic_ener_; + delete[] atomic_virial_; } TEST_F(TestInferDeepPotANoPBC, float_infer) { @@ -325,4 +330,9 @@ TEST_F(TestInferDeepPotANoPBC, float_infer) { for (int ii = 0; ii < natoms * 9; ++ii) { EXPECT_LT(fabs(atomic_virial[ii] - expected_v[ii]), 1e-6); } + delete ener_; + delete[] force_; + delete[] virial_; + delete[] atomic_ener_; + delete[] atomic_virial_; } From 20d9e87a8224b939c335b0f7b740900b9ff80ab5 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 3 Feb 2024 05:00:57 -0500 Subject: [PATCH 05/17] fix declare order Signed-off-by: Jinzhe Zeng --- source/api_c/include/c_api.h | 42 ++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/source/api_c/include/c_api.h b/source/api_c/include/c_api.h index 604005180a..1ab282f28a 100644 --- a/source/api_c/include/c_api.h +++ b/source/api_c/include/c_api.h @@ -79,27 +79,6 @@ extern DP_DeepPot* DP_NewDeepPotWithParam2(const char* c_model, */ extern void DP_DeleteDeepPot(DP_DeepPot* dp); -/** - * @brief Delete a Deep Potential Model Deviation. - * - * @param dp Deep Potential to delete. - */ -extern void DP_DeleteDeepPotModelDevi(DP_DeepPotModelDevi* dp); - -/** - * @brief Delete a Deep Tensor. - * - * @param dp Deep Tensor to delete. - */ -extern void DP_DeleteDeepTensor(DP_DeepTensor* dt); - -/** - * @brief Delete a Dipole Charge Modifier. - * - * @param dp Dipole Charge Modifier to delete. - */ -extern void DP_DeleteDipoleChargeModifier(DP_DipoleChargeModifier* dcm); - /** * @brief Evaluate the energy, force and virial by using a DP. (double version) * @attention The number of frames is assumed to be 1. @@ -519,6 +498,13 @@ extern DP_DeepPotModelDevi* DP_NewDeepPotModelDeviWithParam( const int n_file_contents, const int* size_file_contents); +/** + * @brief Delete a Deep Potential Model Deviation. + * + * @param dp Deep Potential to delete. + */ +extern void DP_DeleteDeepPotModelDevi(DP_DeepPotModelDevi* dp); + /** * @brief Evaluate the energy, force and virial by using a DP model deviation *with neighbor list. (double version) @@ -820,6 +806,13 @@ extern DP_DeepTensor* DP_NewDeepTensorWithParam(const char* c_model, const int gpu_rank, const char* c_name_scope); +/** + * @brief Delete a Deep Tensor. + * + * @param dp Deep Tensor to delete. + */ +extern void DP_DeleteDeepTensor(DP_DeepTensor* dt); + /** * @brief Evaluate the tensor by using a DP. (double version) * @param[in] dt The Deep Tensor to use. @@ -1122,6 +1115,13 @@ extern DP_DipoleChargeModifier* DP_NewDipoleChargeModifier(const char* c_model); extern DP_DipoleChargeModifier* DP_NewDipoleChargeModifierWithParam( const char* c_model, const int gpu_rank, const char* c_name_scope); +/** + * @brief Delete a Dipole Charge Modifier. + * + * @param dp Dipole Charge Modifier to delete. + */ +extern void DP_DeleteDipoleChargeModifier(DP_DipoleChargeModifier* dcm); + /** * @brief Evaluate the force and virial correction by using a dipole charge *modifier with the neighbor list. (double version) From 20ef962c1e2ea826a39596fc9d3e379e97dca86e Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 3 Feb 2024 05:10:52 -0500 Subject: [PATCH 06/17] fix memory leaks Signed-off-by: Jinzhe Zeng --- source/api_c/tests/test_deeppot_a.cc | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/source/api_c/tests/test_deeppot_a.cc b/source/api_c/tests/test_deeppot_a.cc index 3acf6f09a2..b4a9a81f92 100644 --- a/source/api_c/tests/test_deeppot_a.cc +++ b/source/api_c/tests/test_deeppot_a.cc @@ -122,6 +122,12 @@ TEST_F(TestInferDeepPotA, double_infer) { for (int ii = 0; ii < natoms * 9; ++ii) { EXPECT_LT(fabs(atomic_virial[ii] - expected_v[ii]), 1e-10); } + + delete ener_; + delete[] force_; + delete[] virial_; + delete[] atomic_ener_; + delete[] atomic_virial_; } TEST_F(TestInferDeepPotA, float_infer) { @@ -154,6 +160,11 @@ TEST_F(TestInferDeepPotA, float_infer) { for (int ii = 0; ii < natoms * 9; ++ii) { EXPECT_LT(fabs(atomic_virial[ii] - expected_v[ii]), 1e-6); } + delete ener_; + delete[] force_; + delete[] virial_; + delete[] atomic_ener_; + delete[] atomic_virial_; } TEST_F(TestInferDeepPotA, cutoff) { From f1f21cca579516cb546b58dd7abdd14f66878eb6 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 3 Feb 2024 05:18:48 -0500 Subject: [PATCH 07/17] fix memory leak from nlist Signed-off-by: Jinzhe Zeng --- source/api_c/include/c_api.h | 7 +++++++ source/api_c/include/deepmd.hpp | 1 + source/api_c/src/c_api.cc | 9 +++++++++ 3 files changed, 17 insertions(+) diff --git a/source/api_c/include/c_api.h b/source/api_c/include/c_api.h index 1ab282f28a..4baa7dd4a0 100644 --- a/source/api_c/include/c_api.h +++ b/source/api_c/include/c_api.h @@ -25,6 +25,13 @@ extern DP_Nlist* DP_NewNlist(int inum_, int* numneigh_, int** firstneigh_); +/** + * @brief Delete a neighbor list. + * + * @param nl Neighbor list to delete. + */ +extern void DP_DeleteNlist(DP_Nlist* nl); + /** * @brief Check if there is any exceptions throw. * diff --git a/source/api_c/include/deepmd.hpp b/source/api_c/include/deepmd.hpp index 570dadd820..9c1c0dff6e 100644 --- a/source/api_c/include/deepmd.hpp +++ b/source/api_c/include/deepmd.hpp @@ -522,6 +522,7 @@ struct InputNlist { nl(DP_NewNlist(inum_, ilist_, numneigh_, firstneigh_)) { DP_CHECK_OK(DP_NlistCheckOK, nl); }; + ~InputNlist() { DP_DeleteNlist(nl); }; /// @brief C API neighbor list. DP_Nlist *nl; /// @brief Number of core region atoms diff --git a/source/api_c/src/c_api.cc b/source/api_c/src/c_api.cc index 75c4f727e6..172644b268 100644 --- a/source/api_c/src/c_api.cc +++ b/source/api_c/src/c_api.cc @@ -25,6 +25,15 @@ DP_Nlist* DP_NewNlist(int inum_, DP_Nlist* new_nl = new DP_Nlist(nl); return new_nl;) } +void DP_DeleteNlist(DP_Nlist* nl) { + // remove ilist, numneigh, and firstneigh before deleting nl + // as they are not owned by DP_Nlist + nl->nl.ilist = nullptr; + nl->nl.numneigh = nullptr; + nl->nl.firstneigh = nullptr; + delete nl; +} + DP_DeepPot::DP_DeepPot() {} DP_DeepPot::DP_DeepPot(deepmd::DeepPot& dp) : dp(dp) { dfparam = dp.dim_fparam(); From 2904c7c05e34a4398f6ba15f2b14faaf14d8e372 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 3 Feb 2024 05:55:42 -0500 Subject: [PATCH 08/17] fix memory leaks when an exception is throw Signed-off-by: Jinzhe Zeng --- source/api_c/include/deepmd.hpp | 40 +++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/source/api_c/include/deepmd.hpp b/source/api_c/include/deepmd.hpp index 9c1c0dff6e..34206b7fe2 100644 --- a/source/api_c/include/deepmd.hpp +++ b/source/api_c/include/deepmd.hpp @@ -580,7 +580,15 @@ class DeepPot { const int &gpu_rank = 0, const std::string &file_content = "") : dp(nullptr) { - init(model, gpu_rank, file_content); + try { + init(model, gpu_rank, file_content); + } catch (...) { + // Clean up and rethrow, as the destructor will not be called + if (dp) { + DP_DeleteDeepPot(dp); + } + throw; + } }; /** * @brief Initialize the DP. @@ -1107,7 +1115,15 @@ class DeepPotModelDevi { * @param[in] models The names of the frozen model file. **/ DeepPotModelDevi(const std::vector &models) : dp(nullptr) { - init(models); + try { + init(models); + } catch (...) { + // Clean up and rethrow, as the destructor will not be called + if (dp) { + DP_DeleteDeepPotModelDevi(dp); + } + throw; + } }; /** * @brief Initialize the DP model deviation. @@ -1533,7 +1549,15 @@ class DeepTensor { const int &gpu_rank = 0, const std::string &name_scope = "") : dt(nullptr) { - init(model, gpu_rank, name_scope); + try { + init(model, gpu_rank, name_scope); + } catch (...) { + // Clean up and rethrow, as the destructor will not be called + if (dt) { + DP_DeleteDeepTensor(dt); + } + throw; + } }; /** * @brief Initialize the DeepTensor. @@ -1903,7 +1927,15 @@ class DipoleChargeModifier { const int &gpu_rank = 0, const std::string &name_scope = "") : dcm(nullptr) { - init(model, gpu_rank, name_scope); + try { + init(model, gpu_rank, name_scope); + } catch (...) { + // Clean up and rethrow, as the destructor will not be called + if (dp) { + DP_DeleteDipoleChargeModifier(dcm); + } + throw; + } }; /** * @brief Initialize the DipoleChargeModifier. From da558881aeea61ce27a66f8ee67241d1ebc54193 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 3 Feb 2024 05:58:06 -0500 Subject: [PATCH 09/17] delete nl before new Signed-off-by: Jinzhe Zeng --- source/api_c/include/deepmd.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/api_c/include/deepmd.hpp b/source/api_c/include/deepmd.hpp index 34206b7fe2..9c1514a4a5 100644 --- a/source/api_c/include/deepmd.hpp +++ b/source/api_c/include/deepmd.hpp @@ -557,6 +557,8 @@ void inline convert_nlist(InputNlist &to_nlist, to_nlist.numneigh[ii] = from_nlist[ii].size(); to_nlist.firstneigh[ii] = &from_nlist[ii][0]; } + // delete the original nl + DP_DeleteNlist(to_nlist.nl); to_nlist.nl = DP_NewNlist(to_nlist.inum, to_nlist.ilist, to_nlist.numneigh, to_nlist.firstneigh); } From 6d62e0a0a5b77f91b9cd91f1b53f87e99c053a49 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 3 Feb 2024 06:02:13 -0500 Subject: [PATCH 10/17] fix typo Signed-off-by: Jinzhe Zeng --- source/api_c/include/deepmd.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/api_c/include/deepmd.hpp b/source/api_c/include/deepmd.hpp index 9c1514a4a5..966cc1f24e 100644 --- a/source/api_c/include/deepmd.hpp +++ b/source/api_c/include/deepmd.hpp @@ -1933,7 +1933,7 @@ class DipoleChargeModifier { init(model, gpu_rank, name_scope); } catch (...) { // Clean up and rethrow, as the destructor will not be called - if (dp) { + if (dcm) { DP_DeleteDipoleChargeModifier(dcm); } throw; From 0f52b35523db3fc95652433a20b759e93069eeae Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 3 Feb 2024 06:16:38 -0500 Subject: [PATCH 11/17] append liblsan to LD_PRELOAD Signed-off-by: Jinzhe Zeng --- .github/workflows/test_cc.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test_cc.yml b/.github/workflows/test_cc.yml index 747660ca62..35d60a0ea2 100644 --- a/.github/workflows/test_cc.yml +++ b/.github/workflows/test_cc.yml @@ -47,6 +47,7 @@ jobs: TF_INTER_OP_PARALLELISM_THREADS: 1 LAMMPS_PLUGIN_PATH: ${{ github.workspace }}/dp_test/lib/deepmd_lmp LD_LIBRARY_PATH: ${{ github.workspace }}/dp_test/lib + LD_PRELOAD: /lib/x86_64-linux-gnu/liblsan.so.0 # test ipi - run: pytest --cov=deepmd source/ipi/tests env: From 4f16406cec913427afb3c37af9d2d56249fc79f8 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 3 Feb 2024 06:31:26 -0500 Subject: [PATCH 12/17] skip lammps when checking memory leak Signed-off-by: Jinzhe Zeng --- .github/workflows/test_cc.yml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test_cc.yml b/.github/workflows/test_cc.yml index 35d60a0ea2..611cb5d459 100644 --- a/.github/workflows/test_cc.yml +++ b/.github/workflows/test_cc.yml @@ -6,6 +6,9 @@ jobs: testcc: name: Test C++ runs-on: ubuntu-latest + strategy: + matrix: + check_memleak: [true, false] steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 @@ -31,7 +34,7 @@ jobs: TF_INTER_OP_PARALLELISM_THREADS: 1 LMP_CXX11_ABI_0: 1 CMAKE_GENERATOR: Ninja - CXXFLAGS: "-fsanitize=leak" + CXXFLAGS: ${{ matrix.check_memleak && "-fsanitize=leak" || "" }} # test lammps # ASE issue: https://gitlab.com/ase/ase/-/merge_requests/2843 # TODO: remove ase version when ase has new release @@ -40,6 +43,7 @@ jobs: python -m pip install -e .[cpu,test,lmp] "ase @ https://gitlab.com/ase/ase/-/archive/8c5aa5fd6448c5cfb517a014dccf2b214a9dfa8f/ase-8c5aa5fd6448c5cfb517a014dccf2b214a9dfa8f.tar.gz" env: DP_BUILD_TESTING: 1 + if: ${{ !matrix.check_memleak }} - run: pytest --cov=deepmd source/lmp/tests env: OMP_NUM_THREADS: 1 @@ -47,7 +51,7 @@ jobs: TF_INTER_OP_PARALLELISM_THREADS: 1 LAMMPS_PLUGIN_PATH: ${{ github.workspace }}/dp_test/lib/deepmd_lmp LD_LIBRARY_PATH: ${{ github.workspace }}/dp_test/lib - LD_PRELOAD: /lib/x86_64-linux-gnu/liblsan.so.0 + if: ${{ !matrix.check_memleak }} # test ipi - run: pytest --cov=deepmd source/ipi/tests env: @@ -56,6 +60,7 @@ jobs: TF_INTER_OP_PARALLELISM_THREADS: 1 PATH: ${{ github.workspace }}/dp_test/bin:$PATH LD_LIBRARY_PATH: ${{ github.workspace }}/dp_test/lib + if: ${{ !matrix.check_memleak }} - uses: codecov/codecov-action@v3 with: gcov: true From 9e6da6f35e2dab8237368a66b45227592a1ce628 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 3 Feb 2024 06:37:21 -0500 Subject: [PATCH 13/17] must use single quote Signed-off-by: Jinzhe Zeng --- .github/workflows/test_cc.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test_cc.yml b/.github/workflows/test_cc.yml index 611cb5d459..2253a25ee0 100644 --- a/.github/workflows/test_cc.yml +++ b/.github/workflows/test_cc.yml @@ -34,7 +34,7 @@ jobs: TF_INTER_OP_PARALLELISM_THREADS: 1 LMP_CXX11_ABI_0: 1 CMAKE_GENERATOR: Ninja - CXXFLAGS: ${{ matrix.check_memleak && "-fsanitize=leak" || "" }} + CXXFLAGS: ${{ matrix.check_memleak && '-fsanitize=leak' || '' }} # test lammps # ASE issue: https://gitlab.com/ase/ase/-/merge_requests/2843 # TODO: remove ase version when ase has new release From 0cd9cdc87a5954f01edfcd9c090b10ccf08cacb4 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 3 Feb 2024 16:54:24 -0500 Subject: [PATCH 14/17] it seems no need to delete pointers of InputNlist Signed-off-by: Jinzhe Zeng --- source/api_c/src/c_api.cc | 5 ----- 1 file changed, 5 deletions(-) diff --git a/source/api_c/src/c_api.cc b/source/api_c/src/c_api.cc index 172644b268..9acdf174b7 100644 --- a/source/api_c/src/c_api.cc +++ b/source/api_c/src/c_api.cc @@ -26,11 +26,6 @@ DP_Nlist* DP_NewNlist(int inum_, } void DP_DeleteNlist(DP_Nlist* nl) { - // remove ilist, numneigh, and firstneigh before deleting nl - // as they are not owned by DP_Nlist - nl->nl.ilist = nullptr; - nl->nl.numneigh = nullptr; - nl->nl.firstneigh = nullptr; delete nl; } From 8f2d40f313a2fab190e41d84022ce1daa219ee5b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 3 Feb 2024 21:54:56 +0000 Subject: [PATCH 15/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- source/api_c/src/c_api.cc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/source/api_c/src/c_api.cc b/source/api_c/src/c_api.cc index 9acdf174b7..029d020f45 100644 --- a/source/api_c/src/c_api.cc +++ b/source/api_c/src/c_api.cc @@ -25,9 +25,7 @@ DP_Nlist* DP_NewNlist(int inum_, DP_Nlist* new_nl = new DP_Nlist(nl); return new_nl;) } -void DP_DeleteNlist(DP_Nlist* nl) { - delete nl; -} +void DP_DeleteNlist(DP_Nlist* nl) { delete nl; } DP_DeepPot::DP_DeepPot() {} DP_DeepPot::DP_DeepPot(deepmd::DeepPot& dp) : dp(dp) { From 3fad25ad0769a4636304f20c61b5f47da3887933 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 3 Feb 2024 17:08:26 -0500 Subject: [PATCH 16/17] dp is double freed Signed-off-by: Jinzhe Zeng --- examples/infer_water/infer_water.c | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/infer_water/infer_water.c b/examples/infer_water/infer_water.c index 22abf3d52b..cf13f45e3a 100644 --- a/examples/infer_water/infer_water.c +++ b/examples/infer_water/infer_water.c @@ -32,6 +32,5 @@ int main() { free(v); free(ae); free(av); - free(dp); DP_DeleteDeepPot(dp); } From ed9914eaed72ac1098ff0f4512fb458e6c64dd94 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 3 Feb 2024 17:09:34 -0500 Subject: [PATCH 17/17] docs: fix double freed dp Signed-off-by: Jinzhe Zeng --- doc/inference/cxx.md | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/inference/cxx.md b/doc/inference/cxx.md index b6cc525e30..cc7e7be540 100644 --- a/doc/inference/cxx.md +++ b/doc/inference/cxx.md @@ -62,7 +62,6 @@ int main(){ free(v); free(ae); free(av); - free(dp); DP_DeleteDeepPot(dp); } ```