Author: Justin Peel <notmuchtot...@gmail.com> Branch: numpy-dtype Changeset: r46174:f0e8e21ec01c Date: 2011-08-01 23:19 -0600 http://bitbucket.org/pypy/pypy/changeset/f0e8e21ec01c/
Log: Added simple dtypes. Still needs a lot of work. diff --git a/pypy/module/micronumpy/__init__.py b/pypy/module/micronumpy/__init__.py --- a/pypy/module/micronumpy/__init__.py +++ b/pypy/module/micronumpy/__init__.py @@ -34,6 +34,9 @@ 'arcsin': 'interp_ufuncs.arcsin', 'arccos': 'interp_ufuncs.arccos', 'arctan': 'interp_ufuncs.arctan', + + # dtype + 'dtype': 'interp_dtype.Dtype', } appleveldefs = { diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py --- a/pypy/module/micronumpy/interp_numarray.py +++ b/pypy/module/micronumpy/interp_numarray.py @@ -2,6 +2,7 @@ from pypy.interpreter.error import OperationError, operationerrfmt from pypy.interpreter.gateway import interp2app, unwrap_spec from pypy.interpreter.typedef import TypeDef, GetSetProperty +from pypy.module.micronumpy.interp_dtype import Dtype, Float64_num, Int32_num, Float64_dtype, get_dtype from pypy.module.micronumpy.interp_support import Signature from pypy.module.micronumpy import interp_ufuncs from pypy.objspace.std.floatobject import float2string as float2string_orig @@ -11,7 +12,11 @@ from pypy.tool.sourcetools import func_with_new_name import math -TP = lltype.Array(lltype.Float, hints={'nolength': True}) +TPs = [None] * 14 +TPs[Float64_num] = lltype.Array(lltype.Float, hints={'nolength': True}) +TPs[Int32_num] = lltype.Array(lltype.Signed, hints={'nolength': True}) +TP_bool = lltype.Array(lltype.Bool, hints={'nolength': True}) +TP_uint = lltype.Array(lltype.Unsigned, hints={'nolength': True}) numpy_driver = jit.JitDriver(greens = ['signature'], reds = ['result_size', 'i', 'self', 'result']) @@ -206,7 +211,10 @@ raise NotImplementedError def descr_copy(self, space): - return new_numarray(space, self) + return new_numarray(space, self, self.dtype) + + def descr_get_dtype(self, space): + return space.wrap(self.dtype) def descr_get_shape(self, space): return space.newtuple([self.descr_len(space)]) @@ -250,7 +258,8 @@ # part of itself. can be improved. if (concrete.get_root_storage() == w_value.get_concrete().get_root_storage()): - w_value = new_numarray(space, w_value) + # XXX: Need to fill in dtype + w_value = new_numarray(space, w_value, self.dtype) else: w_value = convert_to_array(space, w_value) concrete.setslice(space, start, stop, step, @@ -286,7 +295,8 @@ return w_obj elif space.issequence_w(w_obj): # Convert to array. - return new_numarray(space, w_obj) + # XXX: Need to fill in the dtype + return new_numarray(space, w_obj, Float64_dtype) else: # If it's a scalar return FloatWrapper(space.float_w(w_obj)) @@ -325,7 +335,7 @@ i = 0 signature = self.signature result_size = self.find_size() - result = SingleDimArray(result_size) + result = SingleDimArray(result_size, self.dtype) while i < result_size: numpy_driver.jit_merge_point(signature=signature, result_size=result_size, i=i, @@ -362,6 +372,7 @@ VirtualArray.__init__(self, signature) self.function = function self.values = values + self.dtype = self.values.dtype def _del_sources(self): self.values = None @@ -383,17 +394,19 @@ self.function = function self.left = left self.right = right + try: + self.size = self.left.find_size() + self.dtype = self.left.dtype + except: + self.size = self.right.find_size() + self.dtype = self.right.dtype def _del_sources(self): self.left = None self.right = None def _find_size(self): - try: - return self.left.find_size() - except ValueError: - pass - return self.right.find_size() + return self.size def _eval(self, i): lhs, rhs = self.left.eval(i), self.right.eval(i) @@ -473,9 +486,11 @@ class SingleDimArray(BaseArray): signature = Signature() - def __init__(self, size): + def __init__(self, size, dtype): BaseArray.__init__(self) self.size = size + self.dtype = dtype + TP = TPs[dtype.typenum] self.storage = lltype.malloc(TP, size, zero=True, flavor='raw', track_allocation=False, add_memory_pressure=True) @@ -509,25 +524,38 @@ def __del__(self): lltype.free(self.storage, flavor='raw', track_allocation=False) -def new_numarray(space, w_size_or_iterable): - l = space.listview(w_size_or_iterable) - arr = SingleDimArray(len(l)) +def new_numarray(space, iterable, dtype): + l = space.listview(iterable) + dtype = get_dtype(space, Dtype, dtype) + arr = SingleDimArray(len(l), dtype) i = 0 + convfunc = dtype.convfunc + wrapfunc = dtype.wrapfunc for w_elem in l: - arr.storage[i] = space.float_w(space.float(w_elem)) + arr.storage[i] = wrapfunc(space, convfunc(space, w_elem)) i += 1 return arr -def descr_new_numarray(space, w_type, w_size_or_iterable): - return space.wrap(new_numarray(space, w_size_or_iterable)) +def descr_new_numarray(space, w_type, __args__): + # this isn't such a great check. We should improve it including exceptions. + iterable = __args__.arguments_w[0] + if len(__args__.arguments_w) == 2: + dtype = __args__.arguments_w[1] + return space.wrap(new_numarray(space, __args__.arguments_w[0], + __args__.arguments_w[1])) + else: + # can just use the dtype for float for now. We need to actually be + # able to determine the base dtype of an iterable + dtype = space.wrap('d') + return space.wrap(new_numarray(space, iterable, dtype)) @unwrap_spec(size=int) def zeros(space, size): - return space.wrap(SingleDimArray(size)) + return space.wrap(SingleDimArray(size, Float64_dtype)) @unwrap_spec(size=int) def ones(space, size): - arr = SingleDimArray(size) + arr = SingleDimArray(size, Float64_dtype) for i in xrange(size): arr.storage[i] = 1.0 return space.wrap(arr) @@ -538,6 +566,7 @@ copy = interp2app(BaseArray.descr_copy), shape = GetSetProperty(BaseArray.descr_get_shape), + dtype = GetSetProperty(BaseArray.descr_get_dtype), __len__ = interp2app(BaseArray.descr_len), __getitem__ = interp2app(BaseArray.descr_getitem), diff --git a/pypy/module/micronumpy/interp_support.py b/pypy/module/micronumpy/interp_support.py --- a/pypy/module/micronumpy/interp_support.py +++ b/pypy/module/micronumpy/interp_support.py @@ -1,3 +1,4 @@ +from pypy.module.micronumpy.interp_dtype import Float64_dtype from pypy.rlib.rstruct.runpack import runpack from pypy.rpython.lltypesystem import lltype, rffi from pypy.interpreter.error import OperationError @@ -17,7 +18,7 @@ raise OperationError(space.w_ValueError, space.wrap( "string length %d not divisable by %d" % (length, FLOAT_SIZE))) - a = SingleDimArray(number) + a = SingleDimArray(number, Float64_dtype) start = 0 end = FLOAT_SIZE @@ -39,4 +40,4 @@ if target in self.transitions: return self.transitions[target] self.transitions[target] = new = Signature() - return new \ No newline at end of file + return new diff --git a/pypy/module/micronumpy/test/test_base.py b/pypy/module/micronumpy/test/test_base.py --- a/pypy/module/micronumpy/test/test_base.py +++ b/pypy/module/micronumpy/test/test_base.py @@ -1,5 +1,6 @@ from pypy.conftest import gettestobjspace from pypy.module.micronumpy.interp_numarray import SingleDimArray, FloatWrapper +from pypy.module.micronumpy.interp_dtype import Float64_dtype class BaseNumpyAppTest(object): def setup_class(cls): @@ -7,7 +8,7 @@ class TestSignature(object): def test_binop_signature(self, space): - ar = SingleDimArray(10) + ar = SingleDimArray(10, Float64_dtype) v1 = ar.descr_add(space, ar) v2 = ar.descr_add(space, FloatWrapper(2.0)) assert v1.signature is not v2.signature @@ -17,7 +18,7 @@ assert v1.signature is v4.signature def test_slice_signature(self, space): - ar = SingleDimArray(10) + ar = SingleDimArray(10, Float64_dtype) v1 = ar.descr_getitem(space, space.wrap(slice(1, 5, 1))) v2 = ar.descr_getitem(space, space.wrap(slice(4, 6, 1))) assert v1.signature is v2.signature diff --git a/pypy/module/micronumpy/test/test_zjit.py b/pypy/module/micronumpy/test/test_zjit.py --- a/pypy/module/micronumpy/test/test_zjit.py +++ b/pypy/module/micronumpy/test/test_zjit.py @@ -1,5 +1,6 @@ from pypy.jit.metainterp.test.support import LLJitMixin from pypy.rpython.test.test_llinterp import interpret +from pypy.module.micronumpy.interp_dtype import Float64_dtype from pypy.module.micronumpy.interp_numarray import (SingleDimArray, Signature, FloatWrapper, Call2, SingleDimSlice, add, mul, Call1) from pypy.module.micronumpy.interp_ufuncs import negative @@ -26,7 +27,7 @@ def test_add(self): def f(i): - ar = SingleDimArray(i) + ar = SingleDimArray(i, Float64_dtype) v = Call2(add, ar, ar, Signature()) return v.get_concrete().storage[3] @@ -38,7 +39,7 @@ def test_floatadd(self): def f(i): - ar = SingleDimArray(i) + ar = SingleDimArray(i, Float64_dtype) v = Call2(add, ar, FloatWrapper(4.5), Signature()) return v.get_concrete().storage[3] @@ -52,7 +53,7 @@ space = self.space def f(i): - ar = SingleDimArray(i) + ar = SingleDimArray(i, Float64_dtype) return ar.descr_add(space, ar).descr_sum(space) result = self.meta_interp(f, [5], listops=True, backendopt=True) @@ -65,7 +66,7 @@ space = self.space def f(i): - ar = SingleDimArray(i) + ar = SingleDimArray(i, Float64_dtype) return ar.descr_add(space, ar).descr_prod(space) result = self.meta_interp(f, [5], listops=True, backendopt=True) @@ -78,7 +79,7 @@ space = self.space def f(i): - ar = SingleDimArray(i) + ar = SingleDimArray(i, Float64_dtype) j = 0 while j < i: ar.get_concrete().storage[j] = float(j) @@ -96,7 +97,7 @@ space = self.space def f(i): - ar = SingleDimArray(i) + ar = SingleDimArray(i, Float64_dtype) j = 0 while j < i: ar.get_concrete().storage[j] = float(j) @@ -114,7 +115,7 @@ space = self.space def f(i): - ar = SingleDimArray(i) + ar = SingleDimArray(i, Float64_dtype) j = 0 while j < i: ar.get_concrete().storage[j] = float(j) @@ -132,7 +133,7 @@ space = self.space def f(i): - ar = SingleDimArray(i) + ar = SingleDimArray(i, Float64_dtype) j = 0 while j < i: ar.get_concrete().storage[j] = 1.0 @@ -148,7 +149,7 @@ space = self.space def f(i): - ar = SingleDimArray(i) + ar = SingleDimArray(i, Float64_dtype) return ar.descr_add(space, ar).descr_any(space) result = self.meta_interp(f, [5], listops=True, backendopt=True) @@ -159,7 +160,7 @@ def test_already_forecd(self): def f(i): - ar = SingleDimArray(i) + ar = SingleDimArray(i, Float64_dtype) v1 = Call2(add, ar, FloatWrapper(4.5), Signature()) v2 = Call2(mul, v1, FloatWrapper(4.5), Signature()) v1.force_if_needed() @@ -177,7 +178,7 @@ def test_ufunc(self): space = self.space def f(i): - ar = SingleDimArray(i) + ar = SingleDimArray(i, Float64_dtype) v1 = Call2(add, ar, ar, Signature()) v2 = negative(space, v1) return v2.get_concrete().storage[3] @@ -194,7 +195,7 @@ def f(i): add_sig = Signature() mul_sig = Signature() - ar = SingleDimArray(i) + ar = SingleDimArray(i, Float64_dtype) v1 = Call2(add, ar, ar, ar.signature.transition(add_sig)) v2 = negative(space, v1) @@ -212,7 +213,7 @@ def test_slice(self): def f(i): step = 3 - ar = SingleDimArray(step*i) + ar = SingleDimArray(step*i, Float64_dtype) s = SingleDimSlice(0, step*i, step, i, ar, ar.signature.transition(SingleDimSlice.static_signature)) v = Call2(add, s, s, Signature()) return v.get_concrete().storage[3] @@ -227,7 +228,7 @@ def f(i): step1 = 2 step2 = 3 - ar = SingleDimArray(step2*i) + ar = SingleDimArray(step2*i, Float64_dtype) s1 = SingleDimSlice(0, step1*i, step1, i, ar, ar.signature.transition(SingleDimSlice.static_signature)) s2 = SingleDimSlice(0, step2*i, step2, i, ar, ar.signature.transition(SingleDimSlice.static_signature)) v = Call2(add, s1, s2, Signature()) @@ -244,8 +245,8 @@ def f(i): step = NonConstant(3) - ar = SingleDimArray(step*i) - ar2 = SingleDimArray(i) + ar = SingleDimArray(step*i, Float64_dtype) + ar2 = SingleDimArray(i, Float64_dtype) ar2.storage[1] = 5.5 if NonConstant(False): arg = ar2 _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit