0

I was reading the paper, http://www.cs.utexas.edu/~lin/papers/hpca01.pdf, on Dynamic Branch Prediction with Perceptrons. I was wondering how to implement the perceptron branch predictor in C if given a list of 1000 PC addresses (word addresses) and 1000 number of actual outcome of the branch which are recorded in a trace line. Essentially, I want to use these traces to measure the accuracy of various predictors. The branch outcomes from the trace file should be used to train your predictors. Any suggestions?

David
  • 583
  • 1
  • 10
  • 27

1 Answers1

4

I think its fairly simple. Section 3.2 and 3.3 is all you really have to understand.

Section 3.2 says output percepatron is sum of past histories multipled by their wieghting factors:

#define SIZE_N 62 //or whatever see section 5.3
float history[n] = {0}; //Put branch history here, -1 not taken, 1 taken.
float weight[n] = {0};  //storage for weights

float percepatron(void )
{
    int i;
    float y=0;
    for (i=0;i<SIZE_N;i++) { y+= weight[i] * history[i];}
    return y;
}

Then in 3.3 the weighting factors come from training, which is simply train each one past on past result comparison:

void train(float result, float y, float theta) //passed result of last branch (-1 not taken, 1 taken), and perceptron value
{
    int i;
    if ((y<0) != (result<0)) || (abs(y) < theta))
    {
     for (i=0;i<SIZE_N;i++;) {
          weight[i] = weight[i] + result*history[i];
       }
    }
}

So all thats left is theta, which they tell you:

float theta = (1.93 * SIZE_N) + 14;

So the usage is:

y = percepatron();
//make prediction:
if (y < 0) predict_not_taken();
else predict_taken();
//get actual result
result = get_actual_branch_taken_result();//must return -1 not taken, 1 taken
//train for future predictions
train(y,result,theta);

//Then you need to shift everything down....
for (i=1;i<SIZE_N;i++)
{
  history[i] = history[i-1];
  //weight[i] = history[i-1]; //toggle this and see what happens :-)
}
history[0] = 1; //weighting - see section 3.2
Myforwik
  • 3,438
  • 5
  • 35
  • 42
  • What is the order of execution? Call the percepatron method and then the train function? I am still confused on when the predication is made and is determined to be either correct or not correct. – David Oct 29 '13 at 08:11
  • i have edited answer and fixed mistake in train (where it is meant to compare the sign of y to the sign of result. – Myforwik Oct 29 '13 at 22:17
  • Predict_taken and predict_not_taken are defined as? – David Oct 30 '13 at 00:42
  • 1
    Are you sure you know what you are doing? They are not defined as anything. They are moment where you prodict if or not the branch will be taken. You have some input which is a list of taken/non-taken. You feed them in one by one, growing the history and predicting the next one in advance. Then you see how good you were at predicting. – Myforwik Dec 01 '13 at 21:59
  • 3
    The weights array is actually a two dimensional vector. The perceptron BP method consists of a table of N perceptrons. Each perceptron has weights. Which perceptron to use would be based off of a hashing function which would be the result of a combination of some sort (XOR, concatenation, etc.), of the history bits and some bits of the branch PC. This hash would then be used to index into the table of N perceptrons. – maverick1989 Dec 03 '13 at 01:02