void flush_cache(int cache_size)
{
//Operands for the calibration routine
int operand_size = cache_size/(2*sizeof(double));
double alpha = 43.5; //arbitrary
double* X = new double[operand_size];
double* Y = new double[operand_size];
//Converge difference betweeen most recent iterations to within 5%
double last_duration = 0;
//Arbitrary large value so that the loop runs once
double percent_change = 1000;
long flush_size = 100*cache_size;
int iter_counter = 0;
while(percent_change > 5)
{
//We start at the size of the cache, which would be adequate if the replacement policy was LRU.
//Then we double the flush size every iteration
char* flush_array = new char[flush_size];
//Set them all to NULL so that the sum will add up to zero
memset(flush_array,0,flush_size*sizeof(char));
//Make sure our operands are in cache
fill(X,operand_size);
fill(Y,operand_size);
//Now flush
_read_flush_array(flush_array,flush_size);
double seconds = wall_time();
daxpy(alpha,X,Y,operand_size);
seconds = wall_time() - seconds;
printf("Iteration %d: %g s\n",iter_counter,seconds);
percent_change = (fabs(last_duration - seconds)/double(seconds))*100;
last_duration = seconds;
++iter_counter;
delete [] flush_array;
flush_size *= 2;
}
char* flush_array = new char[flush_size];
memset(flush_array,0,flush_size*sizeof(char));
_read_flush_array(flush_array,flush_size);
delete [] flush_array;
}