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

simonb at codespeak.net simonb at codespeak.net
Tue Sep 4 21:09:39 CEST 2007


Author: simonb
Date: Tue Sep  4 21:09:39 2007
New Revision: 46309

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:
get rid of base attribute. can use data attribute to detect aliases

Modified: pypy/dist/pypy/rpython/numpy/aarray.py
==============================================================================
--- pypy/dist/pypy/rpython/numpy/aarray.py	(original)
+++ pypy/dist/pypy/rpython/numpy/aarray.py	Tue Sep  4 21:09:39 2007
@@ -25,18 +25,11 @@
         #'f' : SomeFloat(), # XX single precision float XX
         'd' : SomeFloat(),
     }
-    def __init__(self, typecode, ndim=1, s_base=None):
+    def __init__(self, typecode, ndim=1):
         if not typecode in self.typecode_to_item:
             raise AnnotatorError("bad typecode: %r"%typecode)
         self.typecode = typecode
         self.ndim = ndim
-        self.s_base = s_base # we are a view into this 
-        if s_base is not None:
-            assert s_base.s_base is None # shallow bases
-
-    def get_base_annotation(self):
-        return self.s_base or self
-        # can't set s_base to self because this screws up __eq__
 
     def can_be_none(self):
         return True
@@ -52,14 +45,12 @@
                 s = SomeTuple([SomeInteger()]*s_array.ndim)
             elif attr == 'ndim':
                 s = SomeInteger()
-            elif attr == 'base':
-                s = s_array.get_base_annotation()
         if s is None:
             return SomeObject.getattr(s_array, s_attr)
         return s
 
     def method_transpose(self):
-        return SomeArray(self.typecode, self.ndim, self)
+        return SomeArray(self.typecode, self.ndim)
 
 class __extend__(pairtype(SomeArray, SomeArray)):
 
@@ -111,7 +102,7 @@
         if s_array.ndim == 0 and len(s_index.items):
             raise AnnotatorError("indexing rank zero array with nonempty tuple")
         if ndim > 0:
-            return SomeArray(s_array.typecode, ndim, s_array.get_base_annotation())
+            return SomeArray(s_array.typecode, ndim)
         return s_array.get_item_type()
 
 # These two up-cast the index to SomeTuple and call above.
@@ -155,7 +146,6 @@
     _about_ = numpy.array
 
     def compute_result_annotation(self, s_list, s_dtype=None):
-        s_base = None
         if isinstance(s_list, SomeList):
             # First guess type from input list
             listitem = s_list.listdef.listitem
@@ -165,7 +155,6 @@
         elif isinstance(s_list, SomeArray):
             typecode = s_list.typecode
             ndim = s_list.ndim
-            s_base = s_list
         else:
             raise AnnotatorError("cannot build array from %s"%s_list)
 
@@ -178,11 +167,7 @@
         if typecode is None or typecode not in valid_typecodes:
             raise AnnotatorError("List item type not supported")
 
-        if isinstance(s_list, SomeList):
-            # make phantom array annotation
-            s_base = SomeArray(typecode, ndim)
-    
-        return SomeArray(typecode, ndim, s_base)
+        return SomeArray(typecode, ndim)
 
     def specialize_call(self, hop, i_dtype=None):
         r_array = hop.r_result

Modified: pypy/dist/pypy/rpython/numpy/rarray.py
==============================================================================
--- pypy/dist/pypy/rpython/numpy/rarray.py	(original)
+++ pypy/dist/pypy/rpython/numpy/rarray.py	Tue Sep  4 21:09:39 2007
@@ -20,8 +20,8 @@
 
 def gen_build_from_shape(ndim):
     unrolling_dims = unrolling_iterable(reversed(range(ndim)))
-    def ll_build_from_shape(ARRAY, BASE, shape):
-        array = ll_allocate(ARRAY, BASE, ndim)
+    def ll_build_from_shape(ARRAY, shape):
+        array = ll_allocate(ARRAY, ndim)
         itemsize = 1
         for i in unrolling_dims:
             attr = 'item%d'%i
@@ -77,7 +77,6 @@
 
     def ll_iter_reset(it, dataptr):
         it.index = 0
-#        it.dataptr = it.ao.dataptr
         it.dataptr = dataptr
         for i in unroll_ndim:
             it.coordinates[i] = 0
@@ -87,7 +86,6 @@
         assert ao.dataptr
         assert ao.ndim == ndim
         it = malloc(ITER)
-#        it.ao = ao
         it.nd_m1 = ndim - 1
         it.size = ll_mul_list(ao.shape, ndim)
         #it.factors[nd-1] = 1
@@ -110,7 +108,6 @@
                 raise Exception("array is not broadcastable to correct shape") # XX raise here ?
             j += 1
         it = malloc(ITER)
-#        it.ao = ao
         it.size = ll_mul_list(ao.shape, ndim)
         it.nd_m1 = ndim - 1
         #it.factors[nd-1] = 1
@@ -160,9 +157,6 @@
 def dim_of_ARRAY(ARRAY):
     return ARRAY.TO.shape.length
 
-def BASE_TYPE(ARRAY):
-    return ARRAY.TO.base.TO
-
 class ArrayRepr(Repr):
     def __init__(self, rtyper, s_array):
         self.s_array = s_array
@@ -173,18 +167,6 @@
         ITEMARRAY = GcArray(self.ITEM, hints={'nolength':True})
         self.INDEXARRAY = FixedSizeArray(NPY_INTP, self.ndim)
         self.itemsize = sizeof(self.ITEM)
-        #BASE = GcForwardReference()
-        BASE = Void # whatever...
-        s_base = s_array.s_base
-        r_base = None
-        if s_base is None:
-            # we are pointing to ourself
-            BASE = Ptr(GcForwardReference())
-        else:
-            #print s_base, s_array
-            assert rtyper.makekey(s_base) != rtyper.makekey(s_array)
-            r_base = rtyper.getrepr(s_base)
-            BASE = Ptr(r_base.STRUCT)
         DATA_PTR = Ptr(FixedSizeArray(self.ITEM, 1))
         self.STRUCT = GcStruct("array",
             ("data", Ptr(ITEMARRAY)), # pointer to raw data buffer 
@@ -193,50 +175,34 @@
             ("shape", self.INDEXARRAY), # size in each dimension
             ("strides", self.INDEXARRAY), # elements to jump to get to the
                                           # next element in each dimension 
-            ("base", BASE), # we are a view into this array
         )
         self.ARRAY = Ptr(self.STRUCT)
-        if s_base is None:
-#            self.ARRAY.TO.base.TO.become(self.STRUCT)
-            BASE.TO.become(self.STRUCT)
-        self.BASE = BASE
-        self.r_base = r_base
         self.lowleveltype = self.ARRAY
         self.ITER = ARRAY_ITER(self.ARRAY, self.INDEXARRAY)
 
     def build_from_array(self, llops, v_array):
-        #self.set_base_repr(self)
         cARRAY = inputconst(lltype.Void, self.lowleveltype.TO) 
-        cBASE = inputconst(lltype.Void, self.BASE.TO) 
-        return llops.gendirectcall(ll_build_alias, cARRAY, cBASE, v_array)
+        return llops.gendirectcall(ll_build_alias, cARRAY, v_array)
 
     def build_from_shape(self, llops, r_tuple, v_tuple):
-        #self.set_base_repr(self)
         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)
-        cBASE = inputconst(lltype.Void, self.BASE.TO) 
-        rval = llops.gendirectcall(ll_build_from_shape, cARRAY, cBASE, v_tuple)
+        rval = llops.gendirectcall(ll_build_from_shape, cARRAY, v_tuple)
         return rval
 
     def rtype_method_transpose(self, hop):
         [v_self] = hop.inputargs(self)
         cARRAY = hop.inputconst(Void, hop.r_result.ARRAY.TO)
-        cBASE = inputconst(lltype.Void, self.BASE.TO) 
-        return hop.gendirectcall(ll_transpose, cARRAY, cBASE, v_self)
+        return hop.gendirectcall(ll_transpose, cARRAY, v_self)
 
     def get_ndim(self, hop, v_array):
         cname = inputconst(Void, 'ndim')
         return hop.llops.genop('getfield', [v_array, cname], resulttype=Signed)
 
-    def get_base(self, hop, v_array):
-        cname = inputconst(Void, 'base')
-        v_base = hop.llops.genop('getfield', [v_array, cname], resulttype=self.ARRAY)
-        return v_base
-
     def get_shape(self, hop, v_array):
         TUPLE = TUPLE_TYPE([Signed]*self.ndim)
         cARRAY = inputconst(lltype.Void, self.lowleveltype.TO) 
@@ -262,8 +228,6 @@
 
     def rtyper_makekey(self):
         key = self.__class__, self.typecode, self.ndim
-        if self.s_base is not None:
-            key = key + self.s_base.rtyper_makekey()
         return key
 
 
@@ -271,8 +235,7 @@
     def rtype_add((r_arr1, r_arr2), hop):
         v_arr1, v_arr2 = hop.inputargs(r_arr1, r_arr2)
         cARRAY = hop.inputconst(Void, hop.r_result.ARRAY.TO)
-        cBASE = foo
-        return hop.gendirectcall(ll_add, cARRAY, cBASE, v_arr1, v_arr2)
+        return hop.gendirectcall(ll_add, cARRAY, v_arr1, v_arr2)
 
 
 #class __extend__(pairtype(ArrayRepr, Repr)): # <------ USE THIS ??
@@ -298,15 +261,15 @@
         
 def gen_getset_item(ndim):
     unrolling_dims = unrolling_iterable(range(ndim))
-    def ll_get_item(ARRAY, BASE, ao, tpl):
-        array = ll_allocate(ARRAY, BASE, ndim)
+    def ll_get_item(ARRAY, ao, tpl):
+        array = ll_allocate(ARRAY, ndim)
         idx = 0
         for i in unrolling_dims:
             idx += ao.strides[i] * getattr(tpl, 'item%d'%i)
         return ao.data[idx]
 
-    def ll_set_item(ARRAY, BASE, ao, tpl, value):
-        array = ll_allocate(ARRAY, BASE, ndim)
+    def ll_set_item(ARRAY, ao, tpl, value):
+        array = ll_allocate(ARRAY, ndim)
         idx = 0
         for i in unrolling_dims:
             idx += ao.strides[i] * getattr(tpl, 'item%d'%i)
@@ -320,9 +283,8 @@
 def gen_get_view(r_tpl):
     ndim = get_view_ndim(r_tpl)
     unroll_r_tpl = unrolling_iterable(enumerate(r_tpl.items_r))
-    def ll_get_view(ARRAY, BASE, ao, tpl):
-        array = ll_allocate(ARRAY, BASE, ndim)
-        array.base = ao
+    def ll_get_view(ARRAY, ao, tpl):
+        array = ll_allocate(ARRAY, ndim)
         dataptr = direct_arrayitems(ao.data)
         src_i = 0
         tgt_i = 0
@@ -337,6 +299,7 @@
                 tgt_i += 1
         assert tgt_i == ndim
         array.dataptr = dataptr
+        array.data = ao.data # keep a ref
         return array
     return ll_get_view
             
@@ -348,16 +311,14 @@
         if ndim == 0:
             # return a scalar
             cARRAY = hop.inputconst(Void, r_arr.ARRAY.TO)
-            cBASE = inputconst(lltype.Void, r_arr.BASE.TO) 
             get_item, set_item = gen_getset_item(r_arr.ndim)
-            return hop.gendirectcall(get_item, cARRAY, cBASE, v_array, v_tuple)
+            return hop.gendirectcall(get_item, cARRAY, v_array, v_tuple)
         r_result = hop.r_result
         ARRAY = r_result.ARRAY
         assert dim_of_ARRAY(ARRAY) == ndim
         cARRAY = hop.inputconst(Void, ARRAY.TO)
-        cBASE = inputconst(lltype.Void, r_result.BASE.TO) 
         ll_get_view = gen_get_view(r_tpl)
-        return hop.gendirectcall(ll_get_view, cARRAY, cBASE, v_array, v_tuple)
+        return hop.gendirectcall(ll_get_view, cARRAY, v_array, v_tuple)
 
     def rtype_setitem((r_arr, r_tpl), hop):
         r_item = hop.args_r[2]
@@ -366,8 +327,7 @@
         if isinstance(r_item, ArrayRepr):
             get_view = gen_get_view(r_tpl)
             cARRAY = hop.inputconst(Void, r_arr.ARRAY.TO)
-            cBASE = inputconst(lltype.Void, r_arr.BASE.TO) 
-            v_view = hop.gendirectcall(get_view, cARRAY, cBASE, v_array, v_tuple)
+            v_view = hop.gendirectcall(get_view, cARRAY, v_array, v_tuple)
             iter_new, iter_next = gen_iter_funcs(r_arr.ndim)        
             assert ndim == r_item.ndim
             cnew = hop.inputconst(Void, iter_new)
@@ -378,16 +338,15 @@
             # Set from scalar
             assert ndim == 0
             cARRAY = hop.inputconst(Void, r_arr.ARRAY.TO)
-            cBASE = inputconst(lltype.Void, r_arr.BASE.TO) 
             get_item, set_item = gen_getset_item(r_arr.ndim)
-            return hop.gendirectcall(set_item, cARRAY, cBASE, v_array, v_tuple, v_item)
+            return hop.gendirectcall(set_item, cARRAY, v_array, v_tuple, v_item)
 
 
 class __extend__(pairtype(ArrayRepr, ArrayRepr)):
     def convert_from_to((r_arr0, r_arr1), v, llops):
         print "%r -> %r"%(
-            (r_arr0, r_arr0.r_base),
-            (r_arr1, r_arr1.r_base),
+            (r_arr0, ),
+            (r_arr1, ),
         )
         assert 0
 
@@ -401,28 +360,25 @@
             assert 0, (r_lst, r_arr.item_repr)
             return NotImplemented
         cARRAY = inputconst(lltype.Void, r_arr.lowleveltype.TO) 
-        cBASE = inputconst(lltype.Void, r_arr.BASE.TO) 
-        return llops.gendirectcall(ll_build_from_list, cARRAY, cBASE, v)
+        return llops.gendirectcall(ll_build_from_list, cARRAY, v)
 
 class __extend__(pairtype(AbstractRangeRepr, ArrayRepr)):
     def convert_from_to((r_rng, r_arr), v, llops):
         #import py;py.test.skip()
         assert 0
         cARRAY = inputconst(lltype.Void, r_arr.lowleveltype.TO) 
-        cBASE = inputconst(lltype.Void, r_arr.BASE.TO) 
-        return llops.gendirectcall(ll_build_from_list, cARRAY, cBASE, v)
+        return llops.gendirectcall(ll_build_from_list, cARRAY, v)
 
-def ll_allocate(ARRAY, BASE, ndim):
+def ll_allocate(ARRAY, ndim):
     array = malloc(ARRAY)
     array.ndim = ndim
-    array.base = nullptr(BASE)
     array.data = nullptr(ARRAY.data.TO)
     array.dataptr = nullptr(ARRAY.dataptr.TO)
     return array
 
-def ll_build_from_list(ARRAY, BASE, lst):
+def ll_build_from_list(ARRAY, lst):
     size = lst.ll_length()
-    array = ll_allocate(ARRAY, BASE, 1)
+    array = ll_allocate(ARRAY, 1)
     array.shape[0] = size
     array.strides[0] = 1
     array.data = malloc(ARRAY.data.TO, size)
@@ -433,12 +389,9 @@
     array.dataptr = direct_arrayitems(array.data)
     return array
 
-def ll_build_alias(ARRAY, BASE, ao):
-    array = ll_allocate(ARRAY, BASE, ao.ndim)
+def ll_build_alias(ARRAY, ao):
+    array = ll_allocate(ARRAY, ao.ndim)
     array.data = ao.data # alias data
-    array.base = ao
-    if ao.base:
-        array.base = ao.base
     for i in range(ao.ndim):
         array.shape[i] = ao.shape[i]
         array.strides[i] = ao.strides[i]
@@ -464,8 +417,8 @@
     array.dataptr = direct_arrayitems(array.data)
     return array
 
-def ll_transpose(ARRAY, BASE, a1):
-    a2 = ll_build_alias(ARRAY, BASE, a1)
+def ll_transpose(ARRAY, a1):
+    a2 = ll_build_alias(ARRAY, a1)
     # XX do something to a2
     return a2
     

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	Tue Sep  4 21:09:39 2007
@@ -182,10 +182,7 @@
         a = t.buildannotator()
         s_array = a.build_types(f, [])
         assert type(s_array) == SomeArray
-        assert type(s_array.s_base) == SomeArray
         assert s_array.ndim == 1
-        assert s_array.s_base.ndim == 2
-        assert s_array.s_base.s_base == None
 
 
 from pypy.objspace.flow.model import checkgraph, flatten, Block, mkentrymap
@@ -258,15 +255,6 @@
         res = interpret(f, [])
         assert res == 1
 
-    def test_specialize_base(self):
-        def f():
-            a = numpy.empty((2,))
-            b = a[:,]
-            return b.base is a
-
-        res = interpret(f, [])
-        assert res
-
     def test_specialize_array_attr_shape(self):
         def f():
             a = numpy.empty((2,3))


More information about the pypy-svn mailing list