[pypy-svn] r46313 - in pypy/dist/pypy/rpython/numpy: . test

simonb at codespeak.net simonb at codespeak.net
Wed Sep 5 02:48:05 CEST 2007


Author: simonb
Date: Wed Sep  5 02:48:03 2007
New Revision: 46313

Modified:
   pypy/dist/pypy/rpython/numpy/aarray.py
   pypy/dist/pypy/rpython/numpy/rarray.py
   pypy/dist/pypy/rpython/numpy/test/test_array.py
Log:
views are working well now

Modified: pypy/dist/pypy/rpython/numpy/aarray.py
==============================================================================
--- pypy/dist/pypy/rpython/numpy/aarray.py	(original)
+++ pypy/dist/pypy/rpython/numpy/aarray.py	Wed Sep  5 02:48:03 2007
@@ -69,6 +69,7 @@
             raise AnnotatorError()
         return SomeArray(typecode, s_arr1.ndim)
 
+    # union ?
     sub = mul = div = add
 
 
@@ -172,7 +173,7 @@
     def specialize_call(self, hop, i_dtype=None):
         r_array = hop.r_result
         v_lst = hop.inputarg(r_array, 0) # coerce list arg to array arg
-        v_result = r_array.build_from_array(hop.llops, v_lst)
+        v_result = r_array.build_from_array(hop, v_lst)
         return v_result
 
 
@@ -180,15 +181,17 @@
     "Annotation and rtyping of calls to numpy.empty"
     _about_ = numpy.empty
 
-    def compute_result_annotation(self, s_tuple, s_dtype=None):
-        if isinstance(s_tuple, SomeTuple):
+    def compute_result_annotation(self, s_arg, s_dtype=None):
+        if isinstance(s_arg, SomeTuple):
+            s_tuple = s_arg
             for s_item in s_tuple.items:
                 if not isinstance(s_item, SomeInteger):
-                    raise AnnotatorError("shape must be tuple of integers")
+                    raise AnnotatorError("shape arg not understood")
             ndim = len(s_tuple.items)
+        elif isinstance(s_arg, SomeInteger):
+            ndim = 1
         else:
-            # XX also build from single int arg
-            raise AnnotatorError("could not build array shape from %s"%s_list)
+            raise AnnotatorError("shape arg not understood")
 
         typecode = 'd'
         if isinstance(s_dtype, SomeChar) and s_dtype.is_constant():
@@ -196,25 +199,25 @@
             s_dtype = None
         return SomeArray(typecode, ndim)
 
-#    def specialize_call(self, hop):
-#        ldef = listdef.ListDef(None, SomeInteger())
-#        r_lst = hop.rtyper.getrepr(SomeList(ldef))
-#        # XX TyperError: don't know how to convert from 
-#        # <TupleRepr * GcStruct tuple2 { item0, item1 }> to 
-#        # <FixedSizeListRepr * GcForwardReference>
-#        [v_lst] = hop.inputargs(r_lst)
-#        r_array = hop.r_result
-#        v_result = r_array.build_from_shape(hop.llops, r_lst, v_lst)
-#        return v_result
-
     def specialize_call(self, hop, i_dtype=None):
-        r_tpl = hop.args_r[0]
+        r_arg = hop.args_r[0]
         # XX also call with single int arg
-        v_tpl = hop.inputarg(r_tpl, 0)
+        v_arg = hop.inputarg(r_arg, 0)
         r_array = hop.r_result
-        v_result = r_array.build_from_shape(hop.llops, r_tpl, v_tpl)
+        v_result = r_array.build_from_shape(hop, r_arg, v_arg)
         return v_result
 
+class ZerosCallEntry(EmptyCallEntry):
+    "Annotation and rtyping of calls to numpy.zeros"
+    _about_ = numpy.zeros
+
+    def specialize_call(self, hop, i_dtype=None):
+        r_arg = hop.args_r[0]
+        # XX also call with single int arg
+        v_arg = hop.inputarg(r_arg, 0)
+        r_array = hop.r_result
+        v_result = r_array.build_from_shape(hop, r_arg, v_arg, zero=True)
+        return v_result
 
 
 # Importing for side effect of registering types with extregistry

Modified: pypy/dist/pypy/rpython/numpy/rarray.py
==============================================================================
--- pypy/dist/pypy/rpython/numpy/rarray.py	(original)
+++ pypy/dist/pypy/rpython/numpy/rarray.py	Wed Sep  5 02:48:03 2007
@@ -18,7 +18,7 @@
 from pypy.annotation import listdef
 from pypy.rpython.memory.lltypelayout import sizeof
 
-def gen_build_from_shape(ndim):
+def gen_build_from_shape(ndim, zero=False):
     unrolling_dims = unrolling_iterable(reversed(range(ndim)))
     def ll_build_from_shape(ARRAY, shape):
         array = ll_allocate(ARRAY, ndim)
@@ -29,7 +29,7 @@
             array.shape[i] = size
             array.strides[i] = itemsize
             itemsize *= size
-        array.data = malloc(ARRAY.data.TO, itemsize)
+        array.data = malloc(ARRAY.data.TO, itemsize, zero=zero)
         array.dataptr = direct_arrayitems(array.data)
         return array
     return ll_build_from_shape
@@ -58,7 +58,7 @@
             ("strides", INDEXARRAY),
             ("backstrides", INDEXARRAY),
             #("factors", INDEXARRAY),
-#            ("ao", ARRAY), # EECK!! this makes too many iter types !!
+            #("ao", ARRAY), # not needed..
             ("dataptr", ARRAY.TO.dataptr), # pointer to current item
             #("contiguous", Bool),
         ))
@@ -147,16 +147,26 @@
     p0[0] = op(p1[0], p2[0])
 
 def ll_array_unary_op(iter_new, iter_next, ITER, array0, array1): 
+    assert array0.ndim == array1.ndim
     it0 = iter_new(ITER, array0)
     it1 = iter_new(ITER, array1)
+    assert it0.size == it1.size
     while it0.index < it0.size:
-        ll_unary_op(it0.dataptr, it1.dataptr)
+        it0.dataptr[0] = it1.dataptr[0]
         iter_next(it0)
         iter_next(it1)
 
+def dim_of_ITER(ITER):
+    return ITER.TO.coordinates.length
+
 def dim_of_ARRAY(ARRAY):
     return ARRAY.TO.shape.length
 
+class ArrayIterRepr(Repr):
+    def __init__(self, rtyper, s_iter):
+        self.s_iter = s_iter
+        self.lowleveltype = self.ITER
+
 class ArrayRepr(Repr):
     def __init__(self, rtyper, s_array):
         self.s_array = s_array
@@ -180,19 +190,26 @@
         self.lowleveltype = self.ARRAY
         self.ITER = ARRAY_ITER(self.ARRAY, self.INDEXARRAY)
 
-    def build_from_array(self, llops, v_array):
+    def build_from_array(self, hop, v_array):
         cARRAY = inputconst(lltype.Void, self.lowleveltype.TO) 
-        return llops.gendirectcall(ll_build_alias, cARRAY, v_array)
+        return hop.llops.gendirectcall(ll_build_alias, cARRAY, v_array)
 
-    def build_from_shape(self, llops, r_tuple, v_tuple):
+    def build_from_shape(self, hop, r_arg, v_arg, zero=False):
         cARRAY = inputconst(lltype.Void, self.lowleveltype.TO) 
-        cTUPLE = inputconst(lltype.Void, r_tuple.lowleveltype.TO)
         ndim = self.s_array.ndim
-        ll_build_from_shape = gen_build_from_shape(ndim)
-        c_ndim = inputconst(lltype.Signed, ndim)
-        assert ndim == len(r_tuple.items_r)
-        rval = llops.gendirectcall(ll_build_from_shape, cARRAY, v_tuple)
-        return rval
+        if isinstance(r_arg, TupleRepr):
+            r_tuple, v_tuple = r_arg, v_arg
+            cTUPLE = inputconst(lltype.Void, r_tuple.lowleveltype.TO)
+            ll_build_from_shape = gen_build_from_shape(ndim, zero)
+            c_ndim = inputconst(lltype.Signed, ndim)
+            assert ndim == len(r_tuple.items_r)
+            return hop.llops.gendirectcall(ll_build_from_shape, cARRAY, v_tuple)
+        else:
+            assert ndim == 1
+            v_size = hop.inputarg(Signed, 0)
+            _malloc = lambda tp, size: malloc(tp, size, zero=zero)
+            cmalloc = inputconst(Void, _malloc)
+            return hop.llops.gendirectcall(ll_build_from_size, cARRAY, v_size, cmalloc)
 
     def rtype_method_transpose(self, hop):
         [v_self] = hop.inputargs(self)
@@ -291,11 +308,11 @@
         for src_i, r_item in unroll_r_tpl:
             if isinstance(r_item, IntegerRepr):
                 r_int = r_item
-                dataptr = direct_ptradd(dataptr, getattr(tpl, 'item%d'%src_i))
+                dataptr = direct_ptradd(dataptr, getattr(tpl, 'item%d'%src_i)*ao.strides[src_i])
             else:
                 r_slice = r_item
                 array.shape[tgt_i] = ao.shape[src_i]
-                array.strides[tgt_i] = ao.strides[tgt_i]
+                array.strides[tgt_i] = ao.strides[src_i]
                 tgt_i += 1
         assert tgt_i == ndim
         array.dataptr = dataptr
@@ -325,15 +342,18 @@
         v_array, v_tuple, v_item = hop.inputargs(r_arr, r_tpl, r_item)
         ndim = get_view_ndim(r_tpl)
         if isinstance(r_item, ArrayRepr):
+            s_view = SomeArray(r_arr.s_array.typecode, ndim)
+            r_view = hop.rtyper.getrepr(s_view)
+            cARRAY = hop.inputconst(Void, r_view.ARRAY.TO)
             get_view = gen_get_view(r_tpl)
-            cARRAY = hop.inputconst(Void, r_arr.ARRAY.TO)
             v_view = hop.gendirectcall(get_view, cARRAY, v_array, v_tuple)
-            iter_new, iter_next = gen_iter_funcs(r_arr.ndim)        
+            iter_new, iter_next = gen_iter_funcs(ndim)        
             assert ndim == r_item.ndim
+            assert dim_of_ITER(r_item.ITER) == dim_of_ITER(r_view.ITER)
             cnew = hop.inputconst(Void, iter_new)
             cnext = hop.inputconst(Void, iter_next)
             cITER = hop.inputconst(Void, r_item.ITER.TO)
-            return hop.gendirectcall(ll_array_unary_op, cnew, cnext, cITER, v_array, v_view) 
+            return hop.gendirectcall(ll_array_unary_op, cnew, cnext, cITER, v_view, v_item) 
         else:
             # Set from scalar
             assert ndim == 0
@@ -344,10 +364,6 @@
 
 class __extend__(pairtype(ArrayRepr, ArrayRepr)):
     def convert_from_to((r_arr0, r_arr1), v, llops):
-        print "%r -> %r"%(
-            (r_arr0, ),
-            (r_arr1, ),
-        )
         assert 0
 
 class __extend__(pairtype(AbstractBaseListRepr, ArrayRepr)):
@@ -372,6 +388,14 @@
     array.dataptr = nullptr(ARRAY.dataptr.TO)
     return array
 
+def ll_build_from_size(ARRAY, size, _malloc):
+    array = ll_allocate(ARRAY, 1)
+    array.shape[0] = size
+    array.strides[0] = 1
+    array.data = _malloc(ARRAY.data.TO, size)
+    array.dataptr = direct_arrayitems(array.data)
+    return array
+
 def ll_build_from_list(ARRAY, lst):
     size = lst.ll_length()
     array = ll_allocate(ARRAY, 1)

Modified: pypy/dist/pypy/rpython/numpy/test/test_array.py
==============================================================================
--- pypy/dist/pypy/rpython/numpy/test/test_array.py	(original)
+++ pypy/dist/pypy/rpython/numpy/test/test_array.py	Wed Sep  5 02:48:03 2007
@@ -190,19 +190,22 @@
 
     def test_specialize_array_create(self):
         def f():
-#            a = numpy.empty((2,))
-#            a[0] = 1
-#            a[1] = 20
-#            return a
             a = numpy.array([1,20])
-#            b = numpy.array(a) # XX
-            b = a
+            b = numpy.array(a)
             return b
 
         res = interpret(f, [])
         assert res.data[0] == 1
         assert res.data[1] == 20
 
+    def test_specialize_array_empty1(self):
+        def f(n):
+            a = numpy.empty(n)
+            return a
+
+        res = interpret(f, [3])
+        assert res.ndim == 1
+
     def test_specialize_array_empty(self):
         def f(n, m):
             a = numpy.empty((n, m))
@@ -211,6 +214,16 @@
         res = interpret(f, [3, 4])
         assert res.ndim == 2
 
+    def test_specialize_array_zeros(self):
+        def f(n, m):
+            a = numpy.zeros((n, m))
+            return a
+
+        res = interpret(f, [3, 4])
+        for i in range(3*4):
+            assert res.dataptr[i] == 0
+        assert res.ndim == 2
+
     def test_specialize_array_access(self):
         def access_with_variable():
             my_array = numpy.array(range(10), dtype='i')
@@ -266,14 +279,14 @@
 
     def test_specialize_array_method(self):
         def f():
-            a = numpy.empty((2,))
+            a = numpy.array([1,2])
             return a.transpose()
 
         res = interpret(f, [])
-#        assert res.data[0] == 1
-#        assert res.data[1] == 2
+        assert res.data[0] == 1
+        assert res.data[1] == 2
 
-    def test_specialize_indexing(self):
+    def test_specialize_view_0(self):
         def f():
             a = numpy.empty((4,3), dtype='i')
             a[0,0] = 5
@@ -287,6 +300,7 @@
         assert res.dataptr[6] == 555
         assert res.shape.item0 == 4
         assert res.strides.item0 == 3
+        assert res.ndim == 1
 
     def test_specialize_multi(self):
         def f(ii, jj):
@@ -298,6 +312,19 @@
         assert interpret(f, [0, 0]) == 0
         assert interpret(f, [3, 4]) == 12
 
+    def test_specialize_view(self):
+        def f(ii, jj):
+            a = numpy.zeros((4, 5))
+            b = numpy.zeros((3, 4))
+            a[0,1] = 5.
+            a[1,1] = 4.
+            a[2,1] = 3.
+            a[3,1] = 2.
+            b[2,:] = a[:,1]
+            return b[ii, jj]
+
+        assert interpret(f, [2, 3]) == 2
+        
     def test_malloc_remove(self):
         py.test.skip('this test requires _always_inline_ magic hook')
         def f():
@@ -326,7 +353,7 @@
             py.test.skip("c compilation disabled")
 
         from pypy.translator.c.test.test_genc import compile
-        self.compile = lambda s, x, y : compile(x, y)
+        self.compile = lambda s, x, y : compile(x, y, backendopt=True)
 
     def test_compile_array_access(self):
         def access_array(index):
@@ -352,4 +379,14 @@
         fn = self.compile(f, [int, int])
         assert fn(2,3) == 2*3
         
+    def test_compile_view(self):
+        def f(ii, jj):
+            a = numpy.zeros((4, 5), dtype='i')
+            b = numpy.zeros((3, 4), dtype='i')
+            b[0,:] = a[:,0]
+            return b[ii, jj]
+
+        fn = self.compile(f, [int, int])
+        assert fn(2,3) == 0
+        
 


More information about the pypy-svn mailing list