|
27 | 27 | """Define functions for patching NumPy with MKL-based NumPy interface.""" |
28 | 28 |
|
29 | 29 | from contextlib import ContextDecorator |
30 | | -from threading import local as threading_local |
| 30 | +from threading import Lock |
31 | 31 |
|
32 | 32 | import numpy as np |
33 | 33 |
|
34 | 34 | import mkl_fft.interfaces.numpy_fft as _nfft |
35 | 35 |
|
36 | | -_tls = threading_local() |
37 | 36 |
|
38 | | - |
39 | | -class _Patch: |
40 | | - """Internal object for patching NumPy with mkl_fft interfaces.""" |
41 | | - |
42 | | - _is_patched = False |
43 | | - __patched_functions__ = _nfft.__all__ |
44 | | - _restore_dict = {} |
| 37 | +class _GlobalPatch(ContextDecorator): |
| 38 | + def __init__(self): |
| 39 | + self._lock = Lock() |
| 40 | + self._patch_count = 0 |
| 41 | + self._restore_dict = {} |
| 42 | + # make _patched_functions a tuple (immutable) |
| 43 | + self._patched_functions = tuple(_nfft.__all__) |
45 | 44 |
|
46 | 45 | def _register_func(self, name, func): |
47 | | - if name not in self.__patched_functions__: |
48 | | - raise ValueError("%s not an mkl_fft function." % name) |
49 | | - f = getattr(np.fft, name) |
50 | | - self._restore_dict[name] = f |
| 46 | + if name not in self._patched_functions: |
| 47 | + raise ValueError(f"{name} not an mkl_fft function.") |
| 48 | + if name not in self._restore_dict: |
| 49 | + self._restore_dict[name] = getattr(np.fft, name) |
51 | 50 | setattr(np.fft, name, func) |
52 | 51 |
|
53 | 52 | def _restore_func(self, name, verbose=False): |
54 | | - if name not in self.__patched_functions__: |
55 | | - raise ValueError("%s not an mkl_fft function." % name) |
| 53 | + if name not in self._patched_functions: |
| 54 | + raise ValueError(f"{name} not an mkl_fft function.") |
56 | 55 | try: |
57 | 56 | val = self._restore_dict[name] |
58 | 57 | except KeyError: |
59 | 58 | if verbose: |
60 | | - print("failed to restore") |
| 59 | + print(f"failed to restore {name}") |
61 | 60 | return |
62 | 61 | else: |
63 | 62 | if verbose: |
64 | | - print("found and restoring...") |
| 63 | + print(f"found and restoring {name}...") |
65 | 64 | setattr(np.fft, name, val) |
66 | 65 |
|
67 | | - def restore(self, verbose=False): |
68 | | - for name in self._restore_dict.keys(): |
69 | | - self._restore_func(name, verbose=verbose) |
70 | | - self._is_patched = False |
71 | | - |
72 | | - def do_patch(self): |
73 | | - for f in self.__patched_functions__: |
74 | | - self._register_func(f, getattr(_nfft, f)) |
75 | | - self._is_patched = True |
| 66 | + def do_patch(self, verbose=False): |
| 67 | + with self._lock: |
| 68 | + if self._patch_count == 0: |
| 69 | + if verbose: |
| 70 | + print("Now patching NumPy FFT submodule with mkl_fft NumPy interface.") |
| 71 | + print( |
| 72 | + "Please direct bug reports to https://github.com/IntelPython/mkl_fft" |
| 73 | + ) |
| 74 | + for f in self._patched_functions: |
| 75 | + self._register_func(f, getattr(_nfft, f)) |
| 76 | + self._patch_count += 1 |
| 77 | + |
| 78 | + def do_restore(self, verbose=False): |
| 79 | + with self._lock: |
| 80 | + if self._patch_count > 0: |
| 81 | + self._patch_count -= 1 |
| 82 | + if self._patch_count == 0: |
| 83 | + if verbose: |
| 84 | + print("Now restoring original NumPy FFT submodule.") |
| 85 | + for name in tuple(self._restore_dict): |
| 86 | + self._restore_func(name, verbose=verbose) |
| 87 | + self._restore_dict.clear() |
76 | 88 |
|
77 | 89 | def is_patched(self): |
78 | | - return self._is_patched |
| 90 | + with self._lock: |
| 91 | + return self._patch_count > 0 |
79 | 92 |
|
| 93 | + def __enter__(self): |
| 94 | + self.do_patch() |
| 95 | + return self |
80 | 96 |
|
81 | | -def _initialize_tls(): |
82 | | - _tls.patch = _Patch() |
83 | | - _tls.initialized = True |
| 97 | + def __exit__(self, *exc): |
| 98 | + self.do_restore() |
| 99 | + return False |
84 | 100 |
|
85 | 101 |
|
86 | | -def _is_tls_initialized(): |
87 | | - return (getattr(_tls, "initialized", None) is not None) and ( |
88 | | - _tls.initialized is True |
89 | | - ) |
| 102 | +_patch = _GlobalPatch() |
90 | 103 |
|
91 | 104 |
|
92 | 105 | def patch_numpy_fft(verbose=False): |
93 | | - if verbose: |
94 | | - print("Now patching NumPy FFT submodule with mkl_fft NumPy interface.") |
95 | | - print( |
96 | | - "Please direct bug reports to https://github.com/IntelPython/mkl_fft" |
97 | | - ) |
98 | | - if not _is_tls_initialized(): |
99 | | - _initialize_tls() |
100 | | - _tls.patch.do_patch() |
| 106 | + """Patch NumPy's fft submodule with mkl_fft's numpy_interface. |
| 107 | +
|
| 108 | + Parameters |
| 109 | + ---------- |
| 110 | + verbose : bool, optional |
| 111 | + print message when starting the patching process. |
| 112 | +
|
| 113 | + """ |
| 114 | + _patch.do_patch(verbose=verbose) |
101 | 115 |
|
102 | 116 |
|
103 | 117 | def restore_numpy_fft(verbose=False): |
104 | | - if verbose: |
105 | | - print("Now restoring original NumPy FFT submodule.") |
106 | | - if not _is_tls_initialized(): |
107 | | - _initialize_tls() |
108 | | - _tls.patch.restore(verbose=verbose) |
| 118 | + """ |
| 119 | + Restore NumPy's fft submodule to its original implementations. |
| 120 | +
|
| 121 | + Parameters |
| 122 | + ---------- |
| 123 | + verbose : bool, optional |
| 124 | + print message when starting restoration process. |
| 125 | +
|
| 126 | + """ |
| 127 | + _patch.do_restore(verbose=verbose) |
109 | 128 |
|
110 | 129 |
|
111 | 130 | def is_patched(): |
112 | | - if not _is_tls_initialized(): |
113 | | - _initialize_tls() |
114 | | - return _tls.patch.is_patched() |
| 131 | + """Return True if NumPy's fft submodule is currently patched by mkl_fft.""" |
| 132 | + return _patch.is_patched() |
115 | 133 |
|
116 | 134 |
|
117 | 135 | class mkl_fft(ContextDecorator): |
| 136 | + """ |
| 137 | + Context manager and decorator to temporarily patch NumPy fft submodule |
| 138 | + with MKL-based implementations. |
| 139 | +
|
| 140 | + Examples |
| 141 | + -------- |
| 142 | + >>> import mkl_fft |
| 143 | + >>> mkl_fft.is_patched() |
| 144 | + # False |
| 145 | +
|
| 146 | + >>> with mkl_fft.mkl_fft(): # Enable mkl_fft in Numpy |
| 147 | + >>> print(mkl_fft.is_patched()) |
| 148 | + # True |
| 149 | +
|
| 150 | + >>> mkl_fft.is_patched() |
| 151 | + # False |
| 152 | +
|
| 153 | + """ |
| 154 | + |
118 | 155 | def __enter__(self): |
119 | 156 | patch_numpy_fft() |
120 | 157 | return self |
|
0 commit comments