コード例 #1
0
ファイル: gen.py プロジェクト: lshadown/traco
def Gen(file_C, _scc, permute):
    # wywolaj esys


    z = 0
    with open("tmp/geo.txt") as f:
        content = f.readlines()
        z = len(content)
    
    Tile = False
    if len(tile_stuff) > 0:
        Tile = True
        
    if Tile:
        l = []
        for i in range(0, z):
            l.append(i)
        p = Pool(z)
        p.map(Tile_loop, l)
        
    for i in range(0, z):
        inp = "tmp/petit_loop_" + str(i) + ".t"
        LD_inp = "tmp/LD_petit_loop_" + str(i) + ".t"
        IND_loop = "tmp/IND_petit_loop_" + str(i) + ".t"
        outp = "tmp/C_output_" + str(i) + ".c"
        source_loop = "tmp/C_loop_" + str(i) + ".c"
        
        
        #tile
        if(Tile) > 0:
            #tiling_v3.tile(source_loop, tile_stuff[0], tile_stuff[1], outp)
            continue
       
        
        # zapisz zaleznosci w pliku desp.txt
        cmd = path_petit + " " + inp + " > tmp/deps"+str(i)+".txt"
        process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
        process.wait()
        
        # dopisz spmd.c
        cmd = path_petit + " -W " + inp
        process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        process.wait()

        # zapisz zaleznosci w pliku _desp.txt dla tewi
        cmd = path_petit + " -b " + inp + " > tmp/_deps"+str(i)+".txt"
        process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
        process.wait()
        
        if _scc==1:
            scc.GenerateSCC(i, permute)
            sys.exit() 
            
        scc.DepGraph(i)
        
        
        priv_stuff = priv_engine.PrivEng("tmp/deps"+str(i)+".txt")
        #print "tmp/deps"+str(i)+".txt"
        #remove deps
        _str = ''
        #print priv_stuff
        if len(priv_stuff[1]) > 0: 
            _str = " --rem_deps="
            for v in priv_stuff[1]:
                _str = _str + str(v) + ","                 
            _str = _str[:-1]

        reduce_op = reduce.Reduction("tmp/deps"+str(i)+".txt")
        if((reduce_op == '' and priv_stuff[2] == 0) or PAR_ALWAYS):     # no par, use framework
            
            # IND Loop
            try:
                IND_Gen(inp, LD_inp, IND_loop)
            except:
                print 'IND loop was not calculated'

            # PARALLELIZE LOOPS
            
            cmd = path_framework + " " + inp + " " + out_command + outp + " " + silent_mode + " --fs_pragma=" + rpath + " " + _str + " --ind_loop=" + IND_loop    
            #process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
            #process.wait()
            stdout=Proc(cmd).call(timeout=5).stdout
            
            fo = open("tmp/debug.txt", "at")
            fo.write(stdout.encode('ascii', 'ignore'))
            fo.close()
        else:
            _file = open(outp, 'w')
            _file.write("#pragma omp parallel for")
            _file.close()

        

        
        
        if os.path.exists("tmp/C_output_" + str(i) + ".c"):
            
            # gdy nie ma zaleznosci
            file = open("tmp/C_output_" + str(i) + ".c", 'r')
            lines = file.readlines()
            file.close()
            
            if len(lines) == 1 and "#pragma" in lines[0]:
                file = open("tmp/C_loop_" + str(i) + ".c", 'r')
                lines2 = file.readlines()
                file.close()
                
                file = open("tmp/C_output_" + str(i) + ".c", 'w')
                lines[0] = lines[0] +"\n"
                file.writelines(lines)
                file.writelines(lines2)
                file.close()
            # --------------------
            
            #sprawdz czy nie ma samej pragmy jak tak to skopiuj pliki dodaj pragmy z private
            CorrectFile("tmp/C_output_" + str(i) + ".c", priv_stuff[0], reduce_op)  
            loop_tools.CorrectLoop("tmp/C_output_" + str(i) + ".c") 
        else:
            parfine.ParInner(i);
    
    # wywolaj output_gen
    output_gen.Output_Gen(file_C, Tile)
コード例 #2
0
def tile(plik, block):
        
    EXP_CODE = True

    BLOCK = block.split(',')
    par_tiling = False
    
    for i in range(len(BLOCK),10):
        BLOCK.append(BLOCK[len(BLOCK)-1])
        
    if(not BLOCK[0].isdigit()):
        par_tiling = True
        #EXP_CODE = False  # nie ma wsparcia na razie dla gen. poszerzonego zbioru dla spar. tilingu, petle zewn. generowane sa z strings

    linestring = open(plik, 'r').read()
    lines = linestring.split('\n')
    
    petit_loop = convert_loop.convert_loop(lines)
    file = open("tmp/tmp_petit.t", 'w')   
 
    for line in petit_loop:
        file.write(line + '\n')
    file.close()
    

    #zapisz zaleznosci
    

    cmd = gen.path_petit + " tmp/tmp_petit.t > tmp/deps.txt"
    process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    process.wait() 

 
    #prywatyzacja
    priv_stuff = priv_engine.PrivEng("tmp/deps.txt")
    
    
    lines = linestring.split('\n')
    stuff = []
    
    for line in lines:
        if 'for' in line:
            stuff.append(functions.Loop(line));
            
    start = time.time()
    if(schedule_mode==1):
        flag = 0
    else:
        flag = 1
    #pobierz relacje zaleznosci
    if(os.path.exists('rel.txt')):
        with open('rel.txt', 'r') as content_file:
            dane = content_file.read()
    else:
        if(len(priv_stuff[1]) > 0 and (priv_stuff[2] == 1 or False)):   # jest par czyli mozna tiling
            dane = gen.RelPrint_WithPriv("tmp/tmp_petit.t", priv_stuff[1])
        else:
            dane = gen.RelPrint("tmp/tmp_petit.t", flag)
        
    output = process.stdout.read()

    end = time.time()
    elapsed = end - start
    if(DEBUG):
        print "Time taken: ", elapsed, "seconds."  

    dane = dane.split("#")
   
    rel = dane[0]
    rel2 = rel

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

    if(DEBUG):
        print rel

    
    # -------------------------------------------------------
    # script for barvinok
    # gdy nie ma zaleznosci
    # problemy do rozwiazania
    # zmienne symboliczne nie wiadomo skad wziasc
    # numery instrukcji
    nodeps=0
    if(rel == ''):
        nodeps=1
        rel = "{["
        for i in range(0,len(stuff)):
            rel = rel + "i" + str(i) + ","
        rel = rel + "v] -> ["
        for i in range(0,len(stuff)):
            rel = rel + "i" + str(i) + "',"
        rel = rel + "v'] : false }"
        loop_tools.MakeLDPetit("tmp/tmp_petit.t", "tmp/tmp_ld_petit.t")
        dane = gen.RelPrint("tmp/tmp_ld_petit.t",1)
        dane = dane.split("#")
        rel2 = dane[0]
        dane.remove(rel2)
        dane = list(set(dane))
        
    for s in dane:
        if "UNKNOWN" in s:
            dane.remove(s)

    
    
    combo = loop_tools.ReadStatementNests("tmp/tmp_petit.t", dane)
    instrukcje = combo[0]
    

    
    
    nest_loop = combo[1]



    if len(instrukcje)==0:
        print 'blad - brak instrukcji'
        sys.exit()
        # wczytaj wszystkie
    
    
    # eksperymentalnie kasowanie innych poziomow
    stuff_reduced = stuff[:] #osobna kopia
    do_kas = []
    for i in range(1, max(nest_loop)+1):
        dups = duplicates(nest_loop, i)
        for s in dups:
            if s != min(dups):
                do_kas.append(s)
        
    for item in do_kas:
        stuff_reduced[item] = '!'
    
    for i in range(0,stuff_reduced.count('!')):
        stuff_reduced.remove('!') # remove
    # -------------------------------


   
    #find_symb = re.compile("^[^(\]|\{)]*\]");
    #if(rel2 == rel):
    #    tmp = re.findall(find_symb, rel) 
    #else:
    #    tmp = re.findall(find_symb, rel2) 

    isl_rel = isl.Map(str(rel))
    isl_relclosure = isl_rel.transitive_closure()[0].coalesce()
    isl_relplus = isl_relclosure
    
    #ident = "{["
    #for i in range(0, len(stuff_reduced)+1):
    #    ident = ident + "i" + str(i) + ","
    #ident = ident[:-1] + "] -> ["
    #for i in range(0, len(stuff_reduced)+1):
    #    ident = ident + "i" + str(i) + ","
    #ident = ident[:-1] +"]}"
    #isl_ident = isl.Map(ident)
    
    isl_ident = isl_rel.identity(isl_rel.get_space())
    isl_relclosure = isl_relclosure.union(isl_ident).coalesce()  # R* = R+ u I
    
    #LATEX
    isl_relclosure.print_(sys.stdout,0, 5)
    
    isl_symb = isl_rel.get_var_names(isl.dim_type.param)
    symb = isl_symb

    
    #if(len(tmp) > 0):
    #    symb = tmp[0]
    #    symb = symb.replace("[", "");
    #    symb = symb.replace("]", "");
    #    symb = symb.replace(" ", "");
    #    symb = symb.split(",");
    
    vars = []  # i
    exvars = []  # ii'
    sym_exvars = []  # ii
    varsprim = []  # i'
    par_vars = [] # iib
    p_vars = [] #iib bez plusa lb
    p_symb = []
    prev_vars = []
    

    
    # moze warto do iloczynu dodac lb a do floor N - lb
    # do testowania +'+'+s['lb']
    i = 0
    for s in stuff_reduced:
        vars.append(s['var']);
        sym_exvars.append(s['var']*2);
        exvars.append(s['var']*2+"'");
        varsprim.append(s['var']+"'");
        if par_tiling:
            par_vars.append(s['var']*2+'b'+'+'+s['lb'])
            p_vars.append(s['var']*2+'b')
        else:
            par_vars.append(s['var']*2 + "*" + BLOCK[i]+'+'+s['lb'])
            # na uzytel nieidealnie zagniezdzonych
            prev_vars.append("(" + s['var']*2 + "-1)*" + BLOCK[i]+'+'+s['lb'])
        i = i+1
        

   
    _SYM = ""
            
    for s in sym_exvars:
        _SYM = _SYM + s + ","
        
        
    for i in range(len(vars),10):
        BLOCK.pop()
        
    if(par_tiling):
        for s in p_vars:
            _SYM = _SYM + s + ","
            p_symb.append(s)
        for s in set(BLOCK):   #remove duplicates
            _SYM = _SYM + s + ","
            p_symb.append(s)        
                

    for s in symb:
        _SYM = _SYM + s + ","
        
    isl_TILE = []
    isl_TILE_LT = []
    isl_TILE_GT = []
    isl_TILE1 = []
    isl_TILE2 = []
    isl_TILEprim = []
    isl_TILEunion = ""
    isl_TILE_prev = []
    isl_TILE_nextcol = []
    isl_TILE_LT_prev = []
    isl_TILE_GT_prev = []
    isl_IT = []
    isl_REST_new = []
    
    for i in range(0, len(instrukcje)):
        iti = isl.Set(imperf_tile.GetIT(instrukcje, stuff_reduced, _SYM, vars, sym_exvars, BLOCK, i))
        isl_IT.append(iti)
    
    # utworz Bij_PREV czyli podmien w par_vars jeden wymiar na prev_vars
    # i unionem wszystkie polacz
    for i in range(0, len(instrukcje)):
        for j in range(0, instrukcje[i]["nest"]):
            tmp_par_vars = []
            for k in range(0, len(par_vars)):
                if(j==k):
                    tmp_par_vars.append(prev_vars[k])
                else:
                    tmp_par_vars.append(par_vars[k])
            S = MakeBij(_SYM, vars, sym_exvars, tmp_par_vars, stuff, BLOCK, instrukcje[i])
            S = isl.Set(str(S).replace("_Bij := ", ""))

            if(len(isl_TILE_prev) <= i):
                isl_TILE_prev.append(S)
                isl_TILE_nextcol.append(S)
            else:
                isl_TILE_prev[i] = isl_TILE_prev[i].union(S).coalesce()


    if(DEBUG):                
        for i in range(0, len(isl_TILE_prev)):
            print "LT'_" + str(i)
            print  isl_TILE_prev[i]

    
    for i in range(0, len(instrukcje)):
        Bij = MakeBij(_SYM, vars, sym_exvars, par_vars, stuff, BLOCK, instrukcje[i])
        if(DEBUG):
            print "TILE" + str(i)
            print Bij
        isl_TILE.append(isl.Set(str(Bij).replace("_Bij := ", "")))
        if(i==0):
            isl_TILEunion = isl_TILE[i]
        else:
            isl_TILEunion = isl_TILEunion.union(isl_TILE[i])
    
    global maxl
    maxl = 0
    for item in instrukcje:
        if item['max_loop'] > maxl:
            maxl = item['max_loop']

    
    SET_II = "[" + _SYM[:-1] + "] -> {[" + ",".join(vars) + ",v] : "
    for i in range(0, len(sym_exvars)):
        SET_II = SET_II + sym_exvars[i] + " >= 0 and " + par_vars[i] + " <= " + stuff[i]['ub'] + " and "
    SET_II = SET_II+ "1=1 }" 
    print SET_II
    isl_SETii = isl.Set(SET_II)   
    if(DEBUG):
        print isl_SETii

    
    for i in range(0, len(instrukcje)):
        Bij = MakeBij(_SYM, vars, sym_exvars, par_vars, stuff, BLOCK, instrukcje[i])
        if(DEBUG):
            print "======"
        
        bltc=0  # czy juz liczono blt
        bgtc=0  #   -- || --     bgt


        for j in range(0, len(instrukcje)):
            BLGT = MakeBLTandBGT_v2(_SYM, vars, sym_exvars, par_vars, varsprim, exvars, stuff, BLOCK, instrukcje[j], instrukcje[i])   # porownaj zagniezdzenia instrukcji

            isltmp = isl.Set(str(BLGT[0]).replace("_BLT := ", ""))
            if(bltc==0):
                isl_TILE_LT.append(isltmp)
                bltc=1
            else:
                isl_TILE_LT[i] = isl_TILE_LT[i].union(isltmp).coalesce()

            isltmp = isl.Set(str(BLGT[1]).replace("_BGT := ", ""))           
            if(bgtc==0):
                isl_TILE_GT.append(isltmp)
                bgtc=1
            else:
                isl_TILE_GT[i] = isl_TILE_GT[i].union(isltmp).coalesce()

        if(DEBUG):
            print "TILE_LT" + str(i)
            print isl_TILE_LT[i]
            print "TILE_GT" + str(i)
            print isl_TILE_GT[i]


        X = isl_TILE_GT[i].apply(isl_relclosure).coalesce()
        
        
        if(DEBUG):
            print "R*(TILE_GT" + str(i) + ")"
            print X


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

        isl_TILE1.append(isl_BCUR)
        
        if(DEBUG):
            print "TILE1_" + str(i)
            print isl_TILE1[i]


        
        C = isl_TILE[i].apply(isl_relplus).coalesce()
        if(DEBUG2):
            print "(R*(TILEi)"
            print C
        C = C.intersect(isl_TILE_LT[i]).coalesce()
        C = C.intersect(isl_IT[i]).coalesce() 
                
        A = isl_TILE_LT[i].apply(isl_relplus).coalesce()       
        A = A.intersect(isl_TILE_prev[i]).coalesce()
        #A = isl_TILE_prev[i]

        

        A = A.subtract(Project(isl_TILE1[i].union(C), sym_exvars)).coalesce()
        #A = A.subtract(Project(C, sym_exvars)).coalesce()
        if(DEBUG2): 
            print "(R+(LTi) intersect LT'i) - instrukcje [ belong to C and TILE1i ]"
            print A
            print "TILE2 (z wczesniejszym union C)"
                 
        A = A.union(C).coalesce()
        
        #dla ladniejszego zapisu
        A = A.intersect(isl_SETii).coalesce()
        #A = isl_TILE_prev[i]
        if(DEBUG2):
            print A    
            
        #print "=============== s" + str(i)
        # print isl_TILE1[i]
        Z = isl_TILE[i].subtract(isl_TILE1[i])
        Z = Project(Z, sym_exvars)
        Z = Z.intersect(isl_TILE_prev[i].intersect(isl_SETii)).coalesce()
        #print Z
        Z = Z.subtract(Project(C, sym_exvars)).coalesce()
        # A = LT'i intersect ( wszystkie punkty R*(TILE_GTi - ISi))
        Z1 = isl_TILE_GT[i].subtract(isl_IT[i])
        print "===="
        #print Z1.apply(isl_relclosure)
        #print isl_TILE1[i]
        #Z = Project(Z1.apply(isl_relclosure), sym_exvars)
        #print Z
        #Z = Z.intersect(isl_TILE_prev[i]).intersect(isl_SETii)
        A = Z.union(C)
        
        
        isl_REST_new.append(isl_TILE[i].subtract(Project(A.union(isl_TILE1[i]), sym_exvars)).coalesce())
        #print isl_REST_new[i]
        isl_TILE2.append(A)
        #isl_TILE2.append(A.intersect(isl_TILE_LT[i]).coalesce())
        
        if(DEBUG):
            print "TILE2_" + str(i)
            print isl_TILE2[i]
        
        isl_TILEprim.append(isl_TILE2[i].union(isl_BCUR).coalesce())
        if(DEBUG):
            print isl_SETii
        isl_TILEprim[i] = isl_TILEprim[i].intersect(isl_SETii)
        if(DEBUG):
            print "TILEprim_" + str(i)
            print isl_TILEprim[i]

    iR = isl_REST_new[1]
    for i in range(1, len(isl_REST_new)):
        iR = iR.union(isl_REST_new[i]).coalesce()
    iR = iR.apply(isl_relclosure)
    iR = Project(iR, sym_exvars)
    
    iT = isl_TILEprim[1]
    for i in range(1, len(isl_REST_new)):
        iT = iT.union(isl_TILEprim[i]).coalesce()
    iT = Project(iT, sym_exvars)
    
    print iT.intersect(iR).coalesce()
    sys.exit()
    

    if(DEBUG):
        print "============================================================================="
    for i in range(0, len(instrukcje)):
        for ii in range(0,2):
            constr = ""
            if i == 1:
                ubjj = 2
            else:
                ubjj = 1
            for jj in range(0,ubjj):
                if ubjj > 1:
                    constr = " && jj = " + str(jj) 

                bset = isl.BasicSet("[ii,jj] -> {[x, y,z] : ii = "+str(ii)+constr+"}")
                #print "TILE_LT" + str(i)
                #print "ii=" + str(ii) + ",jj=" + str(jj)
                #print isl_TILE_LT[i].intersect(bset).coalesce()
                #print "TILE_GT" + str(i)
                #print "ii=" + str(ii) + ",jj=" + str(jj)
                #print isl_TILE_GT[i].intersect(bset).coalesce()



    #file.write('BLOCK;') 
    
    file.close()
    


    # -------------------------------------------------------
    
    
    '''cmd = barv_script + " < tmp/barv_tiling.txt" 
    process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    process.wait()
    output = process.stdout.read()
    end = time.time()
    elapsed = end - start
    print "Time taken: ", elapsed, "seconds."  
            
    
    
    lines = output.split("\n")

    if(DEBUG):
        for i in range(0,lines.index('"$#$"')+1):
            lines.pop(0); 
           
    block_set = [] 
    for i in range(0,lines.index('"$$$#$$$"')+1):
        block_set.append(lines.pop(0));
    block_set.pop();
    
           
    lines = filter(lambda x:x!='',lines)              
    #dodaj s1 do petli
    loop = postprocess_loop(lines)
    bl_2half = loop'''
    
    
    #eksperymentalnie 
    if(EXP_CODE):
        loops = TileGen(sym_exvars, vars, _SYM, symb, p_symb, par_tiling, rel, schedule_mode,  instrukcje, stuff_reduced, BLOCK, isl_TILEprim, varsprim, isl_IT, isl_REST_new)
        loop_x = loops[0]
        loop_x = postprocess_loop(loop_x.split('\n'))
        loop = loop_x
    


    #zmienne 
    v = ""
    for s in stuff_reduced:
        v = v + s['var'] + ","
    v = v + "v"
    
    text_file = open("names_info.txt", "w")
    text_file.write(v)
    text_file.close()
    
    #print block_set # tu mozna rozszerzyc

    #pseudokod
    text_file = open("pseudocode.txt", "w")
    text_file.write(loop)
    text_file.close()
    
 
    
    #przywroc instrukcje
    if(EXP_CODE):
        gen.ParsePrint2("tmp/tmp_petit.t", len(sym_exvars))
    else:
        gen.ParsePrint("tmp/tmp_petit.t") 
        

        
    filePaths = glob.glob(plik)
 
    for filePath in filePaths:
        base = os.path.basename(filePath)
        nazwa = os.path.splitext(base)[0] + "_tiling" + os.path.splitext(base)[1]
    
        # usun puste linie i kopiuj
        file = open("out_pseudocode.txt", 'r')
        lines = file.readlines()
        # oryginalny z elsami
        lines_block = lines[:]
        
               
        if par_tiling and not EXP_CODE:
            for i in range(0, len(sym_exvars)):
                lines.insert(0,'register int ' + sym_exvars[i] + 'b=' + sym_exvars[i] + "*" +BLOCK[i] + ";\n")
                
        if par_tiling and EXP_CODE:
            for i in range(0, len(sym_exvars)):
                lines[i] = lines[i].replace('\n', '') + '{ register int ' + sym_exvars[i] + 'b=c'+str(i)+'*' +BLOCK[i] + ";\n"
                lines.append("}\n")
        
        file.close()
        
        #eksperymentalnie 
        if(not EXP_CODE):
            for i in range(0, len(sym_exvars)):
#            lines.insert(i, "for(int "+sym_exvars[i]+"="+stuff[i]['lb']+"; "+sym_exvars[i]+"<="+stuff[i]['ub']+"; "+sym_exvars[i]+"+="+str(BLOCK[i])+"){\n")       
                lines.insert(i, "for(int "+sym_exvars[i]+"=0; "+sym_exvars[i]+"<=floord("+stuff[i]['ub']+"-"+stuff[i]['lb']+","+str(BLOCK[i])+"); "+sym_exvars[i]+"++){\n")
                lines.append("}\n")
        else:
            if par_tiling:
                # do rozwiazania
                for i in range(0, len(sym_exvars)):
                    lines.insert(i, "int fff"+str(i)+"=floord("+stuff[i]['ub']+"-"+stuff[i]['lb']+","+str(BLOCK[i])+");\n")
         
              
        lines = filter (lambda a: a != '\n',lines)
        
        if nodeps==1:
            lines.insert(0, "#pragma omp parallel\n") 

        
        file = open("out_pseudocode.txt", 'w')
        lines = file.writelines(lines)
        file.close()

        shutil.copyfile("out_pseudocode.txt", nazwa)

    '''if(len(loops) == 2):
コード例 #3
0
def tile(plik, block):

    EXP_CODE = True
    TilCompl = Complex()

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

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

    if (not BLOCK[0].isdigit()):
        par_tiling = True
        #EXP_CODE = False  # nie ma wsparcia na razie dla gen. poszerzonego zbioru dla spar. tilingu, petle zewn. generowane sa z strings

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

    petit_loop = convert_loop.convert_loop(lines)
    file = open("tmp/tmp_petit.t", 'w')

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

    #zapisz zaleznosci

    cmd = gen.path_petit + " tmp/tmp_petit.t > tmp/deps.txt"
    process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    process.wait()

    #prywatyzacja
    priv_stuff = priv_engine.PrivEng("tmp/deps.txt")

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

    for line in lines:
        if 'for' in line:
            stuff.append(functions.Loop(line))

    if (DEBUG):
        print stuff

    start = time.time()
    if (schedule_mode == 1):
        flag = 0
    else:
        flag = 1
    #pobierz relacje zaleznosci
    if (os.path.exists('rel.txt')):
        with open('rel.txt', 'r') as content_file:
            dane = content_file.read()
    else:
        if (len(priv_stuff[1]) > 0 and
            (priv_stuff[2] == 1 or False)):  # jest par czyli mozna tiling
            dane = gen.RelPrint_WithPriv("tmp/tmp_petit.t", priv_stuff[1])
        else:
            dane = gen.RelPrint("tmp/tmp_petit.t", flag)

    output = process.stdout.read()
    end = time.time()
    elapsed = end - start
    print "Time taken: ", elapsed, "seconds."

    dane = dane.split("#")

    rel = dane[0]
    rel2 = rel

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

    # -------------------------------------------------------
    # script for barvinok
    # gdy nie ma zaleznosci
    # problemy do rozwiazania
    # zmienne symboliczne nie wiadomo skad wziasc
    # numery instrukcji
    nodeps = 0
    if (rel == ''):
        nodeps = 1
        rel = "{["
        for i in range(0, len(stuff)):
            rel = rel + "i" + str(i) + ","
        rel = rel + "v] -> ["
        for i in range(0, len(stuff)):
            rel = rel + "i" + str(i) + "',"
        rel = rel + "v'] : false }"
        loop_tools.MakeLDPetit("tmp/tmp_petit.t", "tmp/tmp_ld_petit.t")
        dane = gen.RelPrint("tmp/tmp_ld_petit.t", 1)
        dane = dane.split("#")
        rel2 = dane[0]
        dane.remove(rel2)
        dane = list(set(dane))

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

    # ISL ================================================================
    #isl_rel = isl.Map(str(rel))
    #isl_relclosure = isl_rel.transitive_closure()[0].coalesce()
    #isl_symb = isl_rel.get_var_names(isl.dim_type.param)
    #TilCompl.symb = isl_symb

    #print dane
    combo = loop_tools.ReadStatementNests("tmp/tmp_petit.t", dane)
    instrukcje = combo[0]
    nest_loop = combo[1]

    if len(instrukcje) == 0:
        print 'blad - brak instrukcji'
        sys.exit()
        # wczytaj wszystkie

    # eksperymentalnie kasowanie innych poziomow
    stuff_reduced = stuff[:]  #osobna kopia
    do_kas = []
    for i in range(1, max(nest_loop) + 1):
        dups = duplicates(nest_loop, i)
        for s in dups:
            if s != min(dups):
                do_kas.append(s)

    for item in do_kas:
        stuff_reduced[item] = '!'

    for i in range(0, stuff_reduced.count('!')):
        stuff_reduced.remove('!')  # remove
    # -------------------------------

    file = open("tmp/barv_tiling.txt", 'w')
    file.write('R:=' + rel + ';\n')

    if (DEBUG):
        file.write('print "R";')
        file.write('R;')

    file.write('Rstar:= R^+;')

    if (DEBUG):
        file.write('print "Rstar";')
        file.write('Rstar;')

    find_symb = re.compile("^[^(\]|\{)]*\]")
    if (rel2 == rel):
        tmp = re.findall(find_symb, rel)
    else:
        tmp = re.findall(find_symb, rel2)

    symb = []

    if (len(tmp) > 0):
        symb = tmp[0]
        symb = symb.replace("[", "")
        symb = symb.replace("]", "")
        symb = symb.replace(" ", "")
        symb = symb.split(",")

    vars = []  # i
    exvars = []  # ii'
    sym_exvars = []  # ii
    varsprim = []  # i'
    par_vars = []  # iib
    p_vars = []  #iib bez plusa lb
    p_symb = []

    # 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

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

    #imperf
    #lexarrays = imperf_tile.get_array(instrukcje, sym_exvars, stuff_reduced, symb, _SYM[:-1], BLOCK)
    #print lexarrays

    isl_Bij = ""

    for i in range(0, len(instrukcje)):
        Bij = MakeBij(_SYM, vars, sym_exvars, par_vars, stuff, BLOCK,
                      instrukcje[i])

        isl_Bij_temp = isl.Set(str(Bij).replace("_Bij := ", ""))
        #print isl_Bij

        file.write(Bij)
        if (i == 0):
            file.write('Bij := _Bij;')
            isl_Bij = isl_Bij_temp
        else:
            file.write('Bij := Bij + _Bij;')
            isl_Bij = isl_Bij.union(isl_Bij_temp).coalesce()

    #print isl_Bij

    if (DEBUG):
        file.write('print "Bij";')
        file.write('Bij;')

    for i in range(0, len(instrukcje)):
        BLGT = MakeBLTandBGT_v2(_SYM, vars, sym_exvars, par_vars, varsprim,
                                exvars, stuff, BLOCK, instrukcje[i])
        file.write(BLGT[0])
        file.write(BLGT[1])
        if (i == 0):
            file.write('BLT := _BLT;')
            file.write('BGT := _BGT;')
        else:
            file.write('BLT := BLT + _BLT;')
            file.write('BGT := BGT + _BGT;')

    #file.write(BLGT[0])

    if (DEBUG):
        file.write('print "BLT";')
        file.write('BLT;')

    #file.write(BLGT[1])

    if (DEBUG):
        file.write('print "BGT";')
        file.write('BGT;')

    file.write('X:= Rstar(BGT);')
    file.write('BCUR:=Bij - X;')

    if (DEBUG):
        file.write('print "BCUR";')

    file.write('BPREV:= Rstar(BCUR) * BLT;')

    if (DEBUG):
        file.write('print "BPREV";')
        file.write('BPREV;')

    file.write('BLOCK:= BCUR + BPREV;')

    if (DEBUG):
        file.write('print "BLOCK";')
        file.write('BLOCK;')
        file.write('print "$#$";')

    file.write('BLOCK;')
    file.write('print "$$$#$$$";')

    if (schedule_mode):
        file.write('codegen BLOCK;')
    #file.write('BLOCK;')

    file.close()

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

    cmd = barv_script + " < tmp/barv_tiling.txt"
    process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    process.wait()
    output = process.stdout.read()
    end = time.time()
    elapsed = end - start
    print "Time taken: ", elapsed, "seconds."

    if (DEBUG):
        print output

    lines = output.split("\n")

    if (DEBUG):
        for i in range(0, lines.index('"$#$"') + 1):
            lines.pop(0)

    block_set = []
    for i in range(0, lines.index('"$$$#$$$"') + 1):
        block_set.append(lines.pop(0))
    block_set.pop()

    lines = filter(lambda x: x != '', lines)

    #dodaj s1 do petli
    loop = postprocess_loop(lines)
    bl_2half = loop

    #eksperymentalnie
    if (EXP_CODE):
        loops = iscc.NewTileGen(block_set, sym_exvars, vars, _SYM, symb,
                                p_symb, par_tiling, rel, schedule_mode,
                                instrukcje)
        loop_x = loops[0]
        loop_x = postprocess_loop(loop_x.split('\n'))
        loop = loop_x

    #zmienne
    v = ""
    for s in stuff_reduced:
        v = v + s['var'] + ","
    v = v + "v"

    text_file = open("names_info.txt", "w")
    text_file.write(v)
    text_file.close()

    #print block_set # tu mozna rozszerzyc

    #pseudokod
    text_file = open("pseudocode.txt", "w")
    text_file.write(loop)
    text_file.close()

    #przywroc instrukcje
    if (EXP_CODE):
        gen.ParsePrint2("tmp/tmp_petit.t", len(sym_exvars))
    else:
        gen.ParsePrint("tmp/tmp_petit.t")

    filePaths = glob.glob(plik)

    for filePath in filePaths:
        base = os.path.basename(filePath)
        nazwa = os.path.splitext(base)[0] + "_tiling" + os.path.splitext(
            base)[1]

        # usun puste linie i kopiuj
        file = open("out_pseudocode.txt", 'r')
        lines = file.readlines()
        # oryginalny z elsami
        lines_block = lines[:]

        if par_tiling and not EXP_CODE:
            for i in range(0, len(sym_exvars)):
                lines.insert(
                    0, 'register int ' + sym_exvars[i] + 'b=' + sym_exvars[i] +
                    "*" + BLOCK[i] + ";\n")

        if par_tiling and EXP_CODE:
            for i in range(0, len(sym_exvars)):
                lines[i] = lines[i].replace(
                    '\n',
                    '') + '{ register int ' + sym_exvars[i] + 'b=c' + str(
                        i) + '*' + BLOCK[i] + ";\n"
                lines.append("}\n")

        file.close()

        #eksperymentalnie
        if (not EXP_CODE):
            for i in range(0, len(sym_exvars)):
                #            lines.insert(i, "for(int "+sym_exvars[i]+"="+stuff[i]['lb']+"; "+sym_exvars[i]+"<="+stuff[i]['ub']+"; "+sym_exvars[i]+"+="+str(BLOCK[i])+"){\n")
                lines.insert(
                    i, "for(int " + sym_exvars[i] + "=0; " + sym_exvars[i] +
                    "<=floord(" + stuff[i]['ub'] + "-" + stuff[i]['lb'] + "," +
                    str(BLOCK[i]) + "); " + sym_exvars[i] + "++){\n")
                lines.append("}\n")
        else:
            if par_tiling:
                # do rozwiazania
                for i in range(0, len(sym_exvars)):
                    lines.insert(
                        i, "int fff" + str(i) + "=floord(" + stuff[i]['ub'] +
                        "-" + stuff[i]['lb'] + "," + str(BLOCK[i]) + ");\n")

        lines = filter(lambda a: a != '\n', lines)

        if nodeps == 1:
            lines.insert(0, "#pragma omp parallel\n")

        file = open("out_pseudocode.txt", 'w')
        lines = file.writelines(lines)
        file.close()

        shutil.copyfile("out_pseudocode.txt", nazwa)

    if (len(loops) == 2):
        tiling_schedule.slice_tiling(loops[1], bl_2half, sym_exvars)
    if (len(loops) == 3):  #fs
        tiling_schedule.slice_tiling(loops[2], bl_2half, sym_exvars, 1)
コード例 #4
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