|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: BSD-2-Clause |
| 3 | + |
| 4 | +import threading |
| 5 | +import functools |
| 6 | +import numba.cuda.core.event as ev |
| 7 | +from numba.cuda import HAS_NUMBA |
| 8 | + |
| 9 | +if HAS_NUMBA: |
| 10 | + from numba.core.compiler_lock import ( |
| 11 | + global_compiler_lock as _numba_compiler_lock, |
| 12 | + ) |
| 13 | +else: |
| 14 | + _numba_compiler_lock = None |
| 15 | + |
| 16 | + |
| 17 | +# Lock for the preventing multiple compiler execution |
| 18 | +class _CompilerLock(object): |
| 19 | + def __init__(self): |
| 20 | + self._lock = threading.RLock() |
| 21 | + |
| 22 | + def acquire(self): |
| 23 | + ev.start_event("numba-cuda:compiler_lock") |
| 24 | + self._lock.acquire() |
| 25 | + |
| 26 | + def release(self): |
| 27 | + self._lock.release() |
| 28 | + ev.end_event("numba-cuda:compiler_lock") |
| 29 | + |
| 30 | + def __enter__(self): |
| 31 | + self.acquire() |
| 32 | + |
| 33 | + def __exit__(self, exc_val, exc_type, traceback): |
| 34 | + self.release() |
| 35 | + |
| 36 | + def __call__(self, func): |
| 37 | + @functools.wraps(func) |
| 38 | + def _acquire_compile_lock(*args, **kwargs): |
| 39 | + with self: |
| 40 | + return func(*args, **kwargs) |
| 41 | + |
| 42 | + return _acquire_compile_lock |
| 43 | + |
| 44 | + |
| 45 | +_numba_cuda_compiler_lock = _CompilerLock() |
| 46 | + |
| 47 | + |
| 48 | +# Wrapper that coordinates both numba and numba-cuda compiler locks |
| 49 | +class _DualCompilerLock(object): |
| 50 | + """Wrapper that coordinates both the numba-cuda and upstream numba compiler locks.""" |
| 51 | + |
| 52 | + def __init__(self, cuda_lock, numba_lock): |
| 53 | + self._cuda_lock = cuda_lock |
| 54 | + self._numba_lock = numba_lock |
| 55 | + |
| 56 | + def acquire(self): |
| 57 | + self._numba_lock.acquire() |
| 58 | + self._cuda_lock.acquire() |
| 59 | + |
| 60 | + def release(self): |
| 61 | + self._cuda_lock.release() |
| 62 | + self._numba_lock.release() |
| 63 | + |
| 64 | + def __enter__(self): |
| 65 | + self.acquire() |
| 66 | + |
| 67 | + def __exit__(self, exc_val, exc_type, traceback): |
| 68 | + self.release() |
| 69 | + |
| 70 | + def __call__(self, func): |
| 71 | + @functools.wraps(func) |
| 72 | + def _acquire_compile_lock(*args, **kwargs): |
| 73 | + with self: |
| 74 | + return func(*args, **kwargs) |
| 75 | + |
| 76 | + return _acquire_compile_lock |
| 77 | + |
| 78 | + |
| 79 | +# Create the global compiler lock, wrapping both locks if numba is available |
| 80 | +if HAS_NUMBA: |
| 81 | + global_compiler_lock = _DualCompilerLock( |
| 82 | + _numba_cuda_compiler_lock, _numba_compiler_lock |
| 83 | + ) |
| 84 | +else: |
| 85 | + global_compiler_lock = _numba_cuda_compiler_lock |
0 commit comments