Advertisement
Guest User

Branch_Predictors

a guest
Mar 2nd, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.87 KB | None | 0 0
  1. #!/usr/bin/python
  2. import sys
  3.  
  4. class gshare():
  5.     def __init__(self, num_entries):
  6.     self.num_entries = num_entries
  7.     self.predictions = [0]*self.num_entries
  8.  
  9.     def get_prediction(self, index):
  10.     if self.predictions[index] > 1:
  11.         return 1
  12.     else:
  13.         return 0
  14.  
  15.     def update(self, index, branch_outcome):
  16.     if branch_outcome == 1 and self.predictions[index] < 3:
  17.         self.predictions[index] += 1
  18.  
  19.     if branch_outcome == 0 and self.predictions[index] > 0:
  20.         self.predictions[index] -= 1
  21.  
  22.  
  23.  
  24. class local():
  25.     def __init__(self, num_entries):
  26.     self.num_entries = num_entries
  27.     self.predictions = [0]*self.num_entries
  28.  
  29.     def get_prediction(self, index):
  30.     if self.predictions[index] > 1:
  31.         return 1
  32.     else:
  33.         return 0
  34.  
  35.     def update(self, index, branch_outcome):
  36.     if branch_outcome == 1 and self.predictions[index] < 3:
  37.         self.predictions[index] += 1
  38.  
  39.     if branch_outcome == 0 and self.predictions[index] > 0:
  40.         self.predictions[index] -= 1
  41.  
  42.  
  43.  
  44. class tournament():
  45.     def __init__(self, num_entries):
  46.     self.num_entries = num_entries
  47.     self.predictions = [0]*self.num_entries
  48.  
  49.     def get_prediction(self, index):
  50.     if self.predictions[index] > 1:
  51.         return 1
  52.     else:
  53.         return 0
  54.  
  55.     def update(self, index, global_pred, local_pred, branch_outcome):
  56.     if branch_outcome == global_pred and branch_outcome != local_pred and self.predictions[index] < 3:
  57.         self.predictions[index] += 1
  58.  
  59.     if branch_outcome != global_pred and branch_outcome == local_pred and self.predictions[index] > 0:
  60.         self.predictions[index] -= 1
  61.  
  62.  
  63.  
  64.  
  65. def main():
  66.  
  67.     predictor3 = gshare(4096)
  68.     predictor4 = local(256)
  69.     predictor5 = tournament(1024)
  70.  
  71.     local_hist_length = 8
  72.     local_table_size = 1024
  73.  
  74.     global_hist_length = 12
  75.  
  76.     total_predictions = 0
  77.     max3 = max4 = max5 = 0
  78.     wrong3 = wrong4 = wrong5 = 0
  79.  
  80.     misslist3 = []
  81.     misslist4 = []
  82.     misslist5 = []
  83.    
  84.     global_history = [0]*global_hist_length
  85.  
  86.     local_table = [[0]*local_hist_length for x in xrange(local_table_size)]
  87.  
  88.     _ = sys.stdin.readline()  # throw away first line
  89.    
  90.     for line in sys.stdin# get the two feilds and convert them to integers
  91.     [pc, branch_outcome] = [int(x,0) for x in line.split()]   #int(x,0) - "look at the string, and guess the base"
  92.  
  93.     global_hist_string = ''.join([str(x) for x in global_history])  #convert list to string
  94.     global_hist_pattern = int(global_hist_string, 2)    #convert string to binary number
  95.     global_index = ((pc>>2) % 4096) ^ global_hist_pattern    #XOR history pattern with lower bits of PC
  96.  
  97.     local_table_index = (pc>>2) % local_table_size
  98.     local_hist_string = ''.join([str(x) for x in local_table[local_table_index]])
  99.     local_index = int(local_hist_string, 2)
  100.  
  101.     tour_index = global_index >> 2
  102.  
  103.     this_prediction3 = predictor3.get_prediction(global_index)
  104.     this_prediction4 = predictor4.get_prediction(local_index)
  105.     this_prediction5 = predictor5.get_prediction(tour_index)
  106.  
  107.     if this_prediction5 == 1:
  108.         this_prediction5 = this_prediction3
  109.     else:
  110.         this_prediction5 = this_prediction4
  111.  
  112.     total_predictions += 1
  113.  
  114.  
  115.     if this_prediction3 != branch_outcome:
  116.         wrong3 += 1
  117.             misslist3.append(pc)
  118.  
  119.     if this_prediction4 != branch_outcome:
  120.         wrong4 += 1
  121.             misslist4.append(pc)
  122.  
  123.     if this_prediction5 != branch_outcome:
  124.         wrong5 += 1
  125.             misslist5.append(pc)
  126.  
  127.  
  128.     predictor3.update(global_index, branch_outcome)
  129.     predictor4.update(local_index, branch_outcome)
  130.     predictor5.update(tour_index, this_prediction3, this_prediction4, branch_outcome)
  131.  
  132.     global_history.append(branch_outcome)   #update history tables
  133.     local_table[local_table_index].append(branch_outcome)
  134.  
  135.     global_history = global_history[1:]    #discard the oldest outcomes
  136.     local_table[local_table_index] = local_table[local_table_index][1:]
  137.  
  138.  
  139.     print "\nTotal predictions:", total_predictions
  140.  
  141.     print "wrong predictions3:", wrong3
  142.     print "wrong predictions4:", wrong4
  143.     print "wrong predictions5:", wrong5
  144.     print
  145.  
  146.     print predictor3.__class__.__name__, 100* (total_predictions-wrong3) / float(total_predictions)
  147.     print predictor4.__class__.__name__, 100* (total_predictions-wrong4) / float(total_predictions)
  148.     print predictor5.__class__.__name__, 100* (total_predictions-wrong5) / float(total_predictions)
  149.     print
  150.  
  151.     for addr in set(misslist3):
  152.         count = misslist3.count(addr)
  153.         if count > max3:
  154.             max3 = count
  155.             worst_pc3 = addr
  156.  
  157.     for addr in set(misslist4):
  158.         count = misslist4.count(addr)
  159.         if count > max4:
  160.             max4 = count
  161.             worst_pc4 = addr
  162.  
  163.     for addr in set(misslist5):
  164.         count = misslist5.count(addr)
  165.         if count > max5:
  166.             max5 = count
  167.             worst_pc5 = addr
  168.  
  169.     print "global:      ", hex(worst_pc3), max3
  170.     print "local:       ", hex(worst_pc4), max4
  171.     print "tournament   ", hex(worst_pc5), max5
  172.     print
  173.  
  174.  
  175.  
  176. if __name__ == "__main__":
  177.        main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement