[pypy-svn] r46203 - in pypy/dist/pypy/rpython/numpy: . test
simonb at codespeak.net
simonb at codespeak.net
Fri Aug 31 02:15:40 CEST 2007
Author: simonb
Date: Fri Aug 31 02:15:39 2007
New Revision: 46203
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:
add base attr, cannot figure out how to set a struct attribute to NULL (?????)
Modified: pypy/dist/pypy/rpython/numpy/aarray.py
==============================================================================
--- pypy/dist/pypy/rpython/numpy/aarray.py (original)
+++ pypy/dist/pypy/rpython/numpy/aarray.py Fri Aug 31 02:15:39 2007
@@ -25,11 +25,15 @@
#'f' : SomeFloat(), # XX single precision float XX
'd' : SomeFloat(),
}
- def __init__(self, typecode, ndim=1):
+ def __init__(self, typecode, ndim=1, s_base=None):
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
+
+ def get_base_annotation(self):
+ return self.s_base or self
def can_be_none(self):
return True
@@ -45,6 +49,8 @@
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
@@ -102,7 +108,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)
+ return SomeArray(s_array.typecode, ndim, s_array.get_base_annotation())
return s_array.get_item_type()
# These two up-cast the index to SomeTuple and call above.
Modified: pypy/dist/pypy/rpython/numpy/rarray.py
==============================================================================
--- pypy/dist/pypy/rpython/numpy/rarray.py (original)
+++ pypy/dist/pypy/rpython/numpy/rarray.py Fri Aug 31 02:15:39 2007
@@ -9,7 +9,7 @@
from pypy.rpython.rslice import AbstractSliceRepr
from pypy.rpython.lltypesystem.lltype import \
GcArray, GcStruct, Signed, Ptr, Unsigned, Void, FixedSizeArray, Bool,\
- malloc, direct_arrayitems, direct_ptradd
+ GcForwardReference, malloc, direct_arrayitems, direct_ptradd, _cast_whatever
from pypy.rpython.lltypesystem.rtuple import TupleRepr
from pypy.annotation.model import SomeObject, SomeInteger
from pypy.rpython.numpy.aarray import SomeArray
@@ -78,12 +78,13 @@
it.dataptr = direct_arrayitems(it.ao.data)
for i in unroll_ndim:
it.coordinates[i] = 0
+ ll_iter_reset._always_inline_ = True
- def ll_iter_new(ITER, ao):
+ def ll_iter_new(ITER, ao, iter_reset=ll_iter_reset):
it = malloc(ITER)
+ it.ao = ao
it.nd_m1 = ndim - 1
it.size = ll_mul_list(ao.shape, ndim)
- it.ao = ao
#it.factors[nd-1] = 1
for i in unroll_ndim:
it.dims_m1[i] = ao.shape[i]-1
@@ -91,14 +92,10 @@
it.backstrides[i] = it.strides[i] * it.dims_m1[i]
#if i > 0:
#it.factors[nd-i-1] = it.factors[nd]*ao.shape[nd-i]
- # ll_iter_reset(it)
- it.index = 0
- it.dataptr = direct_arrayitems(it.ao.data)
- for i in unroll_ndim:
- it.coordinates[i] = 0
+ iter_reset(it)
return it
ll_iter_new._always_inline_ = True
-
+
def ll_iter_next(it):
it.index += 1
for i in unroll_ndim_rev:
@@ -108,18 +105,23 @@
break
it.coordinates[i] = 0
it.dataptr = direct_ptradd(it.dataptr, -it.backstrides[i])
- i -= 1
ll_iter_next._always_inline_ = True
return ll_iter_new, ll_iter_next
-def ll_setitem_from_array(iter_new0, iter_next0, ITER0, array0,
- slice,
- iter_new1, iter_next1, ITER1, array1):
+def ll_unary_op(p0, p1, op=lambda x:x):
+ p0[0] = op(p1[0])
+
+def ll_binary_op(p0, p1, p2, op=lambda x,y:x+y):
+ p0[0] = op(p1[0], p2[0])
+
+def ll_array_unary_op(iter_new0, iter_next0, ITER0, array0,
+ iter_new1, iter_next1, ITER1, array1,):
+# op=ll_unary_op):
it0 = iter_new0(ITER0, array0)
it1 = iter_new1(ITER1, array1)
while it0.index < it0.size:
- it0.dataptr[0] = it1.dataptr[0]
+ ll_unary_op(it0.dataptr, it1.dataptr)
iter_next0(it0)
iter_next1(it1)
@@ -133,14 +135,17 @@
ITEMARRAY = GcArray(self.ITEM, hints={'nolength':True})
self.INDEXARRAY = FixedSizeArray(NPY_INTP, self.ndim)
self.itemsize = sizeof(self.ITEM)
- self.ARRAY = Ptr(
- GcStruct("array",
- ("data", Ptr(ITEMARRAY)), # pointer to raw data buffer
- ("ndim", Signed), # number of dimensions (do we need this ?)
- ("shape", self.INDEXARRAY), # size in each dimension
- ("strides", self.INDEXARRAY), # elements to jump to get to the
- # next element in each dimension
- ))
+ FORWARD = GcForwardReference()
+ STRUCT = GcStruct("array",
+ ("data", Ptr(ITEMARRAY)), # pointer to raw data buffer
+ ("ndim", Signed), # number of dimensions
+ ("shape", self.INDEXARRAY), # size in each dimension
+ ("strides", self.INDEXARRAY), # elements to jump to get to the
+ # next element in each dimension
+ ("base", Ptr(FORWARD)), # we are a view into this array
+ )
+ self.ARRAY = Ptr(STRUCT)
+ STRUCT.base.TO.become(STRUCT)
self.lowleveltype = self.ARRAY
self.ITER = ARRAY_ITER(self.ARRAY, self.INDEXARRAY)
@@ -167,14 +172,17 @@
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):
- cname = inputconst(Void, 'shape')
TUPLE = TUPLE_TYPE([Signed]*self.ndim)
cARRAY = inputconst(lltype.Void, self.lowleveltype.TO)
cTUPLE = inputconst(lltype.Void, TUPLE.TO)
ll_get_shape = gen_get_shape(self.ndim)
return hop.llops.gendirectcall(ll_get_shape, cARRAY, cTUPLE, v_array)
- return llops.genop('getfield', [v_array, cname], resulttype=TUPLE)
def rtype_getattr(self, hop):
s_attr = hop.args_s[1]
@@ -203,6 +211,7 @@
return hop.gendirectcall(ll_add, cARRAY, v_arr1, v_arr2)
+#class __extend__(pairtype(ArrayRepr, Repr)): # <------ USE THIS ??
class __extend__(pairtype(ArrayRepr, IntegerRepr)):
def rtype_setitem((r_arr, r_int), hop):
v_array, v_index, v_item = hop.inputargs(r_arr, Signed, r_arr.item_repr)
@@ -234,8 +243,8 @@
cnew1 = hop.inputconst(Void, iter_new1)
cnext1 = hop.inputconst(Void, iter_next1)
# return hop.gendirectcall(ll_setitem_from_array, cnext, cITER0, v_array, v_slc, cITER1, v_item)
- return hop.gendirectcall(ll_setitem_from_array,
- cnew0, cnext0, cITER0, v_array, v_slc, cnew1, cnext1, cITER1, v_item)
+ return hop.gendirectcall(ll_array_unary_op,
+ cnew0, cnext0, cITER0, v_array, cnew1, cnext1, cITER1, v_item)
#class __extend__(pairtype(ArrayRepr, AbstractSliceRepr)):
@@ -263,11 +272,11 @@
cARRAY = inputconst(lltype.Void, r_arr.lowleveltype.TO)
return llops.gendirectcall(ll_build_from_list, cARRAY, v)
+from pypy.rpython.memory.lladdress import NULL
def ll_allocate(ARRAY, ndim):
array = malloc(ARRAY)
array.ndim = ndim
- #array.shape = malloc(ARRAY.shape.TO, array.ndim)
- #array.strides = malloc(ARRAY.strides.TO, array.ndim)
+ #array.base = NULL # how to do this ?????
return array
def ll_build_from_list(ARRAY, lst):
@@ -286,6 +295,8 @@
def ll_build_alias(ARRAY, array):
new_array = ll_allocate(ARRAY, array.ndim)
new_array.data = array.data # alias data
+# new_array.base = array.base or array # this is broken until we set base to NULL above
+ new_array.base = array
for i in range(array.ndim):
new_array.shape[i] = array.shape[i]
new_array.strides[i] = array.strides[i]
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 Fri Aug 31 02:15:39 2007
@@ -164,22 +164,33 @@
from pypy.translator.backendopt.malloc import LLTypeMallocRemover
class Test_specialization:
+ def specialize_view(self, f, args=[], opt=False):
+ t = TranslationContext()
+ a = t.buildannotator()
+ a = a.build_types(f, args)
+ r = t.buildrtyper()
+ r.specialize()
+ if opt:
+ from pypy.translator.backendopt.all import backend_optimizations
+ backend_optimizations(t)
+ t.view()
+
def test_specialize_array_create(self):
- def create_array():
+ def f():
a = numpy.array([1,20])
b = numpy.array(a)
return b
- res = interpret(create_array, [])
+ res = interpret(f, [])
assert res.data[0] == 1
assert res.data[1] == 20
def test_specialize_array_zeros(self):
- def create_array(n, m):
+ def f(n, m):
a = numpy.zeros((n, m))
return a
- res = interpret(create_array, [3, 4])
+ res = interpret(f, [3, 4])
assert res.ndim == 2
def test_specialize_array_access(self):
@@ -196,49 +207,58 @@
assert res == 45
def test_specialize_array_add(self):
- def create_array():
+ def f():
a1 = numpy.array([1.,2.])
a2 = numpy.array([6,9])
return a1 + a2
- res = interpret(create_array, [])
+ res = interpret(f, [])
assert res.data[0] == 7
assert res.data[1] == 11
def test_specialize_array_attr(self):
- def create_array():
+ def f():
a = numpy.array([1,2])
return a.ndim
- res = interpret(create_array, [])
+ res = interpret(f, [])
assert res == 1
+ def test_specialize_base(self):
+ def f():
+ a = numpy.array([1,2])
+ b = numpy.array(a)
+ return b.base is a
+
+ res = interpret(f, [])
+ assert res
+
def test_specialize_array_attr_shape(self):
- def create_array():
+ def f():
a = numpy.zeros((2,3))
return list(a.shape)
- res = interpret(create_array, [])
+ res = interpret(f, [])
assert res[0] == 2
assert res[1] == 3
def test_specialize_array_strides(self):
- def create_array():
+ def f():
a = numpy.zeros((3,4,5))
return a
- res = interpret(create_array, [])
+ res = interpret(f, [])
assert res.strides[0] == 20
assert res.strides[1] == 5
assert res.strides[2] == 1
#assert len(res.data) == 3*4*5 # GcArray has nolength
def test_specialize_array_method(self):
- def create_array():
+ def f():
a = numpy.array([1,2])
return a.transpose()
- res = interpret(create_array, [])
+ res = interpret(f, [])
assert res.data[0] == 1
assert res.data[1] == 2
@@ -253,16 +273,6 @@
assert res.data[1] == 55
assert res.data[2] == 555
- def specialize_view(self, f, args=[]):
- t = TranslationContext()
- a = t.buildannotator()
- a = a.build_types(f, args)
- r = t.buildrtyper()
- r.specialize()
- from pypy.translator.backendopt.all import backend_optimizations
- backend_optimizations(t)
- t.view()
-
def test_malloc_remove(self):
def f():
a = numpy.zeros((3,), dtype='i')
@@ -276,8 +286,8 @@
r.specialize()
from pypy.translator.backendopt.all import backend_optimizations
backend_optimizations(t)
- from pypy.rpython.numpy.rarray import ll_setitem_from_array
- graph = graphof(t, ll_setitem_from_array)
+ from pypy.rpython.numpy.rarray import ll_array_unary_op
+ graph = graphof(t, ll_array_unary_op)
#graph.show()
from pypy.translator.backendopt.test.test_malloc import TestLLTypeMallocRemoval
TestLLTypeMallocRemoval.check_malloc_removed(graph)
More information about the Pypy-commit
mailing list