predict.py 5.28 KB
Newer Older
Alan Marchiori's avatar
Alan Marchiori committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
from static import static_predictor
from local import local_bht
from global_pred  import global_pht # global is a reserved word, cannot use as module name
from twolevel import twolevel_predictor
from hybrid_correlating import hybrid_correlating_predictor
from tournament import tournament_predictor

def predict(lines, verbose = False, predictor = static_predictor()):
    """Input is a generator which yields triples: (PC, branch target, [T]aken / [N]ot taken
    """
    
    stats = {True:0, 
             False:0}
    msg = {True: '',
           False: '*MISSPREDICT*'}
    for pc, target, outcome in lines:
        
        #if verbose:
        #    print()
        #    print ("-"*30)
        prd = predictor.predict(pc, target, outcome, verbose)
        
        if verbose:
            print ("0x{:x} --> 0x{:x}: pred = {} actual = {} {}".format(pc,
                                                                        target,
                                                                        prd,
                                                                        outcome,
                                                                        msg[prd == outcome]))        
        #if verbose:
        #    print ("+"*30)
        stats[prd == outcome] +=  + 1
        
    print ("Correct: {}, misspredict: {}, Accuracy: {:4.2f}%".format(stats[True],
                                                                     stats[False],
                                                                     100*stats[True]/(sum(stats.values()))))
    
    
if __name__ == "__main__":
        
    branches =  """0x31c1e011c8 0x31c1e011b0 T
 0x31c1e011c2 0x31c1e011f0 N
 0x31c1e011c8 0x31c1e011b0 T
 0x31c1e011c2 0x31c1e011f0 N
 0x31c1e011c8 0x31c1e011b0 T
 0x31c1e011c2 0x31c1e011f0 N
 0x31c1e011c8 0x31c1e011b0 T
 0x31c1e011c2 0x31c1e011f0 N
 0x31c1e011c8 0x31c1e011b0 N
 0x31c1e011d4 0x31c1e014d0 N
 0x31c1e011ee 0x31c1e011c4 T
 0x31c1e011c8 0x31c1e011b0 N
 0x31c1e011d4 0x31c1e014d0 N
 0x31c1e011ee 0x31c1e011c4 T
 0x31c1e011c8 0x31c1e011b0 T
 0x31c1e011c2 0x31c1e011f0 N
 0x31c1e011c8 0x31c1e011b0 N
 0x31c1e011d4 0x31c1e014d0 N
 0x31c1e011ee 0x31c1e011c4 T
 0x31c1e011c8 0x31c1e011b0 N
 0x31c1e011d4 0x31c1e014d0 N
 0x31c1e011ee 0x31c1e011c4 T
 0x31c1e011c8 0x31c1e011b0 N"""
    
    def parts_to_ints(parts):
        return ( int(parts[0],16), int(parts[1],16), parts[2] )     
    
    # use this line if you want to read from a file.
    #lines = open('brtrace.out').readlines()
    lines = branches.split('\n')    
    
    verb = True
    
    # which predictor or predictors you want to run...
    predictors = 'static'    
    
    if 'static' in predictors:
        for predictor, string in ( (static_predictor(), 'Static'), ):
            s = (x.strip().split(' ') for x in lines)
            s = (parts_to_ints(x) for x in s)         
            print (string, ': ')
            predict (s, verb, predictor)
      
        print ("="*60)        
    
    if 'local' in predictors:
        #for predictor, string in (
                                  #(local_bht(i), '2-bit local BHT n={:2d}'.format(i)) for i in range (1, 4)  ):
                                  
        for predictor, string in ((local_bht(3), '2-bit local BHT with {} entries'.format(2**3)),):
            s = (x.strip().split(' ') for x in lines)
            s = (parts_to_ints(x) for x in s)         
            print (string, ': ')            
            predict (s, verb, predictor)
    
        print ("="*60)
        
    if 'global' in predictors:
        #for predictor, string in (
         #                         (global_pht(i), 'Global PHT n={:2d}'.format(i)) for i in range (1, 4)  ):
        for predictor, string in ( (global_pht(3), 'Global PHT with {} entries'.format(2**3)), ):
            s = (x.strip().split(' ') for x in lines)
            s = (parts_to_ints(x) for x in s)         
            print (string, ': ')            
            predict (s, verb, predictor)          
            
        print ("="*60)
        
    if 'twolevel' in predictors:
        for predictor, string in ( (twolevel_predictor(3,3), 'Two-level with {} entries and {} counters'.format(8,8)), ):
            s = (x.strip().split(' ') for x in lines)
            s = (parts_to_ints(x) for x in s)         
            print (string, ': ')            
            predict (s, verb, predictor)          
            
        print ("="*60)
   
    if 'hybrid_correlating' in predictors:
       for predictor, string in ( (hybrid_correlating_predictor(2,3), 
                                   'Hybrid correlating with global {} bit shift register and {} BHT counters'.format(2,2**3)), ):
            s = (x.strip().split(' ') for x in lines)
            s = (parts_to_ints(x) for x in s)         
            print (string, ': ')            
            predict (s, verb, predictor)          
       print ("="*60)
       
    if 'tournament' in predictors:
       for predictor, string in ( (tournament_predictor(3, 3, 3), 
                                   'tournament predictor {} entry bht and {} bit shift register'.format(8,8)), ):
            s = (x.strip().split(' ') for x in lines)
            s = (parts_to_ints(x) for x in s)         
            print (string, ': ')            
            predict (s, verb, predictor)          
       print ("="*60)