x = mutable() # stack allocate y = x # copy x into y y.value = 1 # update y.value, which does not affect x.value
@numba.njit
def getxss(data, size, dataindex, sizeindex):
sizeindex += 1
for i in range(size[sizeindex - 1]):
yield dataindex, sizeindex
length = size[sizeindex]
sizeindex += 1
for j in range(length):
dataindex += 1
@numba.njit
def getxs(data, size, dataindex, sizeindex):
for i in range(size[sizeindex]):
yield dataindex, sizeindex
dataindex += 1
@numba.njit
def iterator(numEntries):
for n in range(numEntries):
yield n
@numba.njit
def useiterator(numEntries):
out = 0
for n in iterator(numEntries):
out += n
return out
@numba.njit
def useloop(numEntries):
out = 0
for n in range(numEntries):
out += n
return out
numba.extending.make_attribute_wrapper(IntervalType, "hi", "hi")
@numba.extending.infer_getattr
class StructAttribute(numba.typing.templates.AttributeTemplate):
key = IntervalType
def generic_resolve(self, typ, attr):
if attr == "hi":
return numba.types.float64
@numba.extending.lower_getattr(IntervalType, "hi")
def struct_getattr_impl(context, builder, typ, val):
val = numba.cgutils.create_struct_proxy(typ)(context, builder, value=val)
return numba.targets.imputils.impl_ret_borrowed(context, builder, numba.types.float64, val.hi)
@numba.extending.lower_setattr(IntervalType, "hi")
def struct_setattr_impl(context, builder, sig, args):
assert isinstance(sig.args[0], IntervalType)
assert isinstance(sig.args[1], numba.types.Float)
interval, newvalue = args
val = numba.cgutils.create_struct_proxy(sig.args[0])(context, builder, value=interval)
ptr = val._get_ptr_by_name("hi")
return builder.store(newvalue, ptr)
@numba.njit
def doit10():
interval = Interval(4.4, 6.4)
interval.hi = 99.999
return interval.width
cres = doit10.overloads.values()[0]
print(cres.library.get_llvm_str())
The code isn't clear to me. Is ut just overloading set atr? Wouldn't it be easier if we could just access it as a generic function?
also have you confirmed interval is stack allocated?
import numba
class Interval(object):
def __init__(self, lo, hi):
self.lo = lo
self.hi = hi
def __repr__(self):
return "Interval({}, {})".format(self.lo, self.hi)
class IntervalType(numba.types.Type):
def __init__(self):
super(IntervalType, self).__init__(name="Interval")
intervaltype = IntervalType()
@numba.extending.typeof_impl.register(Interval)
def typeof_index(val, c):
return intervaltype
@numba.extending.type_callable(Interval)
def type_interval(context):
def typer(lo, hi):
if isinstance(lo, numba.types.Float) and isinstance(hi, numba.types.Float):
return intervaltype
return typer
@numba.extending.register_model(IntervalType)
class IntervalModel(numba.extending.models.StructModel):
def __init__(self, dmm, fe_type):
members = [("lo", numba.types.float64), ("hi", numba.types.float64)]
super(IntervalModel, self).__init__(dmm, fe_type, members)
numba.extending.make_attribute_wrapper(IntervalType, "lo", "lo")
numba.extending.make_attribute_wrapper(IntervalType, "hi", "hi")
@numba.extending.lower_builtin(Interval, numba.types.Float, numba.types.Float)
def impl_interval(context, builder, sig, args):
typ = sig.return_type
lo, hi = args
interval = numba.cgutils.create_struct_proxy(typ)(context, builder)
interval.lo = lo
interval.hi = hi
return interval._getvalue()
@numba.extending.unbox(IntervalType)
def unbox_interval(typ, obj, c):
lo_obj = c.pyapi.object_getattr_string(obj, "lo")
hi_obj = c.pyapi.object_getattr_string(obj, "hi")
interval = numba.cgutils.create_struct_proxy(typ)(c.context, c.builder)
interval.lo = c.pyapi.float_as_double(lo_obj)
interval.hi = c.pyapi.float_as_double(hi_obj)
c.pyapi.decref(lo_obj)
c.pyapi.decref(hi_obj)
is_error = numba.cgutils.is_not_null(c.builder, c.pyapi.err_occurred())
return numba.extending.NativeValue(interval._getvalue(), is_error=is_error)
@numba.extending.box(IntervalType)
def box_interval(typ, val, c):
interval = numba.cgutils.create_struct_proxy(typ)(c.context, c.builder, value=val)
lo_obj = c.pyapi.float_from_double(interval.lo)
hi_obj = c.pyapi.float_from_double(interval.hi)
class_obj = c.pyapi.unserialize(c.pyapi.serialize_object(Interval))
res = c.pyapi.call_function_objargs(class_obj, (lo_obj, hi_obj))
c.pyapi.decref(lo_obj)
c.pyapi.decref(hi_obj)
c.pyapi.decref(class_obj)
return res
@numba.njit
def test1():
interval = Interval(4.4, 6.4)
return interval.hi
print(test1())
@numba.njit
def test2():
interval = Interval(4.4, 6.4)
interval.hi = 99.999
return interval.hi
print(test2())
@numba.extending.overload_method(IntervalType, "onlyget")
def interval_onlyget(interval, arg):
if isinstance(arg, numba.types.Float):
def onlyget_impl(interval, arg):
return interval.hi + arg
return onlyget_impl
@numba.njit
def test3():
interval = Interval(4.4, 6.4)
return interval.onlyget(3.14)
print(test3())
@numba.extending.overload_method(IntervalType, "alsoset")
def interval_alsoset(interval, arg):
if isinstance(arg, numba.types.Float):
def alsoset_impl(interval, arg):
interval.hi = interval.hi + arg
return interval.hi
return alsoset_impl
@numba.njit
def test4():
interval = Interval(4.4, 6.4)
return interval.alsoset(3.14)
print(test4())
LoweringError: No definition for lowering Interval.hi = float64
File "test2.py", line 79
[1] During: lowering "(interval).hi = $const0.5" at test2.py (79)
Failed at nopython (nopython mode backend)
No definition for lowering Interval.hi = float64
File "test2.py", line 79
[1] During: lowering "(interval).hi = $const0.5" at test2.py (79)
LoweringError: No definition for lowering Interval.hi = float64
File "test2.py", line 100
[1] During: lowering "(interval).hi = $0.4" at test2.py (100)
Failed at nopython (nopython mode backend)
No definition for lowering Interval.hi = float64
File "test2.py", line 100
[1] During: lowering "(interval).hi = $0.4" at test2.py (100)
[2] During: resolving callee type: BoundFunction((<class '__main__.IntervalType'>, 'alsoset') for Interval)
[3] During: typing of call at test2.py (107)
numba.extending.make_attribute_wrapper(IntervalType, "hi", "hi")
@numba.extending.infer_getattr
class StructAttribute(numba.typing.templates.AttributeTemplate):
key = IntervalType
def generic_resolve(self, typ, attr):
if attr == "hi":
return numba.types.float64
@numba.extending.lower_getattr(IntervalType, "hi")
def struct_getattr_impl(context, builder, typ, val):
val = numba.cgutils.create_struct_proxy(typ)(context, builder, value=val)
return numba.targets.imputils.impl_ret_borrowed(context, builder, numba.types.float64, val.hi)
@numba.extending.lower_setattr(IntervalType, "hi")
def struct_setattr_impl(context, builder, sig, args):
assert isinstance(sig.args[0], IntervalType)
assert isinstance(sig.args[1], numba.types.Float)
interval, newvalue = args
val = numba.cgutils.create_struct_proxy(sig.args[0])(context, builder, value=interval)
ptr = val._get_ptr_by_name("hi")
return builder.store(newvalue, ptr)
@numba.njit
def test2():
interval = Interval(4.4, 6.4)
interval.hi = 99.999 # this line!
return interval.hi
define i32 @"__main__.test2$2."(double* noalias nocapture %retptr, { i8*, i32 }** noalias nocapture readnone %excinfo, i8* noalias nocapture readnone %env) #0 {
entry:
store double 6.400000e+00, double* %retptr, align 8
ret i32 0
}
Traceback (most recent call last):
File "test2.py", line 81, in <module>
print(test2())
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/
dispatcher.py", line 286, in _compile_for_args
return self.compile(tuple(argtypes))
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/
dispatcher.py", line 532, in compile
cres = self._compiler.compile(args, return_type)
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/
dispatcher.py", line 81, in compile
flags=flags, locals=self.locals)
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/
compiler.py", line 693, in compile_extra
return pipeline.compile_extra(func)
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/
compiler.py", line 350, in compile_extra
return self._compile_bytecode()
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/
compiler.py", line 658, in _compile_bytecode
return self._compile_core()
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/
compiler.py", line 645, in _compile_core
res = pm.run(self.status)
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/
compiler.py", line 236, in run
raise patched_exception
numba.errors.LoweringError: Caused By:
Traceback (most recent call last):
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/compiler.py", line 228, in run
stage()
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/compiler.py", line 583, in stage_nopython_backend
self._backend(lowerfn, objectmode=False)
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/compiler.py", line 538, in _backend
lowered = lowerfn()
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/compiler.py", line 525, in backend_nopython_mode
self.flags)
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/compiler.py", line 811, in native_lowering_stage
lower.lower()
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/lowering.py", line 122, in lower
self.lower_normal_function(self.fndesc)
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/lowering.py", line 157, in lower_normal_function
entry_block_tail = self.lower_function_body()
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/lowering.py", line 182, in lower_function_body
self.lower_block(block)
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/lowering.py", line 197, in lower_block
self.lower_inst(inst)
File "/usr/lib/python2.7/contextlib.py", line 35, in __exit__
self.gen.throw(type, value, traceback)
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/errors.py", line 249, in new_error_context
six.reraise(type(newerr), newerr, sys.exc_info()[2])
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/errors.py", line 243, in new_error_context
yield
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/lowering.py", line 197, in lower_block
self.lower_inst(inst)
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/lowering.py", line 327, in lower_inst
impl = self.context.get_setattr(inst.attr, signature)
File "/home/pivarski/.local/lib/python2.7/site-packages/numba/targets/base.py", line 576, in get_setattr
% (typ, attr, valty))
LoweringError: No definition for lowering Interval.hi = float64