Skip to content

Commit

Permalink
Modify the excepthook for pycuda with proper device memory pool clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
ptim0626 committed Dec 8, 2023
1 parent f9ccf07 commit cfc77d2
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions ptypy/accelerate/cuda_pycuda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,18 @@
dev_pool = None


def ctx_hook(type, value, tb):
# if an exception is raised, do the clean-up
cuda.init()
context = cuda.Context.get_current()
def _pycuda_excepthook(type, value, tb):
global dev_pool

# clean-up before raising exception
if context is not None:
context.pop()
context = None
from pycuda.tools import clear_context_caches
clear_context_caches()
# memory pool clean-up, avoid memory leak in the case of raising exception
if dev_pool is not None:
# only do the clean-up if it is present
dev_pool.stop_holding()

# raise the original exception
sys.__excepthook__(type, value, tb)
# hard exit to prevent _finish_up trying again (skip atexit)
os._exit(1)
sys.excepthook = _pycuda_excepthook

sys.excepthook = ctx_hook

def get_context(new_queue=False):

Expand Down

0 comments on commit cfc77d2

Please sign in to comment.