Aluode/PerceptionLabPortable
0
1import numpy as np
2
3from numba.core.typing.typeof import typeof
4from numba.core.typing.asnumbatype import as_numba_type
5
6
7def pndindex(*args):
8 """ Provides an n-dimensional parallel iterator that generates index tuples
9 for each iteration point. Sequentially, pndindex is identical to np.ndindex.
10 """
11 return np.ndindex(*args)
12
13
14class prange(object):
15 """ Provides a 1D parallel iterator that generates a sequence of integers.
16 In non-parallel contexts, prange is identical to range.
17 """
18 def __new__(cls, *args):
19 return range(*args)
20
21
22def _gdb_python_call_gen(func_name, *args):
23 # generates a call to a function containing a compiled in gdb command,
24 # this is to make `numba.gdb*` work in the interpreter.
25 import numba
26 fn = getattr(numba, func_name)
27 argstr = ','.join(['"%s"' for _ in args]) % args
28 defn = """def _gdb_func_injection():\n\t%s(%s)\n
29 """ % (func_name, argstr)
30 l = {}
31 exec(defn, {func_name: fn}, l)
32 return numba.njit(l['_gdb_func_injection'])
33
34
35def gdb(*args):
36 """
37 Calling this function will invoke gdb and attach it to the current process
38 at the call site. Arguments are strings in the gdb command language syntax
39 which will be executed by gdb once initialisation has occurred.
40 """
41 _gdb_python_call_gen('gdb', *args)()
42
43
44def gdb_breakpoint():
45 """
46 Calling this function will inject a breakpoint at the call site that is
47 recognised by both `gdb` and `gdb_init`, this is to allow breaking at
48 multiple points. gdb will stop in the user defined code just after the frame
49 employed by the breakpoint returns.
50 """
51 _gdb_python_call_gen('gdb_breakpoint')()
52
53
54def gdb_init(*args):
55 """
56 Calling this function will invoke gdb and attach it to the current process
57 at the call site, then continue executing the process under gdb's control.
58 Arguments are strings in the gdb command language syntax which will be
59 executed by gdb once initialisation has occurred.
60 """
61 _gdb_python_call_gen('gdb_init', *args)()
62
63
64def literally(obj):
65 """Forces Numba to interpret *obj* as an Literal value.
66
67 *obj* must be either a literal or an argument of the caller function, where
68 the argument must be bound to a literal. The literal requirement
69 propagates up the call stack.
70
71 This function is intercepted by the compiler to alter the compilation
72 behavior to wrap the corresponding function parameters as ``Literal``.
73 It has **no effect** outside of nopython-mode (interpreter, and objectmode).
74
75 The current implementation detects literal arguments in two ways:
76
77 1. Scans for uses of ``literally`` via a compiler pass.
78 2. ``literally`` is overloaded to raise ``numba.errors.ForceLiteralArg``
79 to signal the dispatcher to treat the corresponding parameter
80 differently. This mode is to support indirect use (via a function call).
81
82 The execution semantic of this function is equivalent to an identity
83 function.
84
85 See :ghfile:`numba/tests/test_literal_dispatch.py` for examples.
86 """
87 return obj
88
89
90def literal_unroll(container):
91 return container
92
93
94__all__ = [
95 'typeof',
96 'as_numba_type',
97 'prange',
98 'pndindex',
99 'gdb',
100 'gdb_breakpoint',
101 'gdb_init',
102 'literally',
103 'literal_unroll',
104]
105 