diff options
Diffstat (limited to 'assignment.c')
-rw-r--r-- | assignment.c | 167 |
1 files changed, 69 insertions, 98 deletions
diff --git a/assignment.c b/assignment.c index d3598c5..2f11ea0 100644 --- a/assignment.c +++ b/assignment.c @@ -4,6 +4,7 @@ #include <string.h> #include <math.h> #include <limits.h> +#include <time.h> #include "nmglobal.h" #include "nbench1.h" @@ -33,7 +34,7 @@ typedef struct { /* ** PROTOTYPES */ -static unsigned long DoAssignIteration(long *arraybase, +static clock_t DoAssignIteration(long *arraybase, unsigned long numarrays); static void LoadAssignArrayWithRand(long *arraybase, unsigned long numarrays); @@ -65,79 +66,62 @@ static void second_assignments(long tableau[][ASSIGNCOLS], ** probably non-optimal constructs. ** */ -void DoAssign(void) +void +DoAssign(void) { -AssignStruct *locassignstruct = &global_assignstruct; /* Local structure ptr */ -long *arraybase = NULL; -char *context = "CPU:Assignment"; -unsigned long accumtime; -double iterations; - -/* -** See if we need to do self adjustment code. -*/ -if(locassignstruct->adjust==0) -{ - /* - ** Self-adjustment code. The system begins by working on 1 - ** array. If it does that in no time, then two arrays - ** are built. This process continues until - ** enough arrays are built to handle the tolerance. - */ - locassignstruct->numarrays=1; - while(1) - { - arraybase = realloc(arraybase, sizeof(long) * ASSIGNROWS * ASSIGNCOLS * locassignstruct->numarrays); - if (!arraybase) { + const char* context = "CPU:Assignment"; + AssignStruct* locassignstruct = &global_assignstruct; /* Local structure ptr */ + long* arraybase = NULL; + unsigned long total_time = 0; + int iterations = 0; + + /* + ** See if we need to do self adjustment code. + */ + if (locassignstruct->adjust == FALSE) { + locassignstruct->adjust = TRUE; + /* + ** Self-adjustment code. The system begins by working on 1 + ** array. If it does that in no time, then two arrays + ** are built. This process continues until + ** enough arrays are built to handle the tolerance. + */ + locassignstruct->numarrays = 1; + while (1) { + arraybase = realloc(arraybase, sizeof(long) * ASSIGNROWS * ASSIGNCOLS * locassignstruct->numarrays); + if (!arraybase) { + fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context); + exit(1); + } + + /* + ** Do an iteration of the assignment alg. If the + ** elapsed time is less than or equal to the permitted + ** minimum, then allocate for more arrays and + ** try again. + */ + if (DoAssignIteration(arraybase, locassignstruct->numarrays) > global_min_ticks) { + break; + } + + ++locassignstruct->numarrays; + } + } else { + arraybase = malloc(sizeof(long) * ASSIGNROWS * ASSIGNCOLS * locassignstruct->numarrays); + if (!arraybase) { fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context); exit(1); - } - - /* - ** Do an iteration of the assignment alg. If the - ** elapsed time is less than or equal to the permitted - ** minimum, then allocate for more arrays and - ** try again. - */ - if(DoAssignIteration(arraybase, - locassignstruct->numarrays)>global_min_ticks) - break; /* We're ok...exit */ - - locassignstruct->numarrays++; - } -} else { - arraybase = malloc(sizeof(long) * ASSIGNROWS * ASSIGNCOLS * locassignstruct->numarrays); - if (!arraybase) { - fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context); - exit(1); - } -} - -/* -** All's well if we get here. Do the tests. -*/ -accumtime=0L; -iterations=(double)0.0; - -do { - accumtime+=DoAssignIteration(arraybase, - locassignstruct->numarrays); - iterations+=(double)1.0; -} while(TicksToSecs(accumtime)<locassignstruct->request_secs); - -/* -** Clean up, calculate results, and go home. Be sure to -** show that we don't have to rerun adjustment code. -*/ -free(arraybase); + } + } -locassignstruct->iterspersec=iterations * - (double)locassignstruct->numarrays / TicksToFracSecs(accumtime); + do { + total_time += DoAssignIteration(arraybase, locassignstruct->numarrays); + ++iterations; + } while (total_time < locassignstruct->request_secs * CLOCKS_PER_SEC); -if(locassignstruct->adjust==0) - locassignstruct->adjust=1; + free(arraybase); -return; + locassignstruct->iterspersec = (double)(iterations * CLOCKS_PER_SEC *locassignstruct->numarrays) / (double)total_time; } @@ -147,42 +131,29 @@ return; ** This routine executes one iteration of the assignment test. ** It returns the number of ticks elapsed in the iteration. */ -static unsigned long DoAssignIteration(long *arraybase, - unsigned long numarrays) +static clock_t +DoAssignIteration(long *arraybase, unsigned long numarrays) { -longptr abase; /* local pointer */ -unsigned long elapsed; /* Elapsed ticks */ -unsigned long i; + clock_t start, stop; + longptr abase; + unsigned long i; -/* -** Set up local pointer -*/ -abase.ptrs.p=arraybase; + abase.ptrs.p=arraybase; -/* -** Load up the arrays with a random table. -*/ -LoadAssignArrayWithRand(arraybase,numarrays); + LoadAssignArrayWithRand(arraybase,numarrays); -/* -** Start the stopwatch -*/ -elapsed=StartStopwatch(); + start = clock(); -/* -** Execute assignment algorithms -*/ -for(i=0;i<numarrays;i++) -{ /* abase.ptrs.p+=i*ASSIGNROWS*ASSIGNCOLS; */ - /* Fixed by Eike Dierks */ - Assignment(*abase.ptrs.ap); - abase.ptrs.p+=ASSIGNROWS*ASSIGNCOLS; -} + for (i = 0; i < numarrays; i++) { + /* abase.ptrs.p+=i*ASSIGNROWS*ASSIGNCOLS; */ + /* Fixed by Eike Dierks */ + Assignment(*abase.ptrs.ap); + abase.ptrs.p += ASSIGNROWS * ASSIGNCOLS; + } -/* -** Get elapsed time -*/ -return(StopStopwatch(elapsed)); + stop = clock(); + + return stop - start; } /**************************** |