Author: Maciej Fijalkowski <[email protected]>
Branch:
Changeset: r44877:bb28deeb33f5
Date: 2011-06-10 22:19 +0200
http://bitbucket.org/pypy/pypy/changeset/bb28deeb33f5/
Log: Add tests for the numpy target and fix it
diff --git a/pypy/module/micronumpy/compile.py
b/pypy/module/micronumpy/compile.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/micronumpy/compile.py
@@ -0,0 +1,49 @@
+
+""" This is a set of tools for standalone compiling of numpy expressions.
+It should not be imported by the module itself
+"""
+
+from pypy.module.micronumpy.interp_numarray import FloatWrapper, SingleDimArray
+
+class BogusBytecode(Exception):
+ pass
+
+def create_array(size):
+ a = SingleDimArray(size)
+ for i in range(size):
+ a.storage[i] = float(i % 10)
+ return a
+
+class TrivialSpace(object):
+ def wrap(self, x):
+ return x
+
+def numpy_compile(bytecode, array_size):
+ space = TrivialSpace()
+ stack = []
+ i = 0
+ for b in bytecode:
+ if b == 'a':
+ stack.append(create_array(array_size))
+ i += 1
+ elif b == 'f':
+ stack.append(FloatWrapper(1.2))
+ elif b == '+':
+ right = stack.pop()
+ stack.append(stack.pop().descr_add(space, right))
+ elif b == '-':
+ right = stack.pop()
+ stack.append(stack.pop().descr_sub(space, right))
+ elif b == '*':
+ right = stack.pop()
+ stack.append(stack.pop().descr_mul(space, right))
+ elif b == '/':
+ right = stack.pop()
+ stack.append(stack.pop().descr_div(space, right))
+ else:
+ print "Unknown opcode: %s" % b
+ raise BogusBytecode()
+ if len(stack) != 1:
+ print "Bogus bytecode, uneven stack length"
+ raise BogusBytecode()
+ return stack[0]
diff --git a/pypy/module/micronumpy/test/test_zjit.py
b/pypy/module/micronumpy/test/test_zjit.py
--- a/pypy/module/micronumpy/test/test_zjit.py
+++ b/pypy/module/micronumpy/test/test_zjit.py
@@ -1,8 +1,10 @@
from pypy.jit.metainterp.test.support import LLJitMixin
+from pypy.rpython.test.test_llinterp import interpret
+
from pypy.module.micronumpy.interp_numarray import (SingleDimArray, Signature,
FloatWrapper, Call1, Call2, add, mul)
from pypy.module.micronumpy.interp_ufuncs import negative
-
+from pypy.module.micronumpy.compile import numpy_compile
class FakeSpace(object):
pass
@@ -91,4 +93,20 @@
self.meta_interp(f, [5], listops=True, backendopt=True)
# This is 3, not 2 because there is a bridge for the exit.
- self.check_loop_count(3)
\ No newline at end of file
+ self.check_loop_count(3)
+
+class TestTranslation(object):
+ def test_compile(self):
+ x = numpy_compile('aa+f*f/a-', 10)
+ x = x.compute()
+ assert isinstance(x, SingleDimArray)
+ assert x.size == 10
+ assert x.storage[0] == 0
+ assert x.storage[1] == ((1 + 1) * 1.2) / 1.2 - 1
+
+ def test_translation(self):
+ # we import main to check if the target compiles
+ from pypy.translator.goal.targetnumpystandalone import main
+ from pypy.rpython.annlowlevel import llstr
+
+ interpret(main, [llstr('af+'), 100])
diff --git a/pypy/translator/goal/targetnumpystandalone.py
b/pypy/translator/goal/targetnumpystandalone.py
--- a/pypy/translator/goal/targetnumpystandalone.py
+++ b/pypy/translator/goal/targetnumpystandalone.py
@@ -10,46 +10,32 @@
"""
import time
-from pypy.module.micronumpy.numarray import SingleDimArray, Code, compute
+from pypy.module.micronumpy.compile import numpy_compile
from pypy.jit.codewriter.policy import JitPolicy
-
-def create_array(size):
- a = SingleDimArray(size)
- for i in range(size):
- a.storage[i] = float(i % 10)
- return a
+from pypy.rpython.annlowlevel import hlstr
def entry_point(argv):
if len(argv) != 3:
print __doc__
return 1
- bytecode = argv[1]
- for b in bytecode:
- if b not in 'alf':
- print "WRONG BYTECODE"
- print __doc__
- return 2
try:
size = int(argv[2])
except ValueError:
print "INVALID LITERAL FOR INT:", argv[2]
print __doc__
return 3
- no_arrays = bytecode.count('l')
- no_floats = bytecode.count('f')
- arrays = []
- floats = []
- for i in range(no_arrays):
- arrays.append(create_array(size))
- for i in range(no_floats):
- floats.append(float(i + 1))
- code = Code(bytecode, arrays, floats)
t0 = time.time()
- compute(code)
+ main(argv[0], size)
print "bytecode:", bytecode, "size:", size
print "took:", time.time() - t0
return 0
+def main(bc, size):
+ if not isinstance(bc, str):
+ bc = hlstr(bc) # for tests
+ a = numpy_compile(bc, size)
+ a = a.compute()
+
def target(*args):
return entry_point, None
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit