clock.c #include #include #include #include #include "clock.h" /* * Routines for using the cycle counter */ /* Detect whether running on Alpha */ #ifdef __alpha #define IS_ALPHA 1 #else #define...

Password is: CSOSPRING2020%-


clock.c #include #include #include #include #include "clock.h" /* * Routines for using the cycle counter */ /* Detect whether running on Alpha */ #ifdef __alpha #define IS_ALPHA 1 #else #define IS_ALPHA 0 #endif /* Detect whether running on x86 */ #ifdef __i386__ #define IS_x86 1 #else #define IS_x86 0 #endif #if IS_ALPHA /* Initialize the cycle counter */ static unsigned cyc_hi = 0; static unsigned cyc_lo = 0; /* Use Alpha cycle timer to compute cycles. Then use measured clock speed to compute seconds */ /* * counterRoutine is an array of Alpha instructions to access * the Alpha's processor cycle counter. It uses the rpcc * instruction to access the counter. This 64 bit register is * divided into two parts. The lower 32 bits are the cycles * used by the current process. The upper 32 bits are wall * clock cycles. These instructions read the counter, and * convert the lower 32 bits into an unsigned int - this is the * user space counter value. * NOTE: The counter has a very limited time span. With a * 450MhZ clock the counter can time things for about 9 * seconds. */ static unsigned int counterRoutine[] = { 0x601fc000u, 0x401f0000u, 0x6bfa8001u }; /* Cast the above instructions into a function. */ static unsigned int (*counter)(void)= (void *)counterRoutine; void start_counter() { /* Get cycle counter */ cyc_hi = 0; cyc_lo = counter(); } double get_counter() { unsigned ncyc_hi, ncyc_lo; unsigned hi, lo, borrow; double result; ncyc_lo = counter(); ncyc_hi = 0; lo = ncyc_lo - cyc_lo; borrow = lo > ncyc_lo; hi = ncyc_hi - cyc_hi - borrow; result = (double) hi * (1 < 30)="" *="" 4="" +="" lo;="" if="" (result="">< 0)="" {="" fprintf(stderr,="" "error:="" cycle="" counter="" returning="" negative="" value:="" %.0f\n",="" result);="" }="" return="" result;="" }="" #endif="" *="" alpha="" */="" #if="" is_x86="" *="" $begin="" x86cyclecounter="" */="" *="" initialize="" the="" cycle="" counter="" */="" static="" unsigned="" cyc_hi="0;" static="" unsigned="" cyc_lo="0;" *="" set="" *hi="" and="" *lo="" to="" the="" high="" and="" low="" order="" bits="" of="" the="" cycle="" counter.="" implementation="" requires="" assembly="" code="" to="" use="" the="" rdtsc="" instruction.="" */="" void="" access_counter(unsigned="" *hi,="" unsigned="" *lo)="" {="" asm("rdtsc;="" movl="" %%edx,%0;="" movl="" %%eax,%1"="" *="" read="" cycle="" counter="" */="" :="" "="r"" (*hi),="" "="r"" (*lo)="" *="" and="" move="" results="" to="" */="" :="" *="" no="" input="" */="" *="" the="" two="" outputs="" */="" :="" "%edx",="" "%eax");="" }="" *="" record="" the="" current="" value="" of="" the="" cycle="" counter.="" */="" void="" start_counter()="" {="" access_counter(&cyc_hi,="" &cyc_lo);="" }="" *="" return="" the="" number="" of="" cycles="" since="" the="" last="" call="" to="" start_counter.="" */="" double="" get_counter()="" {="" unsigned="" ncyc_hi,="" ncyc_lo;="" unsigned="" hi,="" lo,="" borrow;="" double="" result;="" *="" get="" cycle="" counter="" */="" access_counter(&ncyc_hi,="" &ncyc_lo);="" *="" do="" double="" precision="" subtraction="" */="" lo="ncyc_lo" -="" cyc_lo;="" borrow="lo"> ncyc_lo; hi = ncyc_hi - cyc_hi - borrow; result = (double) hi * (1 < 30)="" *="" 4="" +="" lo;="" if="" (result="">< 0)="" {="" fprintf(stderr,="" "error:="" counter="" returns="" neg="" value:="" %.0f\n",="" result);="" }="" return="" result;="" }="" *="" $end="" x86cyclecounter="" */="" #endif="" *="" x86="" */="" double="" ovhd()="" {="" *="" do="" it="" twice="" to="" eliminate="" cache="" effects="" */="" int="" i;="" double="" result;="" for="" (i="0;" i="">< 2;="" i++)="" {="" start_counter();="" result="get_counter();" }="" return="" result;="" }="" *="" $begin="" mhz="" */="" *="" estimate="" the="" clock="" rate="" by="" measuring="" the="" cycles="" that="" elapse="" */="" *="" while="" sleeping="" for="" sleeptime="" seconds="" */="" double="" mhz_full(int="" verbose,="" int="" sleeptime)="" {="" double="" rate;="" start_counter();="" sleep(sleeptime);="" rate="get_counter()" (1e6*sleeptime);="" if="" (verbose)="" printf("processor="" clock="" rate="" ~="%.1f" mhz\n",="" rate);="" return="" rate;="" }="" *="" $end="" mhz="" */="" *="" version="" using="" a="" default="" sleeptime="" */="" double="" mhz(int="" verbose)="" {="" return="" mhz_full(verbose,="" 2);="" }="" **="" special="" counters="" that="" compensate="" for="" timer="" interrupt="" overhead="" */="" static="" double="" cyc_per_tick="0.0;" #define="" nevent="" 100="" #define="" threshold="" 1000="" #define="" recordthresh="" 3000="" *="" attempt="" to="" see="" how="" much="" time="" is="" used="" by="" timer="" interrupt="" */="" static="" void="" callibrate(int="" verbose)="" {="" double="" oldt;="" struct="" tms="" t;="" clock_t="" oldc;="" int="" e="0;" times(&t);="" oldc="t.tms_utime;" start_counter();="" oldt="get_counter();" while="" (e="">= THRESHOLD) { clock_t newc; times(&t); newc = t.tms_utime; if (newc > oldc) { double cpt = (newt-oldt)/(newc-oldc); if ((cyc_per_tick == 0.0 || cyc_per_tick > cpt) && cpt > RECORDTHRESH) cyc_per_tick = cpt; /* if (verbose) printf("Saw event lasting %.0f cycles and %d ticks. Ratio = %f\n", newt-oldt, (int) (newc-oldc), cpt); */ e++; oldc = newc; } oldt = newt; } } /* ifdef added by Sanjit - 10/2001 */ #ifdef DEBUG if (verbose) printf("Setting cyc_per_tick to %f\n", cyc_per_tick); #endif } static clock_t start_tick = 0; void start_comp_counter() { struct tms t; if (cyc_per_tick == 0.0) callibrate(1); times(&t); start_tick = t.tms_utime; start_counter(); } double get_comp_counter() { double time = get_counter(); double ctime; struct tms t; clock_t ticks; times(&t); ticks = t.tms_utime - start_tick; ctime = time - ticks*cyc_per_tick; /* printf("Measured %.0f cycles. Ticks = %d. Corrected %.0f cycles\n", time, (int) ticks, ctime); */ return ctime; } clock.h /* Routines for using cycle counter */ /* Start the counter */ void start_counter(); /* Get # cycles since counter started */ double get_counter(); /* Measure overhead for counter */ double ovhd(); /* Determine clock rate of processor (using a default sleeptime) */ double mhz(int verbose); /* Determine clock rate of processor, having more control over accuracy */ double mhz_full(int verbose, int sleeptime); /** Special counters that compensate for timer interrupt overhead */ void start_comp_counter(); double get_comp_counter(); config.h /********************************************************* * config.h - Configuration data for the driver.c program. *********************************************************/ #ifndef _CONFIG_H_ #define _CONFIG_H_ /* * CPEs for the baseline (naive) version of the rotate function that * was handed out to the students. Rd is the measured CPE for a dxd * image. Run the driver.c program on your system to get these * numbers. */ #define R64 14.7 #define R128 40.1 #define R256 46.4 #define R512 65.9 #define R1024 94.5 /* * CPEs for the baseline (naive) version of the smooth function that * was handed out to the students. Sd is the measure CPE for a dxd * image. Run the driver.c program on your system to get these * numbers. */ #define S32 695 #define S64 698 #define S128 702 #define S256 717 #define S512 722 #endif /* _CONFIG_H_ */ defs.h /* * driver.h - Various definitions for the CSO Final Exam. * * DO NOT MODIFY ANYTHING IN THIS FILE */ #ifndef _DEFS_H_ #define _DEFS_H_ #include #define RIDX(i,j,n) ((i)*(n)+(j)) typedef struct { char *team; char *name1, *email1; char *name2, *email2; } team_t; extern team_t team; typedef struct { unsigned short red; unsigned short green; unsigned short blue; } pixel; typedef void (*lab_test_func) (int, pixel*, pixel*); void smooth(int, pixel *, pixel *); void rotate(int, pixel *, pixel *); void register_rotate_functions(void); void register_smooth_functions(void); void add_smooth_function(lab_test_func, char*); void add_rotate_function(lab_test_func, char*); #endif /* _DEFS_H_ */ driver.c /******************************************************************* * * driver.c - Driver program for CSO Final Exam * * In kernels.c, students generate an arbitrary number of rotate and * smooth test functions, which they then register with the driver * program using the add_rotate_function() and add_smooth_function() * functions. * * The driver program runs and measures the registered test functions * and reports their performance. * ********************************************************************/ #include #include #include #include #include #include #include #include #include "fcyc.h" #include "defs.h" #include "config.h" /* Team structure that identifies the students */ extern team_t team; /* Keep track of a number of different test functions */ #define MAX_BENCHMARKS 100 #define DIM_CNT 5 /* Misc constants */ #define BSIZE 32 /* cache block size in bytes */ #define MAX_DIM 1280 /* 1024 + 256 */ #define ODD_DIM 96 /* not a power of 2 */ /* fast versions of min and max */ #define min(a,b) (a < b="" a="" :="" b)="" #define="" max(a,b)="" (a=""> b ? a : b) /* This struct characterizes the results for one benchmark test */ typedef struct { lab_test_func tfunct; /* The test function */ double cpes[DIM_CNT]; /* One CPE result for each dimension */ char *description; /* ASCII description of the test function */ unsigned short valid; /* The function is tested if this
May 15, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here