def test_isl_align_two(): a1 = isl.Aff("[t0, t1, t2] -> { [(32)] }") a2 = isl.Aff("[t1, t0] -> { [(0)] }") a1_aligned, a2_aligned = isl.align_two(a1, a2) assert a1_aligned == isl.Aff("[t1, t0, t2] -> { [(32)] }") assert a2_aligned == isl.Aff("[t1, t0, t2] -> { [(0)] }") b1 = isl.BasicSet("[n0, n1, n2] -> { [i0, i1] : }") b2 = isl.BasicSet("[n0, n2, n1, n3] -> { [i1, i0, i2] : }") b1_aligned, b2_aligned = isl.align_two(b1, b2) assert b1_aligned == isl.BasicSet( "[n0, n2, n1, n3] -> { [i1, i0, i2] : }") assert b2_aligned == isl.BasicSet( "[n0, n2, n1, n3] -> { [i1, i0, i2] : }")
def isl_iteration_set(node: ast.Node): """Builds up an ISL set describing the iteration space by analysing the enclosing loops of the given node. """ conditions = [] degrees_of_freedom = set() for loop in parents_of_type(node, ast.LoopOverCoordinate): if loop.step != 1: raise NotImplementedError( "Loops with strides != 1 are not yet supported.") degrees_of_freedom.update( _degrees_of_freedom_as_string(loop.loop_counter_symbol)) degrees_of_freedom.update(_degrees_of_freedom_as_string(loop.start)) degrees_of_freedom.update(_degrees_of_freedom_as_string(loop.stop)) loop_start_str = remove_brackets(str(loop.start)) loop_stop_str = remove_brackets(str(loop.stop)) ctr_name = loop.loop_counter_name set_string_description = f"{ctr_name} >= {loop_start_str} and {ctr_name} < {loop_stop_str}" conditions.append(remove_brackets(set_string_description)) symbol_names = ','.join(degrees_of_freedom) condition_str = ' and '.join(conditions) set_description = f"{{ [{symbol_names}] : {condition_str} }}" return degrees_of_freedom, isl.BasicSet(set_description)
def test_union_casts(): # https://github.com/inducer/islpy/issues/29 s1 = isl.UnionSet("{[0]}") s2 = isl.BasicSet("{[1]}") s2.union(s1) # works fine s1.union(s2) # does not work
def test_check_bounds_with_caller_assumptions(ctx_factory): import islpy as isl from loopy.diagnostic import LoopyIndexError arange = lp.make_function("{[i]: 0<=i<n}", """ y[i] = i """, name="arange") knl = lp.make_kernel( "{[i]: 0<=i<20}", """ [i]: Y[i] = arange(N) """, [lp.GlobalArg("Y", shape=(20, )), lp.ValueArg("N", dtype=np.int32)], name="epoint") knl = lp.merge([knl, arange]) with pytest.raises(LoopyIndexError): lp.generate_code_v2(knl) knl = knl.with_kernel( lp.assume(knl.default_entrypoint, isl.BasicSet("[N] -> { : N <= 20}"))) lp.auto_test_vs_ref(knl, ctx_factory(), parameters={"N": 15})
def test_basics(): dt = isl.dim_type ctx = isl.Context() space = isl.Space.create_from_names(ctx, set=["a", "b"]) bset = (isl.BasicSet.universe(space).add_constraint( isl.Constraint.eq_from_names(space, { "a": -1, "b": 2 })).add_constraint( isl.Constraint.ineq_from_names(space, { "a": 1, 1: -10 })).add_constraint( isl.Constraint.ineq_from_names(space, { "a": -1, 1: 42 })).project_out(dt.set, 1, 1)) bset2 = isl.BasicSet( # noqa "{[i] : exists (a : i = 2a and i >= 10 and i <= 42)}", context=ctx) points = [] bset.foreach_point(points.append) for pt in points: print(pt)
def SetsAsIterationSpaces(ctx): fig = plt.figure() ax = fig.add_subplot(221) ax.set_autoscale_on(True) ax.locator_params(integer=True) ax.set_aspect('equal') ax.grid(True) # An easier way of creating sets s1 = isl.BasicSet("{[x, y] : x >= 0 and x < 5 and y >= 0 and y < 5}") s2 = isl.BasicSet("{[x, y] : x >= 3 and x < 8 and y >= 3 and y < 8}") print s1 print s2 drawBasicSet(s1, { 0:'x', 1:'y', 'color':'red', 'pointSize':150.0}, ax) drawBasicSet(s2, { 0:'x', 1:'y'}, ax) ax = fig.add_subplot(222) ax.set_autoscale_on(True) ax.locator_params(integer=True) ax.set_aspect('equal') ax.grid(True) u = s1.union(s2) print u drawBasicSet(u, { 0:'x', 1:'y'}, ax) ax = fig.add_subplot(223) ax.set_autoscale_on(True) ax.locator_params(integer=True) ax.set_aspect('equal') ax.grid(True) d = s1.subtract(s2) print d drawBasicSet(d, { 0:'x', 1:'y'}, ax) ax = fig.add_subplot(224) ax.set_autoscale_on(True) ax.locator_params(integer=True) ax.set_aspect('equal') ax.grid(True) h = u.convex_hull() print h drawBasicSet(h, { 0:'x', 1:'y'}, ax) plt.show()
def generate(impero_c, args, precision, scalar_type, kernel_name="loopy_kernel", index_names=[]): """Generates loopy code. :arg impero_c: ImperoC tuple with Impero AST and other data :arg args: list of loopy.GlobalArgs :arg precision: floating-point precision for printing :arg scalar_type: type of scalars as C typename string :arg kernel_name: function name of the kernel :arg index_names: pre-assigned index names :returns: loopy kernel """ ctx = LoopyContext() ctx.indices = impero_c.indices ctx.index_names = defaultdict(lambda: "i", index_names) ctx.precision = precision ctx.scalar_type = scalar_type ctx.epsilon = 10.0 ** (-precision) # Create arguments data = list(args) for i, temp in enumerate(impero_c.temporaries): name = "t%d" % i if isinstance(temp, gem.Constant): data.append(lp.TemporaryVariable(name, shape=temp.shape, dtype=temp.array.dtype, initializer=temp.array, address_space=lp.AddressSpace.LOCAL, read_only=True)) else: shape = tuple([i.extent for i in ctx.indices[temp]]) + temp.shape data.append(lp.TemporaryVariable(name, shape=shape, dtype=numpy.float64, initializer=None, address_space=lp.AddressSpace.LOCAL, read_only=False)) ctx.gem_to_pymbolic[temp] = p.Variable(name) # Create instructions instructions = statement(impero_c.tree, ctx) # Create domains domains = [] for idx, extent in ctx.index_extent.items(): inames = isl.make_zero_and_vars([idx]) domains.append(((inames[0].le_set(inames[idx])) & (inames[idx].lt_set(inames[0] + extent)))) if not domains: domains = [isl.BasicSet("[] -> {[]}")] # Create loopy kernel knl = lp.make_function(domains, instructions, data, name=kernel_name, target=lp.CTarget(), seq_dependencies=True, silenced_warnings=["summing_if_branches_ops"]) # Prevent loopy interchange by loopy knl = lp.prioritize_loops(knl, ",".join(ctx.index_extent.keys())) # Help loopy in scheduling by assigning priority to instructions insn_new = [] for i, insn in enumerate(knl.instructions): insn_new.append(insn.copy(priority=len(knl.instructions) - i)) knl = knl.copy(instructions=insn_new) return knl
def test_tuple(ctx_factory): ctx = ctx_factory() queue = cl.CommandQueue(ctx) import islpy as isl knl = lp.make_kernel([isl.BasicSet("[] -> {[]: }")], """ a, b = make_tuple(1, 2.) """) evt, (a, b) = knl(queue) assert a.get() == 1 assert b.get() == 2.
def test_rename_argument_with_assumptions(): import islpy as isl knl = lp.make_kernel("{[i]: 0<=i<n_old}", """ y[i] = 2.0f """) knl = lp.assume(knl, "n_old=10") knl = lp.rename_argument(knl, "n_old", "n_new") assumptions = knl["loopy_kernel"].assumptions assert "n_old" not in assumptions.get_var_dict() assert "n_new" in assumptions.get_var_dict() assert ((assumptions & isl.BasicSet("[n_new]->{: n_new=10}")) == assumptions)
def create_domains(indices): """ Create ISL domains from indices :arg indices: iterable of (index_name, extent) pairs :returns: A list of ISL sets representing the iteration domain of the indices.""" domains = [] for idx, extent in indices: inames = isl.make_zero_and_vars([idx]) domains.append(((inames[0].le_set(inames[idx])) & (inames[idx].lt_set(inames[0] + extent)))) if not domains: domains = [isl.BasicSet("[] -> {[]}")] return domains
def test_pickling(): instances = [ isl.Aff("[n] -> { [(-1 - floor((-n)/4))] }"), isl.PwAff("[n] -> { [(0)] : n <= 4 and n >= 1; " "[(-1 + n - floor((3n)/4))] : n >= 5 }"), isl.BasicSet("[n] -> {[i,j,k]: i<=j + k and (exists m: m=j+k) " "and n mod 5 = 17}"), isl.Set("[n] -> {[i,j,k]: (i<=j + k and (exists m: m=j+k)) or (k=j)}") ] from pickle import dumps, loads for inst in instances: inst2 = loads(dumps(inst)) assert inst.space == inst2.space assert inst.is_equal(inst2)
def simplify_loop_counter_dependent_conditional(conditional): """Removes conditionals that depend on the loop counter or iteration limits if they are always true/false.""" dofs_in_condition = _degrees_of_freedom_as_string( conditional.condition_expr) dofs_in_loops, iteration_set = isl_iteration_set(conditional) if dofs_in_condition.issubset(dofs_in_loops): symbol_names = ','.join(dofs_in_loops) condition_str = remove_brackets(str(conditional.condition_expr)) condition_set = isl.BasicSet( f"{{ [{symbol_names}] : {condition_str} }}") if condition_set.is_empty(): conditional.replace_by_false_block() intersection = iteration_set.intersect(condition_set) if intersection.is_empty(): conditional.replace_by_false_block() elif intersection == iteration_set: conditional.replace_by_true_block()
def add_diff_inames(self): diff_inames = tuple( self.rule_mapping_context.make_unique_var_name( self.diff_iname_prefix + str(i)) for i in range(len(self.additional_shape))) diff_parameters = set() from loopy.symbolic import get_dependencies for s in self.additional_shape: diff_parameters.update(get_dependencies(s)) diff_domain = isl.BasicSet( "[%s] -> {[%s]}" % (", ".join(diff_parameters), ", ".join(diff_inames))) for i, diff_iname in enumerate(diff_inames): diff_domain = diff_domain & make_slab( diff_domain.space, diff_iname, 0, self.additional_shape[i]) self.new_domains.append(diff_domain) return diff_inames
def test_integer_associativity(): knl = lp.make_kernel( "{[i] : 0<=i<arraylen}", """ e := (i // (ncomp * elemsize)) d := ((i // elemsize) % ncomp) s := (i % elemsize) v[i] = u[ncomp * indices[(s) + elemsize*(e)] + (d)] """) knl = lp.add_and_infer_dtypes(knl, { "u": np.float64, "elemsize, ncomp, indices": np.int32 }) import islpy as isl knl = lp.assume( knl, isl.BasicSet("[elemsize, ncomp] -> " "{ : elemsize>= 0 and ncomp >= 0}")) print(lp.generate_code_v2(knl).device_code()) assert ("u[ncomp * indices[i % elemsize + elemsize " "* loopy_floor_div_int32(i, ncomp * elemsize)] " "+ loopy_mod_pos_b_int32(i / elemsize, ncomp)]" in lp.generate_code_v2(knl).device_code())
def test_count_brick_ish(): a = isl.BasicSet("[n] -> {[i,j]: 0<= i < n and 0<= j < n and j<= i}") def count(bset): result = 1 for i in range(bset.dim(isl.dim_type.set)): dmax = bset.dim_max(i) dmin = bset.dim_min(i) length = isl.PwQPolynomial.from_pw_aff(dmax - dmin + 1) result = result * length return result counts = [count(a)] if hasattr(a, "card"): counts.append(a.card()) for pwq in counts: print("EVAL", pwq, "=", pwq.eval_with_dict(dict(n=10)))
r = isl.Map("{[i,j] -> [i',j'] : ( i' = 1+i and j' = 1+j and 0 <= i <= 2 and 0 <= j <= 2 ) or ( i' = 1+i and 1+j' = j and 0 <= i <= 2 and 1 <= j <= 3 ) or (Exists a: (2a = i)) };"); printer = isl.Printer.to_str(isl.DEFAULT_CONTEXT) printer = printer.set_output_format(3) printer.print_map(r) print(printer.get_str()) b = isl.AstBuild.from_context(isl.Set("{:}")) m = isl.Map("[T, N] -> {S[K,t,i,j] -> [K,t,i,j] : 0 <= t <= T-1 && 1 <= i <= N-2 && 1 <= j <= N-2 && K= 2*t + i -1 }") s = isl.Set("[T, N] -> {[K,t,i,j] : 0 <= t <= T-1 && 1 <= i <= N-2 && 1 <= j <= N-2 && K= 2*t + i -1 }"); m = isl.Map.from_domain_and_range(s,s) m = isl.Map.identity(m.get_space()) m = isl.Map.from_domain(s) print '----' print m print '----' ast = b.ast_from_schedule(m) p = isl.Printer.to_str(isl.DEFAULT_CONTEXT) p = p.set_output_format(isl.format.C) p.flush() p = p.print_ast_node(ast) print(p.get_str()) S = isl.BasicSet("{[i,j]: Exists a: (2*a = i && 0 <= j <= 10)}") print S.get_constraints()
def test_fuzz_expression_code_gen(ctx_factory, expr_type, random_seed): from pymbolic import evaluate def get_numpy_type(x): if expr_type in ["real", "complex"]: if isinstance(x, (complex, np.complexfloating)): return np.complex128 else: return np.float64 elif expr_type in ["int", "int_nonneg"]: return np.int64 else: raise ValueError("unknown expr_type: %s" % expr_type) from random import seed ctx = ctx_factory() queue = cl.CommandQueue(ctx) seed(random_seed) data = [] instructions = [] ref_values = {} if expr_type in ["real", "complex"]: result_type = np.complex128 elif expr_type in ["int", "int_nonneg"]: result_type = np.int64 else: assert False var_names = [] fuzz_iter = iter(generate_random_fuzz_examples(expr_type)) count = 0 while True: if count == 10: break i, expr, var_values = next(fuzz_iter) var_name = "expr%d" % i print(expr) #assert_parse_roundtrip(expr) if expr_type in ["int", "int_nonneg"]: result_type_iinfo = np.iinfo(np.int32) bceval_mapper = BoundsCheckingEvaluationMapper( var_values, lbound=result_type_iinfo.min, ubound=result_type_iinfo.max) print(expr) try: ref_values[var_name] = bceval_mapper(expr) except BoundsCheckError: print(expr) print("BOUNDS CHECK FAILED") continue else: try: ref_values[var_name] = evaluate(expr, var_values) except ZeroDivisionError: continue count += 1 data.append(lp.GlobalArg(var_name, result_type, shape=())) data.extend([ lp.TemporaryVariable(name, get_numpy_type(val)) for name, val in var_values.items() ]) instructions.extend([ lp.Assignment(name, get_numpy_type(val)(val)) for name, val in var_values.items() ]) instructions.append(lp.Assignment(var_name, expr)) if expr_type == "int_nonneg": var_names.extend(var_values) knl = lp.make_kernel("{ : }", instructions, data, seq_dependencies=True) import islpy as isl knl = lp.assume( knl, isl.BasicSet( "[%s] -> { : %s}" % (", ".join(var_names), " and ".join("%s >= 0" % name for name in var_names)))) knl = lp.set_options(knl, return_dict=True) print(knl) evt, lp_values = knl(queue, out_host=True) for name, ref_value in ref_values.items(): lp_value = lp_values[name] if expr_type in ["real", "complex"]: err = abs(ref_value - lp_value) / abs(ref_value) elif expr_type in ["int", "int_nonneg"]: err = abs(ref_value - lp_value) else: assert False if abs(err) > 1e-10: print(80 * "-") print(knl) print(80 * "-") print(lp.generate_code_v2(knl).device_code()) print(80 * "-") print(f"WRONG: {name} rel error={err:g}") print("reference=%r" % ref_value) print("loopy=%r" % lp_value) print(80 * "-") 1 / 0 print(lp.generate_code_v2(knl).device_code())
def tile(plik, block): EXP_CODE = True BLOCK = block.split(',') par_tiling = False for i in range(len(BLOCK),10): BLOCK.append(BLOCK[len(BLOCK)-1]) if(not BLOCK[0].isdigit()): par_tiling = True #EXP_CODE = False # nie ma wsparcia na razie dla gen. poszerzonego zbioru dla spar. tilingu, petle zewn. generowane sa z strings linestring = open(plik, 'r').read() lines = linestring.split('\n') petit_loop = convert_loop.convert_loop(lines) file = open("tmp/tmp_petit.t", 'w') for line in petit_loop: file.write(line + '\n') file.close() #zapisz zaleznosci cmd = gen.path_petit + " tmp/tmp_petit.t > tmp/deps.txt" process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) process.wait() #prywatyzacja priv_stuff = priv_engine.PrivEng("tmp/deps.txt") lines = linestring.split('\n') stuff = [] for line in lines: if 'for' in line: stuff.append(functions.Loop(line)); start = time.time() if(schedule_mode==1): flag = 0 else: flag = 1 #pobierz relacje zaleznosci if(os.path.exists('rel.txt')): with open('rel.txt', 'r') as content_file: dane = content_file.read() else: if(len(priv_stuff[1]) > 0 and (priv_stuff[2] == 1 or False)): # jest par czyli mozna tiling dane = gen.RelPrint_WithPriv("tmp/tmp_petit.t", priv_stuff[1]) else: dane = gen.RelPrint("tmp/tmp_petit.t", flag) output = process.stdout.read() end = time.time() elapsed = end - start if(DEBUG): print "Time taken: ", elapsed, "seconds." dane = dane.split("#") rel = dane[0] rel2 = rel dane.remove(rel) dane = list(set(dane)) if(DEBUG): print rel # ------------------------------------------------------- # script for barvinok # gdy nie ma zaleznosci # problemy do rozwiazania # zmienne symboliczne nie wiadomo skad wziasc # numery instrukcji nodeps=0 if(rel == ''): nodeps=1 rel = "{[" for i in range(0,len(stuff)): rel = rel + "i" + str(i) + "," rel = rel + "v] -> [" for i in range(0,len(stuff)): rel = rel + "i" + str(i) + "'," rel = rel + "v'] : false }" loop_tools.MakeLDPetit("tmp/tmp_petit.t", "tmp/tmp_ld_petit.t") dane = gen.RelPrint("tmp/tmp_ld_petit.t",1) dane = dane.split("#") rel2 = dane[0] dane.remove(rel2) dane = list(set(dane)) for s in dane: if "UNKNOWN" in s: dane.remove(s) combo = loop_tools.ReadStatementNests("tmp/tmp_petit.t", dane) instrukcje = combo[0] nest_loop = combo[1] if len(instrukcje)==0: print 'blad - brak instrukcji' sys.exit() # wczytaj wszystkie # eksperymentalnie kasowanie innych poziomow stuff_reduced = stuff[:] #osobna kopia do_kas = [] for i in range(1, max(nest_loop)+1): dups = duplicates(nest_loop, i) for s in dups: if s != min(dups): do_kas.append(s) for item in do_kas: stuff_reduced[item] = '!' for i in range(0,stuff_reduced.count('!')): stuff_reduced.remove('!') # remove # ------------------------------- #find_symb = re.compile("^[^(\]|\{)]*\]"); #if(rel2 == rel): # tmp = re.findall(find_symb, rel) #else: # tmp = re.findall(find_symb, rel2) isl_rel = isl.Map(str(rel)) isl_relclosure = isl_rel.transitive_closure()[0].coalesce() isl_relplus = isl_relclosure #ident = "{[" #for i in range(0, len(stuff_reduced)+1): # ident = ident + "i" + str(i) + "," #ident = ident[:-1] + "] -> [" #for i in range(0, len(stuff_reduced)+1): # ident = ident + "i" + str(i) + "," #ident = ident[:-1] +"]}" #isl_ident = isl.Map(ident) isl_ident = isl_rel.identity(isl_rel.get_space()) isl_relclosure = isl_relclosure.union(isl_ident).coalesce() # R* = R+ u I #LATEX isl_relclosure.print_(sys.stdout,0, 5) isl_symb = isl_rel.get_var_names(isl.dim_type.param) symb = isl_symb #if(len(tmp) > 0): # symb = tmp[0] # symb = symb.replace("[", ""); # symb = symb.replace("]", ""); # symb = symb.replace(" ", ""); # symb = symb.split(","); vars = [] # i exvars = [] # ii' sym_exvars = [] # ii varsprim = [] # i' par_vars = [] # iib p_vars = [] #iib bez plusa lb p_symb = [] prev_vars = [] # moze warto do iloczynu dodac lb a do floor N - lb # do testowania +'+'+s['lb'] i = 0 for s in stuff_reduced: vars.append(s['var']); sym_exvars.append(s['var']*2); exvars.append(s['var']*2+"'"); varsprim.append(s['var']+"'"); if par_tiling: par_vars.append(s['var']*2+'b'+'+'+s['lb']) p_vars.append(s['var']*2+'b') else: par_vars.append(s['var']*2 + "*" + BLOCK[i]+'+'+s['lb']) # na uzytel nieidealnie zagniezdzonych prev_vars.append("(" + s['var']*2 + "-1)*" + BLOCK[i]+'+'+s['lb']) i = i+1 _SYM = "" for s in sym_exvars: _SYM = _SYM + s + "," for i in range(len(vars),10): BLOCK.pop() if(par_tiling): for s in p_vars: _SYM = _SYM + s + "," p_symb.append(s) for s in set(BLOCK): #remove duplicates _SYM = _SYM + s + "," p_symb.append(s) for s in symb: _SYM = _SYM + s + "," isl_TILE = [] isl_TILE_LT = [] isl_TILE_GT = [] isl_TILE1 = [] isl_TILE2 = [] isl_TILEprim = [] isl_TILEunion = "" isl_TILE_prev = [] isl_TILE_nextcol = [] isl_TILE_LT_prev = [] isl_TILE_GT_prev = [] isl_IT = [] isl_REST_new = [] for i in range(0, len(instrukcje)): iti = isl.Set(imperf_tile.GetIT(instrukcje, stuff_reduced, _SYM, vars, sym_exvars, BLOCK, i)) isl_IT.append(iti) # utworz Bij_PREV czyli podmien w par_vars jeden wymiar na prev_vars # i unionem wszystkie polacz for i in range(0, len(instrukcje)): for j in range(0, instrukcje[i]["nest"]): tmp_par_vars = [] for k in range(0, len(par_vars)): if(j==k): tmp_par_vars.append(prev_vars[k]) else: tmp_par_vars.append(par_vars[k]) S = MakeBij(_SYM, vars, sym_exvars, tmp_par_vars, stuff, BLOCK, instrukcje[i]) S = isl.Set(str(S).replace("_Bij := ", "")) if(len(isl_TILE_prev) <= i): isl_TILE_prev.append(S) isl_TILE_nextcol.append(S) else: isl_TILE_prev[i] = isl_TILE_prev[i].union(S).coalesce() if(DEBUG): for i in range(0, len(isl_TILE_prev)): print "LT'_" + str(i) print isl_TILE_prev[i] for i in range(0, len(instrukcje)): Bij = MakeBij(_SYM, vars, sym_exvars, par_vars, stuff, BLOCK, instrukcje[i]) if(DEBUG): print "TILE" + str(i) print Bij isl_TILE.append(isl.Set(str(Bij).replace("_Bij := ", ""))) if(i==0): isl_TILEunion = isl_TILE[i] else: isl_TILEunion = isl_TILEunion.union(isl_TILE[i]) global maxl maxl = 0 for item in instrukcje: if item['max_loop'] > maxl: maxl = item['max_loop'] SET_II = "[" + _SYM[:-1] + "] -> {[" + ",".join(vars) + ",v] : " for i in range(0, len(sym_exvars)): SET_II = SET_II + sym_exvars[i] + " >= 0 and " + par_vars[i] + " <= " + stuff[i]['ub'] + " and " SET_II = SET_II+ "1=1 }" print SET_II isl_SETii = isl.Set(SET_II) if(DEBUG): print isl_SETii for i in range(0, len(instrukcje)): Bij = MakeBij(_SYM, vars, sym_exvars, par_vars, stuff, BLOCK, instrukcje[i]) if(DEBUG): print "======" bltc=0 # czy juz liczono blt bgtc=0 # -- || -- bgt for j in range(0, len(instrukcje)): BLGT = MakeBLTandBGT_v2(_SYM, vars, sym_exvars, par_vars, varsprim, exvars, stuff, BLOCK, instrukcje[j], instrukcje[i]) # porownaj zagniezdzenia instrukcji isltmp = isl.Set(str(BLGT[0]).replace("_BLT := ", "")) if(bltc==0): isl_TILE_LT.append(isltmp) bltc=1 else: isl_TILE_LT[i] = isl_TILE_LT[i].union(isltmp).coalesce() isltmp = isl.Set(str(BLGT[1]).replace("_BGT := ", "")) if(bgtc==0): isl_TILE_GT.append(isltmp) bgtc=1 else: isl_TILE_GT[i] = isl_TILE_GT[i].union(isltmp).coalesce() if(DEBUG): print "TILE_LT" + str(i) print isl_TILE_LT[i] print "TILE_GT" + str(i) print isl_TILE_GT[i] X = isl_TILE_GT[i].apply(isl_relclosure).coalesce() if(DEBUG): print "R*(TILE_GT" + str(i) + ")" print X isl_BCUR = isl_TILE[i].subtract(X).coalesce() isl_TILE1.append(isl_BCUR) if(DEBUG): print "TILE1_" + str(i) print isl_TILE1[i] C = isl_TILE[i].apply(isl_relplus).coalesce() if(DEBUG2): print "(R*(TILEi)" print C C = C.intersect(isl_TILE_LT[i]).coalesce() C = C.intersect(isl_IT[i]).coalesce() A = isl_TILE_LT[i].apply(isl_relplus).coalesce() A = A.intersect(isl_TILE_prev[i]).coalesce() #A = isl_TILE_prev[i] A = A.subtract(Project(isl_TILE1[i].union(C), sym_exvars)).coalesce() #A = A.subtract(Project(C, sym_exvars)).coalesce() if(DEBUG2): print "(R+(LTi) intersect LT'i) - instrukcje [ belong to C and TILE1i ]" print A print "TILE2 (z wczesniejszym union C)" A = A.union(C).coalesce() #dla ladniejszego zapisu A = A.intersect(isl_SETii).coalesce() #A = isl_TILE_prev[i] if(DEBUG2): print A #print "=============== s" + str(i) # print isl_TILE1[i] Z = isl_TILE[i].subtract(isl_TILE1[i]) Z = Project(Z, sym_exvars) Z = Z.intersect(isl_TILE_prev[i].intersect(isl_SETii)).coalesce() #print Z Z = Z.subtract(Project(C, sym_exvars)).coalesce() # A = LT'i intersect ( wszystkie punkty R*(TILE_GTi - ISi)) Z1 = isl_TILE_GT[i].subtract(isl_IT[i]) print "====" #print Z1.apply(isl_relclosure) #print isl_TILE1[i] #Z = Project(Z1.apply(isl_relclosure), sym_exvars) #print Z #Z = Z.intersect(isl_TILE_prev[i]).intersect(isl_SETii) A = Z.union(C) isl_REST_new.append(isl_TILE[i].subtract(Project(A.union(isl_TILE1[i]), sym_exvars)).coalesce()) #print isl_REST_new[i] isl_TILE2.append(A) #isl_TILE2.append(A.intersect(isl_TILE_LT[i]).coalesce()) if(DEBUG): print "TILE2_" + str(i) print isl_TILE2[i] isl_TILEprim.append(isl_TILE2[i].union(isl_BCUR).coalesce()) if(DEBUG): print isl_SETii isl_TILEprim[i] = isl_TILEprim[i].intersect(isl_SETii) if(DEBUG): print "TILEprim_" + str(i) print isl_TILEprim[i] iR = isl_REST_new[1] for i in range(1, len(isl_REST_new)): iR = iR.union(isl_REST_new[i]).coalesce() iR = iR.apply(isl_relclosure) iR = Project(iR, sym_exvars) iT = isl_TILEprim[1] for i in range(1, len(isl_REST_new)): iT = iT.union(isl_TILEprim[i]).coalesce() iT = Project(iT, sym_exvars) print iT.intersect(iR).coalesce() sys.exit() if(DEBUG): print "=============================================================================" for i in range(0, len(instrukcje)): for ii in range(0,2): constr = "" if i == 1: ubjj = 2 else: ubjj = 1 for jj in range(0,ubjj): if ubjj > 1: constr = " && jj = " + str(jj) bset = isl.BasicSet("[ii,jj] -> {[x, y,z] : ii = "+str(ii)+constr+"}") #print "TILE_LT" + str(i) #print "ii=" + str(ii) + ",jj=" + str(jj) #print isl_TILE_LT[i].intersect(bset).coalesce() #print "TILE_GT" + str(i) #print "ii=" + str(ii) + ",jj=" + str(jj) #print isl_TILE_GT[i].intersect(bset).coalesce() #file.write('BLOCK;') file.close() # ------------------------------------------------------- '''cmd = barv_script + " < tmp/barv_tiling.txt" process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) process.wait() output = process.stdout.read() end = time.time() elapsed = end - start print "Time taken: ", elapsed, "seconds." lines = output.split("\n") if(DEBUG): for i in range(0,lines.index('"$#$"')+1): lines.pop(0); block_set = [] for i in range(0,lines.index('"$$$#$$$"')+1): block_set.append(lines.pop(0)); block_set.pop(); lines = filter(lambda x:x!='',lines) #dodaj s1 do petli loop = postprocess_loop(lines) bl_2half = loop''' #eksperymentalnie if(EXP_CODE): loops = TileGen(sym_exvars, vars, _SYM, symb, p_symb, par_tiling, rel, schedule_mode, instrukcje, stuff_reduced, BLOCK, isl_TILEprim, varsprim, isl_IT, isl_REST_new) loop_x = loops[0] loop_x = postprocess_loop(loop_x.split('\n')) loop = loop_x #zmienne v = "" for s in stuff_reduced: v = v + s['var'] + "," v = v + "v" text_file = open("names_info.txt", "w") text_file.write(v) text_file.close() #print block_set # tu mozna rozszerzyc #pseudokod text_file = open("pseudocode.txt", "w") text_file.write(loop) text_file.close() #przywroc instrukcje if(EXP_CODE): gen.ParsePrint2("tmp/tmp_petit.t", len(sym_exvars)) else: gen.ParsePrint("tmp/tmp_petit.t") filePaths = glob.glob(plik) for filePath in filePaths: base = os.path.basename(filePath) nazwa = os.path.splitext(base)[0] + "_tiling" + os.path.splitext(base)[1] # usun puste linie i kopiuj file = open("out_pseudocode.txt", 'r') lines = file.readlines() # oryginalny z elsami lines_block = lines[:] if par_tiling and not EXP_CODE: for i in range(0, len(sym_exvars)): lines.insert(0,'register int ' + sym_exvars[i] + 'b=' + sym_exvars[i] + "*" +BLOCK[i] + ";\n") if par_tiling and EXP_CODE: for i in range(0, len(sym_exvars)): lines[i] = lines[i].replace('\n', '') + '{ register int ' + sym_exvars[i] + 'b=c'+str(i)+'*' +BLOCK[i] + ";\n" lines.append("}\n") file.close() #eksperymentalnie if(not EXP_CODE): for i in range(0, len(sym_exvars)): # lines.insert(i, "for(int "+sym_exvars[i]+"="+stuff[i]['lb']+"; "+sym_exvars[i]+"<="+stuff[i]['ub']+"; "+sym_exvars[i]+"+="+str(BLOCK[i])+"){\n") lines.insert(i, "for(int "+sym_exvars[i]+"=0; "+sym_exvars[i]+"<=floord("+stuff[i]['ub']+"-"+stuff[i]['lb']+","+str(BLOCK[i])+"); "+sym_exvars[i]+"++){\n") lines.append("}\n") else: if par_tiling: # do rozwiazania for i in range(0, len(sym_exvars)): lines.insert(i, "int fff"+str(i)+"=floord("+stuff[i]['ub']+"-"+stuff[i]['lb']+","+str(BLOCK[i])+");\n") lines = filter (lambda a: a != '\n',lines) if nodeps==1: lines.insert(0, "#pragma omp parallel\n") file = open("out_pseudocode.txt", 'w') lines = file.writelines(lines) file.close() shutil.copyfile("out_pseudocode.txt", nazwa) '''if(len(loops) == 2):
def islApiExamples(): ctx = isl.Context() ## Spaces names = ['s\'', 'x1', 'x2'] space1 = isl.Space.create_from_names(ctx, set = names) names = ['s', 'y1', 'y2'] space2 = isl.Space.create_from_names(ctx, set = names) #Spaces are equal when their dimensions are equal print space1.is_equal(space2) #isl dim type can be set/in/out/param/all print space1.find_dim_by_name(isl._isl.dim_type.all,'x2') #print space1.get_id_dict(isl._isl.dim_type.set) newid = isl.Id(context = ctx, name = 'x0') space1 = space1.set_dim_id(isl._isl.dim_type.set, 1, newid) print space1.get_dim_id(isl._isl.dim_type.set, 1) #Looks like get_id_dict does not work as expected #print space1.get_id_dict(isl._isl.dim_type.set) print space1.get_var_dict() ## Sets space = isl.Space.create_from_names(ctx, set=["i", "j"]) bset1 = (isl.BasicSet.universe(space) .add_constraint(isl.Constraint.ineq_from_names(space, {1: -1, "i": 1})) .add_constraint(isl.Constraint.ineq_from_names(space, {1: 5, "i": -1})) .add_constraint(isl.Constraint.ineq_from_names(space, {1: -1, "j": 1})) .add_constraint(isl.Constraint.ineq_from_names(space, {1: 5, "j": -1}))) print bset1 bset2 = isl.BasicSet("[N]->{[x, y] : x >= 0 and x < 5 and y >= 0 and y < N+4 and N >= 0 and N < 10}") print bset2 print bset2.polyhedral_hull() print bset2.lexmax() print bset2.lexmin() print bset2.lexmin().is_singleton() ## Points points = [] bset1.foreach_point(points.append) print points point = points[0].get_coordinate_val(isl._isl.dim_type.all, 0) pointx = point.get_num_si()/point.get_num_si() # The same thing can be achieved with to_python() provided by islpy print point.to_python() point = points[0].get_coordinate_val(isl._isl.dim_type.all, 1) pointy = point.get_num_si()/point.get_num_si() print (pointx, pointy) ## Dependence computation mapspace = isl.Space.create_from_names(ctx, in_=["i", "j"], out = ["i1", "j1"], params = ["N"]) ## Schedule and AST # Creating an identity schedule bmap = (isl.BasicMap.identity(mapspace) .add_constraint(isl.Constraint.ineq_from_names(mapspace, {1: -1, "i": 1})) .add_constraint(isl.Constraint.ineq_from_names(mapspace, {"N": 1, "i": -1})) .add_constraint(isl.Constraint.ineq_from_names(mapspace, {1: -1, "j": 1})) .add_constraint(isl.Constraint.ineq_from_names(mapspace, {"N": 1, "j": -1}))) #bmap = bmap.insert_dims(isl._isl.dim_type.out, 0, 1) #name = bmap.get_dim_name(isl._isl.dim_type.out, 1) #bmap = bmap.set_dim_name(isl._isl.dim_type.out, dim, 'S_' + name) print bmap astbld = isl.AstBuild.from_context(isl.BasicSet("[N] -> { : }")) astbld = astbld.set_options(isl.UnionMap("{}")) # Printing is strange printer = isl.Printer.to_str(ctx) printer = printer.set_output_format(isl.format.C) printer = (isl.AstBuild.ast_from_schedule(astbld, isl.UnionMap.from_map(bmap))).print_(printer, isl.AstPrintOptions.alloc(ctx)) print printer.get_str() extSet = isl.BasicSet("[n] -> { [i] : exists (a = [i/10] : 0 <= i and i <= n and i - 10 a <= 6) }") print extSet print extSet.get_local_space() print extSet.get_local_space().get_div(0) extMap = isl.BasicMap.from_domain_and_range(extSet, extSet) astbld = isl.AstBuild.from_context(isl.BasicSet("[n] -> { : }")) astbld = astbld.set_options(isl.UnionMap("{}")) # Printing is strange printer = isl.Printer.to_str(ctx) printer = printer.set_output_format(isl.format.C) printer = (isl.AstBuild.ast_from_schedule(astbld, isl.UnionMap.from_map(extMap))).print_(printer, isl.AstPrintOptions.alloc(ctx)) print printer.get_str() gen = isl.BasicMap("[C, R] -> { Ixx[x, y] -> [Dir_i0', T_i0', Dir_i1', T_i1', t, i0', i1'] : t = 0 and i0' = x and i1' = y and x >= 1 and x <= R and y >= 1 and y <= C and R >= 1 and C >= 1 and 64T_i0' <= x and 64T_i0' >= -63 + x and 64T_i0' <= -32 + x - 64Dir_i0' and 64T_i0' >= -95 + x - 64Dir_i0' and Dir_i0' >= -1 and Dir_i0' <= 0 and 64T_i1' <= y and 64T_i1' >= -63 + y and 64T_i1' <= -32 + y - 64Dir_i1' and 64T_i1' >= -95 + y - 64Dir_i1' and Dir_i1' >= -1 and Dir_i1' <= 0 }") #gen = isl.UnionMap("[R, C] -> { harris[x, y] -> [1, x, y] : C = 10 and R = 10 and x >= 2 and x <= 9 and y >= 2 and y <= 9; Iyy[x, y] -> [0, x, y] : C = 10 and R = 10 and x >= 1 and x <= 10 and y >= 1 and y <= 10}") genbld = isl.AstBuild.from_context(isl.BasicSet("[C, R]->{: R > 1 and C > 1}")) #genbld = astbld.set_options(isl.UnionMap("{[i,j] -> unroll[0] : i < 4 or i > 99996}")) id_ = isl.Id.alloc(ctx, "Test1", "FakeObj") gen = gen.set_tuple_id(isl._isl.dim_type.in_, id_) #id_ = isl.Id.alloc(ctx, "Test2", "FakeObj") #print gen.get_tuple_name(isl._isl.dim_type.out) genbld = genbld.set_options(isl.UnionMap("[C, R] -> { [Dir_i0', T_i0', Dir_i1', T_i1', t, i0', i1'] -> unroll[x] : x = 0 or x = 2}")) idl = isl.IdList.alloc(ctx, 3) print idl idl = idl.add(isl.Id.alloc(ctx, 'Dir_i0', None)) idl = idl.add(isl.Id.alloc(ctx, 'T_i0', None)) idl = idl.add(isl.Id.alloc(ctx, 'Dir_i1', None)) idl = idl.add(isl.Id.alloc(ctx, 'T_i1', None)) idl = idl.add(isl.Id.alloc(ctx, 't', None)) idl = idl.add(isl.Id.alloc(ctx, 'i0', None)) idl = idl.add(isl.Id.alloc(ctx, 'i1', None)) genbld = genbld.set_iterators(idl) printer = isl.Printer.to_str(ctx) printer = printer.set_output_format(isl.format.C) #astRoot = isl.AstBuild.ast_from_schedule(genbld, isl.UnionMap.from_map(gen)) print genbld.get_schedule_space() astRoot = genbld.ast_from_schedule(isl.UnionMap.from_map(gen)) print genbld.get_schedule_space() printer = astRoot.print_(printer, isl.AstPrintOptions.alloc(ctx)) print printer.get_str() applyBase = isl.BasicMap("[rows, cols] -> { Dx_2_img2[c, x, y] -> [3, c, x, y] : c >= 0 and c <= 2 and x >= 2 and 4x <= 4 + rows and y >= 2 and 2y <= 2 + cols and rows >= 1 and cols >= 1 }") align = isl.BasicMap("[rows, cols]->{[a, b, c, d] -> [a, d, b, c]}") print applyBase.apply_range(align) mem = isl.BasicMap("[cols, rows] -> { Uy_2_img2[c, x, y] -> [c, T_i1', T_i2', 7, 4x, 4y] : c >= 0 and c <= 2 and x >= 2 and 4x <= 4 + rows and y >= 2 and 4y <= 4 + cols and rows >= 1 and cols >= 1 and 64T_i1' <= -2 + x and 64T_i1' >= -70 + x and 64T_i2' <= -2 + y and 64T_i2' >= -73 + y }") acc = isl.BasicMap("[cols, rows] -> { Uy_2_img2[c, x, y] -> [c, T_i1', T_i2', 7, x-64T_i1', y-64T_i2'] : c >= 0 and c <= 2 and x >= 2 and 4x <= 4 + rows and y >= 2 and 4y <= 4 + cols and rows >= 1 and cols >= 1 and 64T_i1' <= -2 + x and 64T_i1' >= -70 + x and 64T_i2' <= -2 + y and 64T_i2' >= -73 + y and rows >= 256 and cols >= 256}") print acc.range().dim_min(4), acc.range().dim_max(4) split = isl.BasicMap("[cols, rows] -> { Uy_0_img1[c, x, y, _Mul_x, _Mul_y] -> [_T_i1, _T_i2, 1, 5, c, x, y] : exists (e0 = floor((-1 + y)/2): rows = 768 and 64_T_i1 = x - _Mul_x and 64_T_i2 = y - _Mul_y and cols = 1024 and 2e0 = -1 + y and c >= 0 and c <= 2 and x >= 2 and x <= 769 and y >= 2 and y <= 1025) }") print split
isl.Constraint.ineq_from_names(space, { 1: -1, "x": 1 })).add_constraint(isl.Constraint.ineq_from_names(space, { 1: 5, "x": -1 })).add_constraint(isl.Constraint.ineq_from_names(space, { 1: -1, "y": 1 })).add_constraint(isl.Constraint.ineq_from_names(space, { 1: 5, "y": -1 }))) print("set 1 %s:" % bset) bset2 = isl.BasicSet("{[x, y] : x >= 0 and x < 5 and y >= 0 and y < x+4 }") print("set 2: %s" % bset2) bsets_in_union = [] bset.union(bset2).convex_hull().foreach_basic_set(bsets_in_union.append) print(bsets_in_union) union, = bsets_in_union print("union: %s" % union) # ENDEXAMPLE import matplotlib.pyplot as pt def plot_basic_set(bset, *args, **kwargs): # This is a total hack. But it works for what it needs to do. :)
def to_batched(kernel, nbatches, batch_varying_args, batch_iname_prefix="ibatch", sequential=False): """Takes in a kernel that carries out an operation and returns a kernel that carries out a batch of these operations. .. note:: For temporaries in a kernel that are private or read only globals and if `sequential=True`, loopy does not does not batch these variables unless explicitly mentioned in `batch_varying_args`. :arg nbatches: the number of batches. May be a constant non-negative integer or a string, which will be added as an integer argument. :arg batch_varying_args: a list of argument names that vary per-batch. Each such variable will have a batch index added. :arg sequential: A :class:`bool`. If *True*, do not duplicate temporary variables for each batch. This automatically tags the batch iname for sequential execution. """ from pymbolic import var vng = kernel.get_var_name_generator() batch_iname = vng(batch_iname_prefix) batch_iname_expr = var(batch_iname) new_args = [] batch_dom_str = "{{[{iname}]: 0 <= {iname} < {nbatches}}}".format( iname=batch_iname, nbatches=nbatches, ) if not isinstance(nbatches, int): batch_dom_str = "[%s] -> " % nbatches + batch_dom_str new_args.append(ValueArg(nbatches, dtype=kernel.index_dtype)) nbatches_expr = var(nbatches) else: nbatches_expr = nbatches batch_domain = isl.BasicSet(batch_dom_str) new_domains = [batch_domain] + kernel.domains for arg in kernel.args: if arg.name in batch_varying_args: if isinstance(arg, ValueArg): arg = ArrayArg(arg.name, arg.dtype, shape=(nbatches_expr,), dim_tags="c") else: arg = arg.copy( shape=(nbatches_expr,) + arg.shape, dim_tags=("c",) * (len(arg.shape) + 1), dim_names=_add_unique_dim_name("ibatch", arg.dim_names)) new_args.append(arg) kernel = kernel.copy( domains=new_domains, args=new_args) if not sequential: new_temps = {} for temp in kernel.temporary_variables.values(): if temp_needs_batching_if_not_sequential(temp, batch_varying_args): new_temps[temp.name] = temp.copy( shape=(nbatches_expr,) + temp.shape, dim_tags=("c",) * (len(temp.shape) + 1), dim_names=_add_unique_dim_name("ibatch", temp.dim_names)) else: new_temps[temp.name] = temp kernel = kernel.copy(temporary_variables=new_temps) else: import loopy as lp from loopy.kernel.data import ForceSequentialTag kernel = lp.tag_inames(kernel, [(batch_iname, ForceSequentialTag())]) rule_mapping_context = SubstitutionRuleMappingContext( kernel.substitutions, vng) bvc = _BatchVariableChanger(rule_mapping_context, kernel, batch_varying_args, batch_iname_expr, sequential=sequential) kernel = rule_mapping_context.finish_kernel( bvc.map_kernel(kernel)) batch_iname_set = frozenset([batch_iname]) kernel = kernel.copy( instructions=[ insn.copy(within_inames=insn.within_inames | batch_iname_set) for insn in kernel.instructions]) return kernel
def to_batched(knl, nbatches, batch_varying_args, batch_iname_prefix="ibatch", sequential=False): """Takes in a kernel that carries out an operation and returns a kernel that carries out a batch of these operations. :arg nbatches: the number of batches. May be a constant non-negative integer or a string, which will be added as an integer argument. :arg batch_varying_args: a list of argument names that vary per-batch. Each such variable will have a batch index added. :arg sequential: A :class:`bool`. If *True*, do not duplicate temporary variables for each batch. This automatically tags the batch iname for sequential execution. """ from pymbolic import var vng = knl.get_var_name_generator() batch_iname = vng(batch_iname_prefix) batch_iname_expr = var(batch_iname) new_args = [] batch_dom_str = "{[%(iname)s]: 0 <= %(iname)s < %(nbatches)s}" % { "iname": batch_iname, "nbatches": nbatches, } if not isinstance(nbatches, int): batch_dom_str = "[%s] -> " % nbatches + batch_dom_str new_args.append(ValueArg(nbatches, dtype=knl.index_dtype)) nbatches_expr = var(nbatches) else: nbatches_expr = nbatches batch_domain = isl.BasicSet(batch_dom_str) new_domains = [batch_domain] + knl.domains for arg in knl.args: if arg.name in batch_varying_args: if isinstance(arg, ValueArg): arg = GlobalArg(arg.name, arg.dtype, shape=(nbatches_expr, ), dim_tags="c") else: arg = arg.copy(shape=(nbatches_expr, ) + arg.shape, dim_tags=("c", ) * (len(arg.shape) + 1), dim_names=_add_unique_dim_name( "ibatch", arg.dim_names)) new_args.append(arg) knl = knl.copy(domains=new_domains, args=new_args) if not sequential: new_temps = {} for temp in six.itervalues(knl.temporary_variables): if temp.initializer is not None and temp.read_only: new_temps[temp.name] = temp else: new_temps[temp.name] = temp.copy( shape=(nbatches_expr, ) + temp.shape, dim_tags=("c", ) * (len(temp.shape) + 1), dim_names=_add_unique_dim_name("ibatch", temp.dim_names)) knl = knl.copy(temporary_variables=new_temps) else: import loopy as lp from loopy.kernel.data import ForceSequentialTag knl = lp.tag_inames(knl, [(batch_iname, ForceSequentialTag())]) rule_mapping_context = SubstitutionRuleMappingContext( knl.substitutions, vng) bvc = _BatchVariableChanger(rule_mapping_context, knl, batch_varying_args, batch_iname_expr, sequential=sequential) kernel = rule_mapping_context.finish_kernel(bvc.map_kernel(knl)) batch_iname_set = frozenset([batch_iname]) kernel = kernel.copy(instructions=[ insn.copy(forced_iname_deps=insn.forced_iname_deps | batch_iname_set) for insn in kernel.instructions ]) return kernel
def test_creation_error(): # note the (intentional) syntax error with pytest.raises(isl.Error): isl.BasicSet( "[n0, n1] -> " "{ [i0, i1, i2] : 0 <= i0 < n1 and 0 and 0 <= i2 <= 15 }")
def test_get_coefficients_by_name(): my_set = isl.BasicSet("{ [k, l] : 3l >= -k and 3l <= 10 - k " "and k >=0 and k <= 2 }") for c in my_set.get_constraints(): print(c.get_coefficients_by_name())