Skip to content

Commit

Permalink
Final code with correct printing
Browse files Browse the repository at this point in the history
  • Loading branch information
u7karsh committed Oct 22, 2017
1 parent 5544171 commit b95c7d9
Show file tree
Hide file tree
Showing 18 changed files with 6,041,389 additions and 32 deletions.
36 changes: 23 additions & 13 deletions bp.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ bpPT branchPredictorInit(
int btbAssoc
)
{
if( m == 0 ) return NULL;

// Calloc the mem to reset all vars to 0
bpPT bpP = (bpPT) calloc( 1, sizeof(bpT) );
if( type == BP_TYPE_GSHARE || type == BP_TYPE_HYBRID )
Expand Down Expand Up @@ -74,7 +76,7 @@ bpPT branchPredictorInit(
}

// BTB
bpP->btbPresent = TRUE;
bpP->btbPresent = (btbSize > 0) ? TRUE : FALSE;
bpP->btbSize = btbSize;
bpP->btbAssoc = btbAssoc;
bpP->btbSets = ceil( (double) btbSize / (double) (btbAssoc * 4) );
Expand Down Expand Up @@ -162,7 +164,6 @@ bpPathT bpPredict( bpPT bpP, int indexBp, int indexBtb, int tag, boolean *update
for( int assocIndex = 0; assocIndex < bpP->btbAssoc; assocIndex++ ){
if( rowP[assocIndex]->valid == 1 && rowP[assocIndex]->tag == tag ){
hit = TRUE;
//printf("BTB HIT\n");
bpBtbHitUpdateLRU( bpP, indexBtb, assocIndex );
break;
}
Expand Down Expand Up @@ -197,17 +198,21 @@ bpPathT bpPredict( bpPT bpP, int indexBp, int indexBtb, int tag, boolean *update

// Counter is incremented if branch was taken, decremented if not taken
// Counter saturates at [0, 3]
void bpUpdatePredictionTable( bpPT bpP, int index, bpPathT actual )
void bpUpdatePredictionTable( bpPT bpP, int index, bpPathT actual, boolean btbMiss )
{
if( btbMiss ){
bpP->btbPredictions++;
bpP->btbMissTaken += ( actual == BP_PATH_TAKEN ) ? 1 : 0;
return;
}

int counter = bpP->predictionTable[index];
//printf("GSHARE index: %d old value: %d", index, counter);
counter += ( actual == BP_PATH_TAKEN ) ? 1 : -1;

// Saturate
counter = ( counter > 3 ) ? 3 : counter;
counter = ( counter < 0 ) ? 0 : counter;

//printf(" new value %d\n", counter);
bpP->predictionTable[index] = counter;
}

Expand All @@ -232,16 +237,24 @@ void bpPrintPredictionTable( bpPT bpP )
void bpBtbPrintContents( bpPT bpP )
{
if( !bpP ) return;
if( !bpP->btbPresent ) return;

for( int setIndex = 0; setIndex < bpP->btbSets; setIndex++ ){
printf("set\t\t%d:\t\t", setIndex);
printf("set %d\t:\t\t", setIndex);
tagPT *rowP = bpP->tagStoreP[setIndex]->rowP;
for( int assocIndex = 0; assocIndex < bpP->btbAssoc; assocIndex++ ){
printf("%x\t", rowP[assocIndex]->tag);
}
printf("\n");
}
printf("\n");
}

void bpBtbGetMetrics( bpPT bpP, int* pred, int* misPred )
{
*pred = bpP->btbPredictions;
*misPred = bpP->btbMissTaken;
}

//------------------- CONTROLLER FUNCS ---------------------
bpControllerPT bpCreateController( bpPT bpBimodalP, bpPT bpGshareP, int k, bpTypeT type )
Expand Down Expand Up @@ -273,15 +286,14 @@ void bpControllerProcess( bpControllerPT contP, int address, bpPathT actual )
boolean update;
bpGetIndexTag( contP->bpBimodalP, address, &indexBp, &indexBtb, &tag );
predicted = bpPredict( contP->bpBimodalP, indexBp, indexBtb, tag, &update );
if( update )
bpUpdatePredictionTable( contP->bpBimodalP, indexBp, actual );
bpUpdatePredictionTable( contP->bpBimodalP, indexBp, actual, !update );
} else if( type == BP_TYPE_GSHARE ){
int indexBp, indexBtb, tag;
boolean update;
bpGetIndexTag( contP->bpGshareP, address, &indexBp, &indexBtb, &tag );
predicted = bpPredict( contP->bpGshareP, indexBp, indexBtb, tag, &update );
bpUpdatePredictionTable( contP->bpGshareP, indexBp, actual, !update );
if( update ){
bpUpdatePredictionTable( contP->bpGshareP, indexBp, actual );
bpUpdateGlobalBrHistoryTable( contP->bpGshareP, actual );
}
} else{
Expand All @@ -305,12 +317,10 @@ void bpControllerProcess( bpControllerPT contP, int address, bpPathT actual )

if( selectedType == BP_TYPE_BIMODAL ){
// Update bimodal
if( updateBimodal )
bpUpdatePredictionTable( contP->bpBimodalP, bimodalIndexBp, actual );
bpUpdatePredictionTable( contP->bpBimodalP, bimodalIndexBp, actual, !updateBimodal );
} else{
// Update Gshare
if( updateGshare )
bpUpdatePredictionTable( contP->bpGshareP, gshareIndexBp, actual );
bpUpdatePredictionTable( contP->bpGshareP, gshareIndexBp, actual, !updateGshare );
}

// Global Br updates irrespective of decision
Expand Down
6 changes: 5 additions & 1 deletion bp.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ typedef struct _bpT{
int btbIndexMask;
int btbAssoc;
int btbSets;
int btbPredictions;
int btbMissTaken;

tagStorePT *tagStoreP;
//-------------------- BIMODAL/GSHARE BEGIN -------------------------
// Number of bits used to represent prediction table
Expand Down Expand Up @@ -119,10 +122,11 @@ void bpGetIndexTag( bpPT bpP, int address, int* indexBpP, int* indexBtbP, int* t
void bpBtbHitUpdateLRU( bpPT bpP, int index, int setIndex );
int bpBtbFindReplacementUpdateCounterLRU( bpPT bpP, int index, int tag, int overrideSetIndex, int doOverride );
bpPathT bpPredict( bpPT bpP, int indexBp, int indexBtb, int tag, boolean *update );
void bpUpdatePredictionTable( bpPT bpP, int index, bpPathT actual );
void bpUpdatePredictionTable( bpPT bpP, int index, bpPathT actual, boolean btbMiss );
void bpUpdateGlobalBrHistoryTable( bpPT bpP, bpPathT actual );
void bpPrintPredictionTable( bpPT bpP );
void bpBtbPrintContents( bpPT bpP );
void bpBtbGetMetrics( bpPT bpP, int* pred, int* misPred );

// Controller funcs
bpControllerPT bpCreateController( bpPT bpBimodalP, bpPT bpGshareP, int k, bpTypeT type );
Expand Down
93 changes: 75 additions & 18 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ void doTrace( bpControllerPT contP, FILE* fp )
// Just to safeguard on byte reading
ASSERT(bytesRead <= 0, "fscanf read nothing!");
count++;
//printf("%d. PC: %x %c\n", count, address, taken);
if( taken == 't' || taken == 'T' ){
// Taken
bpControllerProcess( contP, address, BP_PATH_TAKEN );
Expand All @@ -34,16 +33,41 @@ void doTrace( bpControllerPT contP, FILE* fp )
} while( !feof(fp) );
}

void printStats( bpControllerPT contP )
void doBtbPrint( bpPT bpP, int pred, int misPred, double misPredRate )
{
if( bpP == NULL ) return;
if( !bpP->btbPresent ) return;

int btbPred, btbMisPred;
bpBtbGetMetrics( bpP, &btbPred, &btbMisPred );
printf("size of BTB: %d\n", bpP->btbSize);
printf("number of branches: %d\n", pred);
printf("number of predictions from branch predictor: %d\n", pred - btbPred);
printf("number of mispredictions from branch predictor: %d\n", misPred - btbMisPred);
printf("number of branches miss in BTB and taken: %d\n", btbMisPred);
printf("total mispredictions: %d\n", misPred);
printf("misprediction rate: %0.2f%%\n", misPredRate * 100.0);
printf("\nFINAL BTB CONTENTS\n");
bpBtbPrintContents( bpP );
}

void printStats( bpControllerPT contP, boolean printBtb )
{
int pred, misPred;
double misPredRate;

printf("OUTPUT\n");

bpControllerGetMetrics( contP, &pred, &misPred, &misPredRate );
printf("number of predictions: %d\n", pred);
printf("number of mispredictions: %d\n", misPred);
printf("misprediction rate: %0.2f%%\n", misPredRate * 100.0);
printf("FINAL BTB CONTENTS\n");
bpBtbPrintContents( contP->bpGshareP );
if( printBtb ){
doBtbPrint( contP->bpBimodalP, pred, misPred, misPredRate );
doBtbPrint( contP->bpGshareP , pred, misPred, misPredRate );
} else{
printf("number of predictions: %d\n", pred);
printf("number of mispredictions: %d\n", misPred);
printf("misprediction rate: %0.2f%%\n", misPredRate * 100.0);
}

if( contP->type == BP_TYPE_HYBRID ){
printf("FINAL CHOOSER CONTENTS\n");
bpControllerPrintChooserTable( contP );
Expand All @@ -61,20 +85,53 @@ void printStats( bpControllerPT contP )
int main( int argc, char** argv )
{
char traceFile[128];
int k = atoi(argv[1]);
int mGshare = atoi(argv[2]);
int mBimodal = atoi(argv[3]);
int n = atoi(argv[4]);
int assocGshare = atoi(argv[5]);
int assocBimodal = atoi(argv[6]);
sprintf( traceFile, "%s", argv[7] );
int k = 0;
int mGshare = 0;
int mBimodal = 0;
int n = 0;
int btbAssoc = 0;
int btbSize = 0;
bpTypeT controllerType = BP_TYPE_BIMODAL;
printf("COMMAND\n");
for( int i = 0; i < argc; i++ )
printf("%s ", argv[i]);
printf("\n");

if ( strcmp( argv[1], "bimodal" ) == 0 ){
ASSERT( argc < 6, "Number of arguments(=%d) less than desired(=6)", argc );
mBimodal = atoi( argv[2] );
btbSize = atoi( argv[3] );
btbAssoc = atoi( argv[4] );
sprintf( traceFile, "%s", argv[5] );
controllerType = BP_TYPE_BIMODAL;
} else if( strcmp( argv[1], "gshare" ) == 0 ){
ASSERT( argc < 7, "Number of arguments(=%d) less than desired(=7)", argc );
mGshare = atoi( argv[2] );
n = atoi( argv[3] );
btbSize = atoi( argv[4] );
btbAssoc = atoi( argv[5] );
sprintf( traceFile, "%s", argv[6] );
controllerType = BP_TYPE_GSHARE;
} else if( strcmp( argv[1], "hybrid" ) == 0 ){
ASSERT( argc < 9, "Number of arguments(=%d) less than desired(=9)", argc );
k = atoi( argv[2] );
mGshare = atoi( argv[3] );
n = atoi( argv[4] );
mBimodal = atoi( argv[5] );
btbSize = atoi( argv[6] );
btbAssoc = atoi( argv[7] );
sprintf( traceFile, "%s", argv[8] );
controllerType = BP_TYPE_HYBRID;
} else{
ASSERT( TRUE, "Illegal predictor requested: %s", argv[1] );
}

FILE* fp = fopen( traceFile, "r" );
ASSERT(!fp, "Unable to read file: %s\n", traceFile);
bpPT bpBimodalP = branchPredictorInit( "bimodal", mBimodal, n, BP_TYPE_BIMODAL, 2048, assocBimodal);
bpPT bpGshareP = branchPredictorInit( "gshare" , mGshare , n, BP_TYPE_GSHARE, 2048, assocGshare);
bpControllerPT contP = bpCreateController( bpBimodalP, bpGshareP, k, BP_TYPE_BIMODAL );
bpPT bpBimodalP = branchPredictorInit( "bimodal", mBimodal, n, BP_TYPE_BIMODAL, btbSize, btbAssoc );
bpPT bpGshareP = branchPredictorInit( "gshare" , mGshare , n, BP_TYPE_GSHARE , btbSize, btbAssoc );
bpControllerPT contP = bpCreateController( bpBimodalP, bpGshareP, k, controllerType );

doTrace( contP, fp );
printStats( contP );
printStats( contP, btbSize > 0 );
}
Loading

0 comments on commit b95c7d9

Please sign in to comment.