def gridsize_expand(ndim): """ Return the absolute size (or shape) in threads of the entire grid of blocks. *ndim* should correspond to the number of dimensions declared when instantiating the kernel. Computation of the first integer is as follows:: cuda.blockDim.x * cuda.gridDim.x and is similar for the other two indices, but using the ``y`` and ``z`` attributes. """ if ndim == 1: fname = "ptx.gridsize.1d" restype = types.int32 elif ndim == 2: fname = "ptx.gridsize.2d" restype = types.UniTuple(types.int32, 2) elif ndim == 3: fname = "ptx.gridsize.3d" restype = types.UniTuple(types.int32, 3) else: raise ValueError('argument can only be 1, 2 or 3') return ir.Intrinsic(fname, typing.signature(restype, types.intp), args=[ndim])
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)
def grid_expand(ndim): """grid(ndim) Return the absolute position of the current thread in the entire grid of blocks. *ndim* should correspond to the number of dimensions declared when instantiating the kernel. If *ndim* is 1, a single integer is returned. If *ndim* is 2 or 3, a tuple of the given number of integers is returned. Computation of the first integer is as follows:: cuda.threadIdx.x + cuda.blockIdx.x * cuda.blockDim.x and is similar for the other two indices, but using the ``y`` and ``z`` attributes. """ if ndim == 1: fname = "ptx.grid.1d" restype = types.int32 elif ndim == 2: fname = "ptx.grid.2d" restype = types.UniTuple(types.int32, 2) elif ndim == 3: fname = "ptx.grid.3d" restype = types.UniTuple(types.int32, 3) else: raise ValueError('argument can only be 1, 2, 3') return ir.Intrinsic(fname, typing.signature(restype, types.intp), args=[ndim])
def normalize_shape(shape): if isinstance(shape, types.UniTuple): if isinstance(shape.dtype, types.Integer): dimtype = types.intp if shape.dtype.signed else types.uintp return types.UniTuple(dimtype, len(shape)) elif isinstance(shape, types.Tuple) and shape.count == 0: # Force (0 x intp) for consistency with other shapes return types.UniTuple(types.intp, 0)
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 negative indexing for i in range(len(tup) + 1): self.assertPreciseEqual(cr.entry_point(tup, -i), tup[-i]) # oob indexes, +ve then -ve with self.assertRaises(IndexError) as raises: cr.entry_point(tup, len(tup)) self.assertEqual("tuple index out of range", str(raises.exception)) with self.assertRaises(IndexError) as raises: cr.entry_point(tup, -(len(tup) + 1)) self.assertEqual("tuple index out of range", str(raises.exception)) # 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)) # test uintp indexing (because, e.g., parfor generates unsigned prange) cr = compile_isolated(pyfunc, [types.UniTuple(types.int64, 3), types.uintp]) for i in range(len(tup)): self.assertPreciseEqual(cr.entry_point(tup, types.uintp(i)), tup[i]) # 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, )) # test unpack, staticgetitem with imprecise type (issue #3895) pyfunc = tuple_unpack_static_getitem_err with self.assertTypingError() as raises: cr = compile_isolated(pyfunc, ()) msg = ("Cannot infer the type of variable 'c', have imprecise type: " "list(undefined).") self.assertIn(msg, str(raises.exception))
def __init__(self, dmm, fe_type): ndim = fe_type.ndim members = [ ("meminfo", types.MemInfoPointer(fe_type.dtype)), ("parent", types.pyobject), ("nitems", types.intp), ("itemsize", types.intp), ("data", types.CPointer(fe_type.dtype)), ("shape", types.UniTuple(types.intp, ndim)), ("strides", types.UniTuple(types.intp, ndim)), ] super(ArrayModel, self).__init__(dmm, fe_type, members)
def test_iter_next(self, flags=enable_pyobj_flags): pyfunc = iter_next_usecase cr = compile_isolated(pyfunc, (types.UniTuple(types.int32, 3), ), flags=flags) cfunc = cr.entry_point self.assertPreciseEqual(cfunc((1, 42, 5)), (1, 42)) cr = compile_isolated(pyfunc, (types.UniTuple(types.int32, 1), ), flags=flags) cfunc = cr.entry_point with self.assertRaises(StopIteration): cfunc((1, ))
def __init__(self, dmm, fe_type): ndim = fe_type.ndim members = [ ('meminfo', types.MemInfoPointer(fe_type.dtype)), ('parent', types.pyobject), ('nitems', types.intp), ('itemsize', types.intp), ('data', types.CPointer(fe_type.dtype)), ('shape', types.UniTuple(types.intp, ndim)), ('strides', types.UniTuple(types.intp, ndim)), ] super(ArrayModel, self).__init__(dmm, fe_type, members)
def test_add(self): pyfunc = add_usecase samples = [(types.Tuple(()), ()), (types.UniTuple(types.int32, 0), ()), (types.UniTuple(types.int32, 1), (42,)), (types.Tuple((types.int64, types.float32)), (3, 4.5)), ] for (ta, a), (tb, b) in itertools.product(samples, samples): cr = compile_isolated(pyfunc, (ta, tb)) expected = pyfunc(a, b) got = cr.entry_point(a, b) self.assertPreciseEqual(got, expected, msg=(ta, tb))
def test_tuple_len(self): def impl(tup): if len(tup) == 3: if tup[2] == 2: return 1 else: return 0 self.assert_prune(impl, (types.UniTuple(types.int64, 3), ), [False, None], tuple([1, 2, 3])) self.assert_prune(impl, (types.UniTuple(types.int64, 2), ), [True, 'both'], tuple([1, 2]))
def test_conversions(self): check = self.check_conversion fromty = types.UniTuple(types.int32, 2) check(fromty, types.UniTuple(types.float32, 2), (4, 5)) check(fromty, types.Tuple((types.float32, types.int16)), (4, 5)) aty = types.UniTuple(types.int32, 0) bty = types.Tuple(()) check(aty, bty, ()) check(bty, aty, ()) with self.assertRaises(errors.TypingError) as raises: check(fromty, types.Tuple((types.float32, )), (4, 5)) msg = "No conversion from UniTuple(int32 x 2) to UniTuple(float32 x 1)" self.assertIn(msg, str(raises.exception))
def load_map(self, path): """Загрузка карты, позиций спрайтов и стен, необходимой информации.""" with open(path, encoding='utf-8', mode='r') as f: level = list(f.readlines()) level = [list(map(int, list(i.strip('\n')))) for i in level] width = len(level[0]) * TILE height = len(level) * TILE conj_dict = {} world_map = Dict.empty(key_type=types.UniTuple(int32, 2), value_type=int32) mini_map = set() collision_objects = [] notes_spawn = [] # notes_spawn = [(2, 2), (2, 2.2), (2, 2.4), (2, 2.6), (2, 2.8), (2, 3), (2, 3.2), (2, 3.4), (2, 1.8), ( 1, 3), (1, 4)] for j, row in enumerate(level): for i, char in enumerate(row): if char: mini_map.add((i * MAP_TILE, j * MAP_TILE)) collision_objects.append( pygame.Rect(i * TILE, j * TILE, TILE, TILE)) world_map[(i * TILE, j * TILE)] = char else: conj_dict[(i, j)] = self.find_new_nodes( i, j, level, width, height) if i != len(level[0]) - 1 and level[j][i + 1] in [2, 5]: notes_spawn.append((i, j)) # pass return conj_dict, world_map, collision_objects, mini_map, notes_spawn, width, height
def __init__(self, dmm, fe_type): ndim = fe_type.ndim members = [('shape', types.UniTuple(types.intp, ndim)), ('indices', types.EphemeralArray(types.intp, ndim)), ('exhausted', types.EphemeralPointer(types.boolean)), ] super(NdIndexModel, self).__init__(dmm, fe_type, members)
def resolve_nonzero(self, ary, args, kws): assert not args assert not kws # 0-dim arrays return one result array ndim = max(ary.ndim, 1) retty = types.UniTuple(types.Array(types.intp, 1, 'C'), ndim) return signature(retty)
def local_array(shape, dtype): shape = _legalize_shape(shape) ndim = len(shape) fname = "ptx.lmem.alloc" restype = types.Array(dtype, ndim, 'C') sig = typing.signature(restype, types.UniTuple(types.intp, ndim), types.Any) return ir.Intrinsic(fname, sig, args=(shape, dtype))
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)))
def build_full_slice_tuple(tyctx, sz): """Creates a sz-tuple of full slices.""" if not isinstance(sz, types.IntegerLiteral): raise errors.RequireLiteralValue(sz) size = int(sz.literal_value) tuple_type = types.UniTuple(dtype=types.slice2_type, count=size) sig = tuple_type(sz) def codegen(context, builder, signature, args): def impl(length, empty_tuple): out = empty_tuple for i in range(length): out = tuple_setitem(out, i, slice(None, None)) return out inner_argtypes = [types.intp, tuple_type] inner_sig = typing.signature(tuple_type, *inner_argtypes) ll_idx_type = context.get_value_type(types.intp) # Allocate an empty tuple empty_tuple = context.get_constant_undef(tuple_type) inner_args = [ll_idx_type(size), empty_tuple] res = context.compile_internal(builder, impl, inner_sig, inner_args) return res return sig, codegen
def test_empty_tuples(self): # Empty tuple fe_args = [ types.UniTuple(types.int16, 0), types.Tuple(()), types.int32 ] self._test_as_arguments(fe_args)
def generic(self, args, kws): assert not kws if len(args) == 1: # 0-dim arrays return one result array ary = args[0] ndim = max(ary.ndim, 1) retty = types.UniTuple(types.Array(types.intp, 1, 'C'), ndim) return signature(retty, ary) elif len(args) == 3: cond, x, y = args retdty = from_dtype( np.promote_types(as_dtype(getattr(args[1], 'dtype', args[1])), as_dtype(getattr(args[2], 'dtype', args[2])))) if isinstance(cond, types.Array): # array where() if isinstance(x, types.Array) and isinstance(y, types.Array): if (cond.ndim == x.ndim == y.ndim): if x.layout == y.layout == cond.layout: retty = types.Array(retdty, x.ndim, x.layout) else: retty = types.Array(retdty, x.ndim, 'C') return signature(retty, *args) else: # x and y both scalar retty = types.Array(retdty, cond.ndim, cond.layout) return signature(retty, *args) else: # scalar where() if not isinstance(x, types.Array): retty = types.Array(retdty, 0, 'C') return signature(retty, *args)
def test_1d_slicing_set_tuple(self, flags=enable_pyobj_flags): """ Tuple to 1d slice assignment """ self.check_1d_slicing_set_sequence(flags, types.UniTuple(types.int16, 2), (8, -42))
def blocked_doors(self): blocked_doors = Dict.empty(key_type=types.UniTuple(int32, 2), value_type=int32) for obj in self.list_of_objects: if obj.flag in {'door_h', 'door_v'} and obj.blocked: i, j = mapping(obj.x, obj.y) blocked_doors[(i, j)] = 0 return blocked_doors
def test_in(self): pyfunc = in_usecase cr = compile_isolated( pyfunc, [types.int64, types.UniTuple(types.int64, 3)]) tup = (4, 1, 5) for i in range(5): self.assertPreciseEqual(cr.entry_point(i, tup), pyfunc(i, tup))
def __init__(self, dmm, fe_type): array_types = fe_type.arrays ndim = fe_type.ndim shape_len = ndim if fe_type.need_shaped_indexing else 1 members = [ ('exhausted', types.EphemeralPointer(types.boolean)), ('arrays', types.Tuple(array_types)), # The iterator's main shape and indices ('shape', types.UniTuple(types.intp, shape_len)), ('indices', types.EphemeralArray(types.intp, shape_len)), ] # Indexing state for the various sub-iterators # XXX use a tuple instead? for i, sub in enumerate(fe_type.indexers): kind, start_dim, end_dim, _ = sub member_name = 'index%d' % i if kind == 'flat': # A single index into the flattened array members.append( (member_name, types.EphemeralPointer(types.intp))) elif kind in ('scalar', 'indexed', '0d'): # Nothing required pass else: assert 0 # Slots holding values of the scalar args # XXX use a tuple instead? for i, ty in enumerate(fe_type.arrays): if not isinstance(ty, types.Array): member_name = 'scalar%d' % i members.append((member_name, types.EphemeralPointer(ty))) super(NdIter, self).__init__(dmm, fe_type, members)
def test_len(self): pyfunc = len_usecase cr = compile_isolated(pyfunc, [types.Tuple((types.int64, types.float32))]) self.assertPreciseEqual(cr.entry_point((4, 5)), 2) cr = compile_isolated(pyfunc, [types.UniTuple(types.int64, 3)]) self.assertPreciseEqual(cr.entry_point((4, 5, 6)), 3)
def test_tuples(self): v = (1, 2) self.assertEqual(typeof(v), types.UniTuple(types.intp, 2)) v = (1, (2.0, 3)) self.assertEqual( typeof(v), types.Tuple((types.intp, types.Tuple( (types.float64, types.intp)))))
def _check_in(self, pyfunc, flags): dtype = types.int64 cres = compile_isolated(pyfunc, (dtype, types.UniTuple(dtype, 3)), flags=flags) cfunc = cres.entry_point for i in (3, 4, 5, 6, 42): tup = (3, 42, 5) self.assertPreciseEqual(pyfunc(i, tup), cfunc(i, tup))
def shared_array(shape, dtype): shape = _legalize_shape(shape) ndim = len(shape) fname = "hsail.smem.alloc" restype = types.Array(dtype, ndim, "C") sig = typing.signature(restype, types.UniTuple(types.intp, ndim), types.Any) return ir.Intrinsic(fname, sig, args=(shape, dtype))
def check_ndindex(self, flags=no_pyobj_flags): pyfunc = gen_ndindex cr = compile_isolated(pyfunc, (types.UniTuple(types.intp, 2), ), flags=flags) shape = (2, 3) pygen = pyfunc(shape) cgen = cr.entry_point(shape) self.check_generator(pygen, cgen)
def check_slice(self, pyfunc): tup = (4, 5, 6, 7) cr = compile_isolated(pyfunc, [types.UniTuple(types.int64, 4)]) self.assertPreciseEqual(cr.entry_point(tup), pyfunc(tup)) cr = compile_isolated(pyfunc, [ types.Tuple((types.int64, types.int32, types.int64, types.int32)) ]) self.assertPreciseEqual(cr.entry_point(tup), pyfunc(tup))
def test_optional_tuple(self): # Unify to optional tuple aty = types.none bty = types.UniTuple(i32, 2) self.assert_unify(aty, bty, types.Optional(types.UniTuple(i32, 2))) aty = types.Optional(types.UniTuple(i16, 2)) bty = types.UniTuple(i32, 2) self.assert_unify(aty, bty, types.Optional(types.UniTuple(i32, 2))) # Unify to tuple of optionals aty = types.Tuple((types.none, i32)) bty = types.Tuple((i16, types.none)) self.assert_unify(aty, bty, types.Tuple((types.Optional(i16), types.Optional(i32)))) aty = types.Tuple((types.Optional(i32), i64)) bty = types.Tuple((i16, types.Optional(i8))) self.assert_unify(aty, bty, types.Tuple((types.Optional(i32), types.Optional(i64))))