本文整理汇总了Python中numba.compiler.compile_isolated函数的典型用法代码示例。如果您正苦于以下问题:Python compile_isolated函数的具体用法?Python compile_isolated怎么用?Python compile_isolated使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了compile_isolated函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: _test_compare
def _test_compare(self, pyfunc):
def eq(pyfunc, cfunc, args):
self.assertIs(cfunc(*args), pyfunc(*args),
"mismatch for arguments %s" % (args,))
# Same-sized tuples
argtypes = [types.Tuple((types.int64, types.float32)),
types.UniTuple(types.int32, 2)]
for ta, tb in itertools.product(argtypes, argtypes):
cr = compile_isolated(pyfunc, (ta, tb))
cfunc = cr.entry_point
for args in [((4, 5), (4, 5)),
((4, 5), (4, 6)),
((4, 6), (4, 5)),
((4, 5), (5, 4))]:
eq(pyfunc, cfunc, args)
# Different-sized tuples
argtypes = [types.Tuple((types.int64, types.float32)),
types.UniTuple(types.int32, 3)]
cr = compile_isolated(pyfunc, tuple(argtypes))
cfunc = cr.entry_point
for args in [((4, 5), (4, 5, 6)),
((4, 5), (4, 4, 6)),
((4, 5), (4, 6, 7))]:
eq(pyfunc, cfunc, args)
开发者ID:denfromufa,项目名称:numba,代码行数:25,代码来源:test_tuples.py
示例2: test_2d_integer_indexing
def test_2d_integer_indexing(self, flags=enable_pyobj_flags,
pyfunc=integer_indexing_2d_usecase):
# C layout
a = np.arange(100, dtype='i4').reshape(10, 10)
arraytype = types.Array(types.int32, 2, 'C')
cr = compile_isolated(pyfunc, (arraytype, types.int32, types.int32),
flags=flags)
cfunc = cr.entry_point
self.assertEqual(pyfunc(a, 0, 0), cfunc(a, 0, 0))
self.assertEqual(pyfunc(a, 9, 9), cfunc(a, 9, 9))
self.assertEqual(pyfunc(a, -1, -1), cfunc(a, -1, -1))
# Any layout
a = np.arange(100, dtype='i4').reshape(10, 10)[::2, ::2]
self.assertFalse(a.flags['C_CONTIGUOUS'])
self.assertFalse(a.flags['F_CONTIGUOUS'])
arraytype = types.Array(types.int32, 2, 'A')
cr = compile_isolated(pyfunc, (arraytype, types.int32, types.int32),
flags=flags)
cfunc = cr.entry_point
self.assertEqual(pyfunc(a, 0, 0), cfunc(a, 0, 0))
self.assertEqual(pyfunc(a, 2, 2), cfunc(a, 2, 2))
self.assertEqual(pyfunc(a, -1, -1), cfunc(a, -1, -1))
开发者ID:hargup,项目名称:numba,代码行数:26,代码来源:test_indexing.py
示例3: test_unknown_function
def test_unknown_function(self):
try:
compile_isolated(foo, ())
except TypingError as e:
self.assertTrue(e.msg.startswith("Untyped global name"), e.msg)
else:
self.fail("Should raise error")
开发者ID:Alexhuszagh,项目名称:numba,代码行数:7,代码来源:test_typingerror.py
示例4: test_unknown_function
def test_unknown_function(self):
try:
compile_isolated(foo, ())
except TypingError as e:
self.assertIn("Untyped global name 'what'", str(e))
else:
self.fail("Should raise error")
开发者ID:cpcloud,项目名称:numba,代码行数:7,代码来源:test_typingerror.py
示例5: test_unknown_attrs
def test_unknown_attrs(self):
try:
compile_isolated(bar, (types.int32,))
except TypingError as e:
self.assertIn("Unknown attribute 'a' of type int32", str(e))
else:
self.fail("Should raise error")
开发者ID:cpcloud,项目名称:numba,代码行数:7,代码来源:test_typingerror.py
示例6: test_1d_integer_indexing
def test_1d_integer_indexing(self, flags=enable_pyobj_flags):
# C layout
pyfunc = integer_indexing_1d_usecase
arraytype = types.Array(types.int32, 1, 'C')
cr = compile_isolated(pyfunc, (arraytype, types.int32), flags=flags)
cfunc = cr.entry_point
a = np.arange(10, dtype='i4')
self.assertEqual(pyfunc(a, 0), cfunc(a, 0))
self.assertEqual(pyfunc(a, 9), cfunc(a, 9))
self.assertEqual(pyfunc(a, -1), cfunc(a, -1))
# Any layout
arraytype = types.Array(types.int32, 1, 'A')
cr = compile_isolated(pyfunc, (arraytype, types.int32), flags=flags)
cfunc = cr.entry_point
a = np.arange(10, dtype='i4')[::2]
self.assertFalse(a.flags['C_CONTIGUOUS'])
self.assertFalse(a.flags['F_CONTIGUOUS'])
self.assertEqual(pyfunc(a, 0), cfunc(a, 0))
self.assertEqual(pyfunc(a, 2), cfunc(a, 2))
self.assertEqual(pyfunc(a, -1), cfunc(a, -1))
# Using a 0-d array as integer index
arraytype = types.Array(types.int32, 1, 'C')
indextype = types.Array(types.int16, 0, 'C')
cr = compile_isolated(pyfunc, (arraytype, indextype), flags=flags)
cfunc = cr.entry_point
a = np.arange(3, 13, dtype=np.int32)
for i in (0, 9, -2):
idx = np.array(i).astype(np.int16)
assert idx.ndim == 0
self.assertEqual(pyfunc(a, idx), cfunc(a, idx))
开发者ID:FedericoStra,项目名称:numba,代码行数:35,代码来源:test_indexing.py
示例7: check_argument_cleanup
def check_argument_cleanup(self, typ, obj):
"""
Check that argument cleanup doesn't leak references.
"""
def f(x, y):
pass
def _objects(obj):
objs = [obj]
if isinstance(obj, tuple):
for v in obj:
objs += _objects(v)
return objs
objects = _objects(obj)
cres = compile_isolated(f, (typ, types.uint32))
with self.assertRefCount(*objects):
cres.entry_point(obj, 1)
with self.assertRefCount(*objects):
with self.assertRaises(OverflowError):
cres.entry_point(obj, -1)
cres = compile_isolated(f, (types.uint32, typ))
with self.assertRefCount(*objects):
cres.entry_point(1, obj)
with self.assertRefCount(*objects):
with self.assertRaises(OverflowError):
cres.entry_point(-1, obj)
开发者ID:Alexhuszagh,项目名称:numba,代码行数:29,代码来源:test_conversion.py
示例8: test_index
def test_index(self):
pyfunc = tuple_index
cr = compile_isolated(pyfunc,
[types.UniTuple(types.int64, 3), types.int64])
tup = (4, 3, 6)
for i in range(len(tup)):
self.assertPreciseEqual(cr.entry_point(tup, i), tup[i])
# Test empty tuple
cr = compile_isolated(pyfunc,
[types.UniTuple(types.int64, 0), types.int64])
with self.assertRaises(IndexError) as raises:
cr.entry_point((), 0)
self.assertEqual("tuple index out of range", str(raises.exception))
# With a compile-time static index (the code generation path is different)
pyfunc = tuple_index_static
for typ in (types.UniTuple(types.int64, 4),
types.Tuple((types.int64, types.int32, types.int64, types.int32))):
cr = compile_isolated(pyfunc, (typ,))
tup = (4, 3, 42, 6)
self.assertPreciseEqual(cr.entry_point(tup), pyfunc(tup))
typ = types.UniTuple(types.int64, 1)
with self.assertTypingError():
cr = compile_isolated(pyfunc, (typ,))
开发者ID:cpcloud,项目名称:numba,代码行数:26,代码来源:test_tuples.py
示例9: test_unknown_attrs
def test_unknown_attrs(self):
try:
compile_isolated(bar, (types.int32,))
except TypingError as e:
self.assertTrue(e.msg.startswith("Unknown attribute"), e.msg)
else:
self.fail("Should raise error")
开发者ID:Alexhuszagh,项目名称:numba,代码行数:7,代码来源:test_typingerror.py
示例10: test_complex2
def test_complex2(self):
pyfunc = docomplex2
x_types = [
types.int32, types.int64, types.float32, types.float64
]
x_values = [1, 1000, 12.2, 23.4]
y_values = [x - 3 for x in x_values]
for ty, x, y in zip(x_types, x_values, y_values):
cres = compile_isolated(pyfunc, [ty, ty])
cfunc = cres.entry_point
self.assertPreciseEqual(pyfunc(x, y), cfunc(x, y),
prec='single' if ty is types.float32 else 'exact')
# Check that complex(float32, float32) really creates a complex64,
# by checking the accuracy of computations.
pyfunc = complex_calc2
x = 1.0 + 2**-50
cres = compile_isolated(pyfunc, [types.float32, types.float32])
cfunc = cres.entry_point
self.assertPreciseEqual(cfunc(x, x), 2.0)
# Control (complex128)
cres = compile_isolated(pyfunc, [types.float64, types.float32])
cfunc = cres.entry_point
self.assertGreater(cfunc(x, x), 2.0)
开发者ID:maartenscholl,项目名称:numba,代码行数:26,代码来源:test_numberctor.py
示例11: test_try_except_raises
def test_try_except_raises(self):
pyfunc = try_except_usecase
for f in [no_pyobj_flags, enable_pyobj_flags]:
with self.assertRaises(errors.UnsupportedError) as e:
compile_isolated(pyfunc, (), flags=f)
msg = "Use of unsupported opcode (SETUP_EXCEPT) found"
self.assertIn(msg, str(e.exception))
开发者ID:numba,项目名称:numba,代码行数:7,代码来源:test_flow_control.py
示例12: test_2d_slicing3
def test_2d_slicing3(self, flags=enable_pyobj_flags):
"""
arr_2d[a:b:c, d]
"""
# C layout
pyfunc = slicing_2d_usecase3
arraytype = types.Array(types.int32, 2, "C")
argtys = (arraytype, types.int32, types.int32, types.int32, types.int32)
cr = compile_isolated(pyfunc, argtys, flags=flags)
cfunc = cr.entry_point
a = np.arange(100, dtype="i4").reshape(10, 10)
args = [(0, 10, 1, 0), (2, 3, 1, 1), (10, 0, -1, 8), (9, 0, -2, 4), (0, 10, 2, 3), (0, -1, 3, 1)]
for arg in args:
expected = pyfunc(a, *arg)
self.assertPreciseEqual(cfunc(a, *arg), expected)
# Any layout
arraytype = types.Array(types.int32, 2, "A")
argtys = (arraytype, types.int32, types.int32, types.int32, types.int32)
cr = compile_isolated(pyfunc, argtys, flags=flags)
cfunc = cr.entry_point
a = np.arange(400, dtype="i4").reshape(20, 20)[::2, ::2]
for arg in args:
expected = pyfunc(a, *arg)
self.assertPreciseEqual(cfunc(a, *arg), expected)
开发者ID:maartenscholl,项目名称:numba,代码行数:29,代码来源:test_indexing.py
示例13: test_random_randrange
def test_random_randrange(self):
for tp, max_width in [(types.int64, 2**63), (types.int32, 2**31)]:
cr1 = compile_isolated(random_randrange1, (tp,))
cr2 = compile_isolated(random_randrange2, (tp, tp))
cr3 = compile_isolated(random_randrange3, (tp, tp, tp))
self._check_randrange(cr1.entry_point, cr2.entry_point,
cr3.entry_point, py_state_ptr, max_width)
开发者ID:denfromufa,项目名称:numba,代码行数:7,代码来源:test_random.py
示例14: test_2d_slicing3
def test_2d_slicing3(self, flags=enable_pyobj_flags):
# C layout
pyfunc = slicing_2d_usecase3
arraytype = types.Array(types.int32, 2, 'C')
argtys = (arraytype, types.int32, types.int32, types.int32,
types.int32)
cr = compile_isolated(pyfunc, argtys, flags=flags)
cfunc = cr.entry_point
a = np.arange(100, dtype='i4').reshape(10, 10)
args = [
(0, 10, 1, 0),
(2, 3, 1, 2),
(10, 0, 1, 9),
(0, 10, -1, 0),
(0, 10, 2, 4),
]
for arg in args:
self.assertEqual(pyfunc(a, *arg), cfunc(a, *arg))
# Any layout
arraytype = types.Array(types.int32, 2, 'A')
argtys = (arraytype, types.int32, types.int32, types.int32,
types.int32)
cr = compile_isolated(pyfunc, argtys, flags=flags)
cfunc = cr.entry_point
a = np.arange(400, dtype='i4').reshape(20, 20)[::2, ::2]
for arg in args:
self.assertEqual(pyfunc(a, *arg), cfunc(a, *arg))
开发者ID:hargup,项目名称:numba,代码行数:32,代码来源:test_indexing.py
示例15: test_func1_isolated
def test_func1_isolated(self):
pyfunc = call_func1_nullary
cr = compile_isolated(pyfunc, ())
self.assertPreciseEqual(cr.entry_point(), 42)
pyfunc = call_func1_unary
cr = compile_isolated(pyfunc, (types.float64,))
self.assertPreciseEqual(cr.entry_point(18.0), 6.0)
开发者ID:esc,项目名称:numba,代码行数:7,代码来源:test_extending.py
示例16: test_1d_slicing5
def test_1d_slicing5(self, flags=enable_pyobj_flags):
pyfunc = slicing_1d_usecase5
arraytype = types.Array(types.int32, 1, 'C')
argtys = (arraytype, types.int32)
cr = compile_isolated(pyfunc, argtys, flags=flags)
cfunc = cr.entry_point
a = np.arange(10, dtype='i4')
args = [3, 2, 10, 0, 5]
for arg in args:
self.assertEqual(pyfunc(a, arg), cfunc(a, arg))
# Any
arraytype = types.Array(types.int32, 1, 'A')
argtys = (arraytype, types.int32)
cr = compile_isolated(pyfunc, argtys, flags=flags)
cfunc = cr.entry_point
a = np.arange(20, dtype='i4')[::2]
self.assertFalse(a.flags['C_CONTIGUOUS'])
self.assertFalse(a.flags['F_CONTIGUOUS'])
for arg in args:
self.assertEqual(pyfunc(a, arg), cfunc(a, arg))
开发者ID:hargup,项目名称:numba,代码行数:26,代码来源:test_indexing.py
示例17: test_2d_slicing
def test_2d_slicing(self, flags=enable_pyobj_flags):
pyfunc = slicing_1d_usecase
arraytype = types.Array(types.int32, 2, 'C')
argtys = (arraytype, types.int32, types.int32, types.int32)
cr = compile_isolated(pyfunc, argtys, flags=flags)
cfunc = cr.entry_point
a = np.arange(100, dtype='i4').reshape(10, 10)
self.assertTrue((pyfunc(a, 0, 10, 1) == cfunc(a, 0, 10, 1)).all())
self.assertTrue((pyfunc(a, 2, 3, 1) == cfunc(a, 2, 3, 1)).all())
self.assertTrue((pyfunc(a, 10, 0, 1) == cfunc(a, 10, 0, 1)).all())
self.assertTrue((pyfunc(a, 0, 10, -1) == cfunc(a, 0, 10, -1)).all())
self.assertTrue((pyfunc(a, 0, 10, 2) == cfunc(a, 0, 10, 2)).all())
pyfunc = slicing_2d_usecase
arraytype = types.Array(types.int32, 2, 'C')
argtys = (arraytype, types.int32, types.int32, types.int32,
types.int32, types.int32, types.int32)
cr = compile_isolated(pyfunc, argtys, flags=flags)
cfunc = cr.entry_point
self.assertTrue((pyfunc(a, 0, 10, 1, 0, 10, 1) ==
cfunc(a, 0, 10, 1, 0, 10, 1)).all())
self.assertTrue((pyfunc(a, 2, 3, 1, 2, 3, 1) ==
cfunc(a, 2, 3, 1, 2, 3, 1)).all())
self.assertTrue((pyfunc(a, 10, 0, 1, 10, 0, 1) ==
cfunc(a, 10, 0, 1, 10, 0, 1)).all())
self.assertTrue((pyfunc(a, 0, 10, -1, 0, 10, -1) ==
cfunc(a, 0, 10, -1, 0, 10, -1)).all())
self.assertTrue((pyfunc(a, 0, 10, 2, 0, 10, 2) ==
cfunc(a, 0, 10, 2, 0, 10, 2)).all())
开发者ID:hargup,项目名称:numba,代码行数:31,代码来源:test_indexing.py
示例18: check_argument_cleanup
def check_argument_cleanup(self, typ, obj):
"""
Check that argument cleanup doesn't leak references.
"""
def f(x, y):
pass
# The exception raised when passing a negative number
# to PyLong_AsUnsignedLongLong
exc_type = OverflowError if sys.version_info >= (2, 7) else TypeError
def _refcounts(obj):
refs = [sys.getrefcount(obj)]
if isinstance(obj, tuple):
refs += [_refcounts(v) for v in obj]
return refs
cres = compile_isolated(f, (typ, types.uint32))
old_refcnt = _refcounts(obj)
cres.entry_point(obj, 1)
self.assertEqual(_refcounts(obj), old_refcnt)
with self.assertRaises(exc_type):
cres.entry_point(obj, -1)
self.assertEqual(_refcounts(obj), old_refcnt)
cres = compile_isolated(f, (types.uint32, typ))
old_refcnt = _refcounts(obj)
cres.entry_point(1, obj)
self.assertEqual(_refcounts(obj), old_refcnt)
with self.assertRaises(exc_type):
cres.entry_point(-1, obj)
self.assertEqual(_refcounts(obj), old_refcnt)
开发者ID:PierreBizouard,项目名称:numba,代码行数:31,代码来源:test_conversion.py
示例19: check_round_scalar
def check_round_scalar(self, unary_pyfunc, binary_pyfunc):
base_values = [-3.0, -2.5, -2.25, -1.5, 1.5, 2.25, 2.5, 2.75]
complex_values = [x * (1 - 1j) for x in base_values]
int_values = [int(x) for x in base_values]
argtypes = (types.float64, types.float32, types.int32,
types.complex64, types.complex128)
argvalues = [base_values, base_values, int_values,
complex_values, complex_values]
pyfunc = binary_pyfunc
for ty, values in zip(argtypes, argvalues):
cres = compile_isolated(pyfunc, (ty, types.int32))
cfunc = cres.entry_point
for decimals in (1, 0, -1):
for v in values:
if decimals > 0:
v *= 10
expected = _fixed_np_round(v, decimals)
got = cfunc(v, decimals)
self.assertPreciseEqual(got, expected)
pyfunc = unary_pyfunc
for ty, values in zip(argtypes, argvalues):
cres = compile_isolated(pyfunc, (ty,))
cfunc = cres.entry_point
for v in values:
expected = _fixed_np_round(v)
got = cfunc(v)
self.assertPreciseEqual(got, expected)
开发者ID:hajs,项目名称:numba,代码行数:29,代码来源:test_array_methods.py
示例20: test_array_expr
def test_array_expr(self):
flags = Flags()
flags.set("enable_pyobject")
global cnd_array_jitted
scalty = types.float64
arrty = types.Array(scalty, 1, 'C')
cr1 = compile_isolated(cnd_array, args=(arrty,), flags=flags)
cnd_array_jitted = cr1.entry_point
cr2 = compile_isolated(blackscholes_arrayexpr_jitted,
args=(arrty, arrty, arrty, scalty, scalty),
flags=flags)
jitted_bs = cr2.entry_point
OPT_N = 400
iterations = 10
stockPrice = randfloat(self.random.random_sample(OPT_N), 5.0, 30.0)
optionStrike = randfloat(self.random.random_sample(OPT_N), 1.0, 100.0)
optionYears = randfloat(self.random.random_sample(OPT_N), 0.25, 10.0)
args = stockPrice, optionStrike, optionYears, RISKFREE, VOLATILITY
callResultGold, putResultGold = blackscholes_arrayexpr(*args)
callResultNumba, putResultNumba = jitted_bs(*args)
delta = np.abs(callResultGold - callResultNumba)
L1norm = delta.sum() / np.abs(callResultGold).sum()
print("L1 norm: %E" % L1norm)
print("Max absolute error: %E" % delta.max())
self.assertEqual(delta.max(), 0)
开发者ID:FedericoStra,项目名称:numba,代码行数:32,代码来源:test_blackscholes.py
注:本文中的numba.compiler.compile_isolated函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论