コード例 #1
0
ファイル: tiling_schedule.py プロジェクト: lshadown/traco
def tile_par3(isl_TILEbis, sym_exvars, isl_rel, isl_relplus, isl_relclosure,
              Extend, _rap, Dodatek, SIMPLIFY, ii_SET):

    #print isl_TILEbis
    srepr, rucs = slicing.Create_Srepr(isl_rel, isl_relclosure)

    fs = 0

    ir = isl_rel.domain().union(isl_rel.range()).coalesce()
    ir = ir.insert_dims(isl.dim_type.set, 0, len(sym_exvars))
    for i in range(0, len(sym_exvars)):
        ir = ir.set_dim_name(isl.dim_type.set, i, sym_exvars[i])

    if (Extend):
        for i in range(0, 2 * len(sym_exvars)):
            ir = ir.insert_dims(isl.dim_type.set, 2 * i, 1)

    srepr = srepr.insert_dims(isl.dim_type.set, 0, len(sym_exvars))
    for i in range(0, len(sym_exvars)):
        srepr = srepr.set_dim_name(isl.dim_type.set, i, sym_exvars[i])

    if (Extend):
        for i in range(0, 2 * len(sym_exvars)):
            srepr = srepr.insert_dims(isl.dim_type.set, 2 * i, 1)

    x = 1
    if (Extend):
        x = 2

    #relacja do obliczenia poczatkow
    Rel_Z = "{["
    for i in range(0, 2 * x * len(sym_exvars)):
        Rel_Z = Rel_Z + "i" + str(i) + ","
    Rel_Z = Rel_Z + "m] -> ["
    for i in range(0, x * len(sym_exvars)):
        Rel_Z = Rel_Z + "i" + str(i) + ","
    Rel_Z = Rel_Z[:-1] + "] };"

    Rel_Z = isl.Map(Rel_Z)

    #relacja do wylapania instrukcji z blokow
    Rel_Y = "{["
    for i in range(0, x * len(sym_exvars)):
        Rel_Y = Rel_Y + "i" + str(i) + ","
    for i in range(0, x * len(sym_exvars) + 1):
        Rel_Y = Rel_Y + "j" + str(i) + ","
    Rel_Y = Rel_Y[:-1] + "] -> ["
    for i in range(0, x * len(sym_exvars)):
        Rel_Y = Rel_Y + "i" + str(i) + ","
    for i in range(0, x * len(sym_exvars) + 1):
        Rel_Y = Rel_Y + "k" + str(i) + ","
    Rel_Y = Rel_Y[:-1] + "] };"

    Rel_Y = isl.Map(Rel_Y)

    Rel_W = "{["
    for i in range(0, x * len(sym_exvars)):
        Rel_W = Rel_W + "i" + str(i) + ","
    Rel_W = Rel_W + "m1] -> ["
    for i in range(0, x * len(sym_exvars)):
        Rel_W = Rel_W + "o" + str(i) + ","
    Rel_W = Rel_W + "m2] : not ("
    for i in range(0, x * len(sym_exvars)):
        Rel_W = Rel_W + "i" + str(i) + " = " + "o" + str(i) + " and "
    Rel_W = Rel_W[:-4] + ")}"

    TILE_SOUR = isl_TILEbis.intersect(srepr).coalesce()

    TILE_IND = isl_TILEbis.subtract(isl_TILEbis.intersect(ir)).coalesce()

    indloop = iscc.iscc_communicate("L :=" + str(TILE_IND) + "; codegen L;")

    if (TILE_SOUR.lexmax() == TILE_SOUR.lexmin()):
        print "FS"
        fs = 1
    else:
        print "========================================================================="
        print "SLICING + TILING"
        if (rucs.is_empty()):
            isl_rel = isl_rel.union(rucs)
            isl_relclosure = isl_relclosure.union(rucs)
            isl_relplus = isl_relplus.union(rucs)

    rplus = tiling_v3.ExtendMap(isl_relplus, sym_exvars, Extend)
    rstar = tiling_v3.ExtendMap(isl_relclosure, sym_exvars, Extend)
    rel_ = tiling_v3.ExtendMap(isl_rel, sym_exvars, Extend)
    rucs = tiling_v3.ExtendMap(rucs, sym_exvars, Extend)

    rbis = isl.Map.from_domain_and_range(isl_TILEbis, isl_TILEbis)

    rs = rbis.intersect(rstar)

    rs = rs.union(rucs.transitive_closure()[0].intersect(rbis)).coalesce()

    Exp = True
    TMP2 = TILE_SOUR
    if (Exp and fs != 1):
        rel_simple = rbis.intersect(rel_)
        print rel_simple

        rel_simple = rel_simple.project_out(isl.dim_type.in_,
                                            x * len(sym_exvars),
                                            x * len(sym_exvars))
        rel_simple = rel_simple.project_out(isl.dim_type.out,
                                            x * len(sym_exvars),
                                            x * len(sym_exvars))

        relw = isl.Map(Rel_W)
        rel_simple = rel_simple.intersect(relw).coalesce()

        rel_simple = rel_simple.coalesce()

        print "RT"
        print rel_simple

        #independent tiles

        indt = rel_simple.domain().union(rel_simple.range()).coalesce()

        print indt
        #print II_SET

        #indt = ii_SET.subtract(indt).coalesce()

        # II_SET z Clana

        rel_simple_plus = rel_simple.transitive_closure()[0]

        print "RT+"
        print rel_simple_plus

        srepr, rucs = slicing.Create_Srepr(rel_simple, rel_simple_plus)

        print srepr

        # NAPRAWIC

        # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        #add independent tiles for srepr

        #srepr = srepr.union(indt).coalesce()

        print "SREPR"
        print srepr
        print "RUCS"
        print rucs

        #sys.exit()

        # srepr = imperf_tile.SimplifySlice(srepr)
        srepr = srepr.insert_dims(isl.dim_type.set, x * len(sym_exvars),
                                  x * len(sym_exvars))
        TILE_SOUR = isl_TILEbis.intersect(srepr).coalesce()

        TMP2 = TILE_SOUR

    if (fs != 1):

        if (rucs.is_empty()):
            TILE_SOUR = TILE_SOUR.subtract(TILE_SOUR.apply(
                rplus)).coalesce()  # remove dependent blocks with srepr

        TILE_RUCS = slicing.Create_RUCS(rel_, rs, TILE_SOUR, TILE_SOUR, 1,
                                        Rel_Y)

        TILE_SOUR = TILE_SOUR.subtract(TILE_RUCS.range()).coalesce()

        print "TILE_RUCS"
        print TILE_RUCS
        print "TILE_SOUR"
        print TILE_SOUR

        TILE_SOUR = TMP2
        z = TILE_SOUR.apply(Rel_Z).coalesce()

        if (SIMPLIFY):
            z = imperf_tile.SimplifySlice(z)

        srepr_loop = iscc.iscc_communicate("L :=" + str(z) +
                                           "; codegen L;")  # albo omega
        #srepr_loop = iscc.oc_communicate(z)

        srepr_loop = srepr_loop.split('\n')

        for i in range(0, len(srepr_loop)):
            if "for" in srepr_loop[i]:
                srepr_loop.insert(i, "#pragma omp parallel for")
                break

        #print srepr_loop

        st_reg = re.compile('\s*\(.*\);')
        vecs = []
        taby = []
        for line in srepr_loop:
            if (st_reg.match(line)):
                vecs.append(isl.Set(iscc.s1_to_vec2(line, len(vecs))))
                taby.append(iscc.correct.whites(line))

        #print vecs

        permutate_maps = Dodatek[6]
        permutate_list = Dodatek[5]

        slices = []

        for i in range(0, len(vecs)):
            vecs[i] = vecs[i].intersect(z).coalesce()

        for vec in vecs:
            vec = vec.insert_dims(isl.dim_type.set, x * len(sym_exvars),
                                  x * len(sym_exvars) + 1)
            #vec = vec.insert_dims(isl.dim_type.set, x*len(sym_exvars), 1)
            slice = vec  #.apply(rs)
            slice = slice.apply(Rel_Y)
            slice = slice.apply(rs).coalesce()
            #print slice
            #print rs1

            #slice = slice.apply(rs1).coalesce()
            #slice = slice.insert_dims(isl.dim_type.set, x*len(sym_exvars), x*len(sym_exvars))
            #slice = slice.intersect(isl_TILEbis).coalesce()

            # experimental permutate

            if (len(permutate_list) > 0):
                print "Tiling + slicing + permutation - experimental"
                RP = permutate_maps[0]

                RIDENT = RP.identity(RP.get_space())

                if (not RP.is_equal(RIDENT)
                    ):  #permute map is not an identity map
                    strRP = str(RP)
                    strh = ""
                    for i in range(0, x * len(sym_exvars)):
                        strh = strh + "xx" + str(i) + ","

                    if (Extend):
                        strkoma = strRP.split(",")
                        strRP = ""
                        for i in range(0, 2 * len(sym_exvars)):
                            strRP = strRP + strkoma[i] + ", yy" + str(
                                i % len(sym_exvars)) + ", "
                        strRP = strRP + strkoma[2 * len(sym_exvars)]

                    strRP = strRP.replace("[", "[" + strh)
                    RP = isl.Map(strRP)

                    slice = slice.apply(RP).coalesce()
            # ---------------------------------------------------------

            if (SIMPLIFY and False):
                slice = imperf_tile.SimplifySlice(slice)

            slices.append(slice)
            #print slice

        cmd = ""
        for i in range(0, len(vecs)):
            print "SLICE"
            print slices[i]
            cmd = cmd + "codegen " + str(slices[i]) + ';print "###";'

        cmd = iscc.iscc_communicate(cmd)

        cmd = cmd.split('"###"')

        new_loop = []
        i = 0
        for line in srepr_loop:
            if (st_reg.match(line)):
                petla = cmd[i].split('\n')
                for s in petla:
                    new_loop.append(taby[i] + s)
                i = i + 1
            else:
                new_loop.append(line)

        nloop = ""
        for line in new_loop:
            if line != '':
                nloop = nloop + line + "\n"
        nloop = nloop[:-1]

        nloop = nloop + "// independent statements \n" + indloop

        nloop = nloop.split('\n')

        # Dodatek = [LPetit, dane,  maxl, step, nazwapar, permutate_list, permutate_maps]

        #permutate list
        nloop = tiling_v3.postprocess_loop(nloop)
        lines = nloop.split('\n')

        loop = imperf_tile.RestoreStatements(lines, Dodatek[0], Dodatek[1],
                                             Dodatek[2], Dodatek[3],
                                             Dodatek[5])

        text_file = open(Dodatek[4], "w")
        text_file.write(loop)
        text_file.close()
        return ""
    else:

        rel_ = rbis.intersect(rel_)

        rel_ = rel_.project_out(isl.dim_type.in_, x * len(sym_exvars),
                                x * len(sym_exvars))
        rel_ = rel_.project_out(isl.dim_type.out, x * len(sym_exvars),
                                x * len(sym_exvars))
        rel_ = rel_.coalesce()

        relw = isl.Map(Rel_W)

        rel_ = rel_.intersect(relw).coalesce()

        print "REL"
        print rel_

        #independent tiles
        #indt = rel_.domain().union(rel_.range()).coalesce()
        #indt = ii_SET.subtract(indt).coalesce()

        indt = ""

        # FS z RK
        sk = fs_rk.fs_rk1(rel_, SIMPLIFY, indt)

        # FS bez RK
        #sk = fs_karl.FSwithoutRG(rel_)

        sk = sk.insert_dims(isl.dim_type.set,
                            x * len(sym_exvars) + 1, x * len(sym_exvars))

        bis = isl_TILEbis.insert_dims(isl.dim_type.set, 0, 1)

        sk = bis.intersect(sk).coalesce()

        if (SIMPLIFY):
            sk = imperf_tile.SimplifySlice(sk)
        #sys.exit(0);
        print "SK"
        print sk

        print "Codegen now..."
        #fsloop = iscc.iscc_communicate("L :=" + str(sk) + "; codegen L;")

        fsloop = iscc.isl_ast_codegen(sk)

        print "Codegen done."

        nloop = fsloop.split('\n')
        nloop = tiling_v3.postprocess_loop(nloop)
        lines = fs_pragma(nloop)

        #Dodatek[2]+1 shift + 1  for k
        #if extend first shift shoud be one smaller
        if (Extend):
            sh = 1
        else:
            sh = 0

        loop = imperf_tile.RestoreStatements(lines, Dodatek[0], Dodatek[1],
                                             Dodatek[2] + 1, Dodatek[3],
                                             Dodatek[5], sh)

        text_file = open(Dodatek[4], "w")
        text_file.write(loop)
        text_file.close()
        return ""
コード例 #2
0
ファイル: fsnew_tiletry.py プロジェクト: lshadown/traco
def fs_new(rel, rel_plus, rtile, LPetit, dane, plik, SIMPLIFY, rap, exact,
           isl_TILEbis, sym_exvars, maxl, step, isl_tilevld, vars):

    codegen = 'isl'

    # compute rtile_plus

    # podmien exact, rel na tile, rel_plus na rtile_plus

    #wq = rtile.transitive_closure()[0]

    #print wp.subtract(wq).coalesce()
    #print wq.subtract(wp).coalesce()

    if (exact):
        print 'R+ exact!'
    else:
        print 'R+ approximated. Iterate way...!'

        r0p_plus = relation_util.oc_IterateClosure(rel)
        rel_plus = r0p_plus

        isl_relclosure = rel_plus
        isl_ident = rel.identity(rel.get_space())
        isl_relclosure = isl_relclosure.union(
            isl_ident).coalesce()  # R* = R+ u I

        print 'Checking (the Pugh method)'

        # R = R compose RINV

        if (rel_plus.subtract(
                rel_plus.apply_range(rel).union(rel)).coalesce().is_empty()):
            print ' .... OK !!'
        else:
            print 'R+ failed.'
            sys.exit(0)

        #file = open('lu_rplus.txt', 'r')
        #isl_relclosure = isl.Map(file.read())
        #print isl_relclosure

    wp = GetRTilePlus(rel_plus, isl_tilevld, sym_exvars, vars).coalesce()
    rel = rtile
    rel_plus = wp

    print '## R'

    print rel

    print '## R+'

    print wp

    rel = rel.subtract(rel_plus.apply_range(rel))

    print '### R = R - R+ compose R'
    print rel

    global_size = rel.dim(isl.dim_type.in_)

    UDS = rel.domain().subtract(rel.range()).coalesce()
    UDD = rel.range().subtract(rel.domain()).coalesce()
    DOM_RAN = rel.range().union(rel.domain()).coalesce()

    cl = clanpy.ClanPy()
    cl.loop_path = plik
    cl.Load()
    cl.RunCandl()

    IS = DOM_RAN
    for i in range(0, len(cl.statements)):
        IS_ = isl.Set(cl.statements[i].domain_map).coalesce()
        print IS_
        set_size = IS_.dim(isl.dim_type.set)

        for j in range(set_size, global_size - 1):
            IS_ = IS_.insert_dims(isl.dim_type.set, j, 1)
            IS_ = IS_.set_dim_name(isl.dim_type.set, j, 'i' + str(j))
            c = isl.Constraint.eq_from_names(
                IS_.get_space(), {
                    IS_.get_dim_name(isl.dim_type.set, j): -1,
                    1: -1
                })
            IS_ = IS_.add_constraint(c).coalesce()

        set_size = IS_.dim(isl.dim_type.set)

        IS_ = IS_.insert_dims(isl.dim_type.set, set_size, 1)
        IS_ = IS_.set_dim_name(isl.dim_type.set, set_size, "v")

        #print cl.statements[i].body
        c = isl.Constraint.eq_from_names(IS_.get_space(), {
            "v": -1,
            1: int(dane[i])
        })
        IS_ = IS_.add_constraint(c).coalesce()

        if i == 0:
            IS = IS_
        else:
            IS = IS.union(IS_).coalesce()

    print "IS"
    print IS

    IND = IS.subtract(DOM_RAN).coalesce()

    print "IND"
    print IND

    n = rel.dim(isl.dim_type.in_)

    inp = []
    outp = []
    for i in range(0, n):
        inp.append("i" + str(i))
        outp.append("o" + str(i))

    # Rlex
    rlex = "{[" + ",".join(inp) + "] -> [" + ",".join(
        outp) + "] : " + tiling_v3.CreateLex(outp, inp) + "}"
    rlex = isl.Map(rlex)

    rip = rel_plus.fixed_power_val(-1)

    re_rel = isl.Map.from_domain_and_range(IS, IS)

    #print re

    print "### RE"

    re_rel = re_rel.intersect(rlex.subtract(rel_plus).subtract(rip)).coalesce()

    print re_rel

    # oblicz Re1
    #Re1 = GetRe1(re_rel, rel_plus)

    #print "### RE2"
    #re_rel = re_rel.subtract(Re1).coalesce()
    #print re_rel
    re2 = re_rel

    W = re_rel.domain().union(re_rel.range()).coalesce()
    D = re_rel.domain().subtract(re_rel.range()).coalesce()

    rel_inv = rel.fixed_power_val(-1)

    print "R^-1"
    print rel_inv

    # R = R compose RINV

    #RR = rel.apply_range(rel_inv)
    RR = rel_inv.apply_range(rel)

    # ------ Jesli RCHECK ----------

    if (1 == 0):
        RRR1 = rel.fixed_power_val(2)
        RRR2 = rel_inv.fixed_power_val(2)
        RRR = RRR1.union(RRR2).coalesce()
        RR = RR.union(RRR).coalesce()

    # ------------------------------

    RR = RR.intersect(rlex).coalesce()

    print "### RR"
    print RR

    IND_lexmin = IND.lexmin()

    IND0ToIND = isl.Map.from_domain_and_range(IND_lexmin, IND).coalesce()

    RRPLUS = RR.transitive_closure()
    RR_EXACT = RRPLUS[1]
    RRPLUS = RRPLUS[0]

    if not RR_EXACT:
        print 'RR+ not exact'
        #sys.exit(0)

    # sprawdz dokladnosc

    R2 = GetR2(re_rel, RRPLUS)

    print '### RRPLUS'
    print RRPLUS

    print '### R2'
    R2 = R2.coalesce()
    print R2

    RRstar = RRPLUS
    RR_ident = RR.identity(RR.get_space())
    RRstar = RRstar.union(RR_ident).coalesce()  # R* = R+ u I

    print "### Rstar"
    print RRstar

    REPR = D.union(DOM_RAN.subtract(W)).coalesce()

    # poprawka
    REPR = R2.domain().subtract(R2.range()).coalesce()
    print '#REPR1'
    print REPR

    tmp = REPR.apply(RRstar).coalesce()
    REPR2 = DOM_RAN.subtract(tmp).coalesce()

    print '#REPR2'
    print REPR2
    REPR = REPR.union(REPR2).coalesce()

    REPR = imperf_tile.SimplifySlice(REPR)

    #####
    #REPR1:= domain    RE2 - range    RE2;
    #REPR2:= (domain    R    union    R) -RR * (REPR1);

    #REPR = REPR.intersect(IS)
    print "### REPR"
    print REPR

    if (1 == 0):
        Rtmp = REPR.polyhedral_hull()
        if (Rtmp.subtract(REPR).coalesce().is_empty()
                and REPR.subtract(Rtmp).coalesce().is_empty()):
            print "upraszczanie"
            REPR = Rtmp

    R1 = RRstar.intersect_domain(REPR.coalesce())

    #  R1 = R1.intersect_range(IS)

    print 'RSCHED obliczanie :'
    print R1
    print "IND0->IND"
    print IND0ToIND
    #print R3
    print "i razem"

    #RSCHED = R1.union(IND0ToIND).coalesce()
    RSCHED = R1

    #upraszczanie
    if (SIMPLIFY and 1 == 0):
        Rtmp = RSCHED.polyhedral_hull()
        if (Rtmp.subtract(RSCHED).coalesce().is_empty()
                and RSCHED.subtract(Rtmp).coalesce().is_empty()):
            print "upraszczanie"
            RSCHED = Rtmp

    #RSCHED = imperf_tile.SimplifyMap(RSCHED)

    print "### RSCHED"
    print RSCHED

    print "### Check "
    Check_set = RSCHED.domain().union(RSCHED.range()).coalesce()

    Check_set = IS.subtract(IND).subtract(Check_set).coalesce()

    if Check_set.is_empty():
        print "OK"
    else:
        print "ERROR ! " + str(Check_set)
        sys.exit(0)

    # generowanie kodu

    D = RSCHED.domain()

    #if(SIMPLIFY):

    print "# DOMAIN RSCHED"

    print D

    D = D.apply(rap)

    D = imperf_tile.SimplifySlice(D)
    D = D.coalesce()
    print rap
    print D

    if (codegen == 'barvinok'):
        looprepr = iscc.iscc_communicate("L :=" + str(D) + "; codegen L;")

    else:  # isl
        looprepr = iscc.isl_ast_codegen(D)

    for i in range(0, 20):
        looprepr = re.sub('\\b' + 'c' + str(i) + '\\b', 't' + str(i), looprepr)

    print looprepr

    looprepr = looprepr.split('\n')

    st_reg = re.compile('\s*\(.*\);')
    vecs = []
    taby = []
    for line in looprepr:
        if (st_reg.match(line)):
            vecs.append(line)  #(isl.Set(iscc.s1_to_vec3(line, len(vecs))))
            taby.append(iscc.correct.whites(line))

    slices = []
    for vec in vecs:

        #vec = isl.Set("[g1,g2,g3] -> {[g1,g2,g3]}")

        vec = GetConstraint(vec)

        slice = vec
        if (not RSCHED.is_empty()):
            slice_ = slice.apply(RSCHED)
            slice = slice.union(slice_).coalesce()
            slice = slice_

        print '-------- IS --------'
        print IS
        slice = slice.intersect(IS).coalesce()
        print slice
        #if(SIMPLIFY):
        #slice = imperf_tile.SimplifySlice(slice)

        # EKSPERIMENTAL CODE wywal z RE instrukcje nie nalezace do RE
        if (1 == 0):
            temp = slice.intersect(W).coalesce()
            if (not temp.is_empty()):
                slice = temp

        slice = slice.apply(rap)

        wlen = len(sym_exvars)
        slice = slice.insert_dims(isl.dim_type.set, 2 * wlen, wlen * 2)
        print slice

        print isl_TILEbis

        slice = slice.intersect(isl_TILEbis)

        slices.append(slice.coalesce())

    new_loop = []
    i = 0
    for line in looprepr:
        if (st_reg.match(line)):
            #print slices[i]
            if (codegen == 'barvinok'):
                petla = iscc.iscc_communicate("L :=" + str(slices[i]) +
                                              "; codegen L;")
                petla = petla.split('\n')
            else:  #isl
                petla = iscc.isl_ast_codegen(slices[i]).split('\n')

            petla = correct.Korekta('', petla)  # dodaj { } do for
            was_par = 0  # poprawic jak sie koncza petle i sa nowe
            for s in petla:
                if 'for (int c' in s and was_par == 0:
                    new_loop.append(taby[i] + imperf_tile.get_tab(s) +
                                    '#pragma omp parallel for')
                    if "{" in s:
                        was_par = 1
                new_loop.append(taby[i] + s)

                if was_par > 0:
                    if "{" in s:
                        was_par = was_par + 1
                    if "}" in s:
                        was_par = was_par - 1
            i = i + 1
        else:
            new_loop.append(line)

    nloop = ""
    for line in new_loop:
        if line != '':
            #            if 'for (int c1' in line:   # c0 przy perf,  c1 przy imperf
            #                line = imperf_tile.get_tab(line) + "#pragma omp parallel for\n" +line
            nloop = nloop + line + "\n"

    nloop = nloop[:-1]

    nloop = nloop.split('\n')

    nloop = tiling_v3.postprocess_loop(nloop)
    lines = nloop.split('\n')

    loop = imperf_tile.RestoreStatements(lines, LPetit, dane, wlen, 1, [])

    #loop = imperf_tile.RestoreStatements(lines, LPetit, dane, maxl, step, permutate_list)

    print "=========================="
    print "OUTPUT CODE"
    print loop

    print UDS
    print UDS - REPR
コード例 #3
0
ファイル: tiling_schedule.py プロジェクト: lshadown/traco
def tile_par2(isl_TILEbis, sym_exvars, isl_rel, isl_relplus, isl_relclosure,
              Extend, _rap, Dodatek, SIMPLIFY):

    #with open('sources_of_slices.txt') as f:
    #    content = f.readlines()
    #    srepr = content[0]

    #srepr = isl.Set(srepr)

    #with open('rucs.txt') as f:
    #    content = f.readlines()
    #    rucs = content[0]

    #_rel = _rel.remove_dims(isl.dim_type.in_, 0,1)
    #_rel = _rel.remove_dims(isl.dim_type.out, 0,1)
    #print _rel
    '''
    ir = isl_rel
    print ir
    ir = ir.insert_dims(isl.dim_type.in_, 0, len(sym_exvars))
    ir = ir.insert_dims(isl.dim_type.out, 0, len(sym_exvars))


    print ir

    ir2 = ir.from_domain_and_range(isl_TILEbis, isl_TILEbis).coalesce()
    ir = ir2.intersect(ir).coalesce()
    print ir
    ir = ir.remove_dims(isl.dim_type.in_, len(sym_exvars),len(sym_exvars))
    ir = ir.remove_dims(isl.dim_type.out, len(sym_exvars),len(sym_exvars))
    ir = ir.coalesce()
    print ir

    sys.exit(0);
    '''

    srepr, rucs = slicing.Create_Srepr(isl_rel, isl_relclosure)
    print srepr

    ir = isl_rel.domain().union(isl_rel.range()).coalesce()
    ir = ir.insert_dims(isl.dim_type.set, 0, len(sym_exvars))
    for i in range(0, len(sym_exvars)):
        ir = ir.set_dim_name(isl.dim_type.set, i, sym_exvars[i])

    if (Extend):
        for i in range(0, 2 * len(sym_exvars)):
            ir = ir.insert_dims(isl.dim_type.set, 2 * i + 1, 1)

    srepr = srepr.insert_dims(isl.dim_type.set, 0, len(sym_exvars))
    for i in range(0, len(sym_exvars)):
        srepr = srepr.set_dim_name(isl.dim_type.set, i, sym_exvars[i])

    if (Extend):
        for i in range(0, 2 * len(sym_exvars)):
            srepr = srepr.insert_dims(isl.dim_type.set, 2 * i + 1, 1)

    x = 1
    if (Extend):
        x = 2

    #relacja do obliczenia poczatkow
    Rel_Z = "{["
    for i in range(0, 2 * x * len(sym_exvars)):
        Rel_Z = Rel_Z + "i" + str(i) + ","
    Rel_Z = Rel_Z + "m] -> ["
    for i in range(0, x * len(sym_exvars)):
        Rel_Z = Rel_Z + "i" + str(i) + ","
    Rel_Z = Rel_Z[:-1] + "] };"

    Rel_Z = isl.Map(Rel_Z)

    #relacja do wylapania instrukcji z blokow
    Rel_Y = "{["
    for i in range(0, x * len(sym_exvars)):
        Rel_Y = Rel_Y + "i" + str(i) + ","
    for i in range(0, x * len(sym_exvars) + 1):
        Rel_Y = Rel_Y + "j" + str(i) + ","
    Rel_Y = Rel_Y[:-1] + "] -> ["
    for i in range(0, x * len(sym_exvars)):
        Rel_Y = Rel_Y + "i" + str(i) + ","
    for i in range(0, x * len(sym_exvars) + 1):
        Rel_Y = Rel_Y + "k" + str(i) + ","
    Rel_Y = Rel_Y[:-1] + "] };"

    Rel_Y = isl.Map(Rel_Y)

    TILE_SOUR = isl_TILEbis.intersect(srepr).coalesce()

    TILE_IND = isl_TILEbis.subtract(isl_TILEbis.intersect(ir)).coalesce()

    indloop = iscc.iscc_communicate("L :=" + str(TILE_IND) + "; codegen L;")

    fs = 0
    if (TILE_SOUR.lexmax() == TILE_SOUR.lexmin()):
        print "FS"
        fs = 1
    else:
        print "SLICING"
        if (rucs.is_empty()):
            isl_rel = isl_rel.union(rucs)
            isl_relclosure = isl_relclosure.union(rucs)
            isl_relplus = isl_relplus.union(rucs)

    rplus = tiling_v3.ExtendMap(isl_relplus, sym_exvars, Extend)
    rstar = tiling_v3.ExtendMap(isl_relclosure, sym_exvars, Extend)
    rel_ = tiling_v3.ExtendMap(isl_rel, sym_exvars, Extend)
    #rucs = tiling_v3.ExtendMap(rucs, sym_exvars, Extend)

    rbis = isl.Map.from_domain_and_range(isl_TILEbis, isl_TILEbis)

    rs = rbis.intersect(rstar)
    rt_red = rbis.intersect(rplus)

    if (fs != 1):

        if (rucs.is_empty()):
            TILE_SOUR = TILE_SOUR.subtract(TILE_SOUR.apply(
                rplus)).coalesce()  # remove dependent blocks with srepr

        TILE_RUCS = slicing.Create_RUCS(rel_, rs, TILE_SOUR, TILE_SOUR, 1,
                                        Rel_Y)

        TILE_SOUR = TILE_SOUR.subtract(TILE_RUCS.range()).coalesce()

        z = TILE_SOUR.apply(Rel_Z).coalesce()

        if (SIMPLIFY):
            z = imperf_tile.SimplifySlice(z)

        srepr_loop = iscc.iscc_communicate("L :=" + str(z) +
                                           "; codegen L;")  # albo omega
        #srepr_loop = iscc.oc_communicate(z)
        print srepr_loop
        srepr_loop = srepr_loop.split('\n')

        for i in range(0, len(srepr_loop)):
            if "for" in srepr_loop[i]:
                srepr_loop.insert(i, "#pragma omp parallel for")
                break

        st_reg = re.compile('\s*\(.*\);')
        vecs = []
        taby = []
        for line in srepr_loop:
            if (st_reg.match(line)):
                vecs.append(isl.Set(iscc.s1_to_vec2(line, len(vecs))))
                taby.append(iscc.correct.whites(line))

        #print vecs

        permutate_maps = Dodatek[6]
        permutate_list = Dodatek[5]

        slices = []

        for i in range(0, len(vecs)):
            vecs[i] = vecs[i].intersect(z).coalesce()

        for vec in vecs:
            vec = vec.insert_dims(isl.dim_type.set, x * len(sym_exvars),
                                  x * len(sym_exvars) + 1)
            slice = vec  #.apply(rs)
            slice = slice.apply(Rel_Y)
            slice = slice.apply(rs).coalesce()

            # experimental permutate

            if (len(permutate_list) > 0):
                print "Tiling + slicing + permutation - experimental"
                RP = permutate_maps[0]

                RIDENT = RP.identity(RP.get_space())

                if (not RP.is_equal(RIDENT)
                    ):  #permute map is not an identity map
                    strRP = str(RP)
                    strh = ""
                    for i in range(0, x * len(sym_exvars)):
                        strh = strh + "xx" + str(i) + ","

                    if (Extend):
                        strkoma = strRP.split(",")
                        strRP = ""
                        for i in range(0, 2 * len(sym_exvars)):
                            strRP = strRP + strkoma[i] + ", yy" + str(
                                i % len(sym_exvars)) + ", "
                        strRP = strRP + strkoma[2 * len(sym_exvars)]

                    strRP = strRP.replace("[", "[" + strh)
                    RP = isl.Map(strRP)

                    slice = slice.apply(RP).coalesce()
            # ---------------------------------------------------------

            if (SIMPLIFY):
                slice = imperf_tile.SimplifySlice(slice)
            slices.append(slice)
            print slice

        cmd = ""
        for i in range(0, len(vecs)):
            cmd = cmd + "codegen " + str(slices[i]) + ';print "###";'

        cmd = iscc.iscc_communicate(cmd)

        cmd = cmd.split('"###"')

        new_loop = []
        i = 0
        for line in srepr_loop:
            if (st_reg.match(line)):
                petla = cmd[i].split('\n')
                for s in petla:
                    new_loop.append(taby[i] + s)
                i = i + 1
            else:
                new_loop.append(line)

        nloop = ""
        for line in new_loop:
            if line != '':
                nloop = nloop + line + "\n"
        nloop = nloop[:-1]

        #nloop = nloop + indloop
        nloop = nloop.split('\n')

        # Dodatek = [LPetit, dane,  maxl, step, nazwapar, permutate_list, permutate_maps]

        #permutate list
        nloop = tiling_v3.postprocess_loop(nloop)
        lines = nloop.split('\n')

        loop = imperf_tile.RestoreStatements(lines, Dodatek[0], Dodatek[1],
                                             Dodatek[2], Dodatek[3],
                                             Dodatek[5])

        text_file = open(Dodatek[4], "w")
        text_file.write(loop)
        text_file.close()
        return ""
    else:
        print "fs (EXPERIMENTAL) ..."

        sym_tmp = []
        rlex = "{["
        for s in sym_exvars:
            rlex = rlex + s + ","
            sym_tmp.append(s + "'")
        rlex = rlex[:-1] + '] -> ['
        for s in sym_exvars:
            rlex = rlex + s + "',"
        rlex = rlex[:-1] + '] : '
        rlex = rlex + tiling_v3.CreateLex(sym_tmp, sym_exvars) + "};"

        rlex = isl.Map(rlex)

        rlex = rlex.insert_dims(isl.dim_type.in_, len(sym_exvars),
                                len(sym_exvars) + 1)
        rlex = rlex.insert_dims(isl.dim_type.out, len(sym_exvars),
                                len(sym_exvars) + 1)

        UDS = isl_rel.domain().subtract(isl_rel.range())

        UDS = UDS.insert_dims(isl.dim_type.set, 0, len(sym_exvars))
        for i in range(0, len(sym_exvars)):
            UDS = UDS.set_dim_name(isl.dim_type.set, i, sym_exvars[i])

        if (Extend):
            for i in range(0, 2 * len(sym_exvars)):
                UDS = UDS.insert_dims(isl.dim_type.set, 2 * i + 1, 1)
                rlex = rlex.insert_dims(isl.dim_type.in_, 2 * i + 1, 1)
                rlex = rlex.insert_dims(isl.dim_type.out, 2 * i + 1, 1)

        tuds = UDS.intersect(isl_TILEbis).coalesce()
        #tuds = tuds.insert_dims(isl.dim_type.set, len(sym_exvars), len(sym_exvars)+1)

        rt_red = rt_red.intersect(rlex).coalesce()

        rk = rt_red.power()
        rk = rk[0]
        rk = isl.Map(iscc.RepairRk(str(rk), 0))

        rp = rt_red.transitive_closure()[0]
        sk = tuds.apply(rk).subtract(tuds.apply(rk).apply(rp)).coalesce()

        sk = sk.apply(Rel_Y).intersect(isl_TILEbis)
        sk = sk.insert_dims(isl.dim_type.set, 0, 1)
        sk = sk.set_dim_name(isl.dim_type.set, 0, "ink")

        c = isl.Constraint.eq_from_names(sk.get_space(), {"k": -1, "ink": 1})
        sk = sk.add_constraint(c)
        sk = tiling_v3.Project(sk, ["k"])

        tuds = tuds.apply(Rel_Y).intersect(isl_TILEbis)
        tuds = tuds.insert_dims(isl.dim_type.set, 0, 1)
        tuds = tuds.set_dim_name(isl.dim_type.set, 0, "ink")

        c = isl.Constraint.eq_from_names(sk.get_space(), {1: 0, "ink": 1})
        tuds = tuds.add_constraint(c)

        sk = sk.union(tuds)

        print sk

        fsloop = iscc.iscc_communicate("L :=" + str(sk) + "; codegen L;")

        nloop = fsloop.split('\n')
        nloop = tiling_v3.postprocess_loop(nloop)
        lines = fs_pragma(nloop)

        #Dodatek[2]+1 shift + 1  for k
        #if extend first shift shoud be one smaller
        if (Extend):
            sh = 1
        else:
            sh = 0

        loop = imperf_tile.RestoreStatements(lines, Dodatek[0], Dodatek[1],
                                             Dodatek[2] + 1, Dodatek[3],
                                             Dodatek[5], sh)

        text_file = open(Dodatek[4], "w")
        text_file.write(loop)
        text_file.close()
        return ""
コード例 #4
0
def GenerateSCC(j, permute):

    gr = DepGraph(j)

    z = mutual_accessibility(gr)


    z_sorted = []
    for zz in z:
        z_sorted.append(zz)

    komp = []
    for zz in z_sorted:
        if not z[zz] in komp:
            komp.append(z[zz])


   
# SCC POLICZONE

    komp2 = komp[:]

    for i in (0, len(komp)):
        for item in komp:
            for stam in item:
                for item2 in komp:
                    if item != item2:
                        for stam2 in item2:
                           if gr.has_edge((stam,stam2)):
                               a = komp2.index(item2)
                               b = komp2.index(item)
                               if(a < b): #zamien miejscami
                                   komp2[b], komp2[a] = komp2[a], komp2[b]




    rel = gen.RelPrint("tmp/petit_loop_"+str(j)+".t", 1)



    
    dane = gr.nodes()
    paczka = loop_tools.ReadStatementNests("tmp/petit_loop_"+str(j)+".t", dane)
    combo = paczka[0]
    instrukcje = combo
    granice = paczka[2]

    max_nest = 0
    for c in combo:
        if c['nest'] > max_nest:
            max_nest = c['nest']
        
   
    pattern = re.compile("\n#.*$")
    rel = pattern.sub("", rel)
    pattern = re.compile("{.*\->")
    
    t = str(re.findall(pattern, rel)[0])
    t = t.replace(" ", "")
    t = t.replace("->", "")
    t = t + " : true};"



    # loop fuse to do in future and permutation too
    if(False):
        komp3 = []
        for komponent in komp2:
            con = 0
            if komp3 == []:
                komp3.append(komponent)
                item = komponent
            else:
                for node in komponent:
                    for node2 in item:
                        if gr.has_edge((node2, node)):
                            con = 1
                if(con == 1):
                    komp3.append(komponent)
                    item = komponent
                else:
                    for node in komponent:
                        item.append(node)

        loop_fuse = False

        if(loop_fuse):
            komp2 = komp3


        
    params = ""
    if("] -> {" in rel):
        params = re.findall(".*\] -> \{", rel)[0].replace("{", "") 
         

    zbiory = []
    for komponent in komp2:
        warunek = "("
        for line in komponent:
            if warunek != "(":
                warunek = warunek + "or"
            warunek = warunek + " v=" + line + " "
        warunek = warunek + ") and true"
        zbior = t.replace("true", warunek)
        zbiory.append(zbior)

    file = open("tmp/barv_scc.txt", 'w')
    
    if(1 == 1):
        file.write('R:=' + rel + ';\n')
        file.write('S:= dom R + ran R;')
        
        i = 0
        for zbior in zbiory:
            file.write('S'+str(i)+':=' + zbior)
            file.write('S'+str(i)+':= S * S'+str(i)+';')
            file.write('codegen S'+str(i)+';')
            i = i + 1
            
    i = 0
    permutate_list = []

    for komponent in komp2:
        set = params+"{["
        ll = find_instr(komponent, instrukcje)   # jesli komponent bedzie z loop fusion to trzeba wybrac najbardziej zagn. instrukcje
        vars = instrukcje[ll]['vars']

        #for q in range(0, max_nest+1):
        #    set = set + "i" + str(q) + ","
        set = set + ','.join(vars) + ",v ] : "

        #set = set[:-1] + "] : "
        for line in komponent:
            path = []
            for item in combo:
                for z in item['st']:
                    if int(line) == int(z):
                        path = item['path']
            for q in range(0, len(vars)):
                if(q < len(path)):
                    set = set + granice[path[q]]['lb'] + " <= "+ vars[q] +" <= " + granice[path[q]]['ub'] + " and "
                    #set = set + granice[path[q]]['lb'] + " <= i" + str(q) + " <= " + granice[path[q]]['ub'] + " and "
                else:
                    set = set + " " + vars[q] + " = -1 and "
            
            set = set + " v = " + line +" and true or "
        set = set + " false}; "


        if(1==0): #permute

            # tylko zaleznosci z scc TODO
            rel = imperf_tile.PermuteBlock("tmp/deps" + str(j) + ".txt", set, instrukcje[ll], params, len(instrukcje[ll]['vars']), permutate_list)



            set = isl.Set(str(set))

            # relacja identity - nie rob nic


            if(rel.is_equal(rel.identity(rel.get_space()))):
                permutate_list.pop()
            else:
                set = set.apply(rel)
            set_size = set.dim(isl.dim_type.set)
            set = set.insert_dims(isl.dim_type.set, set_size-1, max_nest+1-set_size)
            tmp_set = '{[' + ','.join(instrukcje[ll]['vars']) +',-1' * (max_nest - len(instrukcje[ll]['vars'])) + ',v]}'
            set = set.intersect(isl.Set(tmp_set)).coalesce()

            set = 'S'+str(i)+':=' + str(set) + ";"



            print set

            file.write(set)
            file.write('codegen S'+str(i)+';')
            i = i + 1
    
    
    file.close();




    
    cmd = barv_script + " < tmp/barv_scc.txt" 
    process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    process.wait()
    output = process.stdout.read()


    #dodaj s1 do petli
    '''lines = output.split("\n")
    lines = filter(lambda x:x!='',lines)   
    loop = ""
    for i in range(0,len(lines)):
        tmp = lines[i]
        pattern = re.compile("^\W*\(")
        if pattern.match(tmp):
            tmp = tmp.replace("(", "s1(")
            tmp = tmp.replace(", ", ",")
        loop = loop + tmp
# if(i < len(lines)-1):
        loop = loop + "\n"
        
    linestring = open("tmp/petit_loop_"+str(j)+".t", 'r').read()
    petit_lines = linestring.split('\n')
      
    lines = loop.split("\n")
    
    debug = False
    loop = ""
    for line in lines:
        if("s1(" in line):
            pattern = re.compile(",[^,]+\)")
            st = pattern.findall(line)[0].replace(",", "").replace(")", "")
            if(not st.isdigit()):
                print "== ERROR =="
                sys.exit()
            numb_st = int(st)
            st = petit_lines[int(st)-1]
            st = st.replace(",", "][")
            
            # nawiasy powinny byc zmienione jesli poprzedzaja je znaki 
            # alfanumeryczne, inaczej to nawiasy nie od tablic
            st =  re.sub("\s*\(", "(", st)
            st =  re.sub("\s*\)", ")", st)
            st =  re.sub(r'(?<=[a-zA-Z0-9_])\(', '[', st)
            st =  re.sub(r'(?<=[a-zA-Z0-9_])\)', ']', st)
            
            # szukaj wektora
            for item in combo:
                if numb_st in item['st']:
                    vec = item['vars']
                    if debug:
                        loop = loop +  get_tab(line) + "// " + ",".join(item['vars']) + "\n"
                    break
            
            # nowy wektor
            vec_new = line.replace("s1(", "").replace(");", "").replace(" ", "").split(",")[:-1]
            if debug:
                loop = loop +  get_tab(line) + "// " + ",".join(vec_new) + "\n"
            
            st2 = st
            for i in range(0, len(vec)):
                st2 = re.sub('\\b'+vec[i]+'\\b', vec_new[i], st2)  

            if debug:
                loop = loop +  get_tab(line) + "// " + st + ";\n"
                
            loop = loop + get_tab(line) + st2 + ";\n"
        else:
            loop = loop + line.replace('int ', '') + "\n"
            
    #do poprawy - za duzo operacji na plikach przerobic korekte
    '''

    loop = tiling_v3.postprocess_loop(output.split('\n'))
    lines = loop.split('\n')
    loop = imperf_tile.RestoreStatements(lines, "tmp/petit_loop_"+str(j)+".t", dane,  0, 0, permutate_list)


    text_file = open("loop_scc.c", "w")
    text_file.write(loop)
    text_file.close()
    
    loop = ""
    for line in correct.Korekta("loop_scc.c"):
        loop = loop + line + "\n"
    
    text_file = open("loop_scc.c", "w")
    text_file.write(loop)
    text_file.close()
    
    # dopisz do instrukcji wektory zmiennych indeksowych petli
    # uzyskaj tabulacje
    # zastap instrukcje
    # nadaj format C
    
    
    
    if(1==0):
        
        linestring = open("tmp/C_loop_"+str(j)+".c", 'r').read()
        lines = linestring.split('\n')   
        
        lines = linestring.split('\n')
        stuff = []
        
        for line in lines:
            if 'for' in line:
                stuff.append(functions.Loop(line));
        
        v = ""
        for s in stuff:
            v = v + s['var'] + ","
        v = v + "v"
        
        text_file = open("names_info.txt", "w")
        text_file.write(v)
        text_file.close()
        
        text_file = open("pseudocode.txt", "w")
        text_file.write(loop)
        text_file.close()
        
        
        
        gen.ParsePrint("tmp/petit_loop_"+str(j)+".t")
     
        shutil.copyfile("out_pseudocode.txt", "tmp/C_loop_scc"+str(j)+".c")
コード例 #5
0
ファイル: tiling_v3.py プロジェクト: lshadown/traco
def tile(plik,
         block,
         permute,
         output_file="",
         L="0",
         SIMPLIFY="False",
         perfect_mode=False,
         parallel_option=False,
         rplus_file=''):

    SIMPLIFY = False
    DEBUG = True

    schedule_mode = parallel_option

    #print "********** another version **************"
    LPetit = "tmp/tmp_petit" + L + ".t"
    LDeps = "tmp/deps" + L + ".txt"

    # strong perfect
    if (DEBUG):
        if perfect_mode:
            print 'Perfectly nested loop mode: enabled'

    simpl_ub = False

    BLOCK = block.split(',')
    par_tiling = False

    for i in range(len(BLOCK), 10):
        BLOCK.append(BLOCK[len(BLOCK) - 1])

    if (not BLOCK[1].isdigit()):
        par_tiling = True

    linestring = open(plik, 'r').read()
    lines = linestring.split('\n')

    if simpl_ub:
        petit_loop = convert_loop.convert_loop(lines, BLOCK)
    else:
        petit_loop = convert_loop.convert_loop(lines)

    file = open(LPetit, 'w')

    imperf = 0
    endloop = 0
    startloop = 0
    for line in petit_loop:
        #sprawdz przy okazji jaka petla idealnie czy nie
        if 'for' in line and not 'endfor' in line:
            if startloop == 2:
                imperf = 1
            startloop = 1
        else:
            if startloop == 1:
                startloop = 2
        if 'endfor' in line:
            endloop = 1
        if endloop == 1 and 'endfor' not in line and not line.isspace(
        ) and line != '':
            imperf = 1

        file.write(line + '\n')

    file.close()

    cmd = gen.path_petit + " " + LPetit + " > " + LDeps
    process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    process.wait()

    lines = linestring.split('\n')
    stuff = []

    for_level = 0
    for line in lines:
        if 'endfor' in line:
            for_level = for_level - 1
        else:
            if 'for' in line:
                if simpl_ub:
                    stuff.append(functions.Loop(line, BLOCK[for_level]))
                else:
                    stuff.append(functions.Loop(line))
                for_level = for_level + 1

    # na podstawie stuff zbuduj tablice inkrementacji i dekremnatacji poziomu

    #if(schedule_mode == 0):
    #    flag = 1
    #else:
    flag = 1

    # Proba prywatyzacji zmiennych w instrukcjach w gniazdach
    priv_stuff = priv_engine.PrivEng(LDeps)
    priv_box = 1

    if (imperf == 0):
        priv_box = 1

        dane = []
        sts = imperf_tile.Get_ST(LPetit, dane)
        dane = dane + sts
        combo = loop_tools.ReadStatementNests(LPetit, dane)
        instrukcje = combo[0]

        for pp in priv_stuff[3]:
            for i in instrukcje:
                a = set(i['st'])
                b = set(pp[1])
                if (a & b) and (
                        b -
                        a):  # nie wszystkie instrukcje skalara sa w gniezdzie
                    priv_box = 0

    # ==========

    # jesli zmienne prywatne sa w obrebie jednego S1 to mozna

    start = time.time()
    if ((imperf == 0 and priv_box == 0)):
        dane = gen.RelPrint(LPetit, flag)
    else:
        if (len(priv_stuff[1]) > 0):
            dane = gen.RelPrint_WithPriv(LPetit, priv_stuff[1])

        else:
            dane = gen.RelPrint(LPetit, flag)

    output = process.stdout.read()

    end = time.time()
    elapsed = end - start

    print "Dependence analysis: time taken: ", elapsed, "seconds."

    if (DEBUG):
        print dane

    dane = dane.split("#")

    rel = dane[0]
    rel2 = rel

    print rel

    dane.remove(rel)
    dane = list(set(dane))

    if (DEBUG):
        print rel

    # Create LD
    Dependence.Create_LD_petit(L, petit_loop)

    # -------------------------------------------------------
    # script for barvinok
    # gdy nie ma zaleznosci
    # problemy do rozwiazania
    # zmienne symboliczne nie wiadomo skad wziasc
    # numery instrukcji
    nodeps = 0
    if (rel == ''):

        nodeps = 1
        LD_inp = "tmp/LD_petit_loop_" + L + ".t"
        dane2 = gen.RelPrint(LD_inp, 1)
        dane2 = dane2.split("#")
        rel2 = dane2[0]
        try:
            isl_rel2 = isl.Map(str(rel2))
            isl_symb = isl_rel2.get_var_names(isl.dim_type.param)
            symb = isl_symb
        except:
            print "Internal error."
            sys.exit()
        rel = "[" + ",".join(symb) + "] -> {["
        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")

    #dane2.remove(rel2)
    #dane2 = list(set(dane2))
    #for i in range(0, len(dane2)):
    #    dane2[i] = str(int(dane2[i]) - 1)

    for s in dane:
        if "UNKNOWN" in s:
            dane.remove(s)

    # dodaj dane bez relacji
    sts = imperf_tile.Get_ST(LPetit, dane)
    dane = dane + sts

    combo = loop_tools.ReadStatementNests(LPetit, 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
    # -------------------------------

    global maxl
    maxl = 0
    for item in instrukcje:
        if item['max_loop'] > maxl:
            maxl = item['max_loop']

    start = time.time()

    tmp_st = []
    if (perfect_mode):
        firstst = str(instrukcje[0]['st'][0])
        #print rel
        rel = re.sub(r"v = \d+", "v = " + firstst, rel)
        rel = re.sub(r"v' = \d+", "v' = " + firstst, rel)
        #print rel
        tmp_st = instrukcje[0]['st']
        instrukcje[0]['st'] = [instrukcje[0]['st'][0]]
        #print instrukcje[0]['st']
        # wywal reszte zapisz gdzies tablice
        # exit(1)

    # strong perfect mode
    isl_rel = isl.Map(str(rel))
    isl_ident = isl_rel.identity(isl_rel.get_space())

    # k6 only
    #print isl_rel
    #rel_plus = isl_rel.transitive_closure()[0]
    #isl_rel = isl_rel.subtract(rel_plus.apply_range(isl_rel))
    #rel = isl.Map(str("{[i,j,v]->[i,j',v]}"))
    #isl_rel = isl_rel.subtract(rel)
    #print isl_rel
    #exit(0)

    # -------------

    if (perfect_mode):
        if (DEBUG):
            print "Removing internal dependences for perfect nested loop"
        isl_rel = isl_rel.subtract(isl_ident)

    if (DEBUG):
        print isl_rel

    z = isl_rel.dim(isl.dim_type.out)
    zz = maxl - z + 1
    if (zz > 0):
        if (DEBUG):
            print "Error. Dimensions of dependencies are not equal to an amount of loop nests"
            print "Relations will be extended."
        isl_rel = isl_rel.insert_dims(isl.dim_type.in_, z - 1, zz)
        isl_rel = isl_rel.insert_dims(isl.dim_type.out, z - 1, zz)

    # jesli ktorys z blokow ma 1 wywal te zaleznosci

    # w slicgu mozna tak jesli zewn petla tylko zapisuje zmienne prywatne
    if (BLOCK[0] == '1' and schedule_mode == 0):
        z = isl_rel.dim(isl.dim_type.out)
        rtmp = "{["
        for i in range(0, z):
            rtmp = rtmp + "i" + str(i) + ","
        rtmp = rtmp[:-1] + "] -> ["
        for i in range(0, z):
            rtmp = rtmp + "o" + str(i) + ","
        rtmp = rtmp[:-1] + "] : i0 = o0 };"
        rtmp = isl.Map(rtmp)

        isl_rel = isl_rel.intersect(rtmp).coalesce()

    # lata na petle ktore nie ma w relacjach
    #if maxl > isl_rel:

    start = time.time()
    if (DEBUG):
        print '!!!!!!!!!!'
    # **************************************************************************
    exact_rplus = '-1'
    islrp = False
    isl_relclosure = isl_rel
    if (rplus_file != ''):
        with open(rplus_file, "r") as myfile:
            data = myfile.read().replace('\n', '')
            isl_relclosure = isl.Map(str(data))
    else:
        if islrp:
            isl_relclosure = isl_rel.transitive_closure()
            exact_rplus = isl_relclosure[1]
            isl_relclosure = isl_relclosure[0]
        else:
            isl_relclosure = relation_util.oc_IterateClosure(isl_rel)
            exact_rplus = True

    isl_relplus = isl_relclosure
    end = time.time()
    elapsed = end - start
    print "Transitive closure: time taken: ", elapsed, "seconds."

    isl_ident = isl_rel.identity(isl_rel.get_space())

    if (DEBUG):
        print 'R+'
        print isl_relclosure

    #isl_relclosure = rpp
    print "!! exact_rplus " + str(exact_rplus)

    isl_relclosure = isl_relclosure.union(isl_ident).coalesce()  # R* = R+ u I

    if (_debug_):
        if (DEBUG):
            print "R*"
            print isl_relclosure
            print exact_rplus
    # **************************************************************************

    isl_symb = isl_rel.get_var_names(isl.dim_type.param)
    symb = isl_symb

    end = time.time()

    if (DEBUG):
        print end - start

    vars = []  # i
    exvars = []  # ii'
    sym_exvars = []  # ii
    varsprim = []  # i'
    par_vars = []  # iib
    p_vars = []  #iib bez plusa lb
    p_symb = []
    im_par_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'])
        i = i + 1

    # codegen.calculate_position(1, instrukcje, len(sym_exvars))

    for s in stuff:
        if s['var'] in vars:
            i = vars.index(s['var'])
            if par_tiling:
                im_par_vars.append(s['var'] * 2 + 'b' + '+' + s['lb'])
            else:
                im_par_vars.append(s['var'] * 2 + "*" + BLOCK[i] + '+' +
                                   s['lb'])

    _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 + ","

    # wektory same 0 - mozesz dac parallel
    # prywatyzuj calosc, tam gdzie zapis seq, same 0 to parallel bloki (tylko odczyt)
    par_tiled = 0
    delta = isl_rel.deltas()
    chkc = isl.Set("{[0," + ",".join(vars) + "]}")
    delta = delta.subtract(chkc)
    if (delta.is_empty()):
        par_tiled = 1
        if (DEBUG):
            print "Parallel tiling detected."

    ########################################################################

    cl = clanpy.ClanPy()
    cl.loop_path = plik
    cl.Load()
    cl.RunCandl()

    j = 0

    if (len(cl.statements) > 0):
        for i in range(0, len(instrukcje)):
            instrukcje[i]['scatter'] = cl.statements[j].scatering[:]
            j = j + len(instrukcje[i]['st'])

    Rapply = GetRapply(vars, sym_exvars, _SYM)

    isl_TILE = []
    isl_TILE_LT = []
    isl_TILE_GT = []
    isl_TILE1 = []
    isl_TILE2 = []
    isl_TILEprim = []
    isl_TILEbis = []
    isl_TILEorig = []

    permutate_list = []
    permutate_maps = []
    FFF_lines = []
    FFF_idx = []
    FFF_headers = []
    FFF_headers_idx = []

    for i in range(0, len(instrukcje)):
        Bij = MakeBij(_SYM, vars, sym_exvars, im_par_vars, stuff, BLOCK,
                      instrukcje[i], par_tiling)
        print Bij
        print "oooooooooooooooooooooooooooooooooooooooooooooooooooo"
        isl_TILE.append(isl.Set(str(Bij).replace("_Bij := ", "")))

        if (DEBUG):
            print 'T --------------------------------------------'
            print stuff
            print isl_TILE[i]

        # oblicz II_SET
        if (False):
            if (len(symb) > 0):
                ii_set = '[' + ','.join(symb) + '] -> '
            else:
                ii_set = ''
            ii_set = ii_set + '{[' + ','.join(sym_exvars) + '] : '

            for l in range(0, len(sym_exvars)):
                ii_set = ii_set + sym_exvars[l] + ' >= 0 and '
                ii_set = ii_set + BLOCK[l] + '*' + sym_exvars[
                    l] + ' + ' + stuff[l]['lb'] + ' <= ' + stuff[l][
                        'ub'] + ' and '
            ii_set = ii_set + ' 1=1}'
            if (DEBUG):
                print ii_set
            ii_set = isl.Set(ii_set)
            if (DEBUG):
                print 'II_SET ' + str(ii_set)
                print 'CARD II_SET ' + test_isl.StringToCard(str(ii_set))
        # ------------------------------

        bltc = 0  # czy juz liczono blt
        bgtc = 0  #   -- || --     bgt

        for j in range(0, len(instrukcje)):
            BLGT = MakeBLTandBGT_v2(
                _SYM, vars, sym_exvars, im_par_vars, varsprim, exvars, stuff,
                BLOCK, instrukcje[j], instrukcje[i],
                par_tiling)  # porownaj zagniezdzenia instrukcji
            if (DEBUG):
                print '-------LT ----------'
                print BLGT[0]
                print '-------GT ----------'
                print BLGT[1]
            print BLGT[0]
            isltmp = isl.Set(str(BLGT[0]).replace("_BLT := ", ""))
            if (bltc == 0):
                print isltmp
                sys.exit(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 "s" + str(i)
            print "LT"
            print isl_TILE_LT[i]
            print "GT"
            print isl_TILE_GT[i]

        X = isl_TILE_GT[i].apply(isl_relclosure).coalesce()

        isl_BCUR = isl_TILE[i].subtract(X).coalesce()

        isl_TILE1.append(isl_BCUR)

        if (DEBUG):
            print '---------bcur-----'
            print isl_BCUR.apply(isl_relclosure)

        isl_BPREV = isl_BCUR.apply(isl_relclosure).intersect(
            isl_TILE_LT[i]).coalesce()
        isl_BPREV = isl_BPREV.subtract(
            X).coalesce()  # changed according to ACM TOPLAS reviewer

        if (DEBUG):
            print isl_BPREV
            print '---'

        isl_TILE2.append(isl_BPREV)

        #isl_TILEprim.append(isl_TILE1[i])
        isl_TILEprim.append(isl_TILE1[i].union(isl_TILE2[i]).coalesce())

        #poprawka intersect z IS - EKSPERYMENTALNA !!!!
        IS = getIS(plik, isl_rel, dane)
        DOMRAN = isl_rel.domain().union(isl_rel.range()).coalesce()
        INDDD = IS.subtract(DOMRAN).coalesce()

        if (DEBUG):
            print '----------'
            print INDDD
            print IS
        #print IS
        # sys.exit(0)
        #isl_TILEprim[i] = isl_TILEprim[i].intersect(IS).coalesce()

        #for ll in range(0,i):
        #    isl_TILEprim[i] = isl_TILEprim[i].subtract(isl_TILEprim[ll]).coalesce()
        #############################
        print '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^'

        #isl_TILEprim.append(isl_TILE[i])

        # print isl_TILEprim[0]

        isl_TILEbis.append(
            Project(isl_TILEprim[i].apply(Rapply).coalesce(),
                    sym_exvars).coalesce())
        isl_TILEorig.append(
            Project(isl_TILE[i].apply(Rapply).coalesce(),
                    sym_exvars).coalesce())

        ############################################################################
        if (par_tiling):
            idx_bylo = []

            if (len(symb) > 0):
                S = "[" + ",".join(symb) + ","
            else:
                S = "["

            S1 = "{[" + ",".join(sym_exvars) + "," + ",".join(vars) + ",v] :"
            for j in range(0, len(instrukcje[i]['path'])):

                idx = str(instrukcje[i]['path'][j])
                liniabb = " = floord(" + stuff[instrukcje[i]['path'][j]][
                    'ub'] + "-" + stuff[instrukcje[i]['path']
                                        [j]]['lb'] + " ," + BLOCK[j] + ");"

                if 1 == 1 or liniabb not in FFF_headers:  # always true,
                    FFF_headers.append(liniabb)
                    FFF_headers_idx.append(idx)
                    linia = "int fff" + idx + liniabb
                    linia = linia.replace("floord( ", "floord(")
                    FFF_lines.append(linia)
                else:
                    idx = FFF_headers_idx[FFF_headers.index(liniabb)]

                if str({'id': idx, 'j': j}) not in FFF_idx:
                    FFF_idx.append({'id': idx, 'j': j})

                if idx not in idx_bylo:
                    S = S + "fff" + idx + ","
                    idx_bylo.append(idx)
                S1 = S1 + " " + sym_exvars[j] + "<= fff" + idx + " and "

            S = S[:-1] + "] -> " + S1 + " true }"

            if (DEBUG):
                print S

            S = isl.Set(S)

            isl_TILEbis[i] = isl_TILEbis[i].intersect(S).coalesce()
        #########################################################################

        if (permute):
            RP = imperf_tile.PermuteBlock(LDeps,
                                          isl_TILEprim[i], instrukcje[i], symb,
                                          len(sym_exvars), permutate_list)
            if (isinstance(RP, isl.Map)):
                print "Permutation experimental enabled:"
                permutate_maps.append(RP)
                isl_TILEprim[i] = isl_TILEprim[i].apply(RP)
                #print isl_TILEprim[i]

        if (_debug_):
            if (DEBUG):
                print "TILE"
                print isl_TILE[i]
                print "TILE_LT"
                print isl_TILE_LT[i]
                print "TILE_GT"
                print isl_TILE_GT[i]
                print "TILE_ITR (TILE1)"
                print isl_TILE1[i]
                print "TVLD_LT (TILE2)"
                print isl_TILE2[i]
                print "TILE_VLD (TILE')"
                print isl_TILEprim[i]
                #  print "card"
                #  print test_isl.StringToCard(str(isl_TILEprim[i]))
                print "TILE_VLD_ext (TILE'')"
                print isl_TILEbis[i]

            # correct upper bounds for simpl_ub

        if simpl_ub:

            cor_set = ''
            if (len(symb) > 0):
                cor_set = '[' + ','.join(symb) + '] -> '
            else:
                cor_set = ''

            cor_set = cor_set + '{[' + ','.join(sym_exvars) + ',' + ','.join(
                vars) + ',' + 'v] : '

            for j in range(0, len(instrukcje[i]['path'])):
                cor_set = cor_set + vars[j] + " <= " + stuff[
                    instrukcje[i]['path'][j]]['ub'] + " - " + BLOCK[j] + " && "

            cor_set = cor_set + "("
            for v in instrukcje[i]['st']:
                cor_set = cor_set + " v = " + str(v) + " or"
            cor_set = cor_set[:-2] + ")}"

            cor_set = isl.Set(cor_set)

            #print cor_set
            #sys.exit(0)

            isl_TILEbis[i] = isl_TILEbis[i].intersect(cor_set).coalesce()
            isl_TILEorig[i] = isl_TILEorig[i].intersect(cor_set).coalesce()

        if (SIMPLIFY):
            isl_TILEbis[i] = imperf_tile.SimplifySlice(isl_TILEbis[i])

        #print "TILE''" + "dla s" + str(i) + "\n" +  str(isl_TILEbis[i])
    #eksepeymentalnie dodaj pozycje do tilebis jesli wiele gniazd na roznych poziomach

    Extend = False
    if (imperf == 1):
        Extend = True
        #Extend = MultiNest(instrukcje)
    #Extend = True

    #print imperf

    #vis3dimperf.imperf_vis(isl_TILEbis, isl_rel)

    _rap = GetRapply4(vars, sym_exvars, _SYM, instrukcje, 0)

    print _rap
    #vis.Vis(isl_TILEbis[0], stuff, cl.deps, isl.Set(cl.statements[0].domain_map), True)
    #sys.exit(0)

    # zne takie jak z ale bez ext

    if (Extend):
        z = isl_TILEbis[0].apply(_rap)
        zne = isl_TILEbis[0]
    else:
        z = isl_TILEbis[0]

    for j in range(1, len(isl_TILEbis)):
        _rap = GetRapply4(vars, sym_exvars, _SYM, instrukcje, j)
        print _rap
        if (DEBUG):
            print _rap
        if (Extend):
            z = z.union(isl_TILEbis[j].apply(_rap))
            zne = zne.union(isl_TILEbis[j])
        else:
            z = z.union(isl_TILEbis[j])

    if (not Extend):
        zne = z

    print z

    # -----------------------------------------------------------------

    _rap = GetRapply4(vars, sym_exvars, _SYM, instrukcje, 0)

    if (Extend):
        zorig = isl_TILEorig[0].apply(_rap)
    else:
        zorig = isl_TILEorig[0]

    for j in range(1, len(isl_TILEorig)):
        _rap = GetRapply4(vars, sym_exvars, _SYM, instrukcje, j)
        if (Extend):
            zorig = zorig.union(isl_TILEorig[j].apply(_rap))
        else:
            zorig = zorig.union(isl_TILEorig[j])

    zorig = zorig.coalesce()

    # ---------------------------------------------------------------

    z = z.coalesce()
    z = z.remove_redundancies()
    z = z.detect_equalities()
    # z = imperf_tile.SimplifySlice(z)

    #calculate II_SET
    ii_SET = z.remove_dims(isl.dim_type.set,
                           int(isl_TILEbis[i].dim(isl.dim_type.set) / 2),
                           int(isl_TILEbis[i].dim(isl.dim_type.set) /
                               2)).coalesce()

    if (_debug_):
        print "II_SET"
        print ii_SET

    step = 0
    if Extend:
        step = 1

    nazwa = ""
    nazwapar = ""

    filePaths = glob.glob(plik)
    if (output_file != ""):
        nazwa = output_file
        nazwapar = output_file
    else:
        for filePath in filePaths:
            base = os.path.basename(filePath)
            nazwa = os.path.splitext(base)[0] + "_tiling" + os.path.splitext(
                base)[1]
            nazwapar = os.path.splitext(
                base)[0] + "_partiling" + os.path.splitext(base)[1]

    if (perfect_mode):
        if (DEBUG):
            print z
        chang = str(tmp_st[0]) + "]"
        if (DEBUG):
            print chang

        for i in range(1, len(tmp_st)):
            zz = str(z).replace(chang, str(tmp_st[i]) + "]")
            zz = isl.Set(zz)
            z = z.union(zz).coalesce()

        instrukcje[0]['st'] = tmp_st  # correct statements

    #z1 = isl.Set('[n,b] -> {[i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13] : n+2 = 2*b && b > 2 && n > 8 }')
    #  z1 = isl.Set('[n,b] -> {[i3,i4,i5,i6,i9,i10,i11,i12,i13] : n+2 = 2*b && b > 2 && n > 8 }')
    #z = z.intersect(z1)

    if (not schedule_mode or par_tiled):
        print "Cloog start..."
        start = time.time()

        isl_ast = False
        barv = 1

        if (barv == 1):
            if (isl_ast):
                loop_x = iscc.isl_ast_codegen(z)
            else:
                loop_x = iscc.iscc_communicate("L :=" + str(z) +
                                               "; codegen L;")

        else:  #cloog
            #loop_x = codegen.tile_gen_short(vars, sym_exvars, symb,z,instrukcje, Extend)
            with open('loopx', "r") as myfile:
                loop_x = myfile.read()
        #loop_x = iscc.oc_communicate(z)
        #loop_x = codegen.tile_gen(vars, sym_exvars, symb, isl_TILEbis, instrukcje, "", [], stuff)

        end = time.time()
        elapsed = end - start
        print "Code generation: time taken: ", elapsed, "seconds."

        ########################################################################

        loop_x = postprocess_loop(loop_x.split('\n'))
        loop = loop_x

        lines = loop.split('\n')

        loop = imperf_tile.RestoreStatements(lines, LPetit, dane, maxl, step,
                                             permutate_list)

        if par_tiled:
            loop = "#pragma omp parallel for\n" + loop

        for i in range(0, 5):
            if (loop[-1] == '\n'):
                loop = loop[:-1]
        loop = loop + "\n"

        new_gen = True

        if (not new_gen):
            gen.ParsePrint2("tmp/tmp_petit.t", len(sym_exvars))

        if (new_gen):
            text_file = open(nazwa, "w")
            text_file.write(loop)
            text_file.close()

            if par_tiling:
                lines = FFF_lines + correct.Korekta(nazwa)
                for i in range(0, len(lines)):
                    if "for" in lines[i] and "fff" in lines[i]:
                        reg = re.compile("\=.*")
                        linia = reg.sub("", lines[i])
                        reg = re.compile(".*\(")
                        cc = reg.sub("", linia)
                        cc = cc.replace(" ", "")
                        for FF_item in FFF_idx:
                            if "fff" + FF_item['id'] in lines[i]:
                                lines[i] = lines[i] + "\n" + correct.whites(
                                    lines[i]) + "  register int " + sym_exvars[
                                        FF_item['j']] + "b = " + BLOCK[
                                            FF_item['j']] + "*" + cc + ";"
                                break
                    lines[i] = lines[i] + "\n"
                text_file = open(nazwa, "w")
                text_file.writelines(lines)
                text_file.close()
                if schedule_mode:
                    text_file = open(nazwapar, "w")
                    text_file.writelines(lines)
                    text_file.close()

            if (output_file != ""):
                return ""
            if (not schedule_mode and 1 == 0):
                rtile = tiling_schedule.get_RTILE(z, sym_exvars, isl_rel,
                                                  Extend)
                print "RTILE:"
                print rtile
                tiling_schedule.get_RCYCLE(rtile, 0)
                rtileorig = tiling_schedule.get_RTILE(zorig, sym_exvars,
                                                      isl_rel, Extend)
                print "RTILE_ORIG:"
                print rtileorig
                tiling_schedule.get_RCYCLE(rtileorig, 1)

# =====================================================================================================================
# PARALLEL PARALLEL PARALLEL PARALLEL PARALLEL PARALLEL PARALLEL PARALLEL PARALLEL PARALLEL PARALLEL PARALLEL PARALLEL

    Dodatek = [
        LPetit, dane, maxl, step, nazwapar, permutate_list, permutate_maps
    ]

    par_tiled = 0

    if (DEBUG):
        print '!!!!'
        print zorig

    if (schedule_mode and par_tiled != 1):
        if (False):
            tiling_schedule.tile_par(isl_TILEprim, isl_TILEbis, sym_exvars,
                                     symb, isl_rel, isl_relplus,
                                     isl_relclosure)
        else:

            # Rk
            #tiling_schedule.tile_par3(z, sym_exvars, isl_rel, isl_relplus, isl_relclosure, Extend, _rap, Dodatek,SIMPLIFY, ii_SET)

            # fsnew

            rtile = tiling_schedule.get_RTILE(zne, sym_exvars, isl_rel, False)
            #rtileplus = rtile.transitive_closure()

            rapp = _rap
            rapp = rapp.remove_dims(isl._isl.dim_type.in_, 0, len(sym_exvars))
            rapp = rapp.remove_dims(isl._isl.dim_type.out, 0,
                                    len(sym_exvars) * 2)

            if not Extend:
                z = z.apply(_rap)

            fsnew_tiletry.fs_new(isl_rel, isl_relplus, rtile, LPetit, dane,
                                 plik, SIMPLIFY, rapp, exact_rplus, z,
                                 sym_exvars, maxl, step, isl_TILEprim, vars)


# przelec po blokach znajdz relacje jak nie ma to parallel
コード例 #6
0
def sfs(plik, L=0, SIMPLIFY=False, fs=0, acc=False):

    LPetit = "tmp/tmp_petit" + str(L) + ".t"
    LDeps = "tmp/deps" + str(L) + ".txt"

    linestring = open(plik, 'r').read()
    lines = linestring.split('\n')

    petit_loop = convert_loop.convert_loop(lines)
    petit_loop.insert(0, "! start")

    file = open(LPetit, 'w')

    for line in petit_loop:
        file.write(line + '\n')
    file.close()

    loop = Dependence.Kernel_Loop(LPetit)

    #cmd = gen.path_petit + " " + LPetit + " > " + LDeps
    #process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    #process.wait()
    #dane = gen.RelPrint(LPetit, 1)

    dane = loop.dane
    isl_rel = loop.isl_rel

    new_rel = relation_util.ReduceR(isl_rel)

    #isl_rel = new_rel

    #experimental

    instrukcje = loop.instrukcje

    # ------------------------
    #dane2 = []
    #sts = imperf_tile.Get_ST(LPetit, dane2)
    #dane2 = dane2 + sts
    #combo = loop_tools.ReadStatementNests(LPetit, dane2)

    # Create LD
    #    isl_rel2 = Dependence.Create_LD_petit(str(L), petit_loop)
    #    ld_rel = isl_rel2.identity(isl_rel2.get_space())
    #    s1 = isl_rel2.domain()
    #    s2 = isl_rel2.range()
    #    s_all = s1.union(s2).coalesce()
    #    s1 = isl_rel.domain()
    #    s2 = isl_rel.range()
    #    s_rel = s1.union(s2).coalesce()
    #    ind = s_all.subtract(s_rel).coalesce()
    #    print ind

    #sys.exit(0)

    # ------------------------

    #print dane
    #dane = dane.split("#")
    #rel = dane[0]
    #dane.remove(rel)
    #dane = list(set(dane))

    #isl_rel = isl.Map(str(rel)).coalesce()

    #print isl_rel

    SQ = False
    if (SQ):
        #print isl_rel
        RR = isl.Map("{[i,j,k,v] -> [i,j',k',v']}")
        isl_rel = isl_rel.intersect(RR).coalesce()
        #isl_rel = isl_rel.set_dim_name(isl.dim_type.in_, 0, "i")
        #isl_rel = isl_rel.set_dim_name(isl.dim_type.out, 0, "i'")
        #c= isl.Constraint.eq_from_names(isl_rel.get_space(), {"i": 1, "i'":-1})
        #isl_rel = isl_rel.add_constraint(c).coalesce()

    #rel_= isl.Map(str('{[i,j,k,v]->[i,jj,kk,v]}'))
    #isl_rel = isl_rel.intersect(rel_).coalesce()

    isl_relclosure = isl_rel.transitive_closure()

    exact = isl_relclosure[1]
    isl_relclosure = isl_relclosure[0]
    isl_relplus = isl_relclosure
    isl_ident = isl_rel.identity(isl_rel.get_space())
    isl_relclosure = isl_relclosure.union(isl_ident).coalesce()  # R* = R+ u I

    # ------------------------------

    vars = []
    for i in range(0, isl_rel.dim(isl.dim_type.in_) - 1):
        vars.append('i' + str(i))

    rap = GetRapply(vars, instrukcje, len(instrukcje) - 1)

    # ------------------------------

    if (fs == 1):
        fs_rk.fs_rk(isl_rel, isl_relplus, Create_UDS(isl_rel), LPetit, dane,
                    plik, SIMPLIFY, rap, acc, loop)
        sys.exit()

    if (fs == 2):  # na samym R+
        fs_new.fs_new(isl_rel, isl_relplus, isl_relclosure,
                      Create_UDS(isl_rel), LPetit, dane, plik, SIMPLIFY, rap,
                      acc, loop, exact)
        sys.exit()

    srepr = Create_Srepr(isl_rel, isl_relclosure)
    rucs = srepr[1]
    srepr = srepr[0]

    #experimetnal
    # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    #    srepr = srepr.union(ind).coalesce()

    srepr_loop = iscc.iscc_communicate("L :=" + str(srepr) + "; codegen L;")

    print srepr_loop

    srepr_loop = srepr_loop.split('\n')

    for i in range(0, len(srepr_loop)):
        if "for" in srepr_loop[i]:
            srepr_loop.insert(i, "#pragma omp parallel for")
            break

    st_reg = re.compile('\s*\(.*\);')
    vecs = []
    taby = []
    for line in srepr_loop:
        if (st_reg.match(line)):
            vecs.append(isl.Set(iscc.s1_to_vec2(line, len(vecs))))
            taby.append(iscc.correct.whites(line))

    #print srepr_loop

    rucs_s = rucs.transitive_closure()[0].coalesce()

    for i in range(0, len(vecs)):
        vecs[i] = isl_rel.domain().intersect(vecs[i]).coalesce()

    slices = []
    for vec in vecs:
        slice = vec
        if (not rucs.is_empty()):
            slice_ = slice.apply(rucs_s)
            slice = slice.union(slice_).coalesce()
        slice = slice.apply(isl_relclosure)

        if (SIMPLIFY):
            slice = imperf_tile.SimplifySlice(slice)

        slice = slice.apply(rap)

        slices.append(slice.coalesce())

    cmd = ""

    for i in range(0, len(vecs)):
        print slices[i]
        cmd = cmd + "codegen " + str(slices[i]) + ';print "###";'

    cmd = iscc.iscc_communicate(cmd)
    cmd = cmd.split('"###"')

    #print cmd

    new_loop = []
    i = 0
    for line in srepr_loop:
        if (st_reg.match(line)):
            petla = cmd[i].split('\n')
            for s in petla:
                new_loop.append(taby[i] + s)
            i = i + 1
        else:
            new_loop.append(line)

    nloop = ""
    for line in new_loop:
        if line != '':
            nloop = nloop + line + "\n"
    nloop = nloop[:-1]
    nloop = nloop.split('\n')

    nloop = tiling_v3.postprocess_loop(nloop)
    print nloop
    lines = nloop.split('\n')

    if (acc):
        lines = openacc.CreateACCCode(lines, loop.var_st)

    loop = imperf_tile.RestoreStatements(lines, LPetit, dane, 0, 1, [])

    for i in range(0, 5):
        if (loop[-1] == '\n'):
            loop = loop[:-1]
    loop = loop + "\n"

    base = os.path.basename(plik)
    nazwa = os.path.splitext(base)[0] + "_slicing" + os.path.splitext(base)[1]

    text_file = open(nazwa, "w")
    text_file.write(loop)
    text_file.close()

    print nazwa + ' was created.'
コード例 #7
0
ファイル: fs_rk.py プロジェクト: lshadown/traco
def fs_rk(rel, rel_plus, uds, LPetit, dane, plik, SIMPLIFY, rap, acc, loop):

    aprox = 0
    rk = rel.power()  #Rk
    if (rk[1] == 0):
        print "rk approximated..."
        aprox = 1
    rk = rk[0].coalesce()

    rk = isl.Map(iscc.RepairRk(str(rk), 0))  # przesun k do symb
    sk = uds.apply(rk).subtract(uds.apply(rk).apply(rel_plus)).coalesce()

    sq_sk = sk

    sk = sk.insert_dims(isl.dim_type.set, 0, 1)
    sk = sk.set_dim_name(isl.dim_type.set, 0, "ink")

    c = isl.Constraint.eq_from_names(sk.get_space(), {"k": -1, "ink": 1})
    sk = sk.add_constraint(c)
    sk = tiling_v3.Project(sk, ["k"])

    sq_sk2 = sk

    SQ = False
    if (SQ):
        sk = get_sq(sq_sk, sq_sk2, uds)
    else:

        uds = uds.insert_dims(isl.dim_type.set, 0, 1)
        uds = uds.set_dim_name(isl.dim_type.set, 0, "ink")

        c = isl.Constraint.eq_from_names(sk.get_space(), {1: 0, "ink": 1})
        uds = uds.add_constraint(c)

        sk = sk.union(uds).coalesce()

    #if(SIMPLIFY):
    sk = imperf_tile.SimplifySlice(sk)

    rap = rap.insert_dims(isl.dim_type.in_, 0, 1)
    rap = rap.insert_dims(isl.dim_type.out, 0, 1)
    rap = rap.set_dim_name(isl.dim_type.in_, 0, 'ik1')
    rap = rap.set_dim_name(isl.dim_type.out, 0, 'ok1')

    c = isl.Constraint.eq_from_names(rap.get_space(), {'ik1': -1, 'ok1': 1})
    rap = rap.add_constraint(c)
    rap = rap.coalesce()

    #print rap

    sk = sk.apply(rap)

    print sk

    if (aprox == 1):
        if (sk.is_bounded()):
            print "Approximation (no perfect FS) but Sk is bounded"
        else:
            print "Sk is not bounded. The result may not be valid!"
    else:
        print 'Rk exact'
        if (sk.is_bounded()):
            print 'Sk bounded.'
        else:
            print 'Sk NOT bounded'

    nloop = iscc.iscc_communicate("L :=" + str(sk) + "; codegen L;")

    # CODEGEN
    nloop = tiling_v3.postprocess_loop(nloop.split('\n'))

    lines = tiling_schedule.fs_pragma(nloop)

    if (acc):
        lines = openacc.CreateACCCode(lines, loop.var_st)

    loop = imperf_tile.RestoreStatements(lines, LPetit, dane, 1, 1, [], 1)

    for i in range(0, 5):
        if (loop[-1] == '\n'):
            loop = loop[:-1]
    loop = loop + "\n"

    base = os.path.basename(plik)
    nazwa = os.path.splitext(base)[0] + "_fs" + os.path.splitext(base)[1]

    text_file = open(nazwa, "w")
    text_file.write(loop)
    text_file.close()

    print nazwa + ' was created.'