-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathnumba_util.py
More file actions
77 lines (56 loc) · 1.99 KB
/
Copy pathnumba_util.py
File metadata and controls
77 lines (56 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import logging
import sys
import threading
from autonerves import conf
logger = logging.getLogger(__name__)
try:
nopython = conf.instance["general"]["numba"]["nopython"]
cache = conf.instance["general"]["numba"]["cache"]
parallel = conf.instance["general"]["numba"]["parallel"]
except Exception:
nopython = True
cache = True
parallel = False
# Decorated functions are queued here and only handed to numba on the first
# call of any of them, keeping ``import numba`` off the library import path.
# Materialization converts every queued function at once and rebinds the
# defining module's global, because numba's nopython mode must resolve
# cross-calls between decorated functions to real dispatchers at compile time.
_pending = []
_materialize_lock = threading.Lock()
def _materialize_all():
with _materialize_lock:
if not _pending:
return
try:
import numba
except ModuleNotFoundError:
numba = None
while _pending:
func, options, placeholder, state = _pending.pop()
if numba is None:
target = func
else:
target = numba.jit(func, **options)
state["target"] = target
module = sys.modules.get(func.__module__)
if module is not None and getattr(module, func.__name__, None) is placeholder:
setattr(module, func.__name__, target)
def jit(nopython=nopython, cache=cache, parallel=parallel, fastmath=False):
options = dict(
nopython=nopython,
cache=cache,
parallel=parallel,
fastmath=fastmath,
)
def wrapper(func):
import functools
state = {"target": None}
@functools.wraps(func)
def lazy(*args, **kwargs):
if state["target"] is None:
_materialize_all()
return state["target"](*args, **kwargs)
_pending.append((func, options, lazy, state))
return lazy
return wrapper