[pypy-svn] pypy numpy-exp: Progress on interpretation

fijal commits-noreply at bitbucket.org
Wed Feb 9 20:07:16 CET 2011


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: numpy-exp
Changeset: r41761:69e26bb0970e
Date: 2011-02-09 20:45 +0200
http://bitbucket.org/pypy/pypy/changeset/69e26bb0970e/

Log:	Progress on interpretation

diff --git a/pypy/module/micronumpy/numarray.py b/pypy/module/micronumpy/numarray.py
--- a/pypy/module/micronumpy/numarray.py
+++ b/pypy/module/micronumpy/numarray.py
@@ -7,19 +7,72 @@
 
 TP = lltype.GcArray(lltype.Float)
 
-class BaseBytecode(Wrappable):
-    pass
+def compute(bytecode, input):
+    result_size = input[0].size
+    result = SingleDimArray(result_size)
+    bytecode_pos = len(bytecode) - 1
+    input_pos = len(input) - 1
+    valuestack = [0.0] * len(input)
+    valuestackdepth = 0
+    i = 0
+    while i < result_size:
+        # merge point
+        if bytecode_pos == -1:
+            bytecode_pos = len(bytecode) - 1
+            input_pos = len(input) - 1
+            result.storage[i] = valuestack[0]
+            valuestack = [0.0] * len(input)
+            valuestackdepth = 0
+            i += 1
+            # can_enter_jit
+        else:
+            opcode = bytecode[bytecode_pos]
+            if opcode == 'l':
+                valuestack[valuestackdepth] = input[input_pos].storage[i]
+                valuestackdepth += 1
+                input_pos -= 1
+            elif opcode == 'a':
+                a = valuestack[valuestackdepth - 1]
+                b = valuestack[valuestackdepth - 2]
+                valuestack[valuestackdepth - 2] = a + b
+                valuestackdepth -= 1
+            else:
+                raise NotImplementedError
+            bytecode_pos -= 1
+    return result
+    
+class BaseArray(Wrappable):
+    def force(self):
+        bytecode, stack = self.compile()
+        return compute(bytecode, stack)
+    force.unwrap_spec = ['self']
 
-class Add(BaseBytecode):
+    def compile(self):
+        raise NotImplementedError("abstract base class")
+
+class Add(BaseArray):
     def __init__(self, left, right):
         self.left = left
         self.right = right
 
-class SingleDimArray(Wrappable):
+    def compile(self):
+        left_bc, left_stack = self.left.compile()
+        right_bc, right_stack = self.right.compile()
+        return 'a' + left_bc + right_bc, left_stack + right_stack
+
+BaseArray.typedef = TypeDef(
+    'Operation',
+    force=interp2app(BaseArray.force),
+)
+
+class SingleDimArray(BaseArray):
     def __init__(self, size):
         self.size = size
         self.storage = lltype.malloc(TP, size, zero=True)
 
+    def compile(self):
+        return "l", [self]
+
     def descr_getitem(self, space, item):
         if item < 0:
             raise operationerrfmt(space.w_TypeError,
@@ -44,6 +97,9 @@
         return space.wrap(Add(self, w_other))
     descr_add.unwrap_spec = ['self', ObjSpace, W_Root]
 
+    def force(self):
+        return self
+
 def descr_new_numarray(space, w_type, w_size_or_iterable):
     if space.isinstance_w(w_size_or_iterable, space.w_int):
         arr = SingleDimArray(space.int_w(w_size_or_iterable))
@@ -63,5 +119,6 @@
     __getitem__ = interp2app(SingleDimArray.descr_getitem),
     __setitem__ = interp2app(SingleDimArray.descr_setitem),
     __add__ = interp2app(SingleDimArray.descr_add),
+    force = interp2app(SingleDimArray.force),
 )
 

diff --git a/pypy/module/micronumpy/test/test_numpy.py b/pypy/module/micronumpy/test/test_numpy.py
--- a/pypy/module/micronumpy/test/test_numpy.py
+++ b/pypy/module/micronumpy/test/test_numpy.py
@@ -18,6 +18,13 @@
         a = array(range(5))
         assert a[3] == 3
 
+    def test_add(self):
+        from numpy import array
+        a = array(range(5))
+        b = a + a
+        b = b.force()
+        assert b[2] == 2 + 2
+
 class AppTestNumpy(object):
     def setup_class(cls):
         py.test.skip("skip")


More information about the Pypy-commit mailing list