-
-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathcompilation.py
More file actions
252 lines (208 loc) · 7.79 KB
/
compilation.py
File metadata and controls
252 lines (208 loc) · 7.79 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import ctypes
from mathics.version import __version__ # noqa used in loading to check consistency.
from mathics.builtin.base import Builtin, BoxConstruct
from mathics.core.evaluation import Evaluation
from mathics.core.expression import (
Atom,
Expression,
Symbol,
String,
from_python,
Integer,
)
from types import FunctionType
class Compile(Builtin):
"""
<dl>
<dt>'Compile[{$x1$, $x2$, ...}, $expr$]'
<dd>Compiles $expr$ assuming each $xi$ is a $Real$ number.
<dt>'Compile[{{$x1$, $t1$} {$x2$, $t1$} ...}, $expr$]'
<dd>Compiles assuming each $xi$ matches type $ti$.
</dl>
>> cf = Compile[{x, y}, x + 2 y]
= CompiledFunction[{x, y}, x + 2 y, -CompiledCode-]
>> cf[2.5, 4.3]
= 11.1
>> cf = Compile[{{x, _Real}}, Sin[x]]
= CompiledFunction[{x}, Sin[x], -CompiledCode-]
>> cf[1.4]
= 0.98545
#> cf[1/2]
= 0.479426
#> cf[4]
= -0.756802
#> cf[x]
: Invalid argument x should be Integer, Real or boolean.
= CompiledFunction[{x}, Sin[x], -CompiledCode-][x]
#> cf = Compile[{{x, _Real}, {x, _Integer}}, Sin[x + y]]
: Duplicate parameter x found in {{x, _Real}, {x, _Integer}}.
= Compile[{{x, _Real}, {x, _Integer}}, Sin[x + y]]
#> cf = Compile[{{x, _Real}, {y, _Integer}}, Sin[x + z]]
= CompiledFunction[{x, y}, Sin[x + z], -PythonizedCode-]
#> cf = Compile[{{x, _Real}, {y, _Integer}}, Sin[x + y]]
= CompiledFunction[{x, y}, Sin[x + y], -CompiledCode-]
#> cf[1, 2]
= 0.14112
#> cf[x + y]
= CompiledFunction[{x, y}, Sin[x + y], -CompiledCode-][x + y]
Compile supports basic flow control:
>> cf = Compile[{{x, _Real}, {y, _Integer}}, If[x == 0.0 && y <= 0, 0.0, Sin[x ^ y] + 1 / Min[x, 0.5]] + 0.5]
= CompiledFunction[{x, y}, ..., -CompiledCode-]
>> cf[3.5, 2]
= 2.18888
#> cf[0, -2]
= 0.5
Loops and variable assignments are supported as python (not llvmlite)
functions
>> Compile[{{a, _Integer}, {b, _Integer}}, While[b != 0, {a, b} = {b, Mod[a, b]}]; a] (* GCD of a, b *)
= CompiledFunction[{a, b}, a, -PythonizedCode-]
"""
requires = ("llvmlite",)
attributes = ("HoldAll",)
messages = {
"invar": "Variable `1` should be {symbol, type} annotation.",
"invars": "Variables should be a list of {symbol, type} annotations.",
"comperr": "Expression `1` could not be compiled.",
"fdup": "Duplicate parameter `1` found in `2`.",
}
def apply(self, vars, expr, evaluation):
"Compile[vars_, expr_]"
from mathics.builtin.compile import (
_compile,
int_type,
real_type,
bool_type,
CompileArg,
CompileError,
)
# _Complex not implemented
permitted_types = {
Expression("Blank", Symbol("Integer")): int_type,
Expression("Blank", Symbol("Real")): real_type,
Symbol("True"): bool_type,
Symbol("False"): bool_type,
}
if not vars.has_form("List", None):
return evaluation.message("Compile", "invars")
args = []
names = []
for var in vars.get_leaves():
if isinstance(var, Symbol):
symb = var
name = symb.get_name()
typ = real_type
elif var.has_form("List", 2):
symb, typ = var.get_leaves()
if isinstance(symb, Symbol) and typ in permitted_types:
name = symb.get_name()
typ = permitted_types[typ]
else:
return evaluation.message("Compile", "invar", var)
else:
return evaluation.message("Compile", "invar", var)
# check for duplicate names
if name in names:
return evaluation.message("Compile", "fdup", symb, vars)
else:
names.append(name)
args.append(CompileArg(name, typ))
try:
cfunc = _compile(expr, args)
except CompileError:
cfunc = None
if cfunc is None:
try:
def _pythonized_mathics_expr(*x):
inner_evaluation = Evaluation(definitions=evaluation.definitions)
vars = dict(list(zip(names, x[: len(names)])))
pyexpr = expr.replace_vars(vars)
pyexpr = Expression("N", pyexpr).evaluate(inner_evaluation)
res = pyexpr.to_python(n_evaluation=inner_evaluation)
return res
# TODO: check if we can use numba to compile this...
cfunc = _pythonized_mathics_expr
except Exception:
cfunc = None
if cfunc is None:
evaluation.message("Compile", "comperr", expr)
args = Expression("List", *names)
return Expression("Function", args, expr)
code = CompiledCode(cfunc, args)
arg_names = Expression("List", *(Symbol(arg.name) for arg in args))
return Expression("CompiledFunction", arg_names, expr, code)
class CompiledCode(Atom):
def __init__(self, cfunc, args, **kwargs):
super(CompiledCode, self).__init__(**kwargs)
self.cfunc = cfunc
self.args = args
def __str__(self):
if type(self.cfunc) is FunctionType:
return "-PythonizedCode-"
return "-CompiledCode-"
def do_copy(self):
return CompiledCode(self.cfunc, self.args)
def default_format(self, evaluation, form):
return str(self)
def get_sort_key(self, pattern_sort=False):
if pattern_sort:
return super(CompiledCode, self).get_sort_key(True)
else:
return hash(self)
def same(self, other):
return self is other
def to_python(self, *args, **kwargs):
return None
def __hash__(self):
return hash(("CompiledCode", ctypes.addressof(self.cfunc))) # XXX hack
def atom_to_boxes(self, f, evaluation):
return CompiledCodeBox(String(self.__str__()), evaluation=evaluation)
class CompiledCodeBox(BoxConstruct):
"""
Used internally by <i>CompileCode[]</i>.
"""
def _boxes_to_text(self, leaves=None, **options):
if leaves is None:
leaves = self._leaves
return leaves[0].value
def _boxes_to_mathml(self, leaves=None, **options):
if leaves is None:
leaves = self._leaves
return leaves[0].value
def _boxes_to_tex(self, leaves=None, **options):
if leaves is None:
leaves = self._leaves
return leaves[0].value
class CompiledFunction(Builtin):
"""'
<dl>
<dt>'CompiledFunction[$args$...]'
<dd>represents compiled code for evaluating a compiled function.
</dl>
>> sqr = Compile[{x}, x x]
= CompiledFunction[{x}, x ^ 2, -CompiledCode-]
>> Head[sqr]
= CompiledFunction
>> sqr[2]
= 4.
"""
messages = {"argerr": "Invalid argument `1` should be Integer, Real or boolean."}
def apply(self, argnames, expr, code, args, evaluation):
"CompiledFunction[argnames_, expr_, code_CompiledCode][args__]"
argseq = args.get_sequence()
if len(argseq) != len(code.args):
return
py_args = []
for arg in argseq:
if isinstance(arg, Integer):
py_args.append(arg.get_int_value())
elif arg.same(Symbol("True")):
py_args.append(True)
elif arg.same(Symbol("False")):
py_args.append(False)
else:
py_args.append(arg.round_to_float(evaluation))
try:
result = code.cfunc(*py_args)
except (TypeError, ctypes.ArgumentError):
return evaluation.message("CompiledFunction", "argerr", args)
return from_python(result)