本文整理汇总了Python中numba.cgutils.unpack_tuple函数的典型用法代码示例。如果您正苦于以下问题:Python unpack_tuple函数的具体用法?Python unpack_tuple怎么用?Python unpack_tuple使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了unpack_tuple函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: init_specific
def init_specific(self, context, builder, arrty, arr):
zero = context.get_constant(types.intp, 0)
data = arr.data
ndim = arrty.ndim
shapes = cgutils.unpack_tuple(builder, arr.shape, ndim)
indices = cgutils.alloca_once(builder, zero.type,
size=context.get_constant(types.intp,
arrty.ndim))
pointers = cgutils.alloca_once(builder, data.type,
size=context.get_constant(types.intp,
arrty.ndim))
strides = cgutils.unpack_tuple(builder, arr.strides, ndim)
exhausted = cgutils.alloca_once_value(builder, cgutils.false_byte)
# Initialize indices and pointers with their start values.
for dim in range(ndim):
idxptr = cgutils.gep(builder, indices, dim)
ptrptr = cgutils.gep(builder, pointers, dim)
builder.store(data, ptrptr)
builder.store(zero, idxptr)
# 0-sized dimensions really indicate an empty array,
# but we have to catch that condition early to avoid
# a bug inside the iteration logic (see issue #846).
dim_size = shapes[dim]
dim_is_empty = builder.icmp(lc.ICMP_EQ, dim_size, zero)
with cgutils.if_unlikely(builder, dim_is_empty):
builder.store(cgutils.true_byte, exhausted)
self.indices = indices
self.pointers = pointers
self.exhausted = exhausted
示例2: getitem_arraynd_intp
def getitem_arraynd_intp(context, builder, sig, args):
aryty, idxty = sig.args
ary, idx = args
arystty = make_array(aryty)
adapted_ary = arystty(context, builder, ary)
ndim = aryty.ndim
if ndim == 1:
result = _getitem_array1d(context, builder, aryty, adapted_ary, idx,
wraparound=idxty.signed)
elif ndim > 1:
out_ary_ty = make_array(aryty.copy(ndim = ndim - 1))
out_ary = out_ary_ty(context, builder)
in_shapes = cgutils.unpack_tuple(builder, adapted_ary.shape, count=ndim)
in_strides = cgutils.unpack_tuple(builder, adapted_ary.strides,
count=ndim)
data_p = cgutils.get_item_pointer2(builder, adapted_ary.data, in_shapes,
in_strides, aryty.layout, [idx],
wraparound=idxty.signed)
populate_array(out_ary,
data=data_p,
shape=cgutils.pack_array(builder, in_shapes[1:]),
strides=cgutils.pack_array(builder, in_strides[1:]),
itemsize=adapted_ary.itemsize,
parent=adapted_ary.parent,)
result = out_ary._getvalue()
else:
raise NotImplementedError("1D indexing into %dD array" % aryty.ndim)
return result
示例3: dot_3_vm
def dot_3_vm(context, builder, sig, args):
"""
np.dot(vector, matrix, out)
np.dot(matrix, vector, out)
"""
xty, yty, outty = sig.args
assert outty == sig.return_type
dtype = xty.dtype
x = make_array(xty)(context, builder, args[0])
y = make_array(yty)(context, builder, args[1])
out = make_array(outty)(context, builder, args[2])
x_shapes = cgutils.unpack_tuple(builder, x.shape)
y_shapes = cgutils.unpack_tuple(builder, y.shape)
out_shapes = cgutils.unpack_tuple(builder, out.shape)
if xty.ndim < yty.ndim:
# Vector * matrix
# Asked for x * y, we will compute y.T * x
mty = yty
m_shapes = y_shapes
do_trans = yty.layout == 'F'
m_data, v_data = y.data, x.data
def check_args(a, b, out):
m, = a.shape
_m, n = b.shape
if m != _m:
raise ValueError("incompatible array sizes for "
"np.dot(a, b) (vector * matrix)")
if out.shape != (n,):
raise ValueError("incompatible output array size for "
"np.dot(a, b, out) (vector * matrix)")
else:
# Matrix * vector
# We will compute x * y
mty = xty
m_shapes = x_shapes
do_trans = xty.layout == 'C'
m_data, v_data = x.data, y.data
def check_args(a, b, out):
m, _n = a.shape
n, = b.shape
if n != _n:
raise ValueError("incompatible array sizes for np.dot(a, b) "
"(matrix * vector)")
if out.shape != (m,):
raise ValueError("incompatible output array size for "
"np.dot(a, b, out) (matrix * vector)")
context.compile_internal(builder, check_args,
signature(types.none, *sig.args), args)
for val in m_shapes:
check_c_int(context, builder, val)
call_xxgemv(context, builder, do_trans, mty, m_shapes, m_data,
v_data, out.data)
return impl_ret_borrowed(context, builder, sig.return_type,
out._getvalue())
示例4: impl
def impl(context, builder, sig, args):
[tyinp, tyout] = sig.args
[inp, out] = args
if scalar_input:
ndim = 1
else:
ndim = tyinp.ndim
if not scalar_input:
iary = context.make_array(tyinp)(context, builder, inp)
oary = context.make_array(tyout)(context, builder, out)
if asfloat:
sig = typing.signature(types.float64, types.float64)
else:
if scalar_input:
sig = typing.signature(tyout.dtype, tyinp)
else:
sig = typing.signature(tyout.dtype, tyinp.dtype)
fnwork = context.get_function(funckey, sig)
intpty = context.get_value_type(types.intp)
# TODO handle differing shape by mimicking broadcasting
if scalar_input:
shape = cgutils.unpack_tuple(builder, oary.shape, ndim)
else:
shape = cgutils.unpack_tuple(builder, iary.shape, ndim)
with cgutils.loop_nest(builder, shape, intp=intpty) as indices:
if not scalar_input:
pi = cgutils.get_item_pointer(builder, tyinp, iary, indices)
po = cgutils.get_item_pointer(builder, tyout, oary, indices)
if scalar_input:
ival = inp
else:
ival = builder.load(pi)
if asfloat:
if scalar_input:
dval = context.cast(builder, ival, tyinp, types.float64)
else:
dval = context.cast(builder, ival, tyinp.dtype, types.float64)
dres = fnwork(builder, [dval])
res = context.cast(builder, dres, types.float64, tyout.dtype)
elif scalar_input and tyinp != tyout:
tempres = fnwork(builder, [ival])
res = context.cast(builder, tempres, tyinp, tyout.dtype)
elif tyinp.dtype != tyout.dtype:
tempres = fnwork(builder, [ival])
res = context.cast(builder, tempres, tyinp.dtype, tyout.dtype)
else:
res = fnwork(builder, [ival])
builder.store(res, po)
return out
示例5: array_nonzero
def array_nonzero(context, builder, sig, args):
aryty = sig.args[0]
# Return type is a N-tuple of 1D C-contiguous arrays
retty = sig.return_type
outaryty = retty.dtype
ndim = aryty.ndim
nouts = retty.count
ary = make_array(aryty)(context, builder, args[0])
shape = cgutils.unpack_tuple(builder, ary.shape)
strides = cgutils.unpack_tuple(builder, ary.strides)
data = ary.data
layout = aryty.layout
# First count the number of non-zero elements
zero = context.get_constant(types.intp, 0)
one = context.get_constant(types.intp, 1)
count = cgutils.alloca_once_value(builder, zero)
with cgutils.loop_nest(builder, shape, zero.type) as indices:
ptr = cgutils.get_item_pointer2(builder, data, shape, strides,
layout, indices)
val = load_item(context, builder, aryty, ptr)
nz = context.is_true(builder, aryty.dtype, val)
with builder.if_then(nz):
builder.store(builder.add(builder.load(count), one), count)
# Then allocate output arrays of the right size
out_shape = (builder.load(count),)
outs = [_empty_nd_impl(context, builder, outaryty, out_shape)._getvalue()
for i in range(nouts)]
outarys = [make_array(outaryty)(context, builder, out) for out in outs]
out_datas = [out.data for out in outarys]
# And fill them up
index = cgutils.alloca_once_value(builder, zero)
with cgutils.loop_nest(builder, shape, zero.type) as indices:
ptr = cgutils.get_item_pointer2(builder, data, shape, strides,
layout, indices)
val = load_item(context, builder, aryty, ptr)
nz = context.is_true(builder, aryty.dtype, val)
with builder.if_then(nz):
# Store element indices in output arrays
if not indices:
# For a 0-d array, store 0 in the unique output array
indices = (zero,)
cur = builder.load(index)
for i in range(nouts):
ptr = cgutils.get_item_pointer2(builder, out_datas[i],
out_shape, (),
'C', [cur])
store_item(context, builder, outaryty, indices[i], ptr)
builder.store(builder.add(cur, one), index)
tup = context.make_tuple(builder, sig.return_type, outs)
return impl_ret_new_ref(context, builder, sig.return_type, tup)
示例6: getitem_array_tuple
def getitem_array_tuple(context, builder, sig, args):
aryty, idxty = sig.args
ary, idx = args
arystty = make_array(aryty)
ary = arystty(context, builder, ary)
ndim = aryty.ndim
if isinstance(sig.return_type, types.Array):
# Slicing
raw_indices = cgutils.unpack_tuple(builder, idx, aryty.ndim)
start = []
shapes = []
strides = []
oshapes = cgutils.unpack_tuple(builder, ary.shape, ndim)
for ax, (indexval, idxty) in enumerate(zip(raw_indices, idxty)):
if idxty == types.slice3_type:
slice = Slice(context, builder, value=indexval)
cgutils.normalize_slice(builder, slice, oshapes[ax])
start.append(slice.start)
shapes.append(cgutils.get_range_from_slice(builder, slice))
strides.append(cgutils.get_strides_from_slice(builder, ndim,
ary.strides,
slice, ax))
else:
ind = context.cast(builder, indexval, idxty, types.intp)
start.append(ind)
dataptr = cgutils.get_item_pointer(builder, aryty, ary, start,
wraparound=True)
# Build array
retstty = make_array(sig.return_type)
retary = retstty(context, builder)
populate_array(retary,
data=dataptr,
shape=cgutils.pack_array(builder, shapes),
strides=cgutils.pack_array(builder, strides),
itemsize=ary.itemsize,
meminfo=ary.meminfo,
parent=ary.parent)
return retary._getvalue()
else:
# Indexing
indices = cgutils.unpack_tuple(builder, idx, count=len(idxty))
indices = [context.cast(builder, i, t, types.intp)
for t, i in zip(idxty, indices)]
ptr = cgutils.get_item_pointer(builder, aryty, ary, indices,
wraparound=True)
return context.unpack_value(builder, aryty.dtype, ptr)
示例7: _define_atomic_cas
def _define_atomic_cas(module, ordering):
"""Define a llvm function for atomic compare-and-swap.
The generated function is a direct wrapper of the LLVM cmpxchg with the
difference that the a int indicate success (1) or failure (0) is returned
and the last argument is a output pointer for storing the old value.
Note
----
On failure, the generated function behaves like an atomic load. The loaded
value is stored to the last argument.
"""
ftype = ir.FunctionType(ir.IntType(32), [_word_type.as_pointer(),
_word_type, _word_type,
_word_type.as_pointer()])
fn_cas = ir.Function(module, ftype, name="nrt_atomic_cas")
[ptr, cmp, repl, oldptr] = fn_cas.args
bb = fn_cas.append_basic_block()
builder = ir.IRBuilder(bb)
outtup = builder.cmpxchg(ptr, cmp, repl, ordering=ordering)
old, ok = cgutils.unpack_tuple(builder, outtup, 2)
builder.store(old, oldptr)
builder.ret(builder.zext(ok, ftype.return_type))
return fn_cas
示例8: _box_class_instance
def _box_class_instance(typ, val, c):
meminfo, dataptr = cgutils.unpack_tuple(c.builder, val)
# Create Box instance
box_subclassed = _specialize_box(typ)
# Note: the ``box_subclassed`` is kept alive by the cache
int_addr_boxcls = c.context.get_constant(types.uintp, id(box_subclassed))
box_cls = c.builder.inttoptr(int_addr_boxcls, c.pyapi.pyobj)
box = c.pyapi.call_function_objargs(box_cls, ())
# Initialize Box instance
llvoidptr = ir.IntType(8).as_pointer()
addr_meminfo = c.builder.bitcast(meminfo, llvoidptr)
addr_data = c.builder.bitcast(dataptr, llvoidptr)
def set_member(member_offset, value):
# Access member by byte offset
offset = c.context.get_constant(types.uintp, member_offset)
ptr = cgutils.pointer_add(c.builder, box, offset)
casted = c.builder.bitcast(ptr, llvoidptr.as_pointer())
c.builder.store(value, casted)
set_member(_box.box_meminfoptr_offset, addr_meminfo)
set_member(_box.box_dataptr_offset, addr_data)
return box
示例9: ptx_atomic_add_tuple
def ptx_atomic_add_tuple(context, builder, sig, args):
aryty, indty, valty = sig.args
ary, inds, val = args
dtype = aryty.dtype
indices = cgutils.unpack_tuple(builder, inds, count=len(indty))
indices = [context.cast(builder, i, t, types.intp) for t, i in zip(indty, indices)]
if dtype != valty:
raise TypeError("expect %s but got %s" % (dtype, valty))
if aryty.ndim != len(indty):
raise TypeError("indexing %d-D array with %d-D index" % (aryty.ndim, len(indty)))
lary = context.make_array(aryty)(context, builder, ary)
ptr = cgutils.get_item_pointer(builder, aryty, lary, indices)
if aryty.dtype == types.float32:
lmod = builder.module
return builder.call(nvvmutils.declare_atomic_add_float32(lmod), (ptr, val))
elif aryty.dtype == types.float64:
lmod = builder.module
return builder.call(nvvmutils.declare_atomic_add_float64(lmod), (ptr, val))
else:
return builder.atomic_rmw("add", ptr, val, "monotonic")
示例10: _gauss_impl
def _gauss_impl(context, builder, sig, args, state):
# The type for all computations (either float or double)
ty = sig.return_type
llty = context.get_data_type(ty)
state_ptr = get_state_ptr(context, builder, state)
_random = {"py": random.random,
"np": np.random.random}[state]
ret = cgutils.alloca_once(builder, llty, name="result")
gauss_ptr = get_gauss_ptr(builder, state_ptr)
has_gauss_ptr = get_has_gauss_ptr(builder, state_ptr)
has_gauss = cgutils.is_true(builder, builder.load(has_gauss_ptr))
with builder.if_else(has_gauss) as (then, otherwise):
with then:
# if has_gauss: return it
builder.store(builder.load(gauss_ptr), ret)
builder.store(const_int(0), has_gauss_ptr)
with otherwise:
# if not has_gauss: compute a pair of numbers using the Box-Muller
# transform; keep one and return the other
pair = context.compile_internal(builder,
_gauss_pair_impl(_random),
signature(types.UniTuple(ty, 2)),
())
first, second = cgutils.unpack_tuple(builder, pair, 2)
builder.store(first, gauss_ptr)
builder.store(second, ret)
builder.store(const_int(1), has_gauss_ptr)
mu, sigma = args
return builder.fadd(mu,
builder.fmul(sigma, builder.load(ret)))
示例11: dot_2_vv
def dot_2_vv(context, builder, sig, args, conjugate=False):
"""
np.dot(vector, vector)
np.vdot(vector, vector)
"""
aty, bty = sig.args
dtype = sig.return_type
a = make_array(aty)(context, builder, args[0])
b = make_array(bty)(context, builder, args[1])
n, = cgutils.unpack_tuple(builder, a.shape)
def check_args(a, b):
m, = a.shape
n, = b.shape
if m != n:
raise ValueError("incompatible array sizes for np.dot(a, b) "
"(vector * vector)")
context.compile_internal(builder, check_args,
signature(types.none, *sig.args), args)
check_c_int(context, builder, n)
out = cgutils.alloca_once(builder, context.get_value_type(dtype))
call_xxdot(context, builder, conjugate, dtype, n, a.data, b.data, out)
return builder.load(out)
示例12: getitem_array1d
def getitem_array1d(context, builder, sig, args):
aryty, _ = sig.args
if aryty.ndim != 1:
# TODO
raise NotImplementedError("1D indexing into %dD array" % aryty.ndim)
ary, idx = args
arystty = make_array(aryty)
ary = arystty(context, builder, ary)
dataptr = ary.data
if True or WARPAROUND: # TODO target flag
ZERO = context.get_constant(types.intp, 0)
negative = builder.icmp(lc.ICMP_SLT, idx, ZERO)
bbnormal = builder.basic_block
with cgutils.if_unlikely(builder, negative):
# Index is negative, wraparound
[nelem] = cgutils.unpack_tuple(builder, ary.shape, 1)
wrapped = builder.add(nelem, idx)
bbwrapped = builder.basic_block
where = builder.phi(idx.type)
where.add_incoming(idx, bbnormal)
where.add_incoming(wrapped, bbwrapped)
ptr = builder.gep(dataptr, [where])
else:
# No wraparound
ptr = builder.gep(dataptr, [idx])
if context.is_struct_type(aryty.dtype):
return ptr
else:
return builder.load(ptr)
示例13: _box_class_instance
def _box_class_instance(typ, val, c):
meminfo, dataptr = cgutils.unpack_tuple(c.builder, val)
lluintp = c.context.get_data_type(types.uintp)
addr_meminfo = c.pyapi.from_native_value(types.uintp,
c.builder.ptrtoint(meminfo,
lluintp))
addr_dataptr = c.pyapi.from_native_value(types.uintp,
c.builder.ptrtoint(dataptr,
lluintp))
box_subclassed = _specialize_box(typ)
# Note: the ``box_subclassed`` is kept alive by the cache
int_addr_boxcls = c.context.get_constant(types.uintp, id(box_subclassed))
box_cls = c.builder.inttoptr(int_addr_boxcls, c.pyapi.pyobj)
args = [addr_meminfo, addr_dataptr]
res = c.pyapi.call_function_objargs(box_cls, args)
# Clean up
c.pyapi.decref(addr_meminfo)
c.pyapi.decref(addr_dataptr)
return res
示例14: array_nbytes
def array_nbytes(context, builder, typ, value):
"""
nbytes = size * itemsize
"""
arrayty = make_array(typ)
array = arrayty(context, builder, value)
dims = cgutils.unpack_tuple(builder, array.shape, typ.ndim)
return builder.mul(array.nitems, array.itemsize)
示例15: codegen
def codegen(context, builder, signature, args):
# check that the return type is now defined
arrty = signature.return_type
assert arrty.is_precise()
shapes = unpack_tuple(builder, args[0])
# redirect implementation to np.empty
res = _empty_nd_impl(context, builder, arrty, shapes)
return impl_ret_new_ref(context, builder, arrty, res._getvalue())