[pypy-svn] r76252 - in pypy/branch/micronumpy/pypy: module/micronumpy tool

dan at codespeak.net dan at codespeak.net
Fri Jul 16 12:32:27 CEST 2010


Author: dan
Date: Fri Jul 16 12:32:24 2010
New Revision: 76252

Modified:
   pypy/branch/micronumpy/pypy/module/micronumpy/dtype.py
   pypy/branch/micronumpy/pypy/module/micronumpy/microarray.py
   pypy/branch/micronumpy/pypy/tool/numpybench.py
Log:
Worst. Merge. Ever.

Modified: pypy/branch/micronumpy/pypy/module/micronumpy/dtype.py
==============================================================================
--- pypy/branch/micronumpy/pypy/module/micronumpy/dtype.py	(original)
+++ pypy/branch/micronumpy/pypy/module/micronumpy/dtype.py	Fri Jul 16 12:32:24 2010
@@ -41,7 +41,7 @@
                            )
 
 storage_type = lltype.Ptr(lltype.Array(lltype.Char))                           
-null_storage = lltype.nullptr(storage_type.TO)
+null_data = lltype.nullptr(storage_type.TO)
 
 class DescrBase(object): pass
 
@@ -62,7 +62,7 @@
             return space.wrap(self.getitem(data, index))
 
         def w_setitem(self, space, data, index, w_value):
-            value = self.unwrap(space, w_value)
+            value = self.coerce_w(space, w_value)
             self.setitem(data, index, value)
 
         def itemsize(self):
@@ -123,12 +123,14 @@
 
 int_descr = descriptor('i', 'int32', lltype.Signed)
 type(int_descr).unwrap = lambda self, space, value: space.int_w(value)
+type(int_descr).coerce_w = lambda self, space, value: space.int_w(space.int(value))
 _int_index = _typeindex['i']
 _typestring['int32'] = _int_index
 w_int_descr = _w_descriptors[_int_index]
 
 float_descr = descriptor('d', 'float64', lltype.Float)
 type(float_descr).unwrap = lambda self, space, value: space.float_w(value)
+type(float_descr).coerce_w = lambda self, space, value: space.float_w(space.float(value))
 _float_index = _typeindex['d']
 _typestring['float64'] = _float_index
 w_float_descr = _w_descriptors[_float_index]

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	Fri Jul 16 12:32:24 2010
@@ -12,7 +12,7 @@
 from pypy.module import micronumpy
 from pypy.module.micronumpy.array import BaseNumArray
 from pypy.module.micronumpy.array import construct_array, infer_shape
-from pypy.module.micronumpy.dtype import null_storage
+from pypy.module.micronumpy.dtype import null_data
 
 def size_from_shape(shape):
     size = 1
@@ -84,7 +84,7 @@
         elif parent is not None:
             self.data = parent.data
         else:
-            self.data = null_storage
+            self.data = null_data
 
     def descr_len(self, space):
         return space.wrap(self.shape[0])
@@ -209,7 +209,7 @@
     descr_iter.unwrap_spec = ['self', ObjSpace]
 
     def __del__(self):
-        if self.parent is None and self.data is not None:
+        if self.parent is None and self.data != null_data:
             dtype = self.dtype.dtype
             dtype.free(self.data)
 

Modified: pypy/branch/micronumpy/pypy/tool/numpybench.py
==============================================================================
--- pypy/branch/micronumpy/pypy/tool/numpybench.py	(original)
+++ pypy/branch/micronumpy/pypy/tool/numpybench.py	Fri Jul 16 12:32:24 2010
@@ -1,54 +1,33 @@
-from __future__ import division
 try:
-    import numpy as np
+    import numpy as numpy
 except ImportError, e:
-    import micronumpy as np
+    import micronumpy as numpy
 
-def naive_convolve(f, g):
-    # f is an image and is indexed by (v, w)
-    # g is a filter kernel and is indexed by (s, t),
-    #   it needs odd dimensions
-    # h is the output image and is indexed by (x, y),
-    #   it is not cropped
-    if g.shape[0] % 2 != 1 or g.shape[1] % 2 != 1:
-        raise ValueError("Only odd dimensions on filter supported")
-    # smid and tmid are number of pixels between the center pixel
-    # and the edge, ie for a 5x5 filter they will be 2.
-    #
-    # The output size is calculated by adding smid, tmid to each
-    # side of the dimensions of the input image.
-    vmax = f.shape[0]
-    wmax = f.shape[1]
-    smax = g.shape[0]
-    tmax = g.shape[1]
-    smid = smax // 2
-    tmid = tmax // 2
-    xmax = vmax + 2*smid
-    ymax = wmax + 2*tmid
-    # Allocate result image.
-    h = np.zeros([xmax, ymax], dtype=f.dtype)
-    # Do convolution
-    for x in range(xmax):
-        for y in range(ymax):
-            print "(%d, %d)" % (x, y)
-            # Calculate pixel value for h at (x,y). Sum one component
-            # for each pixel (s, t) of the filter g.
-            s_from = max(smid - x, -smid)
-            s_to = min((xmax - x) - smid, smid + 1)
-            t_from = max(tmid - y, -tmid)
-            t_to = min((ymax - y) - tmid, tmid + 1)
-            value = 0
-            for s in range(s_from, s_to):
-                for t in range(t_from, t_to):
-                    v = x - smid + s
-                    w = y - tmid + t
-                    value += g[smid - s, tmid - t] * f[v, w]
-            h[x, y] = value
-    return h
+def generate_image(width, height):
+    return numpy.array([[x + y for x in range(width)] for y in range(height)])
+
+def generate_kernel(width, height):
+    from math import sin, pi
+    #kernel = numpy.zeros((width, height), dtype=int) # FIXME: micronumpy.zeros can't handle missing dtype
+    kernel = [[0] * width] * height
+
+    for i in range(width):
+        for j in range(height):
+            u = i / float(width)
+            v = j / float(height)
+            kernel[j][i] = int((0.5 + sin(u * pi)) * (0.5 + sin(v * pi))) # DOUBLE FIXME: setitem doesn't coerce to array type
+        
+    #return kernel
+    return numpy.array(kernel)
 
 if __name__ == '__main__':
-    image = np.array([[x + y for x in range(200)] for y in range(200)])
-    kernel = np.array([[0, 1, 0],
-                       [1, 1, 1],
-                       [0, 1, 0]])
-    result = naive_convolve(image, kernel)
+    from sys import argv as args
+    width, height, kwidth, kheight = [int(x) for x in args[1:]]
+
+    image = generate_image(width, height)
+    kernel = generate_kernel(kwidth, kheight)
+
+    from timeit import Timer
+    convolve_timer = Timer('naive_convolve(image, kernel)', 'from convolve import naive_convolve; from __main__ import image, kernel; gc.enable()')
+    count = 100
+    print "%.5f sec/pass" % (convolve_timer.timeit(number=count)/count)



More information about the Pypy-commit mailing list