From beb05b928f8ad6bd0c1e3060b7a7734ad677e4d0 Mon Sep 17 00:00:00 2001 From: "xiaojie.wu" Date: Wed, 17 Jan 2024 08:47:21 +0800 Subject: [PATCH] memory leak in dftd3 and dft4 --- gpu4pyscf/__init__.py | 2 +- gpu4pyscf/lib/dftd3.py | 14 ++++++++++---- gpu4pyscf/lib/dftd4.py | 9 ++++++--- gpu4pyscf/lib/tests/test_dftd4.py | 1 + gpu4pyscf/qmmm/test/test_chelpg.py | 6 +++--- 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/gpu4pyscf/__init__.py b/gpu4pyscf/__init__.py index 12273fed..11890d28 100644 --- a/gpu4pyscf/__init__.py +++ b/gpu4pyscf/__init__.py @@ -1,5 +1,5 @@ from . import lib, grad, hessian, solvent, scf, dft -__version__ = '0.6.15' +__version__ = '0.6.16' # monkey patch libxc reference due to a bug in nvcc from pyscf.dft import libxc diff --git a/gpu4pyscf/lib/dftd3.py b/gpu4pyscf/lib/dftd3.py index 6c28bee3..85e38eae 100644 --- a/gpu4pyscf/lib/dftd3.py +++ b/gpu4pyscf/lib/dftd3.py @@ -30,9 +30,14 @@ "d3op": libdftd3.dftd3_load_optimizedpower_damping #OptimizedPowerDampingParam, } - -libdftd3.dftd3_new_error.restype = ctypes.c_void_p -libdftd3.dftd3_new_structure.restype = ctypes.c_void_p +libdftd3.dftd3_new_error.restype = ctypes.c_void_p +libdftd3.dftd3_new_structure.restype = ctypes.c_void_p +libdftd3.dftd3_load_optimizedpower_damping.restype = ctypes.c_void_p +libdftd3.dftd3_load_mzero_damping.restype = ctypes.c_void_p +libdftd3.dftd3_load_mrational_damping.restype = ctypes.c_void_p +libdftd3.dftd3_load_zero_damping.restype = ctypes.c_void_p +libdftd3.dftd3_load_rational_damping.restype = ctypes.c_void_p +libdftd3.dftd3_new_d3_model.restype = ctypes.c_void_p class DFTD3Dispersion(lib.StreamObject): def __init__(self, mol, xc, version='d3bj', atm=False): @@ -63,8 +68,9 @@ def __init__(self, mol, xc, version='d3bj', atm=False): def __del__(self): err = libdftd3.dftd3_new_error() + param = ctypes.cast(self._param, ctypes.c_void_p) + libdftd3.dftd3_delete_param(ctypes.byref(param)) libdftd3.dftd3_delete_structure(err, self._mol) - libdftd3.dftd3_delete_param(err, self._param) libdftd3.dftd3_delete_model(err, self._disp) libdftd3.dftd3_delete_error(err) diff --git a/gpu4pyscf/lib/dftd4.py b/gpu4pyscf/lib/dftd4.py index ae5adc05..47defa15 100644 --- a/gpu4pyscf/lib/dftd4.py +++ b/gpu4pyscf/lib/dftd4.py @@ -20,8 +20,10 @@ libdftd4 = np.ctypeslib.load_library('libdftd4', os.path.abspath(os.path.join(__file__, '..', 'deps', 'lib'))) -libdftd4.dftd4_new_error.restype = ctypes.c_void_p -libdftd4.dftd4_new_structure.restype = ctypes.c_void_p +libdftd4.dftd4_new_error.restype = ctypes.c_void_p +libdftd4.dftd4_new_structure.restype = ctypes.c_void_p +libdftd4.dftd4_new_d4_model.restype = ctypes.c_void_p +libdftd4.dftd4_load_rational_damping.restype = ctypes.c_void_p class DFTD4Dispersion(lib.StreamObject): def __init__(self, mol, xc, atm=False): @@ -54,8 +56,9 @@ def __init__(self, mol, xc, atm=False): def __del__(self): err = libdftd4.dftd4_new_error() + param = ctypes.cast(self._param, ctypes.c_void_p) + libdftd4.dftd4_delete_param(ctypes.byref(param)) libdftd4.dftd4_delete_structure(err, self._mol) - libdftd4.dftd4_delete_param(err, self._param) libdftd4.dftd4_delete_model(err, self._disp) libdftd4.dftd4_delete_error(err) diff --git a/gpu4pyscf/lib/tests/test_dftd4.py b/gpu4pyscf/lib/tests/test_dftd4.py index a8f8737c..0e0f4f03 100644 --- a/gpu4pyscf/lib/tests/test_dftd4.py +++ b/gpu4pyscf/lib/tests/test_dftd4.py @@ -94,6 +94,7 @@ def test_gradient_r2scan_d4(self): dftd4_model = dftd4.DFTD4Dispersion(mol, "r2SCAN", atm=False) res = dftd4_model.get_dispersion(grad=True) assert np.linalg.norm(ref - res['gradient']) < 1e-10 + if __name__ == "__main__": print("Full tests for DFTD4 module") unittest.main() \ No newline at end of file diff --git a/gpu4pyscf/qmmm/test/test_chelpg.py b/gpu4pyscf/qmmm/test/test_chelpg.py index 836ce15e..5154e35b 100644 --- a/gpu4pyscf/qmmm/test/test_chelpg.py +++ b/gpu4pyscf/qmmm/test/test_chelpg.py @@ -49,7 +49,7 @@ def run_dft_chelpg(xc, deltaR): e_dft = mf.kernel() q = chelpg.eval_chelpg_layer_gpu(mf, deltaR=deltaR) return e_dft, q - + class KnownValues(unittest.TestCase): ''' @@ -63,7 +63,7 @@ class KnownValues(unittest.TestCase): CHELPG TRUE SCF_CONVERGENCE 10 $end - + Ground-State ChElPG Net Atomic Charges Atom Charge (a.u.) @@ -78,7 +78,7 @@ def test_rks_b3lyp(self): e_tot, q = run_dft_chelpg('B3LYP', 0.1) assert np.allclose(e_tot, -76.4666495181) assert np.allclose(q, np.array([-0.712558, 0.356292, 0.356266])) - + if __name__ == "__main__": print("Full Tests for SCF")