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 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
|
import sys
import numpy
import interpreter, expressions
typecode_to_kind = {'b': 'bool', 'i': 'int', 'f': 'float',
'c': 'complex', 'n' : 'none'}
kind_to_typecode = {'bool': 'b', 'int': 'i', 'float': 'f',
'complex': 'c', 'none' : 'n'}
type_to_kind = expressions.type_to_kind
kind_to_type = expressions.kind_to_type
class ASTNode(object):
"""Abstract Syntax Tree node.
Members:
astType -- type of node (op, constant, variable, raw, or alias)
astKind -- the type of the result (bool, float, etc.)
value -- value associated with this node.
An opcode, numerical value, a variable name, etc.
children -- the children below this node
reg -- the register assigned to the result for this node.
"""
cmpnames = ['astType', 'astKind', 'value', 'children']
def __init__(self, astType='generic', astKind='unknown',
value=None, children=()):
object.__init__(self)
self.astType = astType
self.astKind = astKind
self.value = value
self.children = tuple(children)
self.reg = None
def __eq__(self, other):
if self.astType == 'alias':
self = self.value
if other.astType == 'alias':
other = other.value
if not isinstance(other, ASTNode):
return False
for name in self.cmpnames:
if getattr(self, name) != getattr(other, name):
return False
return True
def __hash__(self):
if self.astType == 'alias':
self = self.value
hashval = 0
for name in self.cmpnames:
hashval ^= hash(getattr(self, name))
return hashval
def __str__(self):
return 'AST(%s, %s, %s, %s, %s)' % (self.astType, self.astKind,
self.value, self.children, self.reg)
def __repr__(self): return '<AST object at %s>' % id(self)
def key(self):
return (self.astType, self.astKind, self.value, self.children)
def typecode(self):
return kind_to_typecode[self.astKind]
def postorderWalk(self):
for c in self.children:
for w in c.postorderWalk():
yield w
yield self
def allOf(self, *astTypes):
astTypes = set(astTypes)
for w in self.postorderWalk():
if w.astType in astTypes:
yield w
def expressionToAST(ex):
"""Take an expression tree made out of expressions.ExpressionNode,
and convert to an AST tree.
This is necessary as ExpressionNode overrides many methods to act
like a number.
"""
this_ast = ASTNode(ex.astType, ex.astKind, ex.value,
[expressionToAST(c) for c in ex.children])
return this_ast
def sigPerms(s):
"""Generate all possible signatures derived by upcasting the given
signature.
"""
codes = 'bifc'
if not s:
yield ''
elif s[0] in codes:
start = codes.index(s[0])
for x in codes[start:]:
for y in sigPerms(s[1:]):
yield x + y
else:
yield s
def typeCompileAst(ast):
"""Assign appropiate types to each node in the AST.
Will convert opcodes and functions to appropiate upcast version,
and add "cast" ops if needed.
"""
children = list(ast.children)
if ast.astType == 'op':
retsig = ast.typecode()
basesig = ''.join(x.typecode() for x in list(ast.children))
# Find some operation that will work on an acceptable casting of args.
for sig in sigPerms(basesig):
value = ast.value + '_' + retsig + sig
if value in interpreter.opcodes:
break
else:
for sig in sigPerms(basesig):
funcname = ast.value + '_' + retsig + sig
if funcname in interpreter.funccodes:
value = 'func_%s' % (retsig+sig)
children += [ASTNode('raw', 'none',
interpreter.funccodes[funcname])]
break
else:
raise NotImplementedError(
"couldn't find matching opcode for '%s'"
% (ast.value + '_' + retsig+basesig))
# First just cast constants, then cast variables if necessary:
for i, (have, want) in enumerate(zip(basesig, sig)):
if have != want:
kind = typecode_to_kind[want]
if children[i].astType == 'constant':
children[i] = ASTNode('constant', kind, children[i].value)
else:
opname = "cast"
children[i] = ASTNode('op', kind, opname, [children[i]])
else:
value = ast.value
children = ast.children
new_ast = ASTNode(ast.astType, ast.astKind, value,
[typeCompileAst(c) for c in children])
return new_ast
class Register(object):
"""Abstraction for a register in the VM.
Members:
node -- the AST node this corresponds to
temporary -- True if this isn't an input or output
immediate -- not a register, but an immediate value
n -- the physical register number.
None if no number assigned yet.
"""
def __init__(self, astnode, temporary=False):
self.node = astnode
self.temporary = temporary
self.immediate = False
self.n = None
def __str__(self):
if self.temporary:
name = 'Temporary'
else:
name = 'Register'
return '%s(%s, %s, %s)' % (name, self.node.astType,
self.node.astKind, self.n,)
def __repr__(self):
return self.__str__()
class Immediate(Register):
"""Representation of an immediate (integer) operand, instead of
a register.
"""
def __init__(self, astnode):
Register.__init__(self, astnode)
self.immediate = True
def __str__(self):
return 'Immediate(%d)' % (self.node.value,)
def stringToExpression(s, types, context):
"""Given a string, convert it to a tree of ExpressionNode's.
"""
old_ctx = expressions._context.ctx
try:
expressions._context.ctx = context
# first compile to a code object to determine the names
c = compile(s, '<expr>', 'eval')
# make VariableNode's for the names
names = {}
for name in c.co_names:
if name == "None":
names[name] = None
else:
t = types.get(name, float)
names[name] = expressions.VariableNode(name, type_to_kind[t])
names.update(expressions.functions)
# now build the expression
ex = eval(c, names)
if expressions.isConstant(ex):
ex = expressions.ConstantNode(ex, expressions.getKind(ex))
finally:
expressions._context.ctx = old_ctx
return ex
def isReduction(ast):
return ast.value.startswith('sum_') or ast.value.startswith('prod_')
def getInputOrder(ast, input_order=None):
"""Derive the input order of the variables in an expression.
"""
variables = {}
for a in ast.allOf('variable'):
variables[a.value] = a
variable_names = set(variables.keys())
if input_order:
if variable_names != set(input_order):
raise ValueError("input names don't match those found in expression")
ordered_names = input_order
else:
ordered_names = list(variable_names)
ordered_names.sort()
ordered_variables = [variables[v] for v in ordered_names]
return ordered_variables
def convertConstantToKind(x, kind):
return kind_to_type[kind](x)
def getConstants(ast):
const_map = {}
for a in ast.allOf('constant'):
const_map[(a.astKind, a.value)] = a
ordered_constants = const_map.keys()
ordered_constants.sort()
constants_order = [const_map[v] for v in ordered_constants]
constants = [convertConstantToKind(a.value, a.astKind)
for a in constants_order]
return constants_order, constants
def sortNodesByOrder(nodes, order):
order_map = {}
for i, (_, v, _) in enumerate(order):
order_map[v] = i
dec_nodes = [(order_map[n.value], n) for n in nodes]
dec_nodes.sort()
return [a[1] for a in dec_nodes]
def assignLeafRegisters(inodes, registerMaker):
"""Assign new registers to each of the leaf nodes.
"""
leafRegisters = {}
for node in inodes:
key = node.key()
if key in leafRegisters:
node.reg = leafRegisters[key]
else:
node.reg = leafRegisters[key] = registerMaker(node)
def assignBranchRegisters(inodes, registerMaker):
"""Assign temporary registers to each of the branch nodes.
"""
for node in inodes:
node.reg = registerMaker(node, temporary=True)
def collapseDuplicateSubtrees(ast):
"""common subexpression elimination.
"""
seen = {}
aliases = []
for a in ast.allOf('op'):
if a in seen:
target = seen[a]
a.astType = 'alias'
a.value = target
a.children = ()
aliases.append(a)
else:
seen[a] = a
# Set values and registers so optimizeTemporariesAllocation
# doesn't get confused
for a in aliases:
while a.value.astType == 'alias':
a.value = a.value.value
a.reg = a.value.reg
def optimizeTemporariesAllocation(ast):
"""Attempt to minimize the number of temporaries needed, by
reusing old ones.
"""
nodes = list(x for x in ast.postorderWalk() if x.reg.temporary)
users_of = dict((n.reg, set()) for n in nodes)
if nodes and nodes[-1] is not ast:
for c in ast.children:
if c.reg.temporary:
users_of[c.reg].add(ast)
for n in reversed(nodes):
for c in n.children:
if c.reg.temporary:
users_of[c.reg].add(n)
unused = {'bool' : set(), 'int' : set(),
'float' : set(), 'complex' : set()}
for n in nodes:
for reg, users in users_of.iteritems():
if n in users:
users.remove(n)
if not users:
unused[reg.node.astKind].add(reg)
if unused[n.astKind]:
reg = unused[n.astKind].pop()
users_of[reg] = users_of[n.reg]
n.reg = reg
def setOrderedRegisterNumbers(order, start):
"""Given an order of nodes, assign register numbers.
"""
for i, node in enumerate(order):
node.reg.n = start + i
return start + len(order)
def setRegisterNumbersForTemporaries(ast, start):
"""Assign register numbers for temporary registers, keeping track of
aliases and handling immediate operands.
"""
seen = 0
signature = ''
aliases = []
for node in ast.postorderWalk():
if node.astType == 'alias':
aliases.append(node)
node = node.value
if node.reg.immediate:
node.reg.n = node.value
continue
reg = node.reg
if reg.n < 0:
reg.n = start + seen
seen += 1
signature += reg.node.typecode()
for node in aliases:
node.reg = node.value.reg
return start + seen, signature
def convertASTtoThreeAddrForm(ast):
"""Convert an AST to a three address form.
Three address form is (op, reg1, reg2, reg3), where reg1 is the
destination of the result of the instruction.
I suppose this should be called three register form, but three
address form is found in compiler theory.
"""
program = []
for node in ast.allOf('op'):
children = node.children
instr = (node.value, node.reg) \
+ tuple([c.reg for c in children])
program.append(instr)
return program
def compileThreeAddrForm(program):
"""Given a three address form of the program, compile it a string that
the VM understands.
"""
def nToChr(reg):
if reg is None:
return '\xff'
elif reg.n < 0:
raise ValueError("negative value for register number %s" % (reg.n,))
else:
return chr(reg.n)
def quadrupleToString(opcode, store, a1=None, a2=None):
cop = chr(interpreter.opcodes[opcode])
cs = nToChr(store)
ca1 = nToChr(a1)
ca2 = nToChr(a2)
return cop + cs + ca1 + ca2
def toString(*args):
while len(args) < 4:
args += (None,)
opcode, store, a1, a2 = args[0:4]
s = quadrupleToString(opcode, store, a1, a2)
l = [s]
args = args[4:]
while args:
s = quadrupleToString('noop', *args[:3])
l.append(s)
args = args[3:]
return ''.join(l)
prog_str = ''.join([toString(*t) for t in program])
return prog_str
context_info = [
('optimization', ('none', 'moderate', 'aggressive'), 'aggressive'),
]
def getContext(map):
context = {}
for name, allowed, default in context_info:
value = map.pop(name, default)
if value in allowed:
context[name] = value
else:
raise ValueError("'%s' must be one of %s" % (name, allowed))
if map:
raise ValueError("Unknown keyword argument '%s'" % map.popitem()[0])
return context
def precompile(ex, signature=(), copy_args=(), **kwargs):
"""Compile the expression to an intermediate form.
"""
types = dict(signature)
input_order = [name for (name, type) in signature]
context = getContext(kwargs)
if isinstance(ex, str):
ex = stringToExpression(ex, types, context)
# the AST is like the expression, but the node objects don't have
# any odd interpretations
ast = expressionToAST(ex)
# Add a copy for strided or unaligned arrays
for a in ast.postorderWalk():
if a.astType == "variable" and a.value in copy_args:
newVar = ASTNode(*a.key())
a.astType, a.value, a.children = ('op', 'copy', (newVar,))
if ex.astType not in ('op'):
ast = ASTNode('op', value='copy', astKind=ex.astKind, children=(ast,))
ast = typeCompileAst(ast)
reg_num = [-1]
def registerMaker(node, temporary=False):
reg = Register(node, temporary=temporary)
reg.n = reg_num[0]
reg_num[0] -= 1
return reg
assignLeafRegisters(ast.allOf('raw'), Immediate)
assignLeafRegisters(ast.allOf('variable', 'constant'), registerMaker)
assignBranchRegisters(ast.allOf('op'), registerMaker)
collapseDuplicateSubtrees(ast)
input_order = getInputOrder(ast, input_order)
constants_order, constants = getConstants(ast)
if isReduction(ast):
ast.reg.temporary = False
optimizeTemporariesAllocation(ast)
ast.reg.temporary = False
r_output = 0
ast.reg.n = 0
r_inputs = r_output + 1
r_constants = setOrderedRegisterNumbers(input_order, r_inputs)
r_temps = setOrderedRegisterNumbers(constants_order, r_constants)
r_end, tempsig = setRegisterNumbersForTemporaries(ast, r_temps)
threeAddrProgram = convertASTtoThreeAddrForm(ast)
input_names = tuple([a.value for a in input_order])
signature = ''.join(types.get(x, float).__name__[0] for x in input_names)
return threeAddrProgram, signature, tempsig, constants, input_names
def numexpr(ex, signature=(), copy_args=(), **kwargs):
"""Compile an expression built using E.<variable> variables to a function.
ex can also be specified as a string "2*a+3*b".
The order of the input variables and their types can be specified using the
signature parameter, which is a list of (name, type) pairs.
"""
threeAddrProgram, inputsig, tempsig, constants, input_names = \
precompile(ex, signature, copy_args, **kwargs)
program = compileThreeAddrForm(threeAddrProgram)
return interpreter.NumExpr(inputsig, tempsig, program, constants,
input_names)
def disassemble(nex):
"""Given a NumExpr object, return a list which is the program
disassembled.
"""
rev_opcodes = {}
for op in interpreter.opcodes:
rev_opcodes[interpreter.opcodes[op]] = op
r_constants = 1 + len(nex.signature)
r_temps = r_constants + len(nex.constants)
def getArg(pc, offset):
arg = ord(nex.program[pc+offset])
op = rev_opcodes.get(ord(nex.program[pc]))
code = op.split('_')[1][offset-1]
if arg == 255:
return None
if code != 'n':
if arg == 0:
return 'r0'
elif arg < r_constants:
return 'r%d[%s]' % (arg, nex.input_names[arg-1])
elif arg < r_temps:
return 'c%d[%s]' % (arg, nex.constants[arg - r_constants])
else:
return 't%d' % (arg,)
else:
return arg
source = []
for pc in range(0, len(nex.program), 4):
op = rev_opcodes.get(ord(nex.program[pc]))
dest = getArg(pc, 1)
arg1 = getArg(pc, 2)
arg2 = getArg(pc, 3)
source.append( (op, dest, arg1, arg2) )
return source
def getType(a):
t = a.dtype.type
if issubclass(t, numpy.bool_):
return bool
if issubclass(t, numpy.integer):
return int
if issubclass(t, numpy.floating):
return float
if issubclass(t, numpy.complexfloating):
return complex
raise ValueError("unkown type %s" % a.dtype.name)
def getExprNames(text, context):
ex = stringToExpression(text, {}, context)
ast = expressionToAST(ex)
input_order = getInputOrder(ast, None)
return [a.value for a in input_order]
_names_cache = {}
_numexpr_cache = {}
def evaluate(ex, local_dict=None, global_dict=None, **kwargs):
"""Evaluate a simple array expression elementwise.
ex is a string forming an expression, like "2*a+3*b". The values for "a"
and "b" will by default be taken from the calling function's frame
(through use of sys._getframe()). Alternatively, they can be specifed
using the 'local_dict' or 'global_dict' arguments.
Not all operations are supported, and only real
constants and arrays of floats currently work.
"""
if not isinstance(ex, str):
raise ValueError("must specify expression as a string")
# Get the names for this expression
expr_key = (ex, tuple(sorted(kwargs.items())))
if expr_key not in _names_cache:
context = getContext(kwargs)
_names_cache[expr_key] = getExprNames(ex, context)
names = _names_cache[expr_key]
# Get the arguments based on the names.
call_frame = sys._getframe(1)
if local_dict is None:
local_dict = call_frame.f_locals
if global_dict is None:
global_dict = call_frame.f_globals
arguments = []
copy_args = []
for name in names:
try:
a = local_dict[name]
except KeyError:
a = global_dict[name]
# byteswapped arrays are taken care of in the extension.
arguments.append(numpy.asarray(a)) # don't make a data copy, if possible
if (hasattr(a, "flags") and # numpy object
(not a.flags.contiguous or
not a.flags.aligned)):
copy_args.append(name) # do a copy to temporary
# Create a signature
signature = [(name, getType(arg)) for (name, arg) in zip(names, arguments)]
# Look up numexpr if possible. copy_args *must* be added to the key,
# just in case a non-copy expression is already in cache.
numexpr_key = expr_key + (tuple(signature),) + tuple(copy_args)
try:
compiled_ex = _numexpr_cache[numexpr_key]
except KeyError:
compiled_ex = _numexpr_cache[numexpr_key] = \
numexpr(ex, signature, copy_args, **kwargs)
return compiled_ex(*arguments)
|