[pypy-svn] r76831 - in pypy/branch/micronumpy/pypy: jit/tl module/micronumpy module/micronumpy/test

dan at codespeak.net dan at codespeak.net
Thu Sep 2 10:06:59 CEST 2010


Author: dan
Date: Thu Sep  2 10:06:58 2010
New Revision: 76831

Modified:
   pypy/branch/micronumpy/pypy/jit/tl/pypyjit.py
   pypy/branch/micronumpy/pypy/jit/tl/pypyjit_demo.py
   pypy/branch/micronumpy/pypy/module/micronumpy/array.py
   pypy/branch/micronumpy/pypy/module/micronumpy/microarray.py
   pypy/branch/micronumpy/pypy/module/micronumpy/test/test_numpy.py
Log:
More compliance, broke test_subarray though, fixing soon.

Modified: pypy/branch/micronumpy/pypy/jit/tl/pypyjit.py
==============================================================================
--- pypy/branch/micronumpy/pypy/jit/tl/pypyjit.py	(original)
+++ pypy/branch/micronumpy/pypy/jit/tl/pypyjit.py	Thu Sep  2 10:06:58 2010
@@ -37,7 +37,8 @@
 set_opt_level(config, level='jit')
 config.objspace.allworkingmodules = False
 config.objspace.usemodules.pypyjit = True
-config.objspace.usemodules.array = True
+config.objspace.usemodules.array = False
+config.objspace.usemodules.micronumpy = True
 config.objspace.usemodules._weakref = False
 config.objspace.usemodules._sre = False
 set_pypy_opt_level(config, level='jit')

Modified: pypy/branch/micronumpy/pypy/jit/tl/pypyjit_demo.py
==============================================================================
--- pypy/branch/micronumpy/pypy/jit/tl/pypyjit_demo.py	(original)
+++ pypy/branch/micronumpy/pypy/jit/tl/pypyjit_demo.py	Thu Sep  2 10:06:58 2010
@@ -1,64 +1,19 @@
-## base = object
-
-## class Number(base):
-##     __slots__ = ('val', )
-##     def __init__(self, val=0):
-##         self.val = val
-
-##     def __add__(self, other):
-##         if not isinstance(other, int):
-##             other = other.val
-##         return Number(val=self.val + other)
-            
-##     def __cmp__(self, other):
-##         val = self.val
-##         if not isinstance(other, int):
-##             other = other.val
-##         return cmp(val, other)
-
-##     def __nonzero__(self):
-##         return bool(self.val)
-
-## def g(x, inc=2):
-##     return x + inc
-
-## def f(n, x, inc):
-##     while x < n:
-##         x = g(x, inc=1)
-##     return x
-
-## import time
-## #t1 = time.time()
-## #f(10000000, Number(), 1)
-## #t2 = time.time()
-## #print t2 - t1
-## t1 = time.time()
-## f(10000000, 0, 1)
-## t2 = time.time()
-## print t2 - t1
-
+print "Starting execution..."
 try:
     from micronumpy import zeros
+    import micronumpy as numpy
+    import micronumpy
 
-    size = 128
-    dtype = float
+    from convolve import naive_convolve
+    from numpybench import generate_kernel, generate_image
 
-    for run in range(200):
-        ar = zeros((size,), dtype=dtype)
+    image = generate_image(128, 128)
+    kernel = generate_kernel(5, 5)
+
+    for i in range(100):
+        result = naive_convolve(image, kernel)
 
-        for i in range(size):
-            ar[i] = dtype(i) * 5
-            print i
 except Exception, e:
     print "Exception: ", type(e)
     print e
-    
-## def f():
-##     a=7
-##     i=0
-##     while i<4:
-##         if  i<0: break
-##         if  i<0: break
-##         i+=1
-
-## f()
+print "Stopping execution..."

Modified: pypy/branch/micronumpy/pypy/module/micronumpy/array.py
==============================================================================
--- pypy/branch/micronumpy/pypy/module/micronumpy/array.py	(original)
+++ pypy/branch/micronumpy/pypy/module/micronumpy/array.py	Thu Sep  2 10:06:58 2010
@@ -4,6 +4,8 @@
 from pypy.interpreter.gateway import NoneNotWrapped
 from pypy.interpreter.gateway import interp2app
 
+SQUEEZE_ME = -1
+
 def stride_row(shape, i):
     assert i >= 0
     stride = 1
@@ -28,33 +30,33 @@
 
 def normalize_slice_starts(slice_starts, shape):
     for i in range(len(slice_starts)):
+        #print "slice_start[%d]=%d" % (i, slice_starts[i])
         if slice_starts[i] < 0:
             slice_starts[i] += shape[i]
         elif slice_starts[i] >= shape[i]:
+            print "raising"
             raise IndexError("invalid index")
     return slice_starts
 
 def squeeze_shape(shape):
     "Simple squeeze."
+    #return [x for x in shape if x != SQUEEZE_ME]
     return [x for x in shape if x != 1]
 
-def squeeze_slice(current_shape, starts, shape, step):
-    current_shape = current_shape[:]
+def squeeze(starts, shape, step, strides):
+    offset = 0
     i = 0
     stop = len(shape)
     while i < stop:
-        if shape[i] == 1:
+        if shape[i] == SQUEEZE_ME:
             if i == 0:
-                offset += starts[i] # FIXME: eh?
-                offset *= current_shape[i]
-            else:
-                step[i-1] += starts[i] # FIXME: eh?
-                step[i-1] *= current_shape[i]
+                offset += starts[i]
 
-            del current_shape[i]
-            del starts[i] # XXX: I think this needs to be incorporated...
+            del starts[i]
             del shape[i]
             del step[i]
+            del strides[i]
+            stop -= 1
         else:
             i += 1
     return offset
@@ -64,7 +66,7 @@
     try:
         while shape[prefix] == 1: prefix += 1
     except IndexError, e:
-        prefix = len(shape) # XXX - 1?
+        prefix = len(shape)
 
     return prefix
 

Modified: pypy/branch/micronumpy/pypy/module/micronumpy/microarray.py
==============================================================================
--- pypy/branch/micronumpy/pypy/module/micronumpy/microarray.py	(original)
+++ pypy/branch/micronumpy/pypy/module/micronumpy/microarray.py	Thu Sep  2 10:06:58 2010
@@ -15,20 +15,36 @@
 from pypy.module.micronumpy.array import infer_shape
 from pypy.module.micronumpy.array import stride_row, stride_column, size_from_shape
 from pypy.module.micronumpy.array import normalize_slice_starts
-from pypy.module.micronumpy.array import squeeze_slice, squeeze_shape
+from pypy.module.micronumpy.array import squeeze_shape
+from pypy.module.micronumpy.array import squeeze, SQUEEZE_ME
 from pypy.module.micronumpy.array import shape_prefix
 
 from pypy.rpython.lltypesystem.lltype import cast_ptr_to_int
 
+class FlatIter(Wrappable):
+    _immutable_fields_ = ['array', 'stop']
+    def __init__(self, array):
+        self.array = array
+        self.i = 0
+        self.stop = size_from_shape(array.shape)
+
+    def descr_iter(self, space):
+        return space.wrap(self)
+    descr_iter.unwrap_spec = ['self', ObjSpace]
+
+    def descr_next(self, space):
+        return self.array.getitem(space, self.i) # FIXME
+    descr_iter.unwrap_spec = ['self', ObjSpace]
+
 class MicroIter(Wrappable):
-    _immutable_fields_ = ['step', 'stop', 'ndim'] # XXX: removed array
+    _immutable_fields_ = ['array', 'step', 'stop', 'ndim']
     def __init__(self, array):
         self.array = array
         self.i = 0
         self.index = array.slice_starts[:]
-        self.step = array.slice_steps[array.offset]
-        self.stop = array.shape[array.offset]
-        self.ndim = len(array.shape[array.offset:])
+        self.step = array.slice_steps[array.prefix]
+        self.stop = array.shape[array.prefix]
+        self.ndim = len(array.shape) - array.prefix
 
     def descr_iter(self, space):
         return space.wrap(self)
@@ -36,25 +52,24 @@
     
     def descr_next(self, space):
         if self.i < self.stop:
+            print self.index
             if self.ndim > 1:
                 ar = MicroArray(self.array.shape,
                                 self.array.dtype,
                                 parent=self.array,
-                                offset=self.array.offset + 1,
+                                offset=self.array.prefix + 1,
                                 strides=self.array.strides,
                                 slice_starts=self.index,
                                 slice_steps=self.array.slice_steps)
-                self.i += 1
-                self.index[self.array.offset] += self.step
-                return space.wrap(ar)
+                next = space.wrap(ar)
             elif self.ndim == 1:
-                next = self.array.getitem(space, self.array.flatten_index(self.index))
-                self.i += 1
-                self.index[self.array.offset] += self.step
-                return next
+                next = self.array.getitem(space, self.array.flatten_slice_starts(self.index))
             else:
                 raise OperationError(space.w_ValueError,
                        space.wrap("Something is horribly wrong with this array's shape. Has %d dimensions." % len(self.array.shape)))
+            self.i += 1
+            self.index[self.array.prefix] += self.step
+            return next
         else:
             raise OperationError(space.w_StopIteration, space.wrap(""))
     descr_next.unwrap_spec = ['self', ObjSpace]
@@ -64,9 +79,13 @@
                             next = interp2app(MicroIter.descr_next),
                            )
 
+
 class MicroArray(BaseNumArray):
     _immutable_fields_ = ['shape', 'strides', 'offset', 'slice_starts'] # XXX: removed parent
-    def __init__(self, shape, dtype, order='C', strides=[], parent=None, offset=0, slice_starts=[], slice_steps=[]):
+    def __init__(self, shape, dtype,
+                 order='C', strides=[], parent=None,
+                 prefix=0, offset=0,
+                 slice_starts=[], slice_steps=[]):
         assert dtype is not None
 
         self.shape = shape
@@ -74,21 +93,21 @@
         self.parent = parent
         self.order = order
         self.offset = offset
+        self.prefix = prefix
 
         self.slice_starts = slice_starts[:]
-        for i in range(len(shape) - len(slice_starts)):
+        for i in range(len(slice_starts), len(shape)):
             self.slice_starts.append(0)
 
         self.slice_steps = slice_steps[:]
-        for i in range(len(shape) - len(slice_steps)):
+        for i in range(len(slice_steps), len(shape)):
             self.slice_steps.append(1)
 
         size = size_from_shape(shape)
 
         self.strides = strides[:]
-        stridelen = len(self.strides)
-        for i in range(len(self.shape) - stridelen):
-            self.strides.append(self.stride(stridelen + i))
+        for i in range(len(self.strides), len(shape)):
+            self.strides.append(self.stride(i))
 
         if size > 0 and parent is None:
             self.data = dtype.dtype.alloc(size)
@@ -98,7 +117,7 @@
             self.data = null_data
 
     def descr_len(self, space):
-        return space.wrap(self.shape[self.offset])
+        return space.wrap(self.shape[self.prefix])
     descr_len.unwrap_spec = ['self', ObjSpace]
 
     def getitem(self, space, offset):
@@ -110,9 +129,11 @@
             raise OperationError(space.w_IndexError,
                                  space.wrap("index out of bounds"))
 
-    def setitem(self, space, index, w_value):
+    def setitem(self, space, offset, w_value):
+        """Helper function.
+           Sets a value at an offset in the data."""
         try:
-            self.dtype.dtype.w_setitem(space, self.data, index, w_value)
+            self.dtype.dtype.w_setitem(space, self.data, offset, w_value)
         except IndexError, e:
             raise OperationError(space.w_IndexError,
                                  space.wrap("index out of bounds"))
@@ -122,10 +143,20 @@
            Gives offset into subarray, not into data."""
         offset = 0
         for i in range(len(slice_starts)):
+            offset += slice_starts[i] * self.strides[i]
+        #print offset
+        return offset
+
+    def flatten_slice_starts2(self, slice_starts):
+        """Computes offset into subarray from all information.
+           Gives offset into subarray, not into data."""
+        offset = 0
+        for i in range(len(slice_starts)):
             offset += (self.slice_steps[i] * slice_starts[i]) * self.strides[i]
+        print offset
         return offset
 
-    flatten_index = flatten_slice_starts # TODO: migrate to slice_starts for name?
+    flatten_index = flatten_slice_starts2 # TODO: migrate to slice_starts for name?
 
     def stride(self, i):
         if self.order == 'C':
@@ -146,10 +177,13 @@
             index = space.int_w(space.index(w_index))
             # Normalize 
             if index < 0:
-                index += self.shape[self.offset]
+                index += self.shape[self.prefix]
+            elif index > self.shape[self.prefix]:
+                raise OperationError(space.w_IndexError,
+                                     space.wrap("invalid index")) # FIXME: message
 
-            slice_starts[self.offset] += index * self.slice_steps[self.offset]
-            shape[self.offset] = 1
+            slice_starts[self.prefix] += index * self.slice_steps[self.prefix]
+            shape[self.prefix] = 1 #SQUEEZE_ME
             return slice_starts, shape, slice_steps
         except OperationError, e:
             if e.match(space, space.w_TypeError): pass
@@ -157,9 +191,9 @@
 
         if isinstance(w_index, W_SliceObject):
             start, stop, step, length = w_index.indices4(space, self.shape[0])
-            slice_starts[self.offset] += start * slice_steps[self.offset]
-            shape[self.offset] = length
-            slice_steps[self.offset] *= step
+            slice_starts[self.prefix] += start * slice_steps[self.prefix]
+            shape[self.prefix] = length
+            slice_steps[self.prefix] *= step
             return slice_starts, shape, slice_steps
         elif space.is_w(w_index, space.w_Ellipsis):
             return slice_starts, shape, slice_steps
@@ -168,13 +202,16 @@
             indices = space.fixedview(w_index)
 
             indexlen = len(indices)
+            if indexlen != len(self.shape): # FIXME: shape will often be larger...
+                raise OperationError(space.w_IndexError,
+                                     space.wrap("invalid index")) # FIXME: message
 
             for i in range(indexlen):
                 w_index = indices[i]
                 try:
                     index = space.int_w(space.index(w_index))
-                    slice_starts[self.offset + i] += index
-                    shape[self.offset + i] = 1
+                    slice_starts[self.prefix + i] += index
+                    shape[self.prefix + i] = 1 #SQUEEZE_ME
                     continue
 
                 except OperationError, e:
@@ -183,9 +220,9 @@
 
                 if isinstance(w_index, W_SliceObject):
                     start, stop, step, length = w_index.indices4(space, self.shape[i])
-                    slice_starts[self.offset + i] += start * slice_steps[self.offset + i]
-                    shape[self.offset + i] = length
-                    slice_steps[self.offset + i] *= step
+                    slice_starts[self.prefix + i] += start * slice_steps[self.prefix + i]
+                    shape[self.prefix + i] = length
+                    slice_steps[self.prefix + i] *= step
                 elif space.is_w(w_index, space.w_Ellipsis):
                     pass # I can't think of anything we need to do
                 else:
@@ -212,13 +249,14 @@
 
         if size == 1:
             return self.getitem(space,
-                                self.flatten_index(slice_starts))
+                                self.flatten_slice_starts(slice_starts))
         else:
             prefix = shape_prefix(shape)
             ar = MicroArray(shape,
                             dtype=self.dtype,
                             parent=self,
                             offset=prefix, # XXX: what do we do about shapes that needs to be squeezed out?
+                            strides=self.strides[:], 
                             slice_starts=slice_starts,
                             slice_steps=slice_steps)
             return space.wrap(ar)
@@ -372,7 +410,7 @@
     return space.wrap(self.dtype)
 
 def descr_get_shape(space, self):
-    return space.newtuple([space.wrap(x) for x in self.shape[self.offset:]])
+    return space.newtuple([space.wrap(x) for x in self.shape[self.prefix:]])
 
 def descr_get_array_interface(space, self):
     w_dict = space.newdict()

Modified: pypy/branch/micronumpy/pypy/module/micronumpy/test/test_numpy.py
==============================================================================
--- pypy/branch/micronumpy/pypy/module/micronumpy/test/test_numpy.py	(original)
+++ pypy/branch/micronumpy/pypy/module/micronumpy/test/test_numpy.py	Thu Sep  2 10:06:58 2010
@@ -463,6 +463,16 @@
         assert shape[0] == 2
         assert step[0] == 1
 
+    def test_squeeze(self, space):
+        from pypy.module.micronumpy.array import squeeze
+        from pypy.module.micronumpy.microarray import SQUEEZE_ME
+        slice_starts = [1, 2, 3]
+        shape = [1, SQUEEZE_ME, 3]
+        slice_steps = [1, 1, 1]
+        strides = [1, 2, 3]
+
+        offset = squeeze(slice_starts, shape, slice_steps, strides)
+
     def test_slice_setting(self, space):
         from pypy.module.micronumpy.array import size_from_shape
         from pypy.module.micronumpy.microarray import MicroArray



More information about the Pypy-commit mailing list