Skip to content

Commit

Permalink
Manually clean up PETSc objects (#899)
Browse files Browse the repository at this point in the history
Garbage collection works differently (worse) in petsc4py 3.18.3
than in 3.17.4.

Fixes #896
  • Loading branch information
guyer committed Jan 12, 2023
1 parent db5292d commit 4db188c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
8 changes: 8 additions & 0 deletions fipy/matrices/petscMatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def __init__(self, matrix):
"""
self.matrix = matrix

def __del__(self):
self.matrix.destroy()

def copy(self):
return _PETScMatrix(matrix=self.matrix.copy())

Expand Down Expand Up @@ -547,6 +550,11 @@ def copy(self):
copy.matrix = tmp.matrix
return copy

def __del__(self):
if hasattr(self, "_ao_"):
self._ao_.destroy()
super(_PETScBaseMeshMatrix, self).__del__()

@property
def _ao(self):
"""Application Ordering to relate FiPy matrix rows to PETSc matrix rows
Expand Down
2 changes: 2 additions & 0 deletions fipy/solvers/petsc/petscKrylovSolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ def _solve_(self, L, x, b):
self._log.debug('iterations: %d / %d', ksp.its, self.iterations)
self._log.debug('norm: %s', ksp.norm)
self._log.debug('norm_type: %s', ksp.norm_type)

ksp.destroy()
3 changes: 2 additions & 1 deletion fipy/solvers/petsc/petscSolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def _globalMatrixAndVectors(self):
return self.globalVectors

def _deleteGlobalMatrixAndVectors(self):
self.matrix.flush()
del self.matrix
if hasattr(self, "globalVectors"):
globalMatrix, overlappingVector, overlappingRHSvector = self._globalMatrixAndVectors
overlappingVector.destroy()
Expand Down Expand Up @@ -106,5 +106,6 @@ def _calcRHSNorm(self):
def __del__(self):
if hasattr(self, "globalVectors"):
globalMatrix, overlappingVector, overlappingRHSvector = self._globalMatrixAndVectors
del globalMatrix
overlappingVector.destroy()
overlappingRHSvector.destroy()
3 changes: 3 additions & 0 deletions fipy/solvers/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,6 @@ def __enter__(self):

def __exit__(self, exc_type, exc_value, traceback):
pass

def __del__(self):
pass

0 comments on commit 4db188c

Please sign in to comment.