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 no_test_id_user(): ctx = isl.Context() foo = isl.Id("foo", context=ctx) # noqa t = (1, 2) bar = isl.Id("bar", t, context=ctx) assert bar.user is t
def matplotlibTest(): fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.set_autoscale_on(True) ax.locator_params(integer=True) ax.grid(True) ctx = isl.Context() space = isl.Space.create_from_names(ctx, set=["x", "y"]) bset = (isl.BasicSet.universe(space) .add_constraint(isl.Constraint.ineq_from_names(space, {1: -1, "x": 1})) .add_constraint(isl.Constraint.ineq_from_names(space, {1: 5, "x": -1, "y":-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}))) drawBasicSet(bset, { 0:'x', 1:'y'}, ax) plt.show() fig = plt.figure() ax = fig.add_subplot(111) ax.set_autoscale_on(True) ax.locator_params(integer=True) ax.grid(True) drawBasicSet(bset, { 0:'x', 1:'y', 'color':(0, ('red', 'blue'))}, ax) plt.show()
def test_ast_node_list_free(): # from https://github.com/inducer/islpy/issues/21 # by Cambridge Yang ctx = isl.Context() schedule_map = isl.UnionMap.read_from_str( ctx, "[N] -> { S0[i] -> [i, 0] : " "0 <= i < N; S1[i] -> [i, 1] : 0 <= i < N }") ast_build = isl.AstBuild.from_context( isl.Set.read_from_str(ctx, "[N] -> { : }")) ast = ast_build.node_from_schedule_map(schedule_map) print(ast.to_C_str()) # Prints below code: # for (int c0 = 0; c0 < N; c0 += 1) { # S0(c0); # S1(c0); # } # we have S0 and S1 in a ast_node_block, which holds "children" of type # ASTNodeList body = ast.for_get_body() assert body.get_type() == isl.ast_node_type.block body.block_get_children()
def test_schedule_dump(): ctx = isl.Context() s = isl.UnionSet.read_from_str( ctx, "{ S_2[i, j, k] : i <= 99 and i >= 0; S_3[i] : " "i <= 99 and i >= 0; S_0[]; S_1[i] : i <= 99 and i >= 0 }") cst = isl.ScheduleConstraints.on_domain(s) schedule = isl.ScheduleConstraints.compute_schedule(cst) schedule.dump()
def test_get_schedule_map(): ctx = isl.Context() ss = isl.UnionSet.read_from_str( ctx, "[m, n] -> { S_2[i, j, k] : " "j <= -1 + m and j >= 0 and k <= -1 + n and k >= 0 }") cst1 = isl.ScheduleConstraints.on_domain(ss) sub_schedule = isl.ScheduleConstraints.compute_schedule(cst1) sub_schedule.get_map()
def test_from_union_map(): ctx = isl.Context() m = isl.UnionMap.read_from_str(ctx, "[m, n] -> { S_0[] -> [0, 0, 0, 0]; S_1[i] -> [i, 1, 0, 0]; S_3[i] -> " "[1 + i, 3, 0, 0]; S_2[i, j, k] -> [i, 2, j, k] : " "j <= -1 + m and j >= 0 and k <= -1 + n and k >= 0 }") isl.MultiUnionPwAff.from_union_map(m)
def test_error_on_invalid_index(): ctx = isl.Context() my_set = isl.Set( "{ [k, l] : 3l >= -k and 3l <= 10 - k " "and k >=0 and k <= 2 }", context=ctx) p = my_set.sample_point() with pytest.raises(isl.Error): p.get_coordinate_val(isl.dim_type.set, 99999999)
def test_remove_map_if_callback(): ctx = isl.Context() umap = isl.UnionMap.read_from_str(ctx, "{A[0] -> [1]; B[1] -> [2]}") umap1 = umap.remove_map_if(lambda m: False) assert umap1 == umap, "map should not change" umap2 = umap.remove_map_if( lambda m: m.get_tuple_name(isl.dim_type.in_) == "B") assert umap2 == isl.UnionMap.read_from_str(ctx, "{A[0] -> [1]}")
def test_aff_to_expr(): s = isl.Space.create_from_names(isl.Context(), ["a", "b"]) zero = isl.Aff.zero_on_domain(isl.LocalSpace.from_space(s)) one = zero.set_constant_val(1) # noqa a = zero.set_coefficient_val(isl.dim_type.in_, 0, 1) b = zero.set_coefficient_val(isl.dim_type.in_, 1, 1) x = (5 * a + 3 * b) % 17 % 5 print(x) from loopy.symbolic import aff_to_expr print(aff_to_expr(x))
def test_error_on_invalid_index(): ctx = isl.Context() my_set = isl.Set("{ [k, l] : 3l >= -k and 3l <= 10 - k " "and k >=0 and k <= 2 }", context=ctx) p = my_set.sample_point() try: p.get_coordinate_val(isl.dim_type.set, 99999999) except isl.Error: pass else: assert False
def test_remove_map_if_callback_exc(): pytest.skip( "https://github.com/inducer/islpy/pull/33#issuecomment-705165253") ctx = isl.Context() umap = isl.UnionMap.read_from_str(ctx, "{A[0] -> [1]; B[1] -> [2]}") def callback_throws_exception(m): 1 / 0 with pytest.raises(isl.Error): umap3 = umap.remove_map_if(callback_throws_exception) del umap3
def test_pluto_schedule(): pluto = libpluto.LibPluto() print(pluto) ctx = isl.Context() schedule_str = "[p_0, p_1, p_2, p_3, p_4, p_5, p_7] -> { S_1[i0, i1] : i0 >= 0 and i0 <= p_0 and i1 >= 0 and i1 <= p_3 and p_2 >= 0; S_0[i0] : i0 >= 0 and i0 <= p_0}"; deps_str = "[p_0, p_1, p_2, p_3, p_4, p_5, p_7] -> { S_0[i0] -> S_1[o0, o1] : (exists (e0 = [(p_7)/8]: 8o1 = -p_5 + p_7 + 8192i0 - 8192o0 and 8e0 = p_7 and i0 >= 0 and o0 <= p_0 and 8192o0 >= -8p_3 - p_5 + p_7 + 8192i0 and 8192o0 <= -p_5 + p_7 + 8192i0 and p_2 >= 0 and o0 >= 1 + i0)); S_1[i0, i1] -> S_0[o0] : (exists (e0 = [(p_1)/8], e1 = [(p_4)/8], e2 = [(-p_1 + p_7)/8184]: 8192o0 = p_5 - p_7 + 8192i0 + 8i1 and 8e0 = p_1 and 8e1 = p_4 and 8184e2 = -p_1 + p_7 and i1 >= 0 and 8i1 <= 8192p_0 - p_5 + p_7 - 8192i0 and 8184i1 >= 1024 + 1024p_1 - 1023p_5 - p_7 - 8380416i0 and p_2 >= 0 and p_7 <= -1 + p_5 and 8i1 >= 1 + 8p_3 + p_4 - p_5 - 8192i0 and i1 <= p_3 and i0 >= 0 and 8i1 >= 8192 - p_5 + p_7))}" schedule = isl.UnionSet.read_from_str(ctx, schedule_str) deps = isl.UnionMap.read_from_str(ctx, deps_str) opts = pluto.create_options() opts.partlbtile = 1 out_schedule = pluto.schedule(ctx, schedule, deps, opts) print(out_schedule)
def main(): ctx = isl.Context() a_s = isl.Map.read_from_str( ctx, "{ a_4_s[i0, i1, i2] -> [i0 + i2, i1, 10 + i2, 13] : i0 >= 0 and i1 >= 0 and i1 <= 9 and i2 >= 0 and i2 <= 63 and i0 < 20}" ) f1_s = isl.Map.read_from_str( ctx, "{ folding_4_s1[i0, i1, i2] -> [i0 + i2, 9 + i1, 18 + i2, 11] : i0 >= 0 and i1 >= 0 and i1 <= 9 and i2 >= 1 and i2 <= 63 and i0 < 20}" ) f0_s = isl.Map.read_from_str( ctx, "{ folding_4_s0[i0, i1, 0] -> [i0, 9, 10 + i1, 2] : i0 >= 0 and i1 >= 0 and i1 <= 9 and i0 < 20}" ) a_f0 = isl.Map.read_from_str( ctx, "{ a_4_s[i0, i1, 0] -> folding_4_s0[i0, i1, 0] : i0 >= 0 and i1 >= 0 and i1 <= 9 }" ) a_f1 = isl.Map.read_from_str( ctx, "{ a_4_s[i0, i1, i2] -> folding_4_s1[i0, i1, i2] : i0 >= 0 and i1 >= 0 and i1 <= 9 and i2 >= 1 and i2 <= 63 }" ) a_t = a_s.range() f1_t = f1_s.range() f0_t = f0_s.range() a_f0_t = a_f0.apply_domain(a_s).apply_range(f0_s) a_f1_t = a_f1.apply_domain(a_s).apply_range(f1_s) print a_t print f1_t print f0_t print a_f0_t #islplot.plotter.plot_set_points(a_t.project_out(isl.dim_type.set, 0, 1), 'blue') #islplot.plotter.plot_set_points(f0_t.project_out(isl.dim_type.set, 0, 1), 'red') #islplot.plotter.plot_set_points(f1_t.project_out(isl.dim_type.set, 0, 1), 'green') #islplot.plotter.plot_map(a_f0_t) islplot.plotter.plot_map(isl.Map( "{ [i0, i1] -> [i0, 9] : i0 >= 0 and i1 >= 0 and i1 <= 9 and i0 <= 19 }" ), edge_width=3) #islplot.plotter.plot_map(isl.Map.read_from_str(ctx, "{[2,8] -> [x,y]: 1 < x < 9 and 1 < y < 9 and x = y}")) #islplot.plotter.plot_map(a_f1_t) pylab.show()
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
if pointSpec is not None: pointSize = pointSpec if axes.name == '3d': axes.scatter(X, Y, Z, s=pointSize, c = C) elif axes.name == 'rectilinear': axes.scatter(X, Y, s=pointSize, c = C) else: assert False def LocalSpaces(ctx): # A local space is essentially a space with zero or more existentially # quantified variables. pass ctx = isl.Context() #Spaces(ctx) #SetsAndRelations(ctx) SetsAsIterationSpaces(ctx) #LocalSpaces(ctx) 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)
def TileGen(sym_exvars, vars, _SYM, symb, p_symb, par_tiling, rel, schedule, instrukcje, stuff_reduced, BLOCK, isl_TILEprim, varsprim, isl_IT, isl_REST_new): ctx = isl.Context() tileall = [] i = 0 for isl_BL in isl_TILEprim: TA = isl_BL # tak bylo oryginalnie #TA = RemoveOtherSt(isl_BL, instrukcje[i], sym_exvars) tileall.append(Project(TA, sym_exvars)) i = i + 1 R = "R := [" R = R + _SYM # dodaj zmienne symb ilorazow if(par_tiling): for i in range(0,len(sym_exvars)): R = R + " fff" + str(i) + "," R = R[:-1] + "] -> {[" for s in vars: R = R + s + "," R = R + "v] -> [" for s in sym_exvars: R = R + s + "," for s in vars: R = R + s + "," R = R + "v] : " if(par_tiling): for i in range(0,len(sym_exvars)): R = R + " " + sym_exvars[i] + "<= fff" + str(i)+ " and " R = R + " true };\n" isl_Rapply = isl.Map(R.replace('R := ','')) isl_TILEALL = tileall[0] for i in range(1, len(tileall)): isl_TILEALL = isl_TILEALL.union(tileall[i]) if(DEBUG): print "TILE_ALL" print isl_TILEALL isl_BL = [] i=0 for bl in isl_TILEprim: BLj1 = bl.apply(isl_Rapply).coalesce() # przepusc przez R BLj1 = Project(BLj1, sym_exvars) # wywal ii, jj z symb # wywal powtarzajace sie RA = "{[" + ",".join(sym_exvars) + "," + ",".join(vars) + ",v] -> [" + ",".join(varsprim) + "," + ",".join(vars) + ",v] : " + CreateLex(varsprim, sym_exvars) + "}" tmp1 = BLj1 .apply(isl.Map(RA)) tmp1 = BLj1.intersect(tmp1) BLj1 = BLj1.subtract(tmp1) isl_BL.append(BLj1) # mozna juz do gen code if(DEBUG): print "TILE_bis" + str(i) print BLj1 i = i + 1 for i in range(0, len(isl_REST_new)): isl_REST_new[i] = isl_REST_new[i].apply(isl_Rapply).coalesce() # przepusc przez R isl_REST_new[i] = Project(isl_REST_new[i], sym_exvars) # wywal ii, jj z symb isl_REST = [] for i in range(0, len(isl_TILEprim)): if(DEBUG): print "IT" + str(i) print isl_IT[i] tmp = isl_IT[i].subtract(isl_TILEALL) #tmp = SlowSimplify(tmp) isl_REST.append(tmp) if(DEBUG): print "REST" + str(i) print isl_REST[i] # na przyszlosc trzeba tutaj inaczej wyparsowac instrukcje oddzielic ### barv_cmds2 = "" for j in range(0, len(isl_REST)): #if(not "-> { }" in str(isl_REST[j])): if not isl_REST[j].is_empty(): W = isl_REST[j].apply(isl_Rapply) barv_cmds2 = barv_cmds2 + "REST" + str(j) + ":= " + str(W) + ";codegen REST"+str(j)+";" # isl_REST[j] bylo zamiast W start = time.time() loop = iscc.iscc_communicate(barv_cmds2) loop = "" with open("tmp/barv_tiling.txt", "a") as myfile: myfile.write(barv_cmds2) end = time.time() elapsed = end - start if(DEBUG): print "Time taken: ", elapsed, "seconds." loop = codegen.tile_gen(vars, sym_exvars, symb,isl_BL,instrukcje, loop, isl_REST_new) loops = [] loops.append(loop) return loops
def test_id_constructor(): ctx = isl.Context() isl.Id(context=ctx, name='x0')
def test_copy_context(): ctx = isl.Context() import copy assert not ctx._wraps_same_instance_as(copy.copy(ctx)) assert not isl.DEFAULT_CONTEXT._wraps_same_instance_as(copy.copy(ctx))