📄 driver.cpp
字号:
benchmarks_rotate[idx].tfunct(dim, orig, result);
}
static void run_smooth_benchmark(int idx, int dim) {
benchmarks_smooth[idx].tfunct(dim, orig, result);
}
static void test_rotate(int bench_index) {
int i;
int test_num;
char *description = benchmarks_rotate[bench_index].description;
/* Check for odd dimension */
create(ODD_DIM);
run_rotate_benchmark(bench_index, ODD_DIM);
if (check_rotate(ODD_DIM)) {
printf("Benchmark \"%s\" failed correctness check for dimension %d.\n",
benchmarks_rotate[bench_index].description, ODD_DIM);
return;
}
/* run tests on all dimensions */
for(test_num=0; test_num < DIM_CNT; test_num++) {
int dim;
/* Create a test image of the required dimension */
dim = test_dim[test_num];
create(dim);
/* Check that the code works */
run_rotate_benchmark(bench_index, dim);
if (check_rotate(dim)) {
printf("Benchmark \"%s\" failed correctness check for dimension %d.\n",
benchmarks_rotate[bench_index].description, dim);
return;
}
/* Measure rotate cache performance */
{
int tmpdim = dim;
void *arglist[4];
double dimension = (double) dim;
double work = dimension*dimension;
#ifdef DEBUG
printf("DEBUG: dimension=%.1f\n",dimension);
printf("DEBUG: work=%.1f\n",work);
#endif
arglist[0] = (void *) benchmarks_rotate[bench_index].tfunct;
arglist[1] = (void *) &tmpdim;
arglist[2] = (void *) orig;
arglist[3] = (void *) result;
create(dim);
set_cache_measurement_enabled(1);
reset_cache();
func_wrapper(arglist);
benchmarks_rotate[bench_index].cache_attempts[test_num] =
get_read_count() + get_write_count();
benchmarks_rotate[bench_index].cache_hits[test_num] =
benchmarks_rotate[bench_index].cache_attempts[test_num]
- get_read_miss_count() - get_write_miss_count();
benchmarks_rotate[bench_index].cache_hitrate[test_num] =
((float)benchmarks_rotate[bench_index].cache_hits[test_num]) /
((float)benchmarks_rotate[bench_index].cache_attempts[test_num]);
}
}
/* Print results as a table */
printf("Rotate: Version = %s:\n", description);
printf("Dim");
for (i = 0; i < DIM_CNT; i++)
printf("\t%d", test_dim[i]);
printf("\tMean\n");
printf("hitrate");
for (i = 0; i < DIM_CNT; i++) {
printf("\t%.1f", 100.0*((double)(benchmarks_rotate[bench_index].cache_hits[i])) /
((double)(benchmarks_rotate[bench_index].cache_attempts[i])));
}
printf("\n");
/* Compute Improvement */
{
double prod;
double mean;
double ratios[DIM_CNT];
int exists = 0;
prod = 1.0; //* Geometric mean */ /*
printf("Incr.");
for (i = 0; i < DIM_CNT; i++) {
//our naive impl.
ratios[i] = benchmarks_rotate[bench_index].cache_hitrate[i] /
benchmarks_rotate[0].cache_hitrate[i];
prod *= ratios[i];
printf("\t%.2f", ratios[i]);
}
///* Geometric mean */ /*
mean = pow(prod, 1.0/(double) DIM_CNT);
#ifdef FIND_BEST_OF_MANY
if(mean > best_mean) {
best_mean = mean;
best_desc = benchmarks_rotate[bench_index].description;
}
#endif
printf("\t%.2f", mean);
printf("\n\n");
}
#ifdef DEBUG
fflush(stdout);
#endif
return;
}
static void test_smooth(int bench_index) {
int i;
int test_num;
char *description = benchmarks_smooth[bench_index].description;
/* Check for odd dimension */
create(ODD_DIM);
run_smooth_benchmark(bench_index, ODD_DIM);
if (check_smooth(ODD_DIM)) {
printf("Benchmark \"%s\" failed correctness check for dimension %d.\n",
benchmarks_smooth[bench_index].description, ODD_DIM);
return;
}
/* run tests on all dimensions */
for(test_num=0; test_num < DIM_CNT; test_num++) {
int dim;
/* Create a test image of the required dimension */
dim = test_dim[test_num];
create(dim);
/* Check that the code works */
run_smooth_benchmark(bench_index, dim);
if (check_smooth(dim)) {
printf("Benchmark \"%s\" failed correctness check for dimension %d.\n",
benchmarks_smooth[bench_index].description, dim);
return;
}
/* Measure smooth cache performance */
{
int imgdim = dim;
void *arglist[4];
double dimension = (double) dim;
double work = dimension*dimension;
#ifdef DEBUG
printf("DEBUG: dimension=%.1f\n",dimension);
printf("DEBUG: work=%.1f\n",work);
#endif
arglist[0] = (void *) benchmarks_smooth[bench_index].tfunct;
arglist[1] = (void *) &imgdim;
arglist[2] = (void *) orig;
arglist[3] = (void *) result;
create(dim);
set_cache_measurement_enabled(1);
reset_cache();
func_wrapper(arglist);
benchmarks_smooth[bench_index].cache_attempts[test_num] =
get_read_count() + get_write_count();
benchmarks_smooth[bench_index].cache_hits[test_num] =
benchmarks_smooth[bench_index].cache_attempts[test_num]
- get_read_miss_count() - get_write_miss_count();
benchmarks_smooth[bench_index].cache_hitrate[test_num] =
((float)benchmarks_smooth[bench_index].cache_hits[test_num]) /
((float)benchmarks_smooth[bench_index].cache_attempts[test_num]);
}
}
/* Print results as a table */
printf("Smooth: Version = %s:\n", description);
printf("Dim");
for (i = 0; i < DIM_CNT; i++)
printf("\t%d", test_dim[i]);
printf("\tMean\n");
printf("hitrate");
for (i = 0; i < DIM_CNT; i++) {
printf("\t%.1f", 100.0*((double)(benchmarks_smooth[bench_index].cache_hits[i])) /
((double)(benchmarks_smooth[bench_index].cache_attempts[i])));
}
printf("\n");
/* Compute Improvement */
{
double prod;
double mean;
double ratios[DIM_CNT];
int exists = 0;
prod = 1.0; //* Geometric mean */ /*
printf("Incr.");
for (i = 0; i < DIM_CNT; i++) {
//our naive impl.
ratios[i] = benchmarks_smooth[bench_index].cache_hitrate[i] /
benchmarks_smooth[0].cache_hitrate[i];
prod *= ratios[i];
printf("\t%.2f", ratios[i]);
}
///* Geometric mean */ /*
mean = pow(prod, 1.0/(double) DIM_CNT);
printf("\t%.2f", mean);
printf("\n\n");
#ifdef FIND_BEST_OF_MANY
if(mean > best_mean) {
best_mean = mean;
best_desc = benchmarks_smooth[bench_index].description;
}
#endif
}
#ifdef DEBUG
fflush(stdout);
#endif
return;
}
/*******************************************
*******************************************
*
* GRADER-ONLY FUNCTIONS
*
*******************************************
*******************************************
*/
//void test_called_defines(
/*******************************************
*******************************************
*
* FUNCTIONS
*
*******************************************
*******************************************
*/
void add_rotate_function(lab_test_func f, char *description) {
if(rotate_benchmark_count < MAX_BENCHMARKS) {
benchmarks_rotate[rotate_benchmark_count].tfunct = f;
benchmarks_rotate[rotate_benchmark_count].description = description;
benchmarks_rotate[rotate_benchmark_count].valid = 0;
rotate_benchmark_count++;
}
else {
printf("ERROR: tried to register too many rotate functions.\n");
}
}
void add_smooth_function(lab_test_func f, char *description) {
if(smooth_benchmark_count < MAX_BENCHMARKS) {
benchmarks_smooth[smooth_benchmark_count].tfunct = f;
benchmarks_smooth[smooth_benchmark_count].description = description;
benchmarks_smooth[smooth_benchmark_count].valid = 0;
smooth_benchmark_count++;
}
else {
printf("ERROR: tried to register too many smooth functions.\n");
}
}
int main(int argc, char *argv[])
{
extern char* optarg;
int i;
int seed = 1729;
int quit_after_dump = 0;
char c = '0';
char *bench_func_file = NULL;
char *func_dump_file = NULL;
char *group_id = NULL;
/* register all the defined functions */
add_rotate_function(rotate_naive_reference, rotate_naive_reference_descr);
register_rotate_functions();
add_smooth_function(smooth_naive_reference, smooth_naive_reference_descr);
register_smooth_functions();
reset_cache();
set_cache_measurement_enabled(0);
srand(seed);
for(i=0; i<rotate_benchmark_count; i++)
benchmarks_rotate[i].valid = 1;
for(i=0; i<smooth_benchmark_count; i++)
benchmarks_smooth[i].valid = 1;
#ifdef FIND_BEST_OF_MANY
best_mean = 0;
#endif
for(i=0; i<rotate_benchmark_count; i++) {
if (benchmarks_rotate[i].valid) {
test_rotate(i);
}
}
#ifdef FIND_BEST_OF_MANY
printf("Best algo here: %s, \t%f\n\n\n", best_desc, best_mean);
best_mean = 0;
#endif
for(i=0; i<smooth_benchmark_count; i++) {
if(benchmarks_smooth[i].valid) {
test_smooth(i);
}
}
#ifdef FIND_BEST_OF_MANY
printf("Best algo here: %s, \t%f\n\n", best_desc, best_mean);
#endif
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -