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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
#**************************************************************************#
#* FILE ************** accelerate_tools.py ************************#
#**************************************************************************#
#* Author: Patrick Miller February 9 2002 *#
#**************************************************************************#
"""
accelerate_tools contains the interface for on-the-fly building of
C++ equivalents to Python functions.
"""
#**************************************************************************#
from types import InstanceType,FunctionType,IntType,FloatType,StringType,TypeType,XRangeType
import inspect
import md5
import weave
import imp
from bytecodecompiler import CXXCoder,Type_Descriptor,Function_Descriptor
def CStr(s):
"Hacky way to get legal C string from Python string"
if s is None: return '""'
assert type(s) == StringType,"Only None and string allowed"
r = repr('"'+s) # Better for embedded quotes
return '"'+r[2:-1]+'"'
##################################################################
# CLASS INSTANCE #
##################################################################
class Instance(Type_Descriptor):
cxxtype = 'PyObject*'
def __init__(self,prototype):
self.prototype = prototype
return
def check(self,s):
return "PyInstance_Check(%s)"%s
def inbound(self,s):
return s
def outbound(self,s):
return s,0
def get_attribute(self,name):
proto = getattr(self.prototype,name)
T = lookup_type(proto)
code = 'tempPY = PyObject_GetAttrString(%%(rhs)s,"%s");\n'%name
convert = T.inbound('tempPY')
code += '%%(lhsType)s %%(lhs)s = %s;\n'%convert
return T,code
def set_attribute(self,name):
proto = getattr(self.prototype,name)
T = lookup_type(proto)
convert,owned = T.outbound('%(rhs)s')
code = 'tempPY = %s;'%convert
if not owned:
code += ' Py_INCREF(tempPY);'
code += ' PyObject_SetAttrString(%%(lhs)s,"%s",tempPY);'%name
code += ' Py_DECREF(tempPY);\n'
return T,code
##################################################################
# CLASS BASIC #
##################################################################
class Basic(Type_Descriptor):
owned = 1
def check(self,s):
return "%s(%s)"%(self.checker,s)
def inbound(self,s):
return "%s(%s)"%(self.inbounder,s)
def outbound(self,s):
return "%s(%s)"%(self.outbounder,s),self.owned
class Basic_Number(Basic):
def literalizer(self,s):
return str(s)
def binop(self,symbol,a,b):
assert symbol in ['+','-','*','/'],symbol
return '%s %s %s'%(a,symbol,b),self
class Integer(Basic_Number):
cxxtype = "long"
checker = "PyInt_Check"
inbounder = "PyInt_AsLong"
outbounder = "PyInt_FromLong"
class Double(Basic_Number):
cxxtype = "double"
checker = "PyFloat_Check"
inbounder = "PyFloat_AsDouble"
outbounder = "PyFloat_FromDouble"
class String(Basic):
cxxtype = "char*"
checker = "PyString_Check"
inbounder = "PyString_AsString"
outbounder = "PyString_FromString"
def literalizer(self,s):
return CStr(s)
# -----------------------------------------------
# Singletonize the type names
# -----------------------------------------------
Integer = Integer()
Double = Double()
String = String()
import scipy.base as nx
class Vector(Type_Descriptor):
cxxtype = 'PyArrayObject*'
refcount = 1
dims = 1
module_init_code = 'import_array();\n'
inbounder = "(PyArrayObject*)"
outbounder = "(PyObject*)"
owned = 0 # Convertion is by casting!
prerequisites = Type_Descriptor.prerequisites+\
['#include "scipy/arrayobject.h"']
dims = 1
def check(self,s):
return "PyArray_Check(%s) && ((PyArrayObject*)%s)->nd == %d && ((PyArrayObject*)%s)->descr->type_num == %s"%(
s,s,self.dims,s,self.typecode)
def inbound(self,s):
return "%s(%s)"%(self.inbounder,s)
def outbound(self,s):
return "%s(%s)"%(self.outbounder,s),self.owned
def getitem(self,A,v,t):
assert self.dims == len(v),'Expect dimension %d'%self.dims
code = '*((%s*)(%s->data'%(self.cxxbase,A)
for i in range(self.dims):
# assert that ''t[i]'' is an integer
code += '+%s*%s->strides[%d]'%(v[i],A,i)
code += '))'
return code,self.pybase
def setitem(self,A,v,t):
return self.getitem(A,v,t)
class matrix(Vector):
dims = 2
class IntegerVector(Vector):
typecode = 'PyArray_INT'
cxxbase = 'int'
pybase = Integer
class Integermatrix(matrix):
typecode = 'PyArray_INT'
cxxbase = 'int'
pybase = Integer
class LongVector(Vector):
typecode = 'PyArray_LONG'
cxxbase = 'long'
pybase = Integer
class Longmatrix(matrix):
typecode = 'PyArray_LONG'
cxxbase = 'long'
pybase = Integer
class DoubleVector(Vector):
typecode = 'PyArray_DOUBLE'
cxxbase = 'double'
pybase = Double
class Doublematrix(matrix):
typecode = 'PyArray_DOUBLE'
cxxbase = 'double'
pybase = Double
##################################################################
# CLASS XRANGE #
##################################################################
class XRange(Type_Descriptor):
cxxtype = 'XRange'
prerequisites = ['''
class XRange {
public:
XRange(long aLow, long aHigh, long aStep=1)
: low(aLow),high(aHigh),step(aStep)
{
}
XRange(long aHigh)
: low(0),high(aHigh),step(1)
{
}
long low;
long high;
long step;
};''']
# -----------------------------------------------
# Singletonize the type names
# -----------------------------------------------
IntegerVector = IntegerVector()
Integermatrix = Integermatrix()
LongVector = LongVector()
Longmatrix = Longmatrix()
DoubleVector = DoubleVector()
Doublematrix = Doublematrix()
XRange = XRange()
typedefs = {
IntType: Integer,
FloatType: Double,
StringType: String,
(nx.ArrayType,1,'i'): IntegerVector,
(nx.ArrayType,2,'i'): Integermatrix,
(nx.ArrayType,1,'l'): LongVector,
(nx.ArrayType,2,'l'): Longmatrix,
(nx.ArrayType,1,'d'): DoubleVector,
(nx.ArrayType,2,'d'): Doublematrix,
XRangeType : XRange,
}
import math
functiondefs = {
(len,(String,)):
Function_Descriptor(code='strlen(%s)',return_type=Integer),
(len,(LongVector,)):
Function_Descriptor(code='PyArray_Size((PyObject*)%s)',return_type=Integer),
(float,(Integer,)):
Function_Descriptor(code='(double)(%s)',return_type=Double),
(range,(Integer,Integer)):
Function_Descriptor(code='XRange(%s)',return_type=XRange),
(range,(Integer)):
Function_Descriptor(code='XRange(%s)',return_type=XRange),
(math.sin,(Double,)):
Function_Descriptor(code='sin(%s)',return_type=Double),
(math.cos,(Double,)):
Function_Descriptor(code='cos(%s)',return_type=Double),
(math.sqrt,(Double,)):
Function_Descriptor(code='sqrt(%s)',return_type=Double),
}
##################################################################
# FUNCTION LOOKUP_TYPE #
##################################################################
def lookup_type(x):
T = type(x)
try:
return typedefs[T]
except:
import scipy.base as nx
if isinstance(T,nx.ArrayType):
return typedefs[(T,len(x.shape),x.typecode())]
elif T == InstanceType:
return Instance(x)
else:
raise NotImplementedError,T
##################################################################
# class ACCELERATE #
##################################################################
class accelerate:
def __init__(self, function, *args, **kw):
assert type(function) == FunctionType
self.function = function
self.module = inspect.getmodule(function)
if self.module is None:
import __main__
self.module = __main__
self.__call_map = {}
def __cache(self,*args):
raise TypeError
def __call__(self,*args):
try:
return self.__cache(*args)
except TypeError:
# Figure out type info -- Do as tuple so its hashable
signature = tuple( map(lookup_type,args) )
# If we know the function, call it
try:
fast = self.__call_map[signature]
except:
fast = self.singleton(signature)
self.__cache = fast
self.__call_map[signature] = fast
return fast(*args)
def signature(self,*args):
# Figure out type info -- Do as tuple so its hashable
signature = tuple( map(lookup_type,args) )
return self.singleton(signature)
def singleton(self,signature):
identifier = self.identifier(signature)
# Generate a new function, then call it
f = self.function
# See if we have an accelerated version of module
try:
print 'lookup',self.module.__name__+'_weave'
accelerated_module = __import__(self.module.__name__+'_weave')
print 'have accelerated',self.module.__name__+'_weave'
fast = getattr(accelerated_module,identifier)
return fast
except ImportError:
accelerated_module = None
except AttributeError:
pass
P = self.accelerate(signature,identifier)
E = weave.ext_tools.ext_module(self.module.__name__+'_weave')
E.add_function(P)
E.generate_file()
weave.build_tools.build_extension(self.module.__name__+'_weave.cpp',verbose=2)
if accelerated_module:
raise NotImplementedError,'Reload'
else:
accelerated_module = __import__(self.module.__name__+'_weave')
fast = getattr(accelerated_module,identifier)
return fast
def identifier(self,signature):
# Build an MD5 checksum
f = self.function
co = f.func_code
identifier = str(signature)+\
str(co.co_argcount)+\
str(co.co_consts)+\
str(co.co_varnames)+\
co.co_code
return 'F'+md5.md5(identifier).hexdigest()
def accelerate(self,signature,identifier):
P = Python2CXX(self.function,signature,name=identifier)
return P
def code(self,*args):
if len(args) != self.function.func_code.co_argcount:
raise TypeError,'%s() takes exactly %d arguments (%d given)'%(
self.function.__name__,
self.function.func_code.co_argcount,
len(args))
signature = tuple( map(lookup_type,args) )
ident = self.function.__name__
return self.accelerate(signature,ident).function_code()
##################################################################
# CLASS PYTHON2CXX #
##################################################################
class Python2CXX(CXXCoder):
def typedef_by_value(self,v):
T = lookup_type(v)
if T not in self.used:
self.used.append(T)
return T
def function_by_signature(self,signature):
descriptor = functiondefs[signature]
if descriptor.return_type not in self.used:
self.used.append(descriptor.return_type)
return descriptor
def __init__(self,f,signature,name=None):
# Make sure function is a function
import types
assert type(f) == FunctionType
# and check the input type signature
assert reduce(lambda x,y: x and y,
map(lambda x: isinstance(x,Type_Descriptor),
signature),
1),'%s not all type objects'%signature
self.arg_specs = []
self.customize = weave.base_info.custom_info()
CXXCoder.__init__(self,f,signature,name)
return
def function_code(self):
code = self.wrapped_code()
for T in self.used:
if T != None and T.module_init_code:
self.customize.add_module_init_code(T.module_init_code)
return code
def python_function_definition_code(self):
return '{ "%s", wrapper_%s, METH_VARARGS, %s },\n'%(
self.name,
self.name,
CStr(self.function.__doc__))
|